Skip to content

Commit

Permalink
Test/517 ethers helper.rs (#519)
Browse files Browse the repository at this point in the history
* test: added two test cases for get_storage_at

* test: added test for starknet_estimate_fee

* test: added test for starknet get_block_with_txs

* test: added tests for get_block_hash_and_number and get_block_with_txs

* test: added tests for starknet_get_transaction_receipt

* test: added test for get_transaction_by_hash

* fix: fixed failing tests

* feat: moved ethers_helpers tests to its own test file

---------

Co-authored-by: drspacemn <[email protected]>
  • Loading branch information
Godspower-Eze and drspacemn authored Aug 3, 2023
1 parent 229e346 commit a7f494c
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 123 deletions.
123 changes: 0 additions & 123 deletions crates/beerus-core/src/ethers_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,126 +99,3 @@ pub fn block_tag_eq(a: &BlockTag, b: &BlockTag) -> bool {
_ => false,
}
}

#[cfg(test)]
mod tests {
use super::block_tag_eq;
use ethers::types::Bytes;
use helios::types::BlockTag;
use std::str::FromStr;

#[test]
fn test_encode_function_data() {
let abi: ethers::abi::Abi = serde_json::from_str(
r#"
[
{
"inputs": [
{
"internalType": "uint256",
"name": "x",
"type": "uint256"
}
],
"name": "foo",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
"#,
)
.unwrap();
let args = 42u64;
let function_name = "foo";
let encoded = super::encode_function_data(args, abi, function_name).unwrap();
assert_eq!(
encoded,
Bytes::from_str(
"0x2fbebd38000000000000000000000000000000000000000000000000000000000000002a"
)
.unwrap()
);
}

#[test]
fn test_u256_to_bytes32_slice() {
let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into();
let bytes = super::u256_to_bytes32_slice(value);
assert_eq!(
bytes,
[
73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241,
76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35
]
);
}

#[test]
fn test_u256_to_bytes32_type() {
let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into();
let token = super::u256_to_bytes32_type(value);
assert_eq!(
token,
ethers::abi::Token::FixedBytes(vec![
73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241,
76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35
])
);
}

#[test]
fn test_block_string_to_block_tag_type() {
// Testing for Number type
// Given
let block = "123".to_string();

// When
let result = super::block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Number(123);
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);

// Testing for Latest type
// Given
let block = "latest".to_string();

// When
let result = super::block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Latest;
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);

// Testing for Finalized type
// Given
let block = "finalized".to_string();

// When
let result = super::block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Finalized;
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);
}

#[test]
fn test_invalid_block_should_return_error() {
// Testing for invalid type
// Given
let block = "0x123".to_string();

// When
let result = super::block_string_to_block_tag_type(&block);

// Then
match result {
Err(e) => assert_eq!("Invalid BlockTag", e.to_string()),
Ok(_) => panic!("Expected error, got ok"),
}
}
}
129 changes: 129 additions & 0 deletions crates/beerus-core/tests/ethers_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#![cfg(not(target_arch = "wasm32"))]

use core::str::FromStr;

mod tests {

use super::*;
use beerus_core::ethers_helper::{
block_string_to_block_tag_type, block_tag_eq, encode_function_data, u256_to_bytes32_slice,
u256_to_bytes32_type,
};
use ethers::{abi::Abi, types::Bytes};
use helios::types::BlockTag;

#[test]
fn test_encode_function_data() {
let abi: Abi = serde_json::from_str(
r#"
[
{
"inputs": [
{
"internalType": "uint256",
"name": "x",
"type": "uint256"
}
],
"name": "foo",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
"#,
)
.unwrap();
let args = 42u64;
let function_name = "foo";
let encoded = encode_function_data(args, abi, function_name).unwrap();
assert_eq!(
encoded,
Bytes::from_str(
"0x2fbebd38000000000000000000000000000000000000000000000000000000000000002a"
)
.unwrap()
);
}

#[test]
fn test_u256_to_bytes32_slice() {
let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into();
let bytes = u256_to_bytes32_slice(value);
assert_eq!(
bytes,
[
73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241,
76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35
]
);
}

#[test]
fn test_u256_to_bytes32_type() {
let value = "0x4916eedd890707a351fde79168118d8a26f14c1de33ab0ecad3116d7bcba1a23".into();
let token = u256_to_bytes32_type(value);
assert_eq!(
token,
ethers::abi::Token::FixedBytes(vec![
73, 22, 238, 221, 137, 7, 7, 163, 81, 253, 231, 145, 104, 17, 141, 138, 38, 241,
76, 29, 227, 58, 176, 236, 173, 49, 22, 215, 188, 186, 26, 35
])
);
}

#[test]
fn test_block_string_to_block_tag_type() {
// Testing for Number type
// Given
let block = "123".to_string();

// When
let result = block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Number(123);
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);

// Testing for Latest type
// Given
let block = "latest".to_string();

// When
let result = block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Latest;
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);

// Testing for Finalized type
// Given
let block = "finalized".to_string();

// When
let result = block_string_to_block_tag_type(&block).unwrap();

// Then
let expected_result = BlockTag::Finalized;
let equal = block_tag_eq(&result, &expected_result);
assert_eq!(equal, true);
}

#[test]
fn test_invalid_block_should_return_error() {
// Testing for invalid type
// Given
let block = "0x123".to_string();

// When
let result = block_string_to_block_tag_type(&block);

// Then
match result {
Err(e) => assert_eq!("Invalid BlockTag", e.to_string()),
Ok(_) => panic!("Expected error, got ok"),
}
}
}

0 comments on commit a7f494c

Please sign in to comment.