Skip to content
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

Add update tests #1083

Merged
merged 14 commits into from
Aug 23, 2024
Merged

Conversation

andrew-fleming
Copy link
Collaborator

Fixes #1063.

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

@andrew-fleming andrew-fleming requested a review from immrsd August 9, 2024 04:22
Copy link
Collaborator

@immrsd immrsd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I've left just two minor suggestions

Comment on lines 567 to 569
assert_eq!(initial_supply, current_supply, "Incorrect supply before transfer");
assert_eq!(state.balance_of(sender), SUPPLY, "Incorrect balance before transfer");
assert_eq!(state.balance_of(recipient), 0, "Incorrect balance before transfer");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor one:

Suggested change
assert_eq!(initial_supply, current_supply, "Incorrect supply before transfer");
assert_eq!(state.balance_of(sender), SUPPLY, "Incorrect balance before transfer");
assert_eq!(state.balance_of(recipient), 0, "Incorrect balance before transfer");
assert_eq!(initial_supply, current_supply, "Incorrect supply before transfer");
assert_eq!(state.balance_of(sender), SUPPLY, "Incorrect sender balance before transfer");
assert_eq!(state.balance_of(recipient), 0, "Incorrect recipient balance before transfer");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this doesn't add much for the potential inconsistencies it might generate. The rule is to add messages when it very clearly adds clarity. I don't feel this is the case, and if it is not that clear, we should not add them.

Comment on lines 578 to 581
assert_eq!(
state.balance_of(sender), initial_supply - amount, "Incorrect balance after transfer"
);
assert_eq!(state.balance_of(recipient), amount, "Incorrect balance after transfer");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_eq!(
state.balance_of(sender), initial_supply - amount, "Incorrect balance after transfer"
);
assert_eq!(state.balance_of(recipient), amount, "Incorrect balance after transfer");
assert_eq!(
state.balance_of(sender), initial_supply - amount, "Incorrect sender balance after transfer"
);
assert_eq!(state.balance_of(recipient), amount, "Incorrect recipient balance after transfer");

Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks andrew. I still don't feel the messages added add enough clarity to be kept. Besides that, it looks good to me.

@@ -98,7 +98,8 @@ fn test_approve() {
let mut spy = spy_events();

start_cheat_caller_address(contract_address, OWNER());
assert!(state.approve(SPENDER(), VALUE));
let success = state.approve(SPENDER(), VALUE);
assert!(success, "Transfer failed");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert!(success, "Transfer failed");
assert!(success, "Approval failed");

Is this error message adding real value?

Copy link
Collaborator

@immrsd immrsd Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To my mind, it does once you have to debug the failing test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can go to the specific line from the test error message, and in this particular case, I think it is quite clear that that line error implies that the approval failed. How exactly does this help debug?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foundry output doesn't specify the number of the line that failed. However, it does write the variable names passed to the assertion and their values

For example, let's take the test cases for transfers. Both and are called in a single test cases and both helper functions contain the same assertion assert_eq!(initial_supply, current_supply);

How to find out which one has failed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point, but I don't think "Approval failed" is going to help with that if we add the same message. Adding before and after (in the other examples) differentiates and is true it makes it easier to find, but if the message intention is only to find the line easier, we may directly put the line as a message, and I don't think the message should be used for that. We can/should push for snforge to print the line, but I'm still against using messages for this purpose.

Copy link
Collaborator

@immrsd immrsd Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. Previously some error messages, like "Approval failed" here, did make it more obvious which exact assertion has failed. But after migration things got better since SnFoundry does a pretty good job printing the assertion itself and the names of variables passed to it

I shall agree with you, most of the error messages can be safely omitted. In some cases, thought, we should keep them until SnFoundry starts printing the assertion's line number

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericnordelo @immrsd so I changed this back and removed the error messages because whether we need these error messages or not for debugging is clearly debatable. Because it's debatable, I'm favoring consistency with how we've already used the assert_state_x assertions in the erc721 and erc1155 tests (with no error messages).

I propose we create a new issue and continue this discussion there. If we were to conclude that we should include error messages in these assertions, It'd be nice to also update the other token tests as well


assert_state_before_transfer(OWNER(), RECIPIENT());
let success = state.transfer(RECIPIENT(), VALUE);
assert!(success, "Transfer failed");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is error message is adding real value, you guys think it does?

Copy link
Collaborator

@immrsd immrsd Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some testing practices recommend to follow the rule "one test case — one assertion". If it was the case for our tests, then adding error messages would make no difference. But in most cases we have 3+ assertions and an error occurs in a test, then it may be difficult to find out which assertion has failed

Comment on lines 567 to 569
assert_eq!(initial_supply, current_supply, "Incorrect supply before transfer");
assert_eq!(state.balance_of(sender), SUPPLY, "Incorrect balance before transfer");
assert_eq!(state.balance_of(recipient), 0, "Incorrect balance before transfer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this doesn't add much for the potential inconsistencies it might generate. The rule is to add messages when it very clearly adds clarity. I don't feel this is the case, and if it is not that clear, we should not add them.

@immrsd immrsd self-requested a review August 9, 2024 18:37
Copy link
Member

@ericnordelo ericnordelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@andrew-fleming andrew-fleming merged commit 7ce10ff into OpenZeppelin:main Aug 23, 2024
6 checks passed
@andrew-fleming andrew-fleming deleted the add-update-tests branch August 23, 2024 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests for ERC20 update fn
3 participants