Skip to content

Commit

Permalink
Switching from String to anyhow::Error for type erased error type in …
Browse files Browse the repository at this point in the history
…multi-test
  • Loading branch information
hashedone committed Aug 17, 2021
1 parent b2cadc2 commit 163b345
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 239 deletions.
106 changes: 92 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 23 additions & 17 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ mod tests {
None,
)
.unwrap_err();
assert_eq!(err, ContractError::ZeroThreshold {}.to_string());
assert_eq!(ContractError::ZeroThreshold {}, err.downcast().unwrap());

// Total weight less than required weight not allowed
let instantiate_msg = InstantiateMsg {
Expand All @@ -635,7 +635,10 @@ mod tests {
None,
)
.unwrap_err();
assert_eq!(err, ContractError::UnreachableThreshold {}.to_string());
assert_eq!(
ContractError::UnreachableThreshold {},
err.downcast().unwrap()
);

// All valid
let instantiate_msg = InstantiateMsg {
Expand Down Expand Up @@ -703,7 +706,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(SOMEBODY), flex_addr.clone(), &proposal, &[])
.unwrap_err();
assert_eq!(err, ContractError::Unauthorized {}.to_string());
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());

// Wrong expiration option fails
let msgs = match proposal.clone() {
Expand All @@ -724,7 +727,7 @@ mod tests {
&[],
)
.unwrap_err();
assert_eq!(err, ContractError::WrongExpiration {}.to_string());
assert_eq!(ContractError::WrongExpiration {}, err.downcast().unwrap());

// Proposal from voter works
let res = app
Expand Down Expand Up @@ -920,13 +923,13 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(OWNER), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::AlreadyVoted {}.to_string(), err);
assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());

// Only voters can vote
let err = app
.execute_contract(Addr::unchecked(SOMEBODY), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::Unauthorized {}.to_string(), err);
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());

// But voter1 can
let res = app
Expand Down Expand Up @@ -971,14 +974,14 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(VOTER3), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::AlreadyVoted {}.to_string(), err);
assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());

// Expired proposals cannot be voted
app.update_block(expire(voting_period));
let err = app
.execute_contract(Addr::unchecked(VOTER4), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::Expired {}.to_string(), err);
assert_eq!(ContractError::Expired {}, err.downcast().unwrap());
app.update_block(unexpire(voting_period));

// Powerful voter supports it, so it passes
Expand All @@ -999,7 +1002,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(VOTER5), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::NotOpen {}.to_string(), err);
assert_eq!(ContractError::NotOpen {}, err.downcast().unwrap());

// query individual votes
// initial (with 0 weight)
Expand Down Expand Up @@ -1073,7 +1076,10 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(OWNER), flex_addr.clone(), &execution, &[])
.unwrap_err();
assert_eq!(ContractError::WrongExecuteStatus {}.to_string(), err);
assert_eq!(
ContractError::WrongExecuteStatus {},
err.downcast().unwrap()
);

// Vote it, so it passes
let vote = ExecuteMsg::Vote {
Expand All @@ -1098,7 +1104,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(OWNER), flex_addr.clone(), &closing, &[])
.unwrap_err();
assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err);
assert_eq!(ContractError::WrongCloseStatus {}, err.downcast().unwrap());

// Execute works. Anybody can execute Passed proposals
let res = app
Expand Down Expand Up @@ -1128,7 +1134,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(OWNER), flex_addr, &closing, &[])
.unwrap_err();
assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err);
assert_eq!(ContractError::WrongCloseStatus {}, err.downcast().unwrap());
}

#[test]
Expand Down Expand Up @@ -1159,7 +1165,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(SOMEBODY), flex_addr.clone(), &closing, &[])
.unwrap_err();
assert_eq!(ContractError::NotExpired {}.to_string(), err);
assert_eq!(ContractError::NotExpired {}, err.downcast().unwrap());

// Expired proposals can be closed
app.update_block(expire(voting_period));
Expand All @@ -1180,7 +1186,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(SOMEBODY), flex_addr, &closing, &[])
.unwrap_err();
assert_eq!(ContractError::WrongCloseStatus {}.to_string(), err);
assert_eq!(ContractError::WrongCloseStatus {}, err.downcast().unwrap());
}

// uses the power from the beginning of the voting period
Expand Down Expand Up @@ -1289,7 +1295,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(newbie), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::Unauthorized {}.to_string(), err);
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());

// previously removed VOTER3 can still vote, passing the proposal
app.execute_contract(Addr::unchecked(VOTER3), flex_addr.clone(), &yes_vote, &[])
Expand Down Expand Up @@ -1420,7 +1426,7 @@ mod tests {
&[],
)
.unwrap_err();
assert_eq!(ContractError::Unauthorized {}.to_string(), err);
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());

// extra: ensure no one else can call the hook
let hook_hack = ExecuteMsg::MemberChangedHook(MemberChangedHookMsg {
Expand All @@ -1429,7 +1435,7 @@ mod tests {
let err = app
.execute_contract(Addr::unchecked(VOTER2), flex_addr.clone(), &hook_hack, &[])
.unwrap_err();
assert_eq!(ContractError::Unauthorized {}.to_string(), err);
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());
}

// uses the power from the beginning of the voting period
Expand Down
3 changes: 3 additions & 0 deletions packages/multi-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ documentation = "https://docs.cosmwasm.com"
default = ["iterator"]
iterator = ["cosmwasm-std/iterator"]
stargate = ["cosmwasm-std/stargate"]
backtrace = ["anyhow/backtrace"]

[dependencies]
cw0 = { path = "../../packages/cw0", version = "0.8.0" }
Expand All @@ -24,3 +25,5 @@ itertools = "0.10.1"
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
prost = "0.8.0"
anyhow = "1"
thiserror = "1"
Loading

0 comments on commit 163b345

Please sign in to comment.