Skip to content
Snippets Groups Projects
Unverified Commit 17ae0627 authored by thiolliere's avatar thiolliere Committed by GitHub
Browse files

Improve debugging by using `#[track_caller]` in system `assert_last_event` and...

Improve debugging by using `#[track_caller]` in system `assert_last_event` and `assert_has_event` (#7142)

Without track caller the error message of the assert points to the
`assert_last_event` function, which is not useful.
```
thread 'tests::set_metadata_works' panicked at /home/gui/Developpement/polkadot-sdk/substrate/frame/system/src/lib.rs:2034:9:
assertion `left == right` failed: expected event RuntimeEvent::Referenda(Event::MetadataSet { index: 0, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa }) is not equal to the last event RuntimeEvent::Referenda(Event::MetadataSet { index: 1, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
  left: RuntimeEvent::Referenda(Event::MetadataSet { index: 1, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
 right: RuntimeEvent::Referenda(Event::MetadataSet { index: 0, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
```

With the track caller the error message points to the caller, showing
the source of the error:
```
thread 'tests::set_metadata_works' panicked at substrate/frame/referenda/src/tests.rs:639:9:
assertion `left == right` failed: expected event RuntimeEvent::Referenda(Event::MetadataSet { index: 0, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa }) is not equal to the last event RuntimeEvent::Referenda(Event::MetadataSet { index: 1, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
  left: RuntimeEvent::Referenda(Event::MetadataSet { index: 1, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
 right: RuntimeEvent::Referenda(Event::MetadataSet { index: 0, hash: 0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa })
```

I also improved the error message to include a warning when checking
events on block number zero.
parent 682f8cd2
Branches
No related merge requests found
Pipeline #513162 waiting for manual action with stages
in 36 minutes and 20 seconds
......@@ -2062,11 +2062,18 @@ impl<T: Config> Pallet<T> {
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
#[track_caller]
pub fn assert_has_event(event: T::RuntimeEvent) {
let warn = if Self::block_number().is_zero() {
"WARNING: block number is zero, and events are not registered at block number zero.\n"
} else {
""
};
let events = Self::events();
assert!(
events.iter().any(|record| record.event == event),
"expected event {event:?} not found in events {events:?}",
"{warn}expected event {event:?} not found in events {events:?}",
);
}
......@@ -2074,11 +2081,22 @@ impl<T: Config> Pallet<T> {
///
/// NOTE: Events not registered at the genesis block and quietly omitted.
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
#[track_caller]
pub fn assert_last_event(event: T::RuntimeEvent) {
let last_event = Self::events().last().expect("events expected").event.clone();
let warn = if Self::block_number().is_zero() {
"WARNING: block number is zero, and events are not registered at block number zero.\n"
} else {
""
};
let last_event = Self::events()
.last()
.expect(&alloc::format!("{warn}events expected"))
.event
.clone();
assert_eq!(
last_event, event,
"expected event {event:?} is not equal to the last event {last_event:?}",
"{warn}expected event {event:?} is not equal to the last event {last_event:?}",
);
}
......
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