Skip to content
Snippets Groups Projects
Unverified Commit 6a3d10b3 authored by Raymond Cheung's avatar Raymond Cheung Committed by GitHub
Browse files

Simplify event assertion with predicate-based check (#7734)


A follow-up PR to simplify event assertions by introducing
`contains_event`, allowing event checks without needing exact field
matches. This reduces redundancy and makes tests more flexible.

Partially addresses #6119 by providing an alternative way to assert
events.

Reference: [PR #7594 -
Discussion](https://github.com/paritytech/polkadot-sdk/pull/7594#discussion_r1965566349)

---------

Co-authored-by: default avatarcmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: default avatarBranislav Kontur <bkontur@gmail.com>
Co-authored-by: default avatarBastian Köcher <git@kchr.de>
parent cc83fba1
No related merge requests found
Pipeline #517333 waiting for manual action with stages
in 1 hour, 32 minutes, and 35 seconds
......@@ -26,6 +26,21 @@ fn buy_execution<C>(fees: impl Into<Asset>) -> Instruction<C> {
BuyExecution { fees: fees.into(), weight_limit: Unlimited }
}
/// Helper macro to check if a system event exists in the event list.
///
/// Example usage:
/// ```ignore
/// assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
/// assert!(system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. })));
/// ```
macro_rules! system_contains_event {
($runtime:ident, $variant:ident($($pattern:tt)*)) => {
$runtime::System::events().iter().any(|e| {
matches!(e.event, $runtime::RuntimeEvent::$variant($($pattern)*))
})
};
}
#[test]
fn remote_account_ids_work() {
child_account_account_id(1, ALICE);
......@@ -53,11 +68,7 @@ fn dmp() {
});
ParaA::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
});
}
......@@ -81,11 +92,7 @@ fn ump() {
});
Relay::execute_with(|| {
use relay_chain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(relay_chain, System(frame_system::Event::Remarked { .. })));
});
}
......@@ -109,11 +116,7 @@ fn xcmp() {
});
ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::System(frame_system::Event::Remarked { .. })
)));
assert!(system_contains_event!(parachain, System(frame_system::Event::Remarked { .. })));
});
}
......@@ -137,18 +140,12 @@ fn reserve_transfer() {
INITIAL_BALANCE + withdraw_amount
);
// Ensure expected events were emitted
let events = relay_chain::System::events();
let attempted_count = count_relay_chain_events(&events, |event| {
matches!(
event,
relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { .. })
)
});
let sent_count = count_relay_chain_events(&events, |event| {
matches!(event, relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Sent { .. }))
});
assert_eq!(attempted_count, 1, "Expected one XcmPallet::Attempted event");
assert_eq!(sent_count, 1, "Expected one XcmPallet::Sent event");
let attempted_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. }));
let sent_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Sent { .. }));
assert!(attempted_emitted, "Expected XcmPallet::Attempted event emitted");
assert!(sent_emitted, "Expected XcmPallet::Sent event emitted");
});
ParaA::execute_with(|| {
......@@ -193,13 +190,8 @@ fn reserve_transfer_with_error() {
assert!(log_capture.contains("XCM validate_send failed"));
// Verify that XcmPallet::Attempted was NOT emitted (rollback happened)
let events = relay_chain::System::events();
let xcm_attempted_emitted = events.iter().any(|e| {
matches!(
e.event,
relay_chain::RuntimeEvent::XcmPallet(pallet_xcm::Event::Attempted { .. })
)
});
let xcm_attempted_emitted =
system_contains_event!(relay_chain, XcmPallet(pallet_xcm::Event::Attempted { .. }));
assert!(
!xcm_attempted_emitted,
"Expected no XcmPallet::Attempted event due to rollback, but it was emitted"
......@@ -580,13 +572,3 @@ fn query_holding() {
);
});
}
fn count_relay_chain_events<F>(
events: &[frame_system::EventRecord<relay_chain::RuntimeEvent, sp_core::H256>],
predicate: F,
) -> usize
where
F: Fn(&relay_chain::RuntimeEvent) -> bool,
{
events.iter().filter(|e| predicate(&e.event)).count()
}
title: Simplify event assertion with predicate-based check
doc:
- audience: Runtime Dev
description: |-
Simplify event assertions by introducing `contains_event`, reducing duplicated code.
crates:
- name: xcm-simulator-example
bump: patch
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment