From df3d57bd6a4b705e90f6fd24e0858eb653f7f2e8 Mon Sep 17 00:00:00 2001 From: Valentin <77051586+vkgnosis@users.noreply.github.com> Date: Fri, 18 Feb 2022 12:43:35 +0100 Subject: [PATCH] Fix assert usage in tests (#724) Co-authored-by: Dusan Stanivukovic Co-authored-by: Nicholas Rodrigues Lordello --- ethcontract-common/src/bytecode.rs | 4 +--- ethcontract-generate/src/util.rs | 14 ++++++++------ ethcontract-mock/src/details/mod.rs | 26 ++++++++++++-------------- ethcontract-mock/src/details/parse.rs | 8 +++++--- ethcontract-mock/src/details/sign.rs | 19 +++++++++---------- ethcontract/src/int.rs | 8 ++------ 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/ethcontract-common/src/bytecode.rs b/ethcontract-common/src/bytecode.rs index 707f8618..5faf1ced 100644 --- a/ethcontract-common/src/bytecode.rs +++ b/ethcontract-common/src/bytecode.rs @@ -60,9 +60,7 @@ impl Bytecode { S: AsRef, { let name = name.as_ref(); - if name.len() > 38 { - panic!("invalid library name for linking"); - } + assert!(name.len() <= 38, "invalid library name for linking"); // NOTE(nlordell): solc linking works by string search and replace of // '__$name__..__' with the library address; see generated bytecode for diff --git a/ethcontract-generate/src/util.rs b/ethcontract-generate/src/util.rs index 52b846ca..43349da5 100644 --- a/ethcontract-generate/src/util.rs +++ b/ethcontract-generate/src/util.rs @@ -94,16 +94,18 @@ mod tests { #[test] fn parse_address_missing_prefix() { - if parse_address("0000000000000000000000000000000000000000").is_ok() { - panic!("parsing address not starting with 0x should fail"); - } + assert!( + parse_address("0000000000000000000000000000000000000000").is_err(), + "parsing address not starting with 0x should fail" + ); } #[test] fn parse_address_address_too_short() { - if parse_address("0x00000000000000").is_ok() { - panic!("parsing address not starting with 0x should fail"); - } + assert!( + parse_address("0x00000000000000").is_err(), + "parsing address not starting with 0x should fail" + ); } #[test] diff --git a/ethcontract-mock/src/details/mod.rs b/ethcontract-mock/src/details/mod.rs index 25e4db38..6ae858ad 100644 --- a/ethcontract-mock/src/details/mod.rs +++ b/ethcontract-mock/src/details/mod.rs @@ -602,14 +602,13 @@ impl MockTransport { let tx = verify(&raw_tx.0, state.chain_id); let nonce = state.nonce.entry(tx.from).or_insert(0); - if *nonce != tx.nonce.as_u64() { - panic!( - "nonce mismatch for account {:#x}: expected {}, actual {}", - tx.from, - tx.nonce.as_u64(), - nonce - ); - } + assert!( + *nonce == tx.nonce.as_u64(), + "nonce mismatch for account {:#x}: expected {}, actual {}", + tx.from, + tx.nonce.as_u64(), + nonce + ); *nonce += 1; let contract = state.contract(tx.to); @@ -717,9 +716,7 @@ impl Contract { // // We could support receive/fallback functions if data is empty. - if data.len() < 4 { - panic!("transaction has invalid call data"); - } + assert!(data.len() >= 4, "transaction has invalid call data"); let signature = H32::try_from(&data[0..4]).unwrap(); let method = self.method(signature); @@ -785,9 +782,10 @@ impl Method { index: usize, generation: usize, ) -> &mut Expectation { - if generation != self.generation { - panic!("old expectations are not valid after checkpoint"); - } + assert!( + generation == self.generation, + "old expectations are not valid after checkpoint" + ); let expectation: &mut Expectation = self .expectations diff --git a/ethcontract-mock/src/details/parse.rs b/ethcontract-mock/src/details/parse.rs index 6dc719d9..def21a85 100644 --- a/ethcontract-mock/src/details/parse.rs +++ b/ethcontract-mock/src/details/parse.rs @@ -90,8 +90,10 @@ impl Parser { impl Drop for Parser { fn drop(&mut self) { - if !std::thread::panicking() && self.current < self.args.len() { - panic!("too many arguments for rpc call {:?}", self.name); - } + assert!( + std::thread::panicking() || self.current >= self.args.len(), + "too many arguments for rpc call {:?}", + self.name + ); } } diff --git a/ethcontract-mock/src/details/sign.rs b/ethcontract-mock/src/details/sign.rs index fe83a813..8ed2f9ed 100644 --- a/ethcontract-mock/src/details/sign.rs +++ b/ethcontract-mock/src/details/sign.rs @@ -22,13 +22,11 @@ pub fn verify(raw_tx: &[u8], node_chain_id: u64) -> Transaction { err(); } - if res(rlp.at(3)).size() == 0 { - // TODO: - // - // We could support deployments via RPC calls by introducing - // something like `expect_deployment` method to `Mock` struct. - panic!("mock client does not support deploying contracts via transaction, use `Mock::deploy` instead"); - } + // TODO: + // + // We could support deployments via RPC calls by introducing + // something like `expect_deployment` method to `Mock` struct. + assert!(res(rlp.at(3)).size() != 0, "mock client does not support deploying contracts via transaction, use `Mock::deploy` instead"); let nonce: U256 = res(rlp.val_at(0)); let gas_price: U256 = res(rlp.val_at(1)); @@ -46,9 +44,10 @@ pub fn verify(raw_tx: &[u8], node_chain_id: u64) -> Transaction { _ => panic!("invalid transaction signature, v value is out of range"), }; - if chain_id != node_chain_id { - panic!("invalid transaction signature, chain id mismatch"); - } + assert!( + chain_id == node_chain_id, + "invalid transaction signature, chain id mismatch" + ); let msg_hash = { let mut rlp = rlp::RlpStream::new(); diff --git a/ethcontract/src/int.rs b/ethcontract/src/int.rs index acb30201..0132f8ae 100644 --- a/ethcontract/src/int.rs +++ b/ethcontract/src/int.rs @@ -28,9 +28,7 @@ fn twos_complement(u: U256) -> U256 { fn handle_overflow((result, overflow): (T, bool)) -> T { #[cfg(debug_assertions)] { - if overflow { - panic!("overflow"); - } + assert!(!overflow, "overflow"); } let _ = overflow; @@ -1708,9 +1706,7 @@ mod tests { #[test] #[cfg_attr(debug_assertions, should_panic)] fn div_euclid_overflow() { - // We only attempt to print here because of the must_use - // error: unused return value of `int::I256::div_euclid` that must be used - println!("{}", I256::MIN.div_euclid(-I256::one())); + let _ = I256::MIN.div_euclid(-I256::one()); } #[test]