Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(drink test): assert last event #370

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 23 additions & 45 deletions pop-api/examples/fungibles/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use drink::{
assert_err, assert_ok, call,
assert_err, assert_last_contract_event, assert_ok, call,
devnet::{
account_id_from_slice,
error::{
Expand Down Expand Up @@ -69,15 +69,13 @@ fn new_constructor_works(mut session: Session) {
// Token exists after the deployment.
assert!(session.sandbox().asset_exists(&TOKEN));
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Created {
id: TOKEN,
creator: account_id_from_slice(&contract),
admin: account_id_from_slice(&contract),
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -227,15 +225,13 @@ fn transfer_works(mut session: Session) {
assert_eq!(session.sandbox().balance_of(&TOKEN, &contract), AMOUNT - value);
assert_eq!(session.sandbox().balance_of(&TOKEN, &BOB), value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Transfer {
from: Some(account_id_from_slice(&contract)),
to: Some(account_id_from_slice(&BOB)),
value,
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -319,15 +315,13 @@ fn transfer_from_works(mut session: Session) {
assert_eq!(session.sandbox().balance_of(&TOKEN, &ALICE), value);
assert_eq!(session.sandbox().balance_of(&TOKEN, &BOB), value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&ALICE),
spender: account_id_from_slice(&contract),
value,
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -368,29 +362,25 @@ fn approve_works(mut session: Session) {
assert_ok!(approve(&mut session, BOB, value));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &BOB), value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&BOB),
value,
}
.encode()
.as_slice()
);
// Non-additive, sets new value.
assert_ok!(approve(&mut session, ALICE, value - 1));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &ALICE), value - 1);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&ALICE),
value: value - 1,
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -438,29 +428,25 @@ fn increase_allowance_works(mut session: Session) {
assert_ok!(increase_allowance(&mut session, ALICE, value));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &ALICE), AMOUNT + value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&ALICE),
value: AMOUNT + value,
}
.encode()
.as_slice()
);
// Additive.
assert_ok!(increase_allowance(&mut session, ALICE, value));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &ALICE), AMOUNT + value * 2);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&ALICE),
value: AMOUNT + value * 2,
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -521,29 +507,25 @@ fn decrease_allowance_works(mut session: Session) {
assert_ok!(decrease_allowance(&mut session, ALICE, value));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &ALICE), AMOUNT - value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&ALICE),
value: AMOUNT - value,
}
.encode()
.as_slice()
);
// Additive.
assert_ok!(decrease_allowance(&mut session, ALICE, value));
assert_eq!(session.sandbox().allowance(&TOKEN, &contract, &ALICE), AMOUNT - value * 2);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Approval {
owner: account_id_from_slice(&contract),
spender: account_id_from_slice(&ALICE),
value: AMOUNT - value * 2,
}
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -634,11 +616,9 @@ fn mint_works(mut session: Session) {
assert_eq!(session.sandbox().total_supply(&TOKEN), value);
assert_eq!(session.sandbox().balance_of(&TOKEN, &ALICE), value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Transfer { from: None, to: Some(account_id_from_slice(&ALICE)), value }
.encode()
.as_slice()
);
}

Expand Down Expand Up @@ -701,11 +681,9 @@ fn burn_works(mut session: Session) {
assert_eq!(session.sandbox().total_supply(&TOKEN), AMOUNT - value);
assert_eq!(session.sandbox().balance_of(&TOKEN, &ALICE), AMOUNT - value);
// Successfully emit event.
assert_eq!(
last_contract_event(&session).unwrap(),
assert_last_contract_event!(
&session,
Transfer { from: Some(account_id_from_slice(&ALICE)), to: None, value }
.encode()
.as_slice()
);
}

Expand Down
6 changes: 6 additions & 0 deletions pop-api/src/v0/fungibles/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use super::*;
/// Event emitted when allowance by `owner` to `spender` changes.
// Differing style: event name abides by the PSP22 standard.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Approval {
/// The owner providing the allowance.
#[ink(topic)]
Expand All @@ -27,6 +28,7 @@ pub struct Approval {
/// Event emitted when transfer of tokens occurs.
// Differing style: event name abides by the PSP22 standard.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Transfer {
/// The source of the transfer. `None` when minting.
#[ink(topic)]
Expand All @@ -40,6 +42,7 @@ pub struct Transfer {

/// Event emitted when a token is created.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Created {
/// The token identifier.
#[ink(topic)]
Expand All @@ -54,6 +57,7 @@ pub struct Created {

/// Event emitted when a token is in the process of being destroyed.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct DestroyStarted {
/// The token.
#[ink(topic)]
Expand All @@ -62,6 +66,7 @@ pub struct DestroyStarted {

/// Event emitted when new metadata is set for a token.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct MetadataSet {
/// The token.
#[ink(topic)]
Expand All @@ -78,6 +83,7 @@ pub struct MetadataSet {

/// Event emitted when metadata is cleared for a token.
#[ink::event]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct MetadataCleared {
/// The token.
#[ink(topic)]
Expand Down
Loading