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

Improve Event testing api #3043

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kkawula
Copy link
Member

@kkawula kkawula commented Mar 4, 2025

Closes #3041

Introduced changes

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Performed self-review of the code
  • Added changes to CHANGELOG.md

@kkawula kkawula force-pushed the kkawula/3041-improve-eventspy-cheatcode-api branch from 0d983c7 to a5c9b80 Compare March 4, 2025 17:39
@kkawula kkawula marked this pull request as ready for review March 4, 2025 18:17
@kkawula kkawula requested a review from a team as a code owner March 4, 2025 18:17
@cptartur cptartur self-requested a review March 5, 2025 09:01
@@ -2,6 +2,7 @@ use snforge_std::{
declare, ContractClassTrait, DeclareResultTrait, spy_events, EventSpyAssertionsTrait,
EventSpyTrait, // Add for fetching events directly
Event, // A structure describing a raw `Event`
IsEmitted // Method for checking if a given event was ever emitted
Copy link
Member

Choose a reason for hiding this comment

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

It is not a method

@@ -104,16 +104,18 @@ impl EventSpyAssertionsTraitImpl<
}

fn is_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
self: @Events, expected_emitted_by: @ContractAddress, expected_event: @T
self: @Array<(ContractAddress, Event)>,
Copy link
Member

Choose a reason for hiding this comment

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

Rename this pls xD

Comment on lines 111 to 113
let mut expected_keys = array![];
let mut expected_data = array![];
expected_event.append_keys_and_data(ref expected_keys, ref expected_data);
Copy link
Member

Choose a reason for hiding this comment

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

Use into here

Comment on lines 120 to 121
if from == expected_emitted_by
&& event.keys == @expected_keys
Copy link
Member

Choose a reason for hiding this comment

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

Use == here (now you have PartialEq, tuple also implements this)

@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Forge

#### Added

- Testing events api improvements. New `IsEmitted` trait. `Into` and `PartialEq` trait implementations for snforge_std Events.
Copy link
Member

Choose a reason for hiding this comment

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

  1. Into what?
  2. PartialEq is implemented for Event not Events (maybe we should do it for both)?
  3. snforge_std::Event (markdown formatting)

@@ -104,16 +104,18 @@ impl EventSpyAssertionsTraitImpl<
}

fn is_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
Copy link
Member

Choose a reason for hiding this comment

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

[nit] public should be above private

@@ -127,6 +129,34 @@ fn is_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
return is_emitted;
}

pub trait IsEmitted {
fn is_emitted(
self: @Array<(ContractAddress, Event)>,
Copy link
Member

Choose a reason for hiding this comment

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

This should be implemented on @Events not on array imo (you always get Events from get_events and the events field is public). Ignore if it was consulted with the team previously

) -> bool {
let mut expected_keys = array![];
let mut expected_data = array![];
expected_event.append_keys_and_data(ref expected_keys, ref expected_data);

let mut i = 0;
let mut is_emitted = false;
while i < self.events.len() {
let (from, event) = self.events.at(i);
while i < self.len() {
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 use for in Cairo now to iterate over arrays

@@ -127,6 +129,34 @@ fn is_emitted<T, impl TEvent: starknet::Event<T>, impl TDrop: Drop<T>>(
return is_emitted;
}

pub trait IsEmitted {
fn is_emitted(
Copy link
Member

Choose a reason for hiding this comment

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

[nit] idk if is_emitted is good, maybe contains_event to distinct from assert_emitted, but idk

Comment on lines +67 to +68
5. We can make sure that the event mock was emitted.
6. Emitted events can be explicitly compared.
Copy link
Member

Choose a reason for hiding this comment

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

[nit] Idk if 5 and 6 shouldn't be before 3 and 4 since it is the default method.

Also a note, there is no example where snforge_std::Event is directly created, but it is out of scope

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.

Improve EventSpy cheatcode API
2 participants