Skip to content

Commit

Permalink
Improve error matching syntax by using matches!
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Mar 30, 2021
1 parent 009fa34 commit c558087
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 73 deletions.
8 changes: 4 additions & 4 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), &[]);
Expand Down
54 changes: 22 additions & 32 deletions contracts/cw20-atomic-swap/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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", &[]);
Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand All @@ -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]
Expand Down
36 changes: 18 additions & 18 deletions contracts/cw20-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
);
}

Expand Down Expand Up @@ -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(), &[]);
Expand All @@ -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(), &[]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(), &[]);
Expand Down
16 changes: 8 additions & 8 deletions contracts/cw20-escrow/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
16 changes: 5 additions & 11 deletions contracts/cw4-stake/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit c558087

Please sign in to comment.