Unverified Commit 5a944ca2 authored by Andrew Jones's avatar Andrew Jones Committed by GitHub
Browse files

[core] export test::env::EmittedEvent (#476)

* Export EmittedEvent

* Assert transfer event in all tests

* Fmt
parent 1f94b1fc
Pipeline #100499 failed with stages
in 9 minutes and 49 seconds
...@@ -29,6 +29,7 @@ pub use self::{ ...@@ -29,6 +29,7 @@ pub use self::{
call_data::CallData, call_data::CallData,
db::{ db::{
AccountError, AccountError,
EmittedEvent,
PastPrints, PastPrints,
}, },
typed_encoded::TypedEncodedError, typed_encoded::TypedEncodedError,
...@@ -40,7 +41,6 @@ use self::{ ...@@ -40,7 +41,6 @@ use self::{
Block, Block,
ChainSpec, ChainSpec,
Console, Console,
EmittedEvent,
EmittedEventsRecorder, EmittedEventsRecorder,
ExecContext, ExecContext,
}, },
......
...@@ -14,11 +14,13 @@ ...@@ -14,11 +14,13 @@
//! Operations on the off-chain testing environment. //! Operations on the off-chain testing environment.
pub use super::CallData; pub use super::{
CallData,
EmittedEvent,
};
use super::{ use super::{
db::ExecContext, db::ExecContext,
AccountError, AccountError,
EmittedEvent,
EnvInstance, EnvInstance,
OnInstance, OnInstance,
}; };
......
...@@ -172,6 +172,26 @@ mod erc20 { ...@@ -172,6 +172,26 @@ mod erc20 {
.unwrap() .unwrap()
} }
fn assert_transfer_event<I>(
raw_events: I,
transfer_index: usize,
expected_value: u128,
) where
I: IntoIterator<Item = env::test::EmittedEvent>,
{
let raw_event = raw_events
.into_iter()
.nth(transfer_index)
.expect(&format!("No event at index {}", transfer_index));
let event = <Event as scale::Decode>::decode(&mut &raw_event.data[..])
.expect("Invalid contract Event");
if let Event::Transfer(transfer) = event {
assert_eq!(expected_value, transfer.value);
} else {
panic!("Expected a Transfer Event")
}
}
/// The default constructor does its job. /// The default constructor does its job.
#[test] #[test]
fn new_works() { fn new_works() {
...@@ -179,17 +199,11 @@ mod erc20 { ...@@ -179,17 +199,11 @@ mod erc20 {
// Constructor works. // Constructor works.
let _erc20 = Erc20::new(100); let _erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction. // Transfer event triggered during initial construction.
let emitted_events = env::test::recorded_events().collect::<Vec<_>>(); let emitted_events = env::test::recorded_events().collect::<Vec<_>>();
assert_eq!(1, emitted_events.len()); assert_eq!(1, emitted_events.len());
let raw_event = emitted_events.first().unwrap();
let event = <Event as scale::Decode>::decode(&mut &raw_event.data[..]) assert_transfer_event(emitted_events, 0, 100)
.expect("Invalid contract Event");
if let Event::Transfer(transfer) = event {
assert_eq!(100, transfer.value);
} else {
panic!("Expected a Transfer Event")
}
}) })
} }
...@@ -199,8 +213,8 @@ mod erc20 { ...@@ -199,8 +213,8 @@ mod erc20 {
run_test(|| { run_test(|| {
// Constructor works. // Constructor works.
let erc20 = Erc20::new(100); let erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction. // Transfer event triggered during initial construction.
assert_eq!(env::test::recorded_events().count(), 1); assert_transfer_event(env::test::recorded_events(), 0, 100);
// Get the token total supply. // Get the token total supply.
assert_eq!(erc20.total_supply(), 100); assert_eq!(erc20.total_supply(), 100);
}) })
...@@ -212,8 +226,8 @@ mod erc20 { ...@@ -212,8 +226,8 @@ mod erc20 {
run_test(|| { run_test(|| {
// Constructor works // Constructor works
let erc20 = Erc20::new(100); let erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction // Transfer event triggered during initial construction
assert_eq!(env::test::recorded_events().count(), 1); assert_transfer_event(env::test::recorded_events(), 0, 100);
let accounts = env::test::default_accounts::<env::DefaultEnvTypes>() let accounts = env::test::default_accounts::<env::DefaultEnvTypes>()
.expect("Cannot get accounts"); .expect("Cannot get accounts");
// Alice owns all the tokens on deployment // Alice owns all the tokens on deployment
...@@ -228,8 +242,8 @@ mod erc20 { ...@@ -228,8 +242,8 @@ mod erc20 {
run_test(|| { run_test(|| {
// Constructor works. // Constructor works.
let mut erc20 = Erc20::new(100); let mut erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction. // Transfer event triggered during initial construction.
assert_eq!(1, env::test::recorded_events().count()); assert_transfer_event(env::test::recorded_events(), 0, 100);
let accounts = env::test::default_accounts::<env::DefaultEnvTypes>() let accounts = env::test::default_accounts::<env::DefaultEnvTypes>()
.expect("Cannot get accounts"); .expect("Cannot get accounts");
...@@ -237,7 +251,7 @@ mod erc20 { ...@@ -237,7 +251,7 @@ mod erc20 {
// Alice transfers 10 tokens to Bob. // Alice transfers 10 tokens to Bob.
assert_eq!(erc20.transfer(accounts.bob, 10), true); assert_eq!(erc20.transfer(accounts.bob, 10), true);
// The second Transfer event takes place. // The second Transfer event takes place.
assert_eq!(2, env::test::recorded_events().count()); assert_transfer_event(env::test::recorded_events(), 1, 10);
// Bob owns 10 tokens. // Bob owns 10 tokens.
assert_eq!(erc20.balance_of(accounts.bob), 10); assert_eq!(erc20.balance_of(accounts.bob), 10);
}) })
...@@ -248,8 +262,8 @@ mod erc20 { ...@@ -248,8 +262,8 @@ mod erc20 {
run_test(|| { run_test(|| {
// Constructor works. // Constructor works.
let mut erc20 = Erc20::new(100); let mut erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction. // Transfer event triggered during initial construction.
assert_eq!(env::test::recorded_events().count(), 1); assert_transfer_event(env::test::recorded_events(), 0, 100);
let accounts = env::test::default_accounts::<env::DefaultEnvTypes>() let accounts = env::test::default_accounts::<env::DefaultEnvTypes>()
.expect("Cannot get accounts"); .expect("Cannot get accounts");
...@@ -287,8 +301,8 @@ mod erc20 { ...@@ -287,8 +301,8 @@ mod erc20 {
run_test(|| { run_test(|| {
// Constructor works. // Constructor works.
let mut erc20 = Erc20::new(100); let mut erc20 = Erc20::new(100);
// Transfer event triggered during initial contruction. // Transfer event triggered during initial construction.
assert_eq!(env::test::recorded_events().count(), 1); assert_transfer_event(env::test::recorded_events(), 0, 100);
let accounts = env::test::default_accounts::<env::DefaultEnvTypes>() let accounts = env::test::default_accounts::<env::DefaultEnvTypes>()
.expect("Cannot get accounts"); .expect("Cannot get accounts");
...@@ -322,7 +336,7 @@ mod erc20 { ...@@ -322,7 +336,7 @@ mod erc20 {
// Bob transfers tokens from Alice to Eve. // Bob transfers tokens from Alice to Eve.
assert_eq!(erc20.transfer_from(accounts.alice, accounts.eve, 10), true); assert_eq!(erc20.transfer_from(accounts.alice, accounts.eve, 10), true);
// The third event takes place. // The third event takes place.
assert_eq!(env::test::recorded_events().count(), 3); assert_transfer_event(env::test::recorded_events(), 2, 10);
// Eve owns tokens. // Eve owns tokens.
assert_eq!(erc20.balance_of(accounts.eve), 10); assert_eq!(erc20.balance_of(accounts.eve), 10);
}) })
......
Supports Markdown
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