From c5580874756bcfd6992f7a7f0611feeb45477885 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Tue, 30 Mar 2021 04:38:18 +0200 Subject: [PATCH] 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 c848d5f85..2cda55634 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -1179,10 +1179,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 ae6b72ff2..b467196f4 100644 --- a/contracts/cw20-atomic-swap/src/contract.rs +++ b/contracts/cw20-atomic-swap/src/contract.rs @@ -458,10 +458,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 = HandleMsg::Release { @@ -480,11 +480,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); @@ -494,11 +493,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", &[]); @@ -519,10 +514,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] @@ -558,22 +553,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 = HandleMsg::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); @@ -594,10 +584,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 92d2148f6..2bd309020 100644 --- a/contracts/cw20-base/src/contract.rs +++ b/contracts/cw20-base/src/contract.rs @@ -498,8 +498,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") ); } @@ -695,10 +695,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(), &[]); @@ -708,10 +708,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(), &[]); @@ -760,10 +760,10 @@ mod tests { let env = mock_env(); let msg = HandleMsg::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 @@ -816,10 +816,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 20b6b1cd3..878fc3d33 100644 --- a/contracts/cw20-escrow/src/contract.rs +++ b/contracts/cw20-escrow/src/contract.rs @@ -358,10 +358,10 @@ mod tests { let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); let res = execute(deps.as_mut(), mock_env(), info, HandleMsg::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] @@ -441,10 +441,10 @@ mod tests { let id = create.id.clone(); let info = mock_info(&create.arbiter, &[]); let res = execute(deps.as_mut(), mock_env(), info, HandleMsg::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 b876dba49..3f86bb091 100644 --- a/contracts/cw4-stake/src/contract.rs +++ b/contracts/cw4-stake/src/contract.rs @@ -557,17 +557,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]