Skip to content

Commit

Permalink
Check events in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed May 11, 2023
1 parent dc4b819 commit 2dbd526
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions frame/evm-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
/// The account already exists in case creating it.
AccountAlreadyExist,
/// The account doesn't exist in case removing it.
AccountNotExist,
}
}

impl<T: Config> Pallet<T> {
/// Check the account existence.
pub fn account_exists(who: &<T as Config>::AccountId) -> bool {
FullAccount::<T>::contains_key(who)
}
Expand Down Expand Up @@ -169,11 +172,13 @@ impl<T: Config> Pallet<T> {
}
}

/// Interface to handle account creation.
pub trait OnNewAccount<AccountId> {
/// A new account `who` has been registered.
fn on_new_account(who: &AccountId);
}

/// Interface to handle account killing.
pub trait OnKilledAccount<AccountId> {
/// The account with the given id was reaped.
fn on_killed_account(who: &AccountId);
Expand Down
2 changes: 1 addition & 1 deletion frame/evm-system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Test mock for unit tests and benchmarking.
//! Test mock for unit tests.
use frame_support::{
traits::{ConstU32, ConstU64},
Expand Down
11 changes: 11 additions & 0 deletions frame/evm-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ fn create_account_works() {
// Check test preconditions.
assert!(!EvmSystem::account_exists(&account_id));

// Set block number to enable events.
System::set_block_number(1);

// Set mock expectations.
let on_new_account_ctx = MockDummyOnNewAccount::on_new_account_context();
on_new_account_ctx
Expand All @@ -33,6 +36,7 @@ fn create_account_works() {

// Assert state changes.
assert!(EvmSystem::account_exists(&account_id));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::NewAccount { account: account_id } ));

// Assert mock invocations.
on_new_account_ctx.checkpoint();
Expand Down Expand Up @@ -60,6 +64,9 @@ fn remove_account_works() {
let account_id = H160::from_str("1000000000000000000000000000000000000001").unwrap();
<FullAccount<Test>>::insert(account_id.clone(), AccountInfo::<_, _>::default());

// Set block number to enable events.
System::set_block_number(1);

// Set mock expectations.
let on_killed_account_ctx = MockDummyOnKilledAccount::on_killed_account_context();
on_killed_account_ctx
Expand All @@ -73,6 +80,10 @@ fn remove_account_works() {
// Invoke the function under test.
assert_ok!(EvmSystem::remove_account(&account_id));

// Assert state changes.
assert!(!EvmSystem::account_exists(&account_id));
System::assert_has_event(RuntimeEvent::EvmSystem(Event::KilledAccount { account: account_id } ));

// Assert mock invocations.
on_killed_account_ctx.checkpoint();
});
Expand Down

0 comments on commit 2dbd526

Please sign in to comment.