Skip to content

Commit

Permalink
creating test case for emit events
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoB1911 committed Oct 1, 2024
1 parent 6a23ced commit 8286da2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
52 changes: 48 additions & 4 deletions crates/evm/src/backend/starknet_backend.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,18 @@ fn commit_storage(ref self: State) -> Result<(), EVMError> {

#[cfg(test)]
mod tests {
use core::starknet::ClassHash;
use core::starknet::{ClassHash, ContractAddress};
use crate::backend::starknet_backend;
use crate::model::Address;
use crate::model::{Address, Event, MockEvent};
use crate::model::account::Account;
use crate::state::{State, StateTrait};
use crate::test_utils::{
setup_test_environment, uninitialized_account, account_contract, register_account
};
use crate::test_utils::{evm_address};
use snforge_std::{test_address, start_mock_call, get_class_hash};
use snforge_std::{test_address, start_mock_call, get_class_hash, spy_events, EventSpyAssertionsTrait};
use snforge_utils::snforge_utils::{assert_not_called, assert_called};
use super::commit_storage;
use super::{commit_storage, emit_events};
use utils::helpers::compute_starknet_address;
use utils::traits::bytes::U8SpanExTrait;

Expand All @@ -274,6 +274,22 @@ mod tests {
}
}

// Helper function to serialize events into mock events
fn serialize_mock_events(events: Array<Event>, ref mock_events: Array<(ContractAddress, MockEvent)>) {
let contract_address = test_address();

// Serialize and append each event
for event in events {
let mut serialized_keys = Default::default();
let mut serialized_data = Default::default();

Serde::<Array<u256>>::serialize(@event.keys, ref serialized_keys);
Serde::<Array<u8>>::serialize(@event.data, ref serialized_data);

mock_events.append((contract_address, MockEvent { keys: serialized_keys, data: serialized_data }));
}
}

mod test_commit_storage {
use snforge_std::start_mock_call;
use snforge_utils::snforge_utils::{assert_called_with, assert_not_called};
Expand Down Expand Up @@ -479,6 +495,34 @@ mod tests {
assert_called(starknet_address, selector!("set_code_hash"));
assert_called(starknet_address, selector!("set_nonce"));
}

#[test]
fn test_emit_events() {
let mut state: State = Default::default();
let mut mock_events = ArrayTrait::<(ContractAddress, MockEvent)>::new();

// Prepare events
let events = array![
Event { keys: array![], data: array![] }, // Empty event
Event { keys: array![1.into()], data: array![2, 3] }, // Single key, multiple data
Event { keys: array![4.into(), 5.into()], data: array![6] }, // Multiple keys, single data
Event { keys: array![7.into(), 8.into(), 9.into()], data: array![10, 11, 12, 13] } // Multiple keys and data
];

// Serialize events and store in mock_events
serialize_mock_events(events.clone(), ref mock_events);

// Add events to state
for event in events {
state.add_event(event);
};

// Emit and assert events
let mut spy = spy_events();
emit_events(ref state).expect('emit events failed');
assert!(state.events.is_empty());
spy.assert_emitted(@mock_events);
}
}
// #[test]
// #[ignore]
Expand Down
21 changes: 21 additions & 0 deletions crates/evm/src/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ pub struct Event {
pub data: Array<u8>,
}

/// Represents a mock event used for assertions in tests.
#[derive(Drop, Clone, Default, PartialEq)]
pub struct MockEvent {
/// Serialized keys associated with the event.
pub keys: Array<felt252>,
/// Serialized data associated with the event.
pub data: Array<felt252>,
}

/// Implementation of the EventTrait for testing purposes using MockEvent.
/// This is used to simulate event handling, specifically appending keys and data,
impl EventTraitImpl of starknet::Event<MockEvent> {
fn append_keys_and_data(self: @MockEvent, ref keys: Array<felt252>, ref data: Array<felt252>) {
keys.append_span(self.keys.span());
data.append_span(self.data.span());
}
fn deserialize(ref keys: Span<felt252>, ref data: Span<felt252>) -> Option<MockEvent> {
Option::None
}
}

/// Represents an address in both EVM and Starknet formats.
#[derive(Copy, Drop, PartialEq, Default, Debug)]
pub struct Address {
Expand Down

0 comments on commit 8286da2

Please sign in to comment.