Skip to content

[Feature]: support specifying the number of times an event was emitted #49

@0xNeshi

Description

@0xNeshi
Collaborator

What is the feature you would like to see?

The proposed syntax is:

contract.assert_emitted(event: Event).times(number_of_times: u32);

Possible values for number_of_times:

  • 0 - assert that the event was never emitted <-- (optional) could create a new extension like contract.assert_never_emitted that does the same
  • 1+ - assert that event was emitted this exact number of times.

To give a dumb, and yet possible example where this would be useful would be a Vault that can execute multiple pending withdrawal requests in one transaction, collecting fees for the Vault owner and emitting something like FeeCollected for each withdrawal.

#[test]
fn multiple_withdrawals_work(
    owner: Account,
    alice: Account,
    bob: Account
) {
    let vault = Contract::<Vault>::new();

    // set up the vault

    // alice and bob invest in the vault
    
    // alice and bob initiate withdrawal requests
    
    vault.withdraw_pending().expect("ok");
    
    // Assert emitted exactly twice with macro
    assert_emitted!(vault, FeeCollected { }, 2);
    // or as a contract extension
    vault.assert_emitted(FeeCollected { }).times(2);
}

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @0xNeshi

        Issue actions

          [Feature]: support specifying the number of times an event was emitted · Issue #49 · OpenZeppelin/stylus-test-helpers