Skip to content

Commit

Permalink
Fix assert usage in tests (#724)
Browse files Browse the repository at this point in the history
Co-authored-by: Dusan Stanivukovic <[email protected]>
Co-authored-by: Nicholas Rodrigues Lordello <[email protected]>
  • Loading branch information
3 people authored Feb 18, 2022
1 parent 71548b2 commit df3d57b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
4 changes: 1 addition & 3 deletions ethcontract-common/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl Bytecode {
S: AsRef<str>,
{
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
Expand Down
14 changes: 8 additions & 6 deletions ethcontract-generate/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 12 additions & 14 deletions ethcontract-mock/src/details/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -785,9 +782,10 @@ impl Method {
index: usize,
generation: usize,
) -> &mut Expectation<P, R> {
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<P, R> = self
.expectations
Expand Down
8 changes: 5 additions & 3 deletions ethcontract-mock/src/details/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
19 changes: 9 additions & 10 deletions ethcontract-mock/src/details/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
Expand Down
8 changes: 2 additions & 6 deletions ethcontract/src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ fn twos_complement(u: U256) -> U256 {
fn handle_overflow<T>((result, overflow): (T, bool)) -> T {
#[cfg(debug_assertions)]
{
if overflow {
panic!("overflow");
}
assert!(!overflow, "overflow");
}

let _ = overflow;
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit df3d57b

Please sign in to comment.