From 181214124bcd82f30bfd50de73aa3cf2e1d7f75b Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 14:04:46 +0100 Subject: [PATCH 01/12] Implement PartialEq for cw1-subkeys errors --- contracts/cw1-subkeys/src/contract.rs | 12 +++--------- contracts/cw1-subkeys/src/error.rs | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 4e87bc455..3a8abad82 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -1171,10 +1171,7 @@ mod tests { // spender2 cannot spend funds (no initial allowance) let info = mock_info(&spender2, &[]); let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - match res.unwrap_err() { - ContractError::NoAllowance { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::NoAllowance {}, res.unwrap_err()); // But spender1 can (he has enough funds) let info = mock_info(&spender1, &[]); @@ -1189,7 +1186,7 @@ mod tests { let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); match res.unwrap_err() { ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("unexpected error: {}", e), + e => panic!("unexpected error: {:?}", e), } // Owner / admins can do anything (at the contract level) @@ -1218,10 +1215,7 @@ mod tests { // But not for mere mortals let info = mock_info(&spender1, &[]); let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - match res.unwrap_err() { - ContractError::MessageTypeRejected { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::MessageTypeRejected {}, res.unwrap_err()); } #[test] diff --git a/contracts/cw1-subkeys/src/error.rs b/contracts/cw1-subkeys/src/error.rs index 03f43f3e5..5ca741732 100644 --- a/contracts/cw1-subkeys/src/error.rs +++ b/contracts/cw1-subkeys/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From 509056d55597b5a52185816ff8c5904d75f92f96 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 14:11:25 +0100 Subject: [PATCH 02/12] Implement PartialEq for cw1-whitelist errors --- contracts/cw1-whitelist/src/contract.rs | 20 ++++---------------- contracts/cw1-whitelist/src/error.rs | 2 +- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/contracts/cw1-whitelist/src/contract.rs b/contracts/cw1-whitelist/src/contract.rs index 2bf84e4e8..140dcf0a1 100644 --- a/contracts/cw1-whitelist/src/contract.rs +++ b/contracts/cw1-whitelist/src/contract.rs @@ -187,10 +187,7 @@ mod tests { }; let info = mock_info(&anyone, &[]); let res = execute(deps.as_mut(), mock_env(), info, msg); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); // but alice can kick out carl let msg = ExecuteMsg::UpdateAdmins { @@ -209,10 +206,7 @@ mod tests { // carl cannot freeze it let info = mock_info(&carl, &[]); let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Freeze {}); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); // but bob can let info = mock_info(&bob, &[]); @@ -229,10 +223,7 @@ mod tests { }; let info = mock_info(&alice, &[]); let res = execute(deps.as_mut(), mock_env(), info, msg); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); } #[test] @@ -272,10 +263,7 @@ mod tests { // bob cannot execute them let info = mock_info(&bob, &[]); let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); // but carl can let info = mock_info(&carl, &[]); diff --git a/contracts/cw1-whitelist/src/error.rs b/contracts/cw1-whitelist/src/error.rs index dc19f1033..9a5e44903 100644 --- a/contracts/cw1-whitelist/src/error.rs +++ b/contracts/cw1-whitelist/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From bde08cdf0690d3d0e3685b8a09c1031b508a2000 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:12:20 +0100 Subject: [PATCH 03/12] Implement PartialEq for cw20-atomic-swap errors --- contracts/cw20-atomic-swap/src/contract.rs | 53 ++++++---------------- contracts/cw20-atomic-swap/src/error.rs | 2 +- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/contracts/cw20-atomic-swap/src/contract.rs b/contracts/cw20-atomic-swap/src/contract.rs index 61ffeef3d..68e0aee21 100644 --- a/contracts/cw20-atomic-swap/src/contract.rs +++ b/contracts/cw20-atomic-swap/src/contract.rs @@ -339,11 +339,7 @@ mod tests { info.clone(), ExecuteMsg::Create(create.clone()), ); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::InvalidId {}) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!(ContractError::InvalidId {}, res.unwrap_err()); } // Cannot create, no funds @@ -360,11 +356,7 @@ mod tests { info, ExecuteMsg::Create(create.clone()), ); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::EmptyBalance {}) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!(ContractError::EmptyBalance {}, res.unwrap_err()); // Cannot create, expired let info = mock_info(&sender, &balance); @@ -380,11 +372,7 @@ mod tests { info, ExecuteMsg::Create(create.clone()), ); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::Expired) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!(ContractError::Expired {}, res.unwrap_err()); // Cannot create, invalid hash let info = mock_info(&sender, &balance); @@ -400,13 +388,10 @@ mod tests { info, ExecuteMsg::Create(create.clone()), ); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::ParseError(msg)) => { - assert_eq!(msg, "Invalid character \'u\' at position 1".to_string()) - } - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!( + ContractError::ParseError("Invalid character \'u\' at position 1".into()), + res.unwrap_err() + ); // Can create, all valid let info = mock_info(&sender, &balance); @@ -441,11 +426,7 @@ mod tests { info, ExecuteMsg::Create(create.clone()), ); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::AlreadyExists {}) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!(ContractError::AlreadyExists {}, res.unwrap_err()); } #[test] @@ -482,10 +463,9 @@ mod tests { preimage: preimage(), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::Std(StdError::NotFound { .. })) => {} - Err(e) => panic!("unexpected error: {:?}", e), + match res.unwrap_err() { + ContractError::Std(StdError::NotFound { .. }) => {} + e => panic!("unexpected error: {:?}", e), } // Cannot release, invalid hash @@ -494,13 +474,10 @@ mod tests { preimage: "bu115h17".to_string(), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::ParseError(msg)) => { - assert_eq!(msg, "Invalid character \'u\' at position 1".to_string()) - } - Err(e) => panic!("unexpected error: {:?}", e), - } + assert_eq!( + ContractError::ParseError("Invalid character \'u\' at position 1".to_string()), + res.unwrap_err() + ); // Cannot release, wrong hash let release = ExecuteMsg::Release { diff --git a/contracts/cw20-atomic-swap/src/error.rs b/contracts/cw20-atomic-swap/src/error.rs index c8032118b..8c00064e1 100644 --- a/contracts/cw20-atomic-swap/src/error.rs +++ b/contracts/cw20-atomic-swap/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From 9a4849e5a9fa52f516f1e32b2c3bcdaaae22893c Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:18:59 +0100 Subject: [PATCH 04/12] Improve cw20-base tests error handling --- contracts/cw20-base/src/contract.rs | 51 +++++++++-------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/contracts/cw20-base/src/contract.rs b/contracts/cw20-base/src/contract.rs index 68fb0f93e..5714d1f4e 100644 --- a/contracts/cw20-base/src/contract.rs +++ b/contracts/cw20-base/src/contract.rs @@ -502,10 +502,10 @@ mod tests { let info = mock_info(&HumanAddr("creator".to_string()), &[]); let env = mock_env(); let res = instantiate(deps.as_mut(), env.clone(), info.clone(), instantiate_msg); - match res.unwrap_err() { - StdError::GenericErr { msg, .. } => assert_eq!(&msg, "Initial supply greater than cap"), - e => panic!("Unexpected error: {}", e), - } + assert_eq!( + StdError::generic_err("Initial supply greater than cap"), + res.unwrap_err() + ); } #[test] @@ -541,10 +541,7 @@ mod tests { let info = mock_info(&minter, &[]); let env = mock_env(); let res = execute(deps.as_mut(), env, info, msg.clone()); - match res.unwrap_err() { - ContractError::InvalidZeroAmount {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); // but if it exceeds cap (even over multiple rounds), it fails // cap is enforced @@ -555,10 +552,7 @@ mod tests { let info = mock_info(&minter, &[]); let env = mock_env(); let res = execute(deps.as_mut(), env, info, msg.clone()); - match res.unwrap_err() { - ContractError::CannotExceedCap {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::CannotExceedCap {}, res.unwrap_err()); } #[test] @@ -579,10 +573,7 @@ mod tests { let info = mock_info(&HumanAddr::from("anyone else"), &[]); let env = mock_env(); let res = execute(deps.as_mut(), env, info, msg.clone()); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("expected unauthorized error, got {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); } #[test] @@ -597,10 +588,7 @@ mod tests { let info = mock_info(&HumanAddr::from("genesis"), &[]); let env = mock_env(); let res = execute(deps.as_mut(), env, info, msg.clone()); - match res.unwrap_err() { - ContractError::Unauthorized { .. } => {} - e => panic!("expected unauthorized error, got {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); } #[test] @@ -702,10 +690,7 @@ mod tests { amount: Uint128::zero(), }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::InvalidZeroAmount {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); // cannot send more than we have let info = mock_info(addr1.clone(), &[]); @@ -717,7 +702,7 @@ mod tests { let res = execute(deps.as_mut(), env, info, msg); match res.unwrap_err() { ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), + e => panic!("Unexpected error: {:?}", e), } // cannot send from empty account @@ -730,7 +715,7 @@ mod tests { let res = execute(deps.as_mut(), env, info, msg); match res.unwrap_err() { ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), + e => panic!("Unexpected error: {:?}", e), } // valid transfer @@ -769,10 +754,7 @@ mod tests { amount: Uint128::zero(), }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::InvalidZeroAmount {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); assert_eq!( query_token_info(deps.as_ref()).unwrap().total_supply, amount1 @@ -785,7 +767,7 @@ mod tests { let res = execute(deps.as_mut(), env, info, msg); match res.unwrap_err() { ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), + e => panic!("Unexpected error: {:?}", e), } assert_eq!( query_token_info(deps.as_ref()).unwrap().total_supply, @@ -828,10 +810,7 @@ mod tests { msg: Some(send_msg.clone()), }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::InvalidZeroAmount {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); // cannot send more than we have let info = mock_info(addr1.clone(), &[]); @@ -844,7 +823,7 @@ mod tests { let res = execute(deps.as_mut(), env, info, msg); match res.unwrap_err() { ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), + e => panic!("Unexpected error: {:?}", e), } // valid transfer From 6604ef625d6b7c6512ff3a97386f3cd0faafdfaa Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:25:17 +0100 Subject: [PATCH 05/12] Implement PartialEq for cw20-escrow errors --- contracts/cw20-escrow/src/contract.rs | 9 +++------ contracts/cw20-escrow/src/error.rs | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/contracts/cw20-escrow/src/contract.rs b/contracts/cw20-escrow/src/contract.rs index a0fa63e9d..99747b490 100644 --- a/contracts/cw20-escrow/src/contract.rs +++ b/contracts/cw20-escrow/src/contract.rs @@ -365,7 +365,7 @@ mod tests { let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); match res.unwrap_err() { ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {}", e), + e => panic!("Expected NotFound, got {:?}", e), } } @@ -448,7 +448,7 @@ mod tests { let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); match res.unwrap_err() { ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {}", e), + e => panic!("Expected NotFound, got {:?}", e), } } @@ -563,10 +563,7 @@ mod tests { }); let info = mock_info(&baz_token, &[]); let res = execute(deps.as_mut(), mock_env(), info, top_up); - match res.unwrap_err() { - ContractError::NotInWhitelist {} => {} - e => panic!("Unexpected error: {}", e), - } + assert_eq!(ContractError::NotInWhitelist {}, res.unwrap_err()); // top up with second foreign token let foo_token = HumanAddr::from("foo_token"); diff --git a/contracts/cw20-escrow/src/error.rs b/contracts/cw20-escrow/src/error.rs index d78745672..32512ef8b 100644 --- a/contracts/cw20-escrow/src/error.rs +++ b/contracts/cw20-escrow/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From 628600b32cd9b486f4a5d7aaf1d9237bccde00c1 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:35:56 +0100 Subject: [PATCH 06/12] Implement PartialEq for cw20-staking errors --- contracts/cw20-staking/src/contract.rs | 28 +++++++++++++++----------- contracts/cw20-staking/src/error.rs | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/contracts/cw20-staking/src/contract.rs b/contracts/cw20-staking/src/contract.rs index b42da18f9..15d914fe4 100644 --- a/contracts/cw20-staking/src/contract.rs +++ b/contracts/cw20-staking/src/contract.rs @@ -547,10 +547,12 @@ mod tests { // make sure we can instantiate with this let res = instantiate(deps.as_mut(), mock_env(), info, msg.clone()); - match res.unwrap_err() { - ContractError::NotInValidatorSet { .. } => {} - _ => panic!("expected unregistered validator error"), - } + assert_eq!( + ContractError::NotInValidatorSet { + validator: "my-validator".into() + }, + res.unwrap_err() + ); } #[test] @@ -729,10 +731,12 @@ mod tests { // try to bond and make sure we trigger delegation let res = execute(deps.as_mut(), mock_env(), info, bond_msg); - match res.unwrap_err() { - ContractError::EmptyBalance { .. } => {} - e => panic!("Expected wrong denom error, got: {:?}", e), - }; + assert_eq!( + ContractError::EmptyBalance { + denom: "ustake".to_string() + }, + res.unwrap_err() + ); } #[test] @@ -776,10 +780,10 @@ mod tests { }; let info = mock_info(&creator, &[]); let res = execute(deps.as_mut(), mock_env(), info, unbond_msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!( + ContractError::Std(StdError::underflow(0, 600)), + res.unwrap_err() + ); // bob unbonds 600 tokens at 10% tax... // 60 are taken and send to the owner diff --git a/contracts/cw20-staking/src/error.rs b/contracts/cw20-staking/src/error.rs index 3d6499fb1..8b2bb15c0 100644 --- a/contracts/cw20-staking/src/error.rs +++ b/contracts/cw20-staking/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::{StdError, Uint128}; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From f09cd4508cded1996667428bbd32f281ec833ff8 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:38:38 +0100 Subject: [PATCH 07/12] Implement PartialEq for cw721-base errors --- contracts/cw721-base/src/contract.rs | 21 ++++----------------- contracts/cw721-base/src/error.rs | 2 +- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/contracts/cw721-base/src/contract.rs b/contracts/cw721-base/src/contract.rs index b12939984..c69ec593c 100644 --- a/contracts/cw721-base/src/contract.rs +++ b/contracts/cw721-base/src/contract.rs @@ -666,10 +666,7 @@ mod tests { // random cannot mint let random = mock_info("random", &[]); let err = execute(deps.as_mut(), mock_env(), random, mint_msg.clone()).unwrap_err(); - match err { - ContractError::Unauthorized {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, err); // minter can mint let allowed = mock_info(MINTER, &[]); @@ -714,10 +711,7 @@ mod tests { let allowed = mock_info(MINTER, &[]); let err = execute(deps.as_mut(), mock_env(), allowed, mint_msg2).unwrap_err(); - match err { - ContractError::Claimed {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Claimed {}, err); // list the token_ids let tokens = query_all_tokens(deps.as_ref(), None, None).unwrap(); @@ -754,11 +748,7 @@ mod tests { }; let err = execute(deps.as_mut(), mock_env(), random, transfer_msg.clone()).unwrap_err(); - - match err { - ContractError::Unauthorized {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, err); // owner can let random = mock_info("venus", &[]); @@ -816,10 +806,7 @@ mod tests { let random = mock_info("random", &[]); let err = execute(deps.as_mut(), mock_env(), random, send_msg.clone()).unwrap_err(); - match err { - ContractError::Unauthorized {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, err); // but owner can let random = mock_info("venus", &[]); diff --git a/contracts/cw721-base/src/error.rs b/contracts/cw721-base/src/error.rs index 14d97acc5..b665db5c8 100644 --- a/contracts/cw721-base/src/error.rs +++ b/contracts/cw721-base/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From df5dfd406885991be6784c044aa7e5cc7abda5a7 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 16:46:52 +0100 Subject: [PATCH 08/12] Implement PartialEq for cw3-fixed-multisig errors --- contracts/cw3-fixed-multisig/src/contract.rs | 120 +++---------------- contracts/cw3-fixed-multisig/src/error.rs | 2 +- 2 files changed, 16 insertions(+), 106 deletions(-) diff --git a/contracts/cw3-fixed-multisig/src/contract.rs b/contracts/cw3-fixed-multisig/src/contract.rs index d9829dd2e..bc04ca018 100644 --- a/contracts/cw3-fixed-multisig/src/contract.rs +++ b/contracts/cw3-fixed-multisig/src/contract.rs @@ -560,13 +560,7 @@ mod tests { max_voting_period, }; let res = instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::NoVoters {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::NoVoters {}, res.unwrap_err()); // Zero required weight fails let instantiate_msg = InstantiateMsg { @@ -575,13 +569,7 @@ mod tests { max_voting_period, }; let res = instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::ZeroWeight {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::ZeroWeight {}, res.unwrap_err()); // Total weight less than required weight not allowed let required_weight = 100; @@ -591,13 +579,7 @@ mod tests { required_weight, max_voting_period, ); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::UnreachableWeight {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::UnreachableWeight {}, res.unwrap_err()); // All valid let required_weight = 1; @@ -640,13 +622,7 @@ mod tests { latest: None, }; let res = execute(deps.as_mut(), mock_env(), info, proposal.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::Unauthorized {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); // Wrong expiration option fails let info = mock_info(OWNER, &[]); @@ -657,13 +633,7 @@ mod tests { latest: Some(Expiration::AtHeight(123456)), }; let res = execute(deps.as_mut(), mock_env(), info, proposal_wrong_exp); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::WrongExpiration {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::WrongExpiration {}, res.unwrap_err()); // Proposal from voter works let info = mock_info(VOTER3, &[]); @@ -739,24 +709,12 @@ mod tests { vote: Vote::Yes, }; let res = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::AlreadyVoted {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::AlreadyVoted {}, res.unwrap_err()); // Only voters can vote let info = mock_info(SOMEBODY, &[]); let res = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::Unauthorized {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); // But voter1 can let info = mock_info(VOTER1, &[]); @@ -806,13 +764,7 @@ mod tests { // Once voted, votes cannot be changed let res = execute(deps.as_mut(), mock_env(), info.clone(), yes_vote.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::AlreadyVoted {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::AlreadyVoted {}, res.unwrap_err()); assert_eq!(tally, get_tally(deps.as_ref(), proposal_id)); // Expired proposals cannot be voted @@ -821,13 +773,7 @@ mod tests { Duration::Height(duration) => mock_env_height(duration + 1), }; let res = execute(deps.as_mut(), env, info, no_vote); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::Expired {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::Expired {}, res.unwrap_err()); // Vote it again, so it passes let info = mock_info(VOTER4, &[]); @@ -852,13 +798,7 @@ mod tests { // non-Open proposals cannot be voted let info = mock_info(VOTER5, &[]); let res = execute(deps.as_mut(), mock_env(), info, yes_vote); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::NotOpen {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::NotOpen {}, res.unwrap_err()); } #[test] @@ -891,13 +831,7 @@ mod tests { // Only Passed can be executed let execution = ExecuteMsg::Execute { proposal_id }; let res = execute(deps.as_mut(), mock_env(), info.clone(), execution.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::WrongExecuteStatus {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::WrongExecuteStatus {}, res.unwrap_err()); // Vote it, so it passes let vote = ExecuteMsg::Vote { @@ -926,13 +860,7 @@ mod tests { // In passing: Try to close Passed fails let closing = ExecuteMsg::Close { proposal_id }; let res = execute(deps.as_mut(), mock_env(), info, closing); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::WrongCloseStatus {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); // Execute works. Anybody can execute Passed proposals let info = mock_info(SOMEBODY, &[]); @@ -956,13 +884,7 @@ mod tests { // In passing: Try to close Executed fails let closing = ExecuteMsg::Close { proposal_id }; let res = execute(deps.as_mut(), mock_env(), info, closing); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::WrongCloseStatus {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); } #[test] @@ -999,13 +921,7 @@ mod tests { // Non-expired proposals cannot be closed let res = execute(deps.as_mut(), mock_env(), info.clone(), closing.clone()); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::NotExpired {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::NotExpired {}, res.unwrap_err()); // Expired proposals can be closed let info = mock_info(OWNER, &[]); @@ -1050,12 +966,6 @@ mod tests { // Trying to close it again fails let res = execute(deps.as_mut(), mock_env(), info, closing); - - // Verify - assert!(res.is_err()); - match res.unwrap_err() { - ContractError::WrongCloseStatus {} => {} - e => panic!("unexpected error: {}", e), - } + assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); } } diff --git a/contracts/cw3-fixed-multisig/src/error.rs b/contracts/cw3-fixed-multisig/src/error.rs index 037d75cf3..78e0eb036 100644 --- a/contracts/cw3-fixed-multisig/src/error.rs +++ b/contracts/cw3-fixed-multisig/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::StdError; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From f0f9f0bf3d3c418f2031dba3fa127a8a7435341b Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 26 Mar 2021 17:01:12 +0100 Subject: [PATCH 09/12] Implement PartialEq for cw3-flex-multisig errors --- contracts/cw3-flex-multisig/src/contract.rs | 44 ++++++++++----------- contracts/cw3-flex-multisig/src/error.rs | 2 +- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/contracts/cw3-flex-multisig/src/contract.rs b/contracts/cw3-flex-multisig/src/contract.rs index f048f6a0e..1b418c87f 100644 --- a/contracts/cw3-flex-multisig/src/contract.rs +++ b/contracts/cw3-flex-multisig/src/contract.rs @@ -638,11 +638,9 @@ mod tests { &[], "zero required weight", ); - - // Verify assert_eq!( - res.unwrap_err(), - ContractError::ZeroThreshold {}.to_string() + ContractError::ZeroThreshold {}.to_string(), + res.unwrap_err() ); // Total weight less than required weight not allowed @@ -658,11 +656,9 @@ mod tests { &[], "high required weight", ); - - // Verify assert_eq!( - res.unwrap_err(), - ContractError::UnreachableThreshold {}.to_string() + ContractError::UnreachableThreshold {}.to_string(), + res.unwrap_err() ); // All valid @@ -722,7 +718,7 @@ mod tests { let proposal = pay_somebody_proposal(); // Only voters can propose let res = app.execute_contract(SOMEBODY, &flex_addr, &proposal, &[]); - assert_eq!(res.unwrap_err(), ContractError::Unauthorized {}.to_string()); + assert_eq!(ContractError::Unauthorized {}.to_string(), res.unwrap_err()); // Wrong expiration option fails let msgs = match proposal.clone() { @@ -737,8 +733,8 @@ mod tests { }; let res = app.execute_contract(OWNER, &flex_addr, &proposal_wrong_exp, &[]); assert_eq!( - res.unwrap_err(), - ContractError::WrongExpiration {}.to_string() + ContractError::WrongExpiration {}.to_string(), + res.unwrap_err() ); // Proposal from voter works @@ -932,13 +928,13 @@ mod tests { let err = app .execute_contract(OWNER, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::AlreadyVoted {}.to_string()); + assert_eq!(ContractError::AlreadyVoted {}.to_string(), err); // Only voters can vote let err = app .execute_contract(SOMEBODY, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::Unauthorized {}.to_string()); + assert_eq!(ContractError::Unauthorized {}.to_string(), err); // But voter1 can let res = app @@ -983,14 +979,14 @@ mod tests { let err = app .execute_contract(VOTER3, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::AlreadyVoted {}.to_string()); + assert_eq!(ContractError::AlreadyVoted {}.to_string(), err); // Expired proposals cannot be voted app.update_block(expire(voting_period)); let err = app .execute_contract(VOTER4, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::Expired {}.to_string()); + assert_eq!(ContractError::Expired {}.to_string(), err); app.update_block(unexpire(voting_period)); // Powerful voter supports it, so it passes @@ -1011,7 +1007,7 @@ mod tests { let err = app .execute_contract(VOTER5, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::NotOpen {}.to_string()); + assert_eq!(ContractError::NotOpen {}.to_string(), err); // query individual votes // initial (with 0 weight) @@ -1085,7 +1081,7 @@ mod tests { let err = app .execute_contract(OWNER, &flex_addr, &execution, &[]) .unwrap_err(); - assert_eq!(err, ContractError::WrongExecuteStatus {}.to_string()); + assert_eq!(ContractError::WrongExecuteStatus {}.to_string(), err); // Vote it, so it passes let vote = ExecuteMsg::Vote { @@ -1110,7 +1106,7 @@ mod tests { let err = app .execute_contract(OWNER, &flex_addr, &closing, &[]) .unwrap_err(); - assert_eq!(err, ContractError::WrongCloseStatus {}.to_string()); + assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err); // Execute works. Anybody can execute Passed proposals let res = app @@ -1135,7 +1131,7 @@ mod tests { let err = app .execute_contract(OWNER, &flex_addr, &closing, &[]) .unwrap_err(); - assert_eq!(err, ContractError::WrongCloseStatus {}.to_string()); + assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err); } #[test] @@ -1166,7 +1162,7 @@ mod tests { let err = app .execute_contract(SOMEBODY, &flex_addr, &closing, &[]) .unwrap_err(); - assert_eq!(err, ContractError::NotExpired {}.to_string()); + assert_eq!(ContractError::NotExpired {}.to_string(), err); // Expired proposals can be closed app.update_block(expire(voting_period)); @@ -1187,7 +1183,7 @@ mod tests { let err = app .execute_contract(SOMEBODY, &flex_addr, &closing, &[]) .unwrap_err(); - assert_eq!(err, ContractError::WrongCloseStatus {}.to_string()); + assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err); } // uses the power from the beginning of the voting period @@ -1296,7 +1292,7 @@ mod tests { let err = app .execute_contract(newbie, &flex_addr, &yes_vote, &[]) .unwrap_err(); - assert_eq!(err, ContractError::Unauthorized {}.to_string()); + assert_eq!(ContractError::Unauthorized {}.to_string(), err); // previously removed VOTER3 can still vote, passing the proposal app.execute_contract(VOTER3, &flex_addr, &yes_vote, &[]) @@ -1412,7 +1408,7 @@ mod tests { let err = app .execute_contract(VOTER3, &flex_addr, &cash_proposal, &[]) .unwrap_err(); - assert_eq!(err, ContractError::Unauthorized {}.to_string()); + assert_eq!(ContractError::Unauthorized {}.to_string(), err); // extra: ensure no one else can call the hook let hook_hack = ExecuteMsg::MemberChangedHook(MemberChangedHookMsg { @@ -1421,7 +1417,7 @@ mod tests { let err = app .execute_contract(VOTER2, &flex_addr, &hook_hack, &[]) .unwrap_err(); - assert_eq!(err, ContractError::Unauthorized {}.to_string()); + assert_eq!(ContractError::Unauthorized {}.to_string(), err); } // uses the power from the beginning of the voting period diff --git a/contracts/cw3-flex-multisig/src/error.rs b/contracts/cw3-flex-multisig/src/error.rs index df1096d3d..7709fa7be 100644 --- a/contracts/cw3-flex-multisig/src/error.rs +++ b/contracts/cw3-flex-multisig/src/error.rs @@ -1,7 +1,7 @@ use cosmwasm_std::{HumanAddr, StdError}; use thiserror::Error; -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum ContractError { #[error("{0}")] Std(#[from] StdError), From f5a671d08d85fe5e26716b1200edfe33fc33676a Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Tue, 30 Mar 2021 04:38:18 +0200 Subject: [PATCH 10/12] Improve error matching syntax by using `matches!` --- contracts/cw1-subkeys/src/contract.rs | 8 ++-- contracts/cw20-atomic-swap/src/contract.rs | 54 +++++++++------------- contracts/cw20-base/src/contract.rs | 36 +++++++-------- contracts/cw20-escrow/src/contract.rs | 16 +++---- contracts/cw4-stake/src/contract.rs | 16 ++----- 5 files changed, 57 insertions(+), 73 deletions(-) diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 3a8abad82..44c60099b 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -1184,10 +1184,10 @@ mod tests { // And then cannot (not enough funds anymore) let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::Underflow { .. }) + )); // Owner / admins can do anything (at the contract level) let info = mock_info(&owner.clone(), &[]); diff --git a/contracts/cw20-atomic-swap/src/contract.rs b/contracts/cw20-atomic-swap/src/contract.rs index 68e0aee21..7842cd4b5 100644 --- a/contracts/cw20-atomic-swap/src/contract.rs +++ b/contracts/cw20-atomic-swap/src/contract.rs @@ -463,10 +463,10 @@ mod tests { preimage: preimage(), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - match res.unwrap_err() { - ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); // Cannot release, invalid hash let release = ExecuteMsg::Release { @@ -485,11 +485,10 @@ mod tests { preimage: hex::encode(b"This is 32 bytes, but incorrect."), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::InvalidPreimage {}) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::InvalidPreimage {} + )); // Cannot release, expired let env = mock_env_height(123457); @@ -499,11 +498,7 @@ mod tests { preimage: preimage(), }; let res = execute(deps.as_mut(), env, info, release); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::Expired) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert!(matches!(res.unwrap_err(), ContractError::Expired)); // Can release, valid id, valid hash, and not expired let info = mock_info("somebody", &[]); @@ -524,10 +519,10 @@ mod tests { // Cannot release again let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - match res.unwrap_err() { - ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); } #[test] @@ -563,22 +558,17 @@ mod tests { id: "swap0002".to_string(), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), refund); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::Std(StdError::NotFound { .. })) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); // Cannot refund, not expired yet let refund = ExecuteMsg::Refund { id: "swap0001".to_string(), }; let res = execute(deps.as_mut(), mock_env(), info.clone(), refund); - match res { - Ok(_) => panic!("expected error"), - Err(ContractError::NotExpired {}) => {} - Err(e) => panic!("unexpected error: {:?}", e), - } + assert!(matches!(res.unwrap_err(), ContractError::NotExpired { .. })); // Anyone can refund, if already expired let env = mock_env_height(123457); @@ -599,10 +589,10 @@ mod tests { // Cannot refund again let res = execute(deps.as_mut(), env, info, refund); - match res.unwrap_err() { - ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); } #[test] diff --git a/contracts/cw20-base/src/contract.rs b/contracts/cw20-base/src/contract.rs index 5714d1f4e..e4db42c8a 100644 --- a/contracts/cw20-base/src/contract.rs +++ b/contracts/cw20-base/src/contract.rs @@ -503,8 +503,8 @@ mod tests { let env = mock_env(); let res = instantiate(deps.as_mut(), env.clone(), info.clone(), instantiate_msg); assert_eq!( - StdError::generic_err("Initial supply greater than cap"), - res.unwrap_err() + res.unwrap_err(), + StdError::generic_err("Initial supply greater than cap") ); } @@ -700,10 +700,10 @@ mod tests { amount: too_much, }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::Underflow { .. }) + )); // cannot send from empty account let info = mock_info(addr2.clone(), &[]); @@ -713,10 +713,10 @@ mod tests { amount: transfer, }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::Underflow { .. }) + )); // valid transfer let info = mock_info(addr1.clone(), &[]); @@ -765,10 +765,10 @@ mod tests { let env = mock_env(); let msg = ExecuteMsg::Burn { amount: too_much }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::Underflow { .. }) + )); assert_eq!( query_token_info(deps.as_ref()).unwrap().total_supply, amount1 @@ -821,10 +821,10 @@ mod tests { msg: Some(send_msg.clone()), }; let res = execute(deps.as_mut(), env, info, msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::Underflow { .. }) + )); // valid transfer let info = mock_info(addr1.clone(), &[]); diff --git a/contracts/cw20-escrow/src/contract.rs b/contracts/cw20-escrow/src/contract.rs index 99747b490..ed1c44c36 100644 --- a/contracts/cw20-escrow/src/contract.rs +++ b/contracts/cw20-escrow/src/contract.rs @@ -363,10 +363,10 @@ mod tests { let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); - match res.unwrap_err() { - ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); } #[test] @@ -446,10 +446,10 @@ mod tests { let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); - match res.unwrap_err() { - ContractError::Std(StdError::NotFound { .. }) => {} - e => panic!("Expected NotFound, got {:?}", e), - } + assert!(matches!( + res.unwrap_err(), + ContractError::Std(StdError::NotFound { .. }) + )); } #[test] diff --git a/contracts/cw4-stake/src/contract.rs b/contracts/cw4-stake/src/contract.rs index 6a17bae6e..b2c8a28da 100644 --- a/contracts/cw4-stake/src/contract.rs +++ b/contracts/cw4-stake/src/contract.rs @@ -562,17 +562,11 @@ mod tests { let mut env = mock_env(); env.block.height += 5; let info = mock_info(USER2, &[]); - let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); - match err { - ContractError::Std(StdError::Underflow { - minuend, - subtrahend, - }) => { - assert_eq!(minuend.as_str(), "5000"); - assert_eq!(subtrahend.as_str(), "5100"); - } - e => panic!("Unexpected error: {:?}", e), - } + let res = execute(deps.as_mut(), env, info, msg); + assert_eq!( + res.unwrap_err(), + ContractError::Std(StdError::underflow(5000, 5100)) + ); } #[test] From e14ba4ecd3b8110e4d0c682f0fa9035c3784fd72 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Tue, 30 Mar 2021 11:17:27 +0200 Subject: [PATCH 11/12] Move unwrap_err() outside of assert Standardize error args order Add extra matches! / error builders --- contracts/cw1-subkeys/src/contract.rs | 12 +-- contracts/cw1-whitelist/src/contract.rs | 16 ++-- contracts/cw20-atomic-swap/src/contract.rs | 86 +++++++++----------- contracts/cw20-base/src/allowances.rs | 65 ++++++--------- contracts/cw20-base/src/contract.rs | 49 +++++------ contracts/cw20-bonding/src/contract.rs | 8 +- contracts/cw20-escrow/src/contract.rs | 18 ++-- contracts/cw20-ics20/src/contract.rs | 7 +- contracts/cw20-staking/src/contract.rs | 19 ++--- contracts/cw3-fixed-multisig/src/contract.rs | 65 ++++++++------- contracts/cw3-flex-multisig/src/contract.rs | 57 +++++++------ contracts/cw4-stake/src/contract.rs | 7 +- contracts/cw721-base/src/contract.rs | 8 +- 13 files changed, 190 insertions(+), 227 deletions(-) diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 44c60099b..cf9c668d9 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -1170,8 +1170,8 @@ mod tests { // spender2 cannot spend funds (no initial allowance) let info = mock_info(&spender2, &[]); - let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - assert_eq!(ContractError::NoAllowance {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::NoAllowance {}); // But spender1 can (he has enough funds) let info = mock_info(&spender1, &[]); @@ -1183,9 +1183,9 @@ mod tests { ); // And then cannot (not enough funds anymore) - let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); + let err = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()).unwrap_err(); assert!(matches!( - res.unwrap_err(), + err, ContractError::Std(StdError::Underflow { .. }) )); @@ -1214,8 +1214,8 @@ mod tests { // But not for mere mortals let info = mock_info(&spender1, &[]); - let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - assert_eq!(ContractError::MessageTypeRejected {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::MessageTypeRejected {}); } #[test] diff --git a/contracts/cw1-whitelist/src/contract.rs b/contracts/cw1-whitelist/src/contract.rs index 140dcf0a1..4f99b0e67 100644 --- a/contracts/cw1-whitelist/src/contract.rs +++ b/contracts/cw1-whitelist/src/contract.rs @@ -186,8 +186,8 @@ mod tests { admins: vec![anyone.clone()], }; let info = mock_info(&anyone, &[]); - let res = execute(deps.as_mut(), mock_env(), info, msg); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); // but alice can kick out carl let msg = ExecuteMsg::UpdateAdmins { @@ -205,8 +205,8 @@ mod tests { // carl cannot freeze it let info = mock_info(&carl, &[]); - let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Freeze {}); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Freeze {}).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); // but bob can let info = mock_info(&bob, &[]); @@ -222,8 +222,8 @@ mod tests { admins: vec![alice.clone()], }; let info = mock_info(&alice, &[]); - let res = execute(deps.as_mut(), mock_env(), info, msg); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, msg).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); } #[test] @@ -262,8 +262,8 @@ mod tests { // bob cannot execute them let info = mock_info(&bob, &[]); - let res = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, execute_msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); // but carl can let info = mock_info(&carl, &[]); diff --git a/contracts/cw20-atomic-swap/src/contract.rs b/contracts/cw20-atomic-swap/src/contract.rs index 7842cd4b5..f971b45de 100644 --- a/contracts/cw20-atomic-swap/src/contract.rs +++ b/contracts/cw20-atomic-swap/src/contract.rs @@ -333,13 +333,14 @@ mod tests { recipient: HumanAddr::from("rcpt0001"), expires: Expiration::AtHeight(123456), }; - let res = execute( + let err = execute( deps.as_mut(), mock_env(), info.clone(), ExecuteMsg::Create(create.clone()), - ); - assert_eq!(ContractError::InvalidId {}, res.unwrap_err()); + ) + .unwrap_err(); + assert_eq!(err, ContractError::InvalidId {}); } // Cannot create, no funds @@ -350,13 +351,14 @@ mod tests { recipient: "rcpt0001".into(), expires: Expiration::AtHeight(123456), }; - let res = execute( + let err = execute( deps.as_mut(), mock_env(), info, ExecuteMsg::Create(create.clone()), - ); - assert_eq!(ContractError::EmptyBalance {}, res.unwrap_err()); + ) + .unwrap_err(); + assert_eq!(err, ContractError::EmptyBalance {}); // Cannot create, expired let info = mock_info(&sender, &balance); @@ -366,13 +368,14 @@ mod tests { recipient: "rcpt0001".into(), expires: Expiration::AtTime(1), }; - let res = execute( + let err = execute( deps.as_mut(), mock_env(), info, ExecuteMsg::Create(create.clone()), - ); - assert_eq!(ContractError::Expired {}, res.unwrap_err()); + ) + .unwrap_err(); + assert_eq!(err, ContractError::Expired {}); // Cannot create, invalid hash let info = mock_info(&sender, &balance); @@ -382,15 +385,16 @@ mod tests { recipient: "rcpt0001".into(), expires: Expiration::AtHeight(123456), }; - let res = execute( + let err = execute( deps.as_mut(), mock_env(), info, ExecuteMsg::Create(create.clone()), - ); + ) + .unwrap_err(); assert_eq!( - ContractError::ParseError("Invalid character \'u\' at position 1".into()), - res.unwrap_err() + err, + ContractError::ParseError("Invalid character \'u\' at position 1".into()) ); // Can create, all valid @@ -420,13 +424,14 @@ mod tests { recipient: "rcpt0001".into(), expires: Expiration::AtHeight(123456), }; - let res = execute( + let err = execute( deps.as_mut(), mock_env(), info, ExecuteMsg::Create(create.clone()), - ); - assert_eq!(ContractError::AlreadyExists {}, res.unwrap_err()); + ) + .unwrap_err(); + assert_eq!(err, ContractError::AlreadyExists {}); } #[test] @@ -462,21 +467,18 @@ mod tests { id: "swap0002".to_string(), preimage: preimage(), }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), mock_env(), info.clone(), release).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); // Cannot release, invalid hash let release = ExecuteMsg::Release { id: "swap0001".to_string(), preimage: "bu115h17".to_string(), }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), release); + let err = execute(deps.as_mut(), mock_env(), info.clone(), release).unwrap_err(); assert_eq!( - ContractError::ParseError("Invalid character \'u\' at position 1".to_string()), - res.unwrap_err() + err, + ContractError::ParseError("Invalid character \'u\' at position 1".to_string()) ); // Cannot release, wrong hash @@ -484,11 +486,8 @@ mod tests { id: "swap0001".to_string(), preimage: hex::encode(b"This is 32 bytes, but incorrect."), }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - assert!(matches!( - res.unwrap_err(), - ContractError::InvalidPreimage {} - )); + let err = execute(deps.as_mut(), mock_env(), info.clone(), release).unwrap_err(); + assert!(matches!(err, ContractError::InvalidPreimage {})); // Cannot release, expired let env = mock_env_height(123457); @@ -497,8 +496,8 @@ mod tests { id: "swap0001".to_string(), preimage: preimage(), }; - let res = execute(deps.as_mut(), env, info, release); - assert!(matches!(res.unwrap_err(), ContractError::Expired)); + let err = execute(deps.as_mut(), env, info, release).unwrap_err(); + assert!(matches!(err, ContractError::Expired)); // Can release, valid id, valid hash, and not expired let info = mock_info("somebody", &[]); @@ -518,11 +517,8 @@ mod tests { ); // Cannot release again - let res = execute(deps.as_mut(), mock_env(), info.clone(), release); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), mock_env(), info.clone(), release).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); } #[test] @@ -557,18 +553,15 @@ mod tests { let refund = ExecuteMsg::Refund { id: "swap0002".to_string(), }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), refund); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), mock_env(), info.clone(), refund).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); // Cannot refund, not expired yet let refund = ExecuteMsg::Refund { id: "swap0001".to_string(), }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), refund); - assert!(matches!(res.unwrap_err(), ContractError::NotExpired { .. })); + let err = execute(deps.as_mut(), mock_env(), info.clone(), refund).unwrap_err(); + assert!(matches!(err, ContractError::NotExpired { .. })); // Anyone can refund, if already expired let env = mock_env_height(123457); @@ -588,11 +581,8 @@ mod tests { ); // Cannot refund again - let res = execute(deps.as_mut(), env, info, refund); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), env, info, refund).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); } #[test] diff --git a/contracts/cw20-base/src/allowances.rs b/contracts/cw20-base/src/allowances.rs index 36fc4f272..7bd45304c 100644 --- a/contracts/cw20-base/src/allowances.rs +++ b/contracts/cw20-base/src/allowances.rs @@ -481,11 +481,8 @@ mod tests { amount: Uint128(7777), expires: None, }; - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::CannotSetOwnAccount {} => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert_eq!(err, ContractError::CannotSetOwnAccount {}); // decrease self-allowance let msg = ExecuteMsg::DecreaseAllowance { @@ -493,11 +490,8 @@ mod tests { amount: Uint128(7777), expires: None, }; - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::CannotSetOwnAccount {} => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert_eq!(err, ContractError::CannotSetOwnAccount {}); } #[test] @@ -556,11 +550,11 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert!(matches!( + err, + ContractError::Std(StdError::Underflow { .. }) + )); // let us increase limit, but set the expiration (default env height is 12_345) let info = mock_info(owner.clone(), &[]); @@ -580,11 +574,8 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Expired {} => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert_eq!(err, ContractError::Expired {}); } #[test] @@ -639,11 +630,11 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert!(matches!( + err, + ContractError::Std(StdError::Underflow { .. }) + )); // let us increase limit, but set the expiration (default env height is 12_345) let info = mock_info(owner.clone(), &[]); @@ -662,11 +653,8 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Expired {} => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert_eq!(err, ContractError::Expired {}); } #[test] @@ -746,11 +734,11 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Std(StdError::Underflow { .. }) => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert!(matches!( + err, + ContractError::Std(StdError::Underflow { .. }) + )); // let us increase limit, but set the expiration to current block (expired) let info = mock_info(owner.clone(), &[]); @@ -771,10 +759,7 @@ mod tests { }; let info = mock_info(spender.clone(), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env.clone(), info.clone(), msg); - match res.unwrap_err() { - ContractError::Expired {} => {} - e => panic!("Unexpected error: {}", e), - } + let err = execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap_err(); + assert_eq!(err, ContractError::Expired {}); } } diff --git a/contracts/cw20-base/src/contract.rs b/contracts/cw20-base/src/contract.rs index e4db42c8a..c7bd1af83 100644 --- a/contracts/cw20-base/src/contract.rs +++ b/contracts/cw20-base/src/contract.rs @@ -501,9 +501,10 @@ mod tests { }; let info = mock_info(&HumanAddr("creator".to_string()), &[]); let env = mock_env(); - let res = instantiate(deps.as_mut(), env.clone(), info.clone(), instantiate_msg); + let err = + instantiate(deps.as_mut(), env.clone(), info.clone(), instantiate_msg).unwrap_err(); assert_eq!( - res.unwrap_err(), + err, StdError::generic_err("Initial supply greater than cap") ); } @@ -540,8 +541,8 @@ mod tests { }; let info = mock_info(&minter, &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env, info, msg.clone()); - assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::InvalidZeroAmount {}); // but if it exceeds cap (even over multiple rounds), it fails // cap is enforced @@ -551,8 +552,8 @@ mod tests { }; let info = mock_info(&minter, &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env, info, msg.clone()); - assert_eq!(ContractError::CannotExceedCap {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::CannotExceedCap {}); } #[test] @@ -572,8 +573,8 @@ mod tests { }; let info = mock_info(&HumanAddr::from("anyone else"), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env, info, msg.clone()); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); } #[test] @@ -587,8 +588,8 @@ mod tests { }; let info = mock_info(&HumanAddr::from("genesis"), &[]); let env = mock_env(); - let res = execute(deps.as_mut(), env, info, msg.clone()); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg.clone()).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); } #[test] @@ -689,8 +690,8 @@ mod tests { recipient: addr2.clone(), amount: Uint128::zero(), }; - let res = execute(deps.as_mut(), env, info, msg); - assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); + assert_eq!(err, ContractError::InvalidZeroAmount {}); // cannot send more than we have let info = mock_info(addr1.clone(), &[]); @@ -699,9 +700,9 @@ mod tests { recipient: addr2.clone(), amount: too_much, }; - let res = execute(deps.as_mut(), env, info, msg); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); assert!(matches!( - res.unwrap_err(), + err, ContractError::Std(StdError::Underflow { .. }) )); @@ -712,9 +713,9 @@ mod tests { recipient: addr1.clone(), amount: transfer, }; - let res = execute(deps.as_mut(), env, info, msg); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); assert!(matches!( - res.unwrap_err(), + err, ContractError::Std(StdError::Underflow { .. }) )); @@ -753,8 +754,8 @@ mod tests { let msg = ExecuteMsg::Burn { amount: Uint128::zero(), }; - let res = execute(deps.as_mut(), env, info, msg); - assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); + assert_eq!(err, ContractError::InvalidZeroAmount {}); assert_eq!( query_token_info(deps.as_ref()).unwrap().total_supply, amount1 @@ -764,9 +765,9 @@ mod tests { let info = mock_info(addr1.clone(), &[]); let env = mock_env(); let msg = ExecuteMsg::Burn { amount: too_much }; - let res = execute(deps.as_mut(), env, info, msg); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); assert!(matches!( - res.unwrap_err(), + err, ContractError::Std(StdError::Underflow { .. }) )); assert_eq!( @@ -809,8 +810,8 @@ mod tests { amount: Uint128::zero(), msg: Some(send_msg.clone()), }; - let res = execute(deps.as_mut(), env, info, msg); - assert_eq!(ContractError::InvalidZeroAmount {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); + assert_eq!(err, ContractError::InvalidZeroAmount {}); // cannot send more than we have let info = mock_info(addr1.clone(), &[]); @@ -820,9 +821,9 @@ mod tests { amount: too_much, msg: Some(send_msg.clone()), }; - let res = execute(deps.as_mut(), env, info, msg); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); assert!(matches!( - res.unwrap_err(), + err, ContractError::Std(StdError::Underflow { .. }) )); diff --git a/contracts/cw20-bonding/src/contract.rs b/contracts/cw20-bonding/src/contract.rs index 167f526e3..0262dc67c 100644 --- a/contracts/cw20-bonding/src/contract.rs +++ b/contracts/cw20-bonding/src/contract.rs @@ -309,7 +309,7 @@ mod tests { use super::*; use crate::msg::CurveType; use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; - use cosmwasm_std::{coin, Decimal}; + use cosmwasm_std::{coin, Decimal, StdError}; use cw0::PaymentError; const DENOM: &str = "satoshi"; @@ -486,7 +486,7 @@ mod tests { amount: Uint128(3000), }; let err = execute(deps.as_mut(), mock_env(), info, burn).unwrap_err(); - assert_eq!("Cannot subtract 3000 from 2000", err.to_string().as_str()); + assert_eq!(err, ContractError::Std(StdError::underflow(3000, 2000))); // burn 1000 EPOXY to get back 15BTC (*10^8) let info = mock_info(INVESTOR, &[]); @@ -597,8 +597,8 @@ mod tests { }; let err = execute(deps.as_mut(), mock_env(), info, burn_from).unwrap_err(); assert_eq!( - "Cannot subtract 3300000 from 3000000", - err.to_string().as_str() + err, + ContractError::Std(StdError::underflow(3300000, 3000000)) ); // burn 1_000_000 EPOXY to get back 1_500 DENOM (constant curve) diff --git a/contracts/cw20-escrow/src/contract.rs b/contracts/cw20-escrow/src/contract.rs index ed1c44c36..039f945b6 100644 --- a/contracts/cw20-escrow/src/contract.rs +++ b/contracts/cw20-escrow/src/contract.rs @@ -362,11 +362,8 @@ mod tests { // second attempt fails (not found) let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); - let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); } #[test] @@ -445,11 +442,8 @@ mod tests { // second attempt fails (not found) let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); - let res = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }); - assert!(matches!( - res.unwrap_err(), - ContractError::Std(StdError::NotFound { .. }) - )); + let err = execute(deps.as_mut(), mock_env(), info, ExecuteMsg::Approve { id }).unwrap_err(); + assert!(matches!(err, ContractError::Std(StdError::NotFound { .. }))); } #[test] @@ -562,8 +556,8 @@ mod tests { msg: Some(to_binary(&base).unwrap()), }); let info = mock_info(&baz_token, &[]); - let res = execute(deps.as_mut(), mock_env(), info, top_up); - assert_eq!(ContractError::NotInWhitelist {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, top_up).unwrap_err(); + assert_eq!(err, ContractError::NotInWhitelist {}); // top up with second foreign token let foo_token = HumanAddr::from("foo_token"); diff --git a/contracts/cw20-ics20/src/contract.rs b/contracts/cw20-ics20/src/contract.rs index 09333b2da..2daea086c 100644 --- a/contracts/cw20-ics20/src/contract.rs +++ b/contracts/cw20-ics20/src/contract.rs @@ -227,7 +227,7 @@ mod test { assert_eq!(0, chan_res.total_sent.len()); assert_eq!(0, chan_res.balances.len()); - let not_found = query( + let err = query( deps.as_ref(), mock_env(), QueryMsg::Channel { @@ -235,10 +235,7 @@ mod test { }, ) .unwrap_err(); - assert_eq!( - not_found, - StdError::not_found("cw20_ics20::state::ChannelInfo") - ); + assert_eq!(err, StdError::not_found("cw20_ics20::state::ChannelInfo")); } #[test] diff --git a/contracts/cw20-staking/src/contract.rs b/contracts/cw20-staking/src/contract.rs index 15d914fe4..06d064c9b 100644 --- a/contracts/cw20-staking/src/contract.rs +++ b/contracts/cw20-staking/src/contract.rs @@ -546,12 +546,12 @@ mod tests { let info = mock_info(&creator, &[]); // make sure we can instantiate with this - let res = instantiate(deps.as_mut(), mock_env(), info, msg.clone()); + let err = instantiate(deps.as_mut(), mock_env(), info, msg.clone()).unwrap_err(); assert_eq!( + err, ContractError::NotInValidatorSet { validator: "my-validator".into() - }, - res.unwrap_err() + } ); } @@ -730,12 +730,12 @@ mod tests { let info = mock_info(&bob, &[coin(500, "photon")]); // try to bond and make sure we trigger delegation - let res = execute(deps.as_mut(), mock_env(), info, bond_msg); + let err = execute(deps.as_mut(), mock_env(), info, bond_msg).unwrap_err(); assert_eq!( + err, ContractError::EmptyBalance { denom: "ustake".to_string() - }, - res.unwrap_err() + } ); } @@ -779,11 +779,8 @@ mod tests { amount: Uint128(600), }; let info = mock_info(&creator, &[]); - let res = execute(deps.as_mut(), mock_env(), info, unbond_msg); - assert_eq!( - ContractError::Std(StdError::underflow(0, 600)), - res.unwrap_err() - ); + let err = execute(deps.as_mut(), mock_env(), info, unbond_msg).unwrap_err(); + assert_eq!(err, ContractError::Std(StdError::underflow(0, 600))); // bob unbonds 600 tokens at 10% tax... // 60 are taken and send to the owner diff --git a/contracts/cw3-fixed-multisig/src/contract.rs b/contracts/cw3-fixed-multisig/src/contract.rs index bc04ca018..68257e3d3 100644 --- a/contracts/cw3-fixed-multisig/src/contract.rs +++ b/contracts/cw3-fixed-multisig/src/contract.rs @@ -559,8 +559,9 @@ mod tests { required_weight: 1, max_voting_period, }; - let res = instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg); - assert_eq!(ContractError::NoVoters {}, res.unwrap_err()); + let err = + instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg).unwrap_err(); + assert_eq!(err, ContractError::NoVoters {}); // Zero required weight fails let instantiate_msg = InstantiateMsg { @@ -568,18 +569,20 @@ mod tests { required_weight: 0, max_voting_period, }; - let res = instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg); - assert_eq!(ContractError::ZeroWeight {}, res.unwrap_err()); + let err = + instantiate(deps.as_mut(), mock_env(), info.clone(), instantiate_msg).unwrap_err(); + assert_eq!(err, ContractError::ZeroWeight {}); // Total weight less than required weight not allowed let required_weight = 100; - let res = setup_test_case( + let err = setup_test_case( deps.as_mut(), info.clone(), required_weight, max_voting_period, - ); - assert_eq!(ContractError::UnreachableWeight {}, res.unwrap_err()); + ) + .unwrap_err(); + assert_eq!(err, ContractError::UnreachableWeight {}); // All valid let required_weight = 1; @@ -621,8 +624,8 @@ mod tests { msgs: msgs.clone(), latest: None, }; - let res = execute(deps.as_mut(), mock_env(), info, proposal.clone()); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, proposal.clone()).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); // Wrong expiration option fails let info = mock_info(OWNER, &[]); @@ -632,8 +635,8 @@ mod tests { msgs: msgs.clone(), latest: Some(Expiration::AtHeight(123456)), }; - let res = execute(deps.as_mut(), mock_env(), info, proposal_wrong_exp); - assert_eq!(ContractError::WrongExpiration {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, proposal_wrong_exp).unwrap_err(); + assert_eq!(err, ContractError::WrongExpiration {}); // Proposal from voter works let info = mock_info(VOTER3, &[]); @@ -708,13 +711,13 @@ mod tests { proposal_id, vote: Vote::Yes, }; - let res = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()); - assert_eq!(ContractError::AlreadyVoted {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()).unwrap_err(); + assert_eq!(err, ContractError::AlreadyVoted {}); // Only voters can vote let info = mock_info(SOMEBODY, &[]); - let res = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()); - assert_eq!(ContractError::Unauthorized {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, yes_vote.clone()).unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}); // But voter1 can let info = mock_info(VOTER1, &[]); @@ -763,8 +766,8 @@ mod tests { assert_eq!(tally, get_tally(deps.as_ref(), proposal_id)); // Once voted, votes cannot be changed - let res = execute(deps.as_mut(), mock_env(), info.clone(), yes_vote.clone()); - assert_eq!(ContractError::AlreadyVoted {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info.clone(), yes_vote.clone()).unwrap_err(); + assert_eq!(err, ContractError::AlreadyVoted {}); assert_eq!(tally, get_tally(deps.as_ref(), proposal_id)); // Expired proposals cannot be voted @@ -772,8 +775,8 @@ mod tests { Duration::Time(duration) => mock_env_time(duration + 1), Duration::Height(duration) => mock_env_height(duration + 1), }; - let res = execute(deps.as_mut(), env, info, no_vote); - assert_eq!(ContractError::Expired {}, res.unwrap_err()); + let err = execute(deps.as_mut(), env, info, no_vote).unwrap_err(); + assert_eq!(err, ContractError::Expired {}); // Vote it again, so it passes let info = mock_info(VOTER4, &[]); @@ -797,8 +800,8 @@ mod tests { // non-Open proposals cannot be voted let info = mock_info(VOTER5, &[]); - let res = execute(deps.as_mut(), mock_env(), info, yes_vote); - assert_eq!(ContractError::NotOpen {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, yes_vote).unwrap_err(); + assert_eq!(err, ContractError::NotOpen {}); } #[test] @@ -830,8 +833,8 @@ mod tests { // Only Passed can be executed let execution = ExecuteMsg::Execute { proposal_id }; - let res = execute(deps.as_mut(), mock_env(), info.clone(), execution.clone()); - assert_eq!(ContractError::WrongExecuteStatus {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info.clone(), execution.clone()).unwrap_err(); + assert_eq!(err, ContractError::WrongExecuteStatus {}); // Vote it, so it passes let vote = ExecuteMsg::Vote { @@ -859,8 +862,8 @@ mod tests { // In passing: Try to close Passed fails let closing = ExecuteMsg::Close { proposal_id }; - let res = execute(deps.as_mut(), mock_env(), info, closing); - assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, closing).unwrap_err(); + assert_eq!(err, ContractError::WrongCloseStatus {}); // Execute works. Anybody can execute Passed proposals let info = mock_info(SOMEBODY, &[]); @@ -883,8 +886,8 @@ mod tests { // In passing: Try to close Executed fails let closing = ExecuteMsg::Close { proposal_id }; - let res = execute(deps.as_mut(), mock_env(), info, closing); - assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, closing).unwrap_err(); + assert_eq!(err, ContractError::WrongCloseStatus {}); } #[test] @@ -920,8 +923,8 @@ mod tests { let info = mock_info(SOMEBODY, &[]); // Non-expired proposals cannot be closed - let res = execute(deps.as_mut(), mock_env(), info.clone(), closing.clone()); - assert_eq!(ContractError::NotExpired {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info.clone(), closing.clone()).unwrap_err(); + assert_eq!(err, ContractError::NotExpired {}); // Expired proposals can be closed let info = mock_info(OWNER, &[]); @@ -965,7 +968,7 @@ mod tests { ); // Trying to close it again fails - let res = execute(deps.as_mut(), mock_env(), info, closing); - assert_eq!(ContractError::WrongCloseStatus {}, res.unwrap_err()); + let err = execute(deps.as_mut(), mock_env(), info, closing).unwrap_err(); + assert_eq!(err, ContractError::WrongCloseStatus {}); } } diff --git a/contracts/cw3-flex-multisig/src/contract.rs b/contracts/cw3-flex-multisig/src/contract.rs index 1b418c87f..1532b7cf1 100644 --- a/contracts/cw3-flex-multisig/src/contract.rs +++ b/contracts/cw3-flex-multisig/src/contract.rs @@ -631,17 +631,16 @@ mod tests { threshold: Threshold::AbsoluteCount { weight: 0 }, max_voting_period, }; - let res = app.instantiate_contract( - flex_id, - OWNER, - &instantiate_msg, - &[], - "zero required weight", - ); - assert_eq!( - ContractError::ZeroThreshold {}.to_string(), - res.unwrap_err() - ); + let err = app + .instantiate_contract( + flex_id, + OWNER, + &instantiate_msg, + &[], + "zero required weight", + ) + .unwrap_err(); + assert_eq!(err, ContractError::ZeroThreshold {}.to_string()); // Total weight less than required weight not allowed let instantiate_msg = InstantiateMsg { @@ -649,17 +648,16 @@ mod tests { threshold: Threshold::AbsoluteCount { weight: 100 }, max_voting_period, }; - let res = app.instantiate_contract( - flex_id, - OWNER, - &instantiate_msg, - &[], - "high required weight", - ); - assert_eq!( - ContractError::UnreachableThreshold {}.to_string(), - res.unwrap_err() - ); + let err = app + .instantiate_contract( + flex_id, + OWNER, + &instantiate_msg, + &[], + "high required weight", + ) + .unwrap_err(); + assert_eq!(err, ContractError::UnreachableThreshold {}.to_string()); // All valid let instantiate_msg = InstantiateMsg { @@ -717,8 +715,10 @@ mod tests { let proposal = pay_somebody_proposal(); // Only voters can propose - let res = app.execute_contract(SOMEBODY, &flex_addr, &proposal, &[]); - assert_eq!(ContractError::Unauthorized {}.to_string(), res.unwrap_err()); + let err = app + .execute_contract(SOMEBODY, &flex_addr, &proposal, &[]) + .unwrap_err(); + assert_eq!(err, ContractError::Unauthorized {}.to_string()); // Wrong expiration option fails let msgs = match proposal.clone() { @@ -731,11 +731,10 @@ mod tests { msgs, latest: Some(Expiration::AtHeight(123456)), }; - let res = app.execute_contract(OWNER, &flex_addr, &proposal_wrong_exp, &[]); - assert_eq!( - ContractError::WrongExpiration {}.to_string(), - res.unwrap_err() - ); + let err = app + .execute_contract(OWNER, &flex_addr, &proposal_wrong_exp, &[]) + .unwrap_err(); + assert_eq!(err, ContractError::WrongExpiration {}.to_string()); // Proposal from voter works let res = app diff --git a/contracts/cw4-stake/src/contract.rs b/contracts/cw4-stake/src/contract.rs index b2c8a28da..deb39dd89 100644 --- a/contracts/cw4-stake/src/contract.rs +++ b/contracts/cw4-stake/src/contract.rs @@ -562,11 +562,8 @@ mod tests { let mut env = mock_env(); env.block.height += 5; let info = mock_info(USER2, &[]); - let res = execute(deps.as_mut(), env, info, msg); - assert_eq!( - res.unwrap_err(), - ContractError::Std(StdError::underflow(5000, 5100)) - ); + let err = execute(deps.as_mut(), env, info, msg).unwrap_err(); + assert_eq!(err, ContractError::Std(StdError::underflow(5000, 5100))); } #[test] diff --git a/contracts/cw721-base/src/contract.rs b/contracts/cw721-base/src/contract.rs index c69ec593c..26b04ec2f 100644 --- a/contracts/cw721-base/src/contract.rs +++ b/contracts/cw721-base/src/contract.rs @@ -666,7 +666,7 @@ mod tests { // random cannot mint let random = mock_info("random", &[]); let err = execute(deps.as_mut(), mock_env(), random, mint_msg.clone()).unwrap_err(); - assert_eq!(ContractError::Unauthorized {}, err); + assert_eq!(err, ContractError::Unauthorized {}); // minter can mint let allowed = mock_info(MINTER, &[]); @@ -711,7 +711,7 @@ mod tests { let allowed = mock_info(MINTER, &[]); let err = execute(deps.as_mut(), mock_env(), allowed, mint_msg2).unwrap_err(); - assert_eq!(ContractError::Claimed {}, err); + assert_eq!(err, ContractError::Claimed {}); // list the token_ids let tokens = query_all_tokens(deps.as_ref(), None, None).unwrap(); @@ -748,7 +748,7 @@ mod tests { }; let err = execute(deps.as_mut(), mock_env(), random, transfer_msg.clone()).unwrap_err(); - assert_eq!(ContractError::Unauthorized {}, err); + assert_eq!(err, ContractError::Unauthorized {}); // owner can let random = mock_info("venus", &[]); @@ -806,7 +806,7 @@ mod tests { let random = mock_info("random", &[]); let err = execute(deps.as_mut(), mock_env(), random, send_msg.clone()).unwrap_err(); - assert_eq!(ContractError::Unauthorized {}, err); + assert_eq!(err, ContractError::Unauthorized {}); // but owner can let random = mock_info("venus", &[]); From bc9584dfb68abe3c1e322b4b8d32be311ae954cb Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Tue, 30 Mar 2021 11:34:16 +0200 Subject: [PATCH 12/12] Add additional Base error wrapper --- contracts/cw20-bonding/src/contract.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/contracts/cw20-bonding/src/contract.rs b/contracts/cw20-bonding/src/contract.rs index 0262dc67c..87c0cb767 100644 --- a/contracts/cw20-bonding/src/contract.rs +++ b/contracts/cw20-bonding/src/contract.rs @@ -486,7 +486,12 @@ mod tests { amount: Uint128(3000), }; let err = execute(deps.as_mut(), mock_env(), info, burn).unwrap_err(); - assert_eq!(err, ContractError::Std(StdError::underflow(3000, 2000))); + assert_eq!( + err, + ContractError::Base(cw20_base::ContractError::Std(StdError::underflow( + 2000, 3000 + ))) + ); // burn 1000 EPOXY to get back 15BTC (*10^8) let info = mock_info(INVESTOR, &[]); @@ -598,7 +603,9 @@ mod tests { let err = execute(deps.as_mut(), mock_env(), info, burn_from).unwrap_err(); assert_eq!( err, - ContractError::Std(StdError::underflow(3300000, 3000000)) + ContractError::Base(cw20_base::ContractError::Std(StdError::underflow( + 3000000, 3300000 + ))) ); // burn 1_000_000 EPOXY to get back 1_500 DENOM (constant curve)