-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switching from String to anyhow::Error for error type in multi-test #389
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 { | ||
|
@@ -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() { | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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 { | ||
|
@@ -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 | ||
|
@@ -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] | ||
|
@@ -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)); | ||
|
@@ -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 | ||
|
@@ -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, &[]) | ||
|
@@ -1420,7 +1426,7 @@ mod tests { | |
&[], | ||
) | ||
.unwrap_err(); | ||
assert_eq!(ContractError::Unauthorized {}.to_string(), err); | ||
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to look at docs for How does it know the type? Does it work with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Downcast is basically generic with signature Note - because of how it worsk, the downcasted error have to be on the righ side of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see it does runtime checks... I tried: let unwrapped = err.downcast::<&str>().unwrap();
assert_eq!("unauthorized", unwrapped); and got
Anyway, makes better sense to me how this works in practice. Code is fine, just my learning the new lib. |
||
|
||
// extra: ensure no one else can call the hook | ||
let hook_hack = ExecuteMsg::MemberChangedHook(MemberChangedHookMsg { | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼 I think this is more robust. Probably more efficient too.