ethers-rs/ethers-contract/tests/abigen.rs

77 lines
2.2 KiB
Rust
Raw Permalink Normal View History

#![cfg(feature = "abigen")]
//! Test cases to validate the `abigen!` macro
use ethers_contract::{abigen, EthEvent};
use ethers_core::abi::Tokenizable;
#[test]
fn can_gen_human_readable() {
abigen!(
SimpleContract,
r#"[
event ValueChanged(address indexed author, string oldValue, string newValue)
]"#,
event_derives(serde::Deserialize, serde::Serialize)
);
assert_eq!("ValueChanged", ValueChangedFilter::name());
assert_eq!(
"ValueChanged(address,string,string)",
ValueChangedFilter::abi_signature()
);
}
#[test]
fn can_gen_structs_readable() {
abigen!(
SimpleContract,
r#"[
struct Value {address addr; string value;}
struct Addresses {address[] addr; string s;}
event ValueChanged(Value indexed old, Value newValue, Addresses _a)
]"#,
event_derives(serde::Deserialize, serde::Serialize)
);
let value = Addresses {
addr: vec!["eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee".parse().unwrap()],
s: "hello".to_string(),
};
let token = value.clone().into_token();
assert_eq!(value, Addresses::from_token(token).unwrap());
assert_eq!("ValueChanged", ValueChangedFilter::name());
assert_eq!(
"ValueChanged((address,string),(address,string),(address[],string))",
ValueChangedFilter::abi_signature()
);
}
#[test]
fn can_gen_structs_with_arrays_readable() {
abigen!(
SimpleContract,
r#"[
struct Value {address addr; string value;}
struct Addresses {address[] addr; string s;}
event ValueChanged(Value indexed old, Value newValue, Addresses[] _a)
]"#,
event_derives(serde::Deserialize, serde::Serialize)
);
assert_eq!(
"ValueChanged((address,string),(address,string),(address[],string)[])",
ValueChangedFilter::abi_signature()
);
}
fn assert_tokenizeable<T: Tokenizable>() {}
#[test]
fn can_generate_internal_structs() {
abigen!(
VerifierContract,
"ethers-contract/tests/solidity-contracts/verifier_abi.json",
event_derives(serde::Deserialize, serde::Serialize)
);
assert_tokenizeable::<VerifyingKey>();
assert_tokenizeable::<G1Point>();
assert_tokenizeable::<G2Point>();
}