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

Test token hooks #1198

Merged
merged 10 commits into from
Nov 7, 2024
Merged

Test token hooks #1198

merged 10 commits into from
Nov 7, 2024

Conversation

ggonzalez94
Copy link
Collaborator

@ggonzalez94 ggonzalez94 commented Nov 4, 2024

Fixes #1062

  • Checking that the hooks are called is a bit tricky, the best way to do it is emitting an event from a new mock that also lets you validate that they are called with the right parameters.
  • I didn't embed camelCase implementations for the new mock since I don't think there's a any reason to support it on the hooks tests.

PR Checklist

  • Tests

Copy link

codecov bot commented Nov 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.56%. Comparing base (8404eb9) to head (2f0899d).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1198      +/-   ##
==========================================
- Coverage   91.57%   91.56%   -0.02%     
==========================================
  Files          47       47              
  Lines        1223     1233      +10     
==========================================
+ Hits         1120     1129       +9     
- Misses        103      104       +1     

see 6 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8404eb9...2f0899d. Read the comment docs.

@ggonzalez94 ggonzalez94 marked this pull request as ready for review November 4, 2024 16:43
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!

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!

packages/test_common/src/mocks/erc20.cairo Outdated Show resolved Hide resolved
Co-authored-by: immrsd <103599616+immrsd@users.noreply.github.com>
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Good call on using events to test the hooks! I left a question and some minor suggestions. Also, I think we should apply the same tests to erc1155 as well so all token hooks are tested before we close out the issue

Comment on lines 656 to 676
fn assert_event_before_update(
ref self: EventSpy,
contract: ContractAddress,
from: ContractAddress,
recipient: ContractAddress,
amount: u256
) {
let expected = ERC20MockWithHooks::Event::BeforeUpdate(
ERC20MockWithHooks::BeforeUpdate { from, recipient, amount }
);
self.assert_emitted_single(contract, expected);
}

fn assert_event_after_update(
ref self: EventSpy,
contract: ContractAddress,
from: ContractAddress,
recipient: ContractAddress,
amount: u256
) {
let expected = ERC20MockWithHooks::Event::AfterUpdate(
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about adding assert_no_events_left_from(contract) to these?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you agree, I'd apply this to erc721 as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

update itself emits a Transfer event, so I think this would only make sense on the after hook. But not sure how valuable it is?
I'll prioritize the other comments + adding the tests to ERC1155 and then see if I can add this

Copy link
Collaborator

Choose a reason for hiding this comment

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

update itself emits a Transfer event, so I think this would only make sense on the after hook

To be comprehensive, my thinking is to test that only the target hook is executed and only once. I suppose you can argue that we know it works as expected through the myriad other tests we have...your call if you think it's worth it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we are getting the release today I'll go ahead and merge. Will try to add this to a next PR. Thanks!

packages/token/src/tests/erc20/test_erc20.cairo Outdated Show resolved Hide resolved
packages/token/src/tests/erc20/test_erc20.cairo Outdated Show resolved Hide resolved
packages/token/src/tests/erc721/test_erc721.cairo Outdated Show resolved Hide resolved
packages/token/src/tests/erc721/test_erc721.cairo Outdated Show resolved Hide resolved
@ggonzalez94
Copy link
Collaborator Author

Comments have been incorporated + renamed the new mocks to Snake<ERCStandard>MockWithHooks to be more consistent with the rest of the mocks in the same file.
This is now ready for final review and merge

@ggonzalez94 ggonzalez94 requested a review from immrsd November 7, 2024 12:54
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

LGTM! Left a final non-blocking comment

@ggonzalez94 ggonzalez94 merged commit 5fec9a5 into main Nov 7, 2024
8 checks passed
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 token hooks
4 participants