-
Notifications
You must be signed in to change notification settings - Fork 386
Add generate_spy_event_helpers macro #1481
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
base: main
Are you sure you want to change the base?
Conversation
Add verification of indexed keys in event assertions
- Tests will fail if indexed fields are changed in event definitions - Must explicitly specify expected indexed fields
…into feature/check-indexed-keys-#1054
…into feature/check-indexed-keys-#1054
…into feature/check-indexed-keys-#1054
…luding AccessControl, Ownable, Account, EthAccount, Votes, Pausable, ERC1155, ERC20, ERC4626, and ERC721. Each test verifies the correct indexed keys for events such as role grants, ownership transfers, and token transfers.
…rki/cairo-contracts into feat/check-indexed-keys-#1054
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1481 +/- ##
==========================================
- Coverage 93.11% 92.46% -0.65%
==========================================
Files 82 82
Lines 2251 2230 -21
==========================================
- Hits 2096 2062 -34
- Misses 155 168 +13
... and 17 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
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.
Looking good! Left a suggestion to introduce a helper trait for building expected events
/// | ||
/// let expected = Event { keys, data }; | ||
/// self.assert_only_event(contract, expected); | ||
/// } |
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.
What do you think about introducing a helper trait for building expected events?
#[generate_trait]
pub impl ExpectedEventImpl of ExpectedEvent {
fn new() -> Event {
Event { keys: array![], data: array![] }
}
fn key<T, +Serde<T>, +Drop<T>>(self: Event, value: T) -> Event {
let Event { mut keys, data } = self;
value.serialize(ref keys);
Event { keys, data }
}
fn data<T, +Serde<T>, +Drop<T>>(self: Event, value: T) -> Event {
let Event { keys, mut data } = self;
value.serialize(ref data);
Event { keys, data }
}
}
The the generated code (and also checks that won't use the macro) will become more concise and just look nicer. For example, the code example in the article will change from this:
let mut keys = array![];
keys.append_serde(selector!("Transfer")); // event selector
keys.append_serde(from); // key #1
keys.append_serde(to); // key #2
let mut data = array![];
data.append_serde(value); // non-key data
let expected = Event { keys, data };
spy.assert_emitted(
@array![(contract_address, expected)],
);
to this:
let expected = ExpectedEvent::new()
.key(selector!("Transfer"))
.key(from)
.key(to)
.data(value);
spy.assert_emitted(
@array![(contract_address, expected)]
);
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.
Looks very helpful indeed
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.
Note that we need to merge this PR first, I'm going to add that trait there and refactor the existing tests to use it.
…eat/check-indexed-keys-#1054
…eat/check-indexed-keys-#1054
…dd-spy-event-helper-macro
Fixes #1480
This PR is still missing documentation, and we can improve: