From edd4189d5638d6cc49b3ecd2cb125c2a89f5b5f1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 18 Nov 2023 18:51:53 +0100 Subject: [PATCH 1/3] chore: restructure tests --- .../{abi_item_test.rs => it/abi_items.rs} | 2 - .../tests/{json_abi_test.rs => it/abis.rs} | 2 - .../event_params.rs} | 2 - .../tests/{test_helpers.rs => it/macros.rs} | 2 - crates/json-abi/tests/it/main.rs | 9 + .../tests/{param_tests.rs => it/params.rs} | 2 - .../state_mutability.rs} | 2 - crates/json-abi/tests/{ => it}/test.rs | 6 +- crates/sol-types/tests/macros/main.rs | 2 + crates/sol-types/tests/macros/sol/eip712.rs | 65 ++++++ crates/sol-types/tests/macros/sol/json.rs | 118 +++++++++++ .../tests/{sol.rs => macros/sol/mod.rs} | 197 +----------------- .../tests/{macros.rs => macros/udvt.rs} | 0 crates/syn-solidity/src/ident/mod.rs | 53 +++++ crates/syn-solidity/src/item/function.rs | 20 ++ crates/syn-solidity/tests/ident.rs | 48 ----- crates/syn-solidity/tests/parse.rs | 21 -- 17 files changed, 277 insertions(+), 274 deletions(-) rename crates/json-abi/tests/{abi_item_test.rs => it/abi_items.rs} (99%) rename crates/json-abi/tests/{json_abi_test.rs => it/abis.rs} (99%) rename crates/json-abi/tests/{event_param_test.rs => it/event_params.rs} (99%) rename crates/json-abi/tests/{test_helpers.rs => it/macros.rs} (95%) create mode 100644 crates/json-abi/tests/it/main.rs rename crates/json-abi/tests/{param_tests.rs => it/params.rs} (99%) rename crates/json-abi/tests/{state_mutability_test.rs => it/state_mutability.rs} (96%) rename crates/json-abi/tests/{ => it}/test.rs (93%) create mode 100644 crates/sol-types/tests/macros/main.rs create mode 100644 crates/sol-types/tests/macros/sol/eip712.rs create mode 100644 crates/sol-types/tests/macros/sol/json.rs rename crates/sol-types/tests/{sol.rs => macros/sol/mod.rs} (74%) rename crates/sol-types/tests/{macros.rs => macros/udvt.rs} (100%) delete mode 100644 crates/syn-solidity/tests/ident.rs delete mode 100644 crates/syn-solidity/tests/parse.rs diff --git a/crates/json-abi/tests/abi_item_test.rs b/crates/json-abi/tests/it/abi_items.rs similarity index 99% rename from crates/json-abi/tests/abi_item_test.rs rename to crates/json-abi/tests/it/abi_items.rs index 100ed2ae2..9da1d2181 100644 --- a/crates/json-abi/tests/abi_item_test.rs +++ b/crates/json-abi/tests/it/abi_items.rs @@ -1,5 +1,3 @@ -mod test_helpers; - use alloy_json_abi::{ AbiItem, Event, EventParam, Function, InternalType::{Other, Struct}, diff --git a/crates/json-abi/tests/json_abi_test.rs b/crates/json-abi/tests/it/abis.rs similarity index 99% rename from crates/json-abi/tests/json_abi_test.rs rename to crates/json-abi/tests/it/abis.rs index 68880742a..71fcd4b5c 100644 --- a/crates/json-abi/tests/json_abi_test.rs +++ b/crates/json-abi/tests/it/abis.rs @@ -1,5 +1,3 @@ -mod test_helpers; - use alloy_json_abi::{ Constructor, Error, Event, EventParam, Fallback, Function, JsonAbi, Param, Receive, StateMutability, diff --git a/crates/json-abi/tests/event_param_test.rs b/crates/json-abi/tests/it/event_params.rs similarity index 99% rename from crates/json-abi/tests/event_param_test.rs rename to crates/json-abi/tests/it/event_params.rs index 0f1def5e1..28eecf1a0 100644 --- a/crates/json-abi/tests/event_param_test.rs +++ b/crates/json-abi/tests/it/event_params.rs @@ -1,5 +1,3 @@ -mod test_helpers; - use alloy_json_abi::{EventParam, Param}; #[test] diff --git a/crates/json-abi/tests/test_helpers.rs b/crates/json-abi/tests/it/macros.rs similarity index 95% rename from crates/json-abi/tests/test_helpers.rs rename to crates/json-abi/tests/it/macros.rs index 7637060e1..406a73cd6 100644 --- a/crates/json-abi/tests/test_helpers.rs +++ b/crates/json-abi/tests/it/macros.rs @@ -1,4 +1,3 @@ -#[macro_export] macro_rules! assert_ser_de { ($type:ty, $value:expr) => {{ let ser = serde_json::to_string(&$value).unwrap(); @@ -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(); diff --git a/crates/json-abi/tests/it/main.rs b/crates/json-abi/tests/it/main.rs new file mode 100644 index 000000000..3104e309d --- /dev/null +++ b/crates/json-abi/tests/it/main.rs @@ -0,0 +1,9 @@ +#[macro_use] +mod macros; + +mod abi_items; +mod abis; +mod event_params; +mod params; +mod state_mutability; +mod test; diff --git a/crates/json-abi/tests/param_tests.rs b/crates/json-abi/tests/it/params.rs similarity index 99% rename from crates/json-abi/tests/param_tests.rs rename to crates/json-abi/tests/it/params.rs index afcc6297e..1eb0dc6ee 100644 --- a/crates/json-abi/tests/param_tests.rs +++ b/crates/json-abi/tests/it/params.rs @@ -1,5 +1,3 @@ -mod test_helpers; - use alloy_json_abi::{InternalType::Struct, Param}; #[test] diff --git a/crates/json-abi/tests/state_mutability_test.rs b/crates/json-abi/tests/it/state_mutability.rs similarity index 96% rename from crates/json-abi/tests/state_mutability_test.rs rename to crates/json-abi/tests/it/state_mutability.rs index 3ca406571..9d1345714 100644 --- a/crates/json-abi/tests/state_mutability_test.rs +++ b/crates/json-abi/tests/it/state_mutability.rs @@ -1,5 +1,3 @@ -mod test_helpers; - use alloy_json_abi::StateMutability; #[test] diff --git a/crates/json-abi/tests/test.rs b/crates/json-abi/tests/it/test.rs similarity index 93% rename from crates/json-abi/tests/test.rs rename to crates/json-abi/tests/it/test.rs index 39d84251b..babd7695c 100644 --- a/crates/json-abi/tests/test.rs +++ b/crates/json-abi/tests/it/test.rs @@ -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::(s).unwrap(); assert_eq!(f.signature(), expected); @@ -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"); diff --git a/crates/sol-types/tests/macros/main.rs b/crates/sol-types/tests/macros/main.rs new file mode 100644 index 000000000..943c88f58 --- /dev/null +++ b/crates/sol-types/tests/macros/main.rs @@ -0,0 +1,2 @@ +mod sol; +mod udvt; diff --git a/crates/sol-types/tests/macros/sol/eip712.rs b/crates/sol-types/tests/macros/sol/eip712.rs new file mode 100644 index 000000000..4b155323c --- /dev/null +++ b/crates/sol-types/tests/macros/sol/eip712.rs @@ -0,0 +1,65 @@ +use alloy_primitives::B256; +use alloy_sol_types::{eip712_domain, sol, SolStruct}; + +#[test] +fn eip712_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 eip712_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::().unwrap() + ) +} diff --git a/crates/sol-types/tests/macros/sol/json.rs b/crates/sol-types/tests/macros/sol/json.rs new file mode 100644 index 000000000..1b811c9ad --- /dev/null +++ b/crates/sol-types/tests/macros/sol/json.rs @@ -0,0 +1,118 @@ +use alloy_primitives::{Address, U256}; +use alloy_sol_types::{sol, SolCall, SolError}; + +#[test] +fn abigen_json_large_array() { + sol!(LargeArray, "../json-abi/tests/abi/LargeArray.json"); + assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])"); +} + +#[test] +fn abigen_json_seaport() { + use alloy_sol_types::SolStruct; + use std::borrow::Cow; + use Seaport::*; + + sol!(Seaport, "../json-abi/tests/abi/Seaport.json"); + + // 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!( + ::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 abigen_json_aggregation_router_v5() { + sol!(AggregationRouterV5, "../json-abi/tests/abi/AggregationRouterV5.json"); + + assert_eq!( + ::SIGNATURE, + "ETHTransferFailed()" + ); + assert_eq!(::SIGNATURE, "InvalidMsgValue()"); +} + +// Handle contract types in JSON ABI +// https://github.com/alloy-rs/core/issues/351 +#[test] +fn abigen_json_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 abigen_json_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 abigen_json_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 abigen_json_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 abigen_json_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 abigen_json_0x_proxy() { + sol!(ZeroXExchangeProxy, "../json-abi/tests/abi/ZeroxExchangeProxy.json"); +} diff --git a/crates/sol-types/tests/sol.rs b/crates/sol-types/tests/macros/sol/mod.rs similarity index 74% rename from crates/sol-types/tests/sol.rs rename to crates/sol-types/tests/macros/sol/mod.rs index 4575a2294..46bdb3975 100644 --- a/crates/sol-types/tests/sol.rs +++ b/crates/sol-types/tests/macros/sol/mod.rs @@ -1,8 +1,13 @@ -use alloy_primitives::{hex, keccak256, Address, B256, I256, U256}; -use alloy_sol_types::{eip712_domain, sol, SolCall, SolError, SolEvent, SolStruct, SolType}; +use alloy_primitives::{hex, keccak256, Address, I256, U256}; +use alloy_sol_types::{sol, SolCall, SolError, SolEvent, SolStruct, SolType}; use serde::Serialize; use serde_json::Value; +#[cfg(feature = "json")] +mod json; + +mod eip712; + #[test] fn e2e() { sol! { @@ -653,191 +658,3 @@ fn duplicate_attributes() { } } } - -#[test] -#[cfg(feature = "json")] -fn abigen_json_large_array() { - sol!(LargeArray, "../json-abi/tests/abi/LargeArray.json"); - assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])"); -} - -#[test] -#[cfg(feature = "json")] -fn abigen_json_seaport() { - use alloy_sol_types::SolStruct; - use std::borrow::Cow; - use Seaport::*; - - sol!(Seaport, "../json-abi/tests/abi/Seaport.json"); - - // 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!( - ::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] -#[cfg(feature = "json")] -fn abigen_json_aggregation_router_v5() { - sol!(AggregationRouterV5, "../json-abi/tests/abi/AggregationRouterV5.json"); - - assert_eq!( - ::SIGNATURE, - "ETHTransferFailed()" - ); - assert_eq!(::SIGNATURE, "InvalidMsgValue()"); -} - -// Handle contract types in JSON ABI -// https://github.com/alloy-rs/core/issues/351 -#[test] -#[cfg(feature = "json")] -fn abigen_json_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] -#[cfg(feature = "json")] -fn abigen_json_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] -#[cfg(feature = "json")] -fn abigen_json_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] -#[cfg(feature = "json")] -fn abigen_json_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] -#[cfg(feature = "json")] -fn abigen_json_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] -#[cfg(feature = "json")] -fn abigen_json_0x_proxy() { - sol!(ZeroXExchangeProxy, "../json-abi/tests/abi/ZeroxExchangeProxy.json"); -} - -#[test] -fn eip712_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 eip712_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::().unwrap() - ) -} diff --git a/crates/sol-types/tests/macros.rs b/crates/sol-types/tests/macros/udvt.rs similarity index 100% rename from crates/sol-types/tests/macros.rs rename to crates/sol-types/tests/macros/udvt.rs diff --git a/crates/syn-solidity/src/ident/mod.rs b/crates/syn-solidity/src/ident/mod.rs index e99580a88..59facd56a 100644 --- a/crates/syn-solidity/src/ident/mod.rs +++ b/crates/syn-solidity/src/ident/mod.rs @@ -153,3 +153,56 @@ fn check_dollar(input: ParseStream<'_>) -> Result<()> { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::sol_path; + + #[test] + fn ident() { + let id: SolIdent = syn::parse_str("a").unwrap(); + assert_eq!(id, SolIdent::new("a")); + } + + #[test] + fn keywords() { + // keywords in Rust, but not Solidity; we try to make them "raw", although some, like + // `crate` can never be made identifiers. See ./kw.c`. + let difference: &[&str] = &include!("./difference.expr"); + for &s in difference { + let id: SolIdent = syn::parse_str(s).unwrap(); + assert_eq!(id, SolIdent::new(s)); + assert_eq!(id.to_string(), format!("r#{s}")); + assert_eq!(id.as_string(), s); + } + + // keywords in both languages; we don't make them "raw" because they are always invalid. + let intersection: &[&str] = &include!("./intersection.expr"); + for &s in intersection { + let id: SolIdent = syn::parse_str(s).unwrap(); + assert_eq!(id, SolIdent::new(s)); + assert_eq!(id.to_string(), s); + assert_eq!(id.as_string(), s); + } + } + + #[test] + fn ident_path() { + let path: SolPath = syn::parse_str("a.b.c").unwrap(); + assert_eq!(path, sol_path!["a", "b", "c"]); + } + + #[test] + fn ident_path_trailing() { + let _e = syn::parse_str::("a.b.").unwrap_err(); + } + + #[test] + fn ident_dollar() { + assert!(syn::parse_str::("$hello") + .unwrap_err() + .to_string() + .contains("Solidity identifiers starting with `$` are unsupported.")); + } +} diff --git a/crates/syn-solidity/src/item/function.rs b/crates/syn-solidity/src/item/function.rs index aa609aeec..a4d0eccab 100644 --- a/crates/syn-solidity/src/item/function.rs +++ b/crates/syn-solidity/src/item/function.rs @@ -403,11 +403,31 @@ impl FunctionBody { mod tests { use super::*; use pretty_assertions::assert_eq; + use proc_macro2::Span; use std::{ error::Error, io::Write, process::{Command, Stdio}, }; + use syn::parse_quote; + + #[test] + fn modifiers() { + let none: ItemFunction = parse_quote! { + modifier noParens { + _; + } + }; + let some: ItemFunction = parse_quote! { + modifier withParens() { + _; + } + }; + assert_eq!(none.kind, FunctionKind::new_modifier(Span::call_site())); + assert_eq!(none.kind, some.kind); + assert_eq!(none.paren_token, None); + assert_eq!(some.paren_token, Some(Default::default())); + } #[test] #[cfg_attr(miri, ignore = "takes too long")] diff --git a/crates/syn-solidity/tests/ident.rs b/crates/syn-solidity/tests/ident.rs deleted file mode 100644 index 56afb472c..000000000 --- a/crates/syn-solidity/tests/ident.rs +++ /dev/null @@ -1,48 +0,0 @@ -use syn_solidity::{sol_path, SolIdent, SolPath}; - -#[test] -fn ident() { - let id: SolIdent = syn::parse_str("a").unwrap(); - assert_eq!(id, SolIdent::new("a")); -} - -#[test] -fn keywords() { - // keywords in Rust, but not Solidity; we try to make them "raw", although some, like `crate` - // can never be made identifiers. See `src/ident/kw.c`. - let difference: &[&str] = &include!("../src/ident/difference.expr"); - for &s in difference { - let id: SolIdent = syn::parse_str(s).unwrap(); - assert_eq!(id, SolIdent::new(s)); - assert_eq!(id.to_string(), format!("r#{s}")); - assert_eq!(id.as_string(), s); - } - - // keywords in both languages; we don't make them "raw" because they are always invalid. - let intersection: &[&str] = &include!("../src/ident/intersection.expr"); - for &s in intersection { - let id: SolIdent = syn::parse_str(s).unwrap(); - assert_eq!(id, SolIdent::new(s)); - assert_eq!(id.to_string(), s); - assert_eq!(id.as_string(), s); - } -} - -#[test] -fn ident_path() { - let path: SolPath = syn::parse_str("a.b.c").unwrap(); - assert_eq!(path, sol_path!["a", "b", "c"]); -} - -#[test] -fn ident_path_trailing() { - let _e = syn::parse_str::("a.b.").unwrap_err(); -} - -#[test] -fn ident_dollar() { - assert!(syn::parse_str::("$hello") - .unwrap_err() - .to_string() - .contains("Solidity identifiers starting with `$` are unsupported.")); -} diff --git a/crates/syn-solidity/tests/parse.rs b/crates/syn-solidity/tests/parse.rs deleted file mode 100644 index da8c29b90..000000000 --- a/crates/syn-solidity/tests/parse.rs +++ /dev/null @@ -1,21 +0,0 @@ -use proc_macro2::Span; -use syn::parse_quote; -use syn_solidity::{FunctionKind, ItemFunction}; - -#[test] -fn modifiers() { - let none: ItemFunction = parse_quote! { - modifier noParens { - _; - } - }; - let some: ItemFunction = parse_quote! { - modifier withParens() { - _; - } - }; - assert_eq!(none.kind, FunctionKind::new_modifier(Span::call_site())); - assert_eq!(none.kind, some.kind); - assert_eq!(none.paren_token, None); - assert_eq!(some.paren_token, Some(Default::default())); -} From b266fdb312313dc5afdf5f80e3a7661ad973cec5 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 18 Nov 2023 18:58:33 +0100 Subject: [PATCH 2/3] names --- crates/sol-types/tests/macros/sol/json.rs | 26 +++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/sol-types/tests/macros/sol/json.rs b/crates/sol-types/tests/macros/sol/json.rs index 1b811c9ad..ef2c3346e 100644 --- a/crates/sol-types/tests/macros/sol/json.rs +++ b/crates/sol-types/tests/macros/sol/json.rs @@ -1,19 +1,17 @@ use alloy_primitives::{Address, U256}; -use alloy_sol_types::{sol, SolCall, SolError}; +use alloy_sol_types::{sol, SolCall, SolError, SolStruct}; +use std::borrow::Cow; #[test] -fn abigen_json_large_array() { +fn large_array() { sol!(LargeArray, "../json-abi/tests/abi/LargeArray.json"); assert_eq!(LargeArray::callWithLongArrayCall::SIGNATURE, "callWithLongArray(uint64[128])"); } #[test] -fn abigen_json_seaport() { - use alloy_sol_types::SolStruct; - use std::borrow::Cow; - use Seaport::*; - +fn seaport() { sol!(Seaport, "../json-abi/tests/abi/Seaport.json"); + use Seaport::*; // BasicOrderType is a uint8 UDVT let _ = BasicOrderType::from(0u8); @@ -34,7 +32,7 @@ fn abigen_json_seaport() { // Handle multiple identical error objects in the JSON ABI // https://github.com/alloy-rs/core/issues/344 #[test] -fn abigen_json_aggregation_router_v5() { +fn aggregation_router_v5() { sol!(AggregationRouterV5, "../json-abi/tests/abi/AggregationRouterV5.json"); assert_eq!( @@ -47,7 +45,7 @@ fn abigen_json_aggregation_router_v5() { // Handle contract types in JSON ABI // https://github.com/alloy-rs/core/issues/351 #[test] -fn abigen_json_uniswap_v3_position() { +fn uniswap_v3_position() { sol!(UniswapV3Position, "../json-abi/tests/abi/UniswapV3Position.json"); let _ = UniswapV3Position::getLiquidityByRangeCall { @@ -72,7 +70,7 @@ fn abigen_json_uniswap_v3_position() { // Ensure a trailing comma for single-element tuples in old JSON ABI // https://github.com/alloy-rs/core/issues/360 #[test] -fn abigen_json_double_exponent_interest_setter() { +fn double_exponent_interest_setter() { sol!(DoubleExponentInterestSetter, "../json-abi/tests/abi/DoubleExponentInterestSetter.json"); let _ = DoubleExponentInterestSetter::getInterestRateCall { _0: Address::ZERO, @@ -84,7 +82,7 @@ fn abigen_json_double_exponent_interest_setter() { // Same as `event_tokenize_fields` // https://github.com/alloy-rs/core/issues/361 #[test] -fn abigen_json_uniswap_v2_factory() { +fn uniswap_v2_factory() { sol!(UniswapV2Factory, "../json-abi/tests/abi/UniswapV2Factory.json"); let _ = UniswapV2Factory::PairCreated { token0: Address::ZERO, @@ -97,7 +95,7 @@ fn abigen_json_uniswap_v2_factory() { // Fully qualify `SolInterface::NAME` which conflicted with the `NAME` call // https://github.com/alloy-rs/core/issues/361 #[test] -fn abigen_json_gnosis_safe() { +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() }; @@ -106,13 +104,13 @@ fn abigen_json_gnosis_safe() { // Have enough recursion depth to handle `BlurExchange` types // https://github.com/alloy-rs/core/issues/371 #[test] -fn abigen_json_blur_exchange() { +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 abigen_json_0x_proxy() { +fn zerox_proxy() { sol!(ZeroXExchangeProxy, "../json-abi/tests/abi/ZeroxExchangeProxy.json"); } From 1341046c1368ca7f232023569181d28bdd774738 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sat, 18 Nov 2023 18:58:57 +0100 Subject: [PATCH 3/3] names2 --- crates/sol-types/tests/macros/sol/eip712.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sol-types/tests/macros/sol/eip712.rs b/crates/sol-types/tests/macros/sol/eip712.rs index 4b155323c..db8c438a1 100644 --- a/crates/sol-types/tests/macros/sol/eip712.rs +++ b/crates/sol-types/tests/macros/sol/eip712.rs @@ -2,7 +2,7 @@ use alloy_primitives::B256; use alloy_sol_types::{eip712_domain, sol, SolStruct}; #[test] -fn eip712_encode_type_nesting() { +fn encode_type_nesting() { sol! { struct A { uint256 a; @@ -31,7 +31,7 @@ fn eip712_encode_type_nesting() { } #[test] -fn eip712_encode_data_nesting() { +fn encode_data_nesting() { sol! { struct Person { string name;