Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restructure tests #421

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod test_helpers;

use alloy_json_abi::{
AbiItem, Event, EventParam, Function,
InternalType::{Other, Struct},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod test_helpers;

use alloy_json_abi::{
Constructor, Error, Event, EventParam, Fallback, Function, JsonAbi, Param, Receive,
StateMutability,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod test_helpers;

use alloy_json_abi::{EventParam, Param};

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[macro_export]
macro_rules! assert_ser_de {
($type:ty, $value:expr) => {{
let ser = serde_json::to_string(&$value).unwrap();
Expand All @@ -7,7 +6,6 @@ macro_rules! assert_ser_de {
}};
}

#[macro_export]
macro_rules! assert_json_eq {
($left:expr, $right:expr) => {{
let left_val: serde_json::Value = serde_json::from_str($left).unwrap();
Expand Down
9 changes: 9 additions & 0 deletions crates/json-abi/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[macro_use]
mod macros;

mod abi_items;
mod abis;
mod event_params;
mod params;
mod state_mutability;
mod test;
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod test_helpers;

use alloy_json_abi::{InternalType::Struct, Param};

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod test_helpers;

use alloy_json_abi::StateMutability;

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn complex_error() {

#[test]
fn big_function() {
let s = include_str!("abi/LargeFunction.json");
let s = include_str!("../abi/LargeFunction.json");
let expected = "fulfillAvailableAdvancedOrders(((address,address,(uint8,address,uint256,uint256,uint256)[],(uint8,address,uint256,uint256,uint256,address)[],uint8,uint256,uint256,bytes32,uint256,bytes32,uint256),uint120,uint120,bytes,bytes)[],(uint256,uint8,uint256,uint256,bytes32[])[],(uint256,uint256)[][],(uint256,uint256)[][],bytes32,address,uint256)";
let f = serde_json::from_str::<alloy_json_abi::Function>(s).unwrap();
assert_eq!(f.signature(), expected);
Expand All @@ -50,8 +50,8 @@ fn big_function() {
#[cfg_attr(miri, ignore = "takes too long")]
fn test_constructor() {
// Parse the ABI JSON file
let abi_items_wo_constructor = include_str!("abi/Abiencoderv2Test.json");
let abi_items_w_constructor = include_str!("abi/Seaport.json");
let abi_items_wo_constructor = include_str!("../abi/Abiencoderv2Test.json");
let abi_items_w_constructor = include_str!("../abi/Seaport.json");

let abi_wo_constructor: JsonAbi =
serde_json::from_str(abi_items_wo_constructor).expect("Failed to parse ABI JSON string");
Expand Down
2 changes: 2 additions & 0 deletions crates/sol-types/tests/macros/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod sol;
mod udvt;
65 changes: 65 additions & 0 deletions crates/sol-types/tests/macros/sol/eip712.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use alloy_primitives::B256;
use alloy_sol_types::{eip712_domain, sol, SolStruct};

#[test]
fn encode_type_nesting() {
sol! {
struct A {
uint256 a;
}

struct B {
bytes32 b;
}

struct C {
A a;
B b;
}

struct D {
C c;
A a;
B b;
}
}

assert_eq!(A::eip712_encode_type(), "A(uint256 a)");
assert_eq!(B::eip712_encode_type(), "B(bytes32 b)");
assert_eq!(C::eip712_encode_type(), "C(A a,B b)A(uint256 a)B(bytes32 b)");
assert_eq!(D::eip712_encode_type(), "D(C c,A a,B b)A(uint256 a)B(bytes32 b)C(A a,B b)");
}

#[test]
fn encode_data_nesting() {
sol! {
struct Person {
string name;
address wallet;
}

struct Mail {
Person from;
Person to;
string contents;
}
}
let domain = eip712_domain! {};

let mail = Mail {
from: Person {
name: "Cow".to_owned(),
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826".parse().unwrap(),
},
to: Person {
name: "Bob".to_owned(),
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB".parse().unwrap(),
},
contents: "Hello, Bob!".to_owned(),
};

assert_eq!(
alloy_sol_types::SolStruct::eip712_signing_hash(&mail, &domain),
"25c3d40a39e639a4d0b6e4d2ace5e1281e039c88494d97d8d08f99a6ea75d775".parse::<B256>().unwrap()
)
}
116 changes: 116 additions & 0 deletions crates/sol-types/tests/macros/sol/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use alloy_primitives::{Address, U256};
use alloy_sol_types::{sol, SolCall, SolError, SolStruct};
use std::borrow::Cow;

#[test]
fn large_array() {
sol!(LargeArray, "../json-abi/tests/abi/LargeArray.json");
assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])");
}

#[test]
fn seaport() {
sol!(Seaport, "../json-abi/tests/abi/Seaport.json");
use Seaport::*;

// BasicOrderType is a uint8 UDVT
let _ = BasicOrderType::from(0u8);

// BasicOrderParameters is a struct that contains UDVTs (basicOrderType) and a
// struct array. The only component should be the struct of the struct array.
let root_type = "BasicOrderParameters(address considerationToken,uint256 considerationIdentifier,uint256 considerationAmount,address offerer,address zone,address offerToken,uint256 offerIdentifier,uint256 offerAmount,uint8 basicOrderType,uint256 startTime,uint256 endTime,bytes32 zoneHash,uint256 salt,bytes32 offererConduitKey,bytes32 fulfillerConduitKey,uint256 totalOriginalAdditionalRecipients,AdditionalRecipient[] additionalRecipients,bytes signature)";
let component = "AdditionalRecipient(uint256 amount,address recipient)";

assert_eq!(BasicOrderParameters::eip712_root_type(), root_type);
assert_eq!(BasicOrderParameters::eip712_components(), [Cow::Borrowed(component)]);
assert_eq!(
<BasicOrderParameters as SolStruct>::eip712_encode_type(),
root_type.to_string() + component
);
}

// Handle multiple identical error objects in the JSON ABI
// https://github.com/alloy-rs/core/issues/344
#[test]
fn aggregation_router_v5() {
sol!(AggregationRouterV5, "../json-abi/tests/abi/AggregationRouterV5.json");

assert_eq!(
<AggregationRouterV5::ETHTransferFailed as SolError>::SIGNATURE,
"ETHTransferFailed()"
);
assert_eq!(<AggregationRouterV5::InvalidMsgValue as SolError>::SIGNATURE, "InvalidMsgValue()");
}

// Handle contract types in JSON ABI
// https://github.com/alloy-rs/core/issues/351
#[test]
fn uniswap_v3_position() {
sol!(UniswapV3Position, "../json-abi/tests/abi/UniswapV3Position.json");

let _ = UniswapV3Position::getLiquidityByRangeCall {
pool_: Address::ZERO,
self_: Address::ZERO,
lowerTick_: 0,
upperTick_: 0,
};
assert_eq!(
UniswapV3Position::getLiquidityByRangeCall::SIGNATURE,
"getLiquidityByRange(address,address,int24,int24)"
);

let _ =
UniswapV3Position::getPositionIdCall { self_: Address::ZERO, lowerTick_: 0, upperTick_: 0 };
assert_eq!(
UniswapV3Position::getPositionIdCall::SIGNATURE,
"getPositionId(address,int24,int24)"
);
}

// Ensure a trailing comma for single-element tuples in old JSON ABI
// https://github.com/alloy-rs/core/issues/360
#[test]
fn double_exponent_interest_setter() {
sol!(DoubleExponentInterestSetter, "../json-abi/tests/abi/DoubleExponentInterestSetter.json");
let _ = DoubleExponentInterestSetter::getInterestRateCall {
_0: Address::ZERO,
borrowWei: U256::ZERO,
supplyWei: U256::ZERO,
};
}

// Same as `event_tokenize_fields`
// https://github.com/alloy-rs/core/issues/361
#[test]
fn uniswap_v2_factory() {
sol!(UniswapV2Factory, "../json-abi/tests/abi/UniswapV2Factory.json");
let _ = UniswapV2Factory::PairCreated {
token0: Address::ZERO,
token1: Address::ZERO,
pair: Address::ZERO,
_3: U256::ZERO,
};
}

// Fully qualify `SolInterface::NAME` which conflicted with the `NAME` call
// https://github.com/alloy-rs/core/issues/361
#[test]
fn gnosis_safe() {
sol!(GnosisSafe, "../json-abi/tests/abi/GnosisSafe.json");
let GnosisSafe::NAMECall {} = GnosisSafe::NAMECall {};
let GnosisSafe::NAMEReturn { _0: _ } = GnosisSafe::NAMEReturn { _0: String::new() };
}

// Have enough recursion depth to handle `BlurExchange` types
// https://github.com/alloy-rs/core/issues/371
#[test]
fn blur_exchange() {
sol!(BlurExchange, "../json-abi/tests/abi/BlurExchange.json");
let BlurExchange::NAMECall {} = BlurExchange::NAMECall {};
let BlurExchange::NAMEReturn { _0: _ } = BlurExchange::NAMEReturn { _0: String::new() };
}

#[test]
fn zerox_proxy() {
sol!(ZeroXExchangeProxy, "../json-abi/tests/abi/ZeroxExchangeProxy.json");
}
Loading