Skip to content

Commit

Permalink
chore: be able to match in between events rather than strictly full-s…
Browse files Browse the repository at this point in the history
…equences
  • Loading branch information
Evalir committed May 11, 2023
1 parent 28af854 commit 420d814
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
12 changes: 11 additions & 1 deletion evm/src/executor/inspector/cheatcodes/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,23 @@ pub fn handle_expect_emit(state: &mut Cheatcodes, log: RawLog, address: &Address
event_to_fill_or_check.found &= expected.data == log.data;
}
}

// If we found the event, we can push it to the back of the queue
// and begin expecting the next event.
if event_to_fill_or_check.found {
state.expected_emits.push_back(event_to_fill_or_check);
} else {
// We did not match this event, so we need to keep waiting for the right one to appear.
state.expected_emits.push_front(event_to_fill_or_check);
}

}
// Fill the event.
None => {
event_to_fill_or_check.log = Some(log);
state.expected_emits.push_back(event_to_fill_or_check);
}
}
state.expected_emits.push_back(event_to_fill_or_check);
}

#[derive(Clone, Debug, Default)]
Expand Down
42 changes: 42 additions & 0 deletions testdata/cheats/ExpectEmit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ contract Emitter {
/// Ref: issue #760
event SomethingElse(uint256 data);

event SomethingNonIndexed(uint256 data);

function emitEvent(uint256 topic1, uint256 topic2, uint256 topic3, uint256 data) public {
emit Something(topic1, topic2, topic3, data);
}
Expand All @@ -36,6 +38,13 @@ contract Emitter {
emitNested(Emitter(address(this)), 1, 2, 3, 4);
}

function emitOutOfExactOrder() public {
emit SomethingNonIndexed(1);
emit Something(1, 2, 3, 4);
emit Something(1, 2, 3, 4);
emit Something(1, 2, 3, 4);
}

function emitNested(Emitter inner, uint256 topic1, uint256 topic2, uint256 topic3, uint256 data) public {
inner.emitEvent(topic1, topic2, topic3, data);
}
Expand Down Expand Up @@ -74,6 +83,8 @@ contract ExpectEmitTest is DSTest {

event SomethingElse(uint256 indexed topic1);

event SomethingNonIndexed(uint256 data);

function setUp() public {
emitter = new Emitter();
}
Expand Down Expand Up @@ -217,6 +228,37 @@ contract ExpectEmitTest is DSTest {
);
}

function testExpectEmitCanMatchWithoutExactOrder() public {
cheats.expectEmit(true, true, true, true);
emit Something(1, 2, 3, 4);
cheats.expectEmit(true, true, true, true);
emit Something(1, 2, 3, 4);

emitter.emitOutOfExactOrder();
}

function testFailExpectEmitCanMatchWithoutExactOrder() public {
cheats.expectEmit(true, true, true, true);
emit Something(1, 2, 3, 4);
// This should fail, as this event is never emitted
// in between the other two Something events.
cheats.expectEmit(true, true, true, true);
emit SomethingElse(1);
cheats.expectEmit(true, true, true, true);
emit Something(1, 2, 3, 4);

emitter.emitOutOfExactOrder();
}

function testExpectEmitCanMatchWithoutExactOrder2() public {
cheats.expectEmit(true, true, true, true);
emit SomethingNonIndexed(1);
cheats.expectEmit(true, true, true, true);
emit Something(1, 2, 3, 4);

emitter.emitOutOfExactOrder();
}

function testExpectEmitAddress() public {
cheats.expectEmit(address(emitter));
emit Something(1, 2, 3, 4);
Expand Down

0 comments on commit 420d814

Please sign in to comment.