Skip to content

Commit

Permalink
Add testing for ERC721 events (#678)
Browse files Browse the repository at this point in the history
* feat: add testing for events

* feat: add tests for ownable events

* feat: add tests for pausable events

* feat: add tests for erc20 events

* feat: add tests for erc721 events

* Update src/openzeppelin/tests/security/test_pausable.cairo

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>

* feat: apply review updates

* feat: add test helper for events

* feat: improve tests

* feat: apply review updates

* feat: apply review updates

* feat: apply review updates

* feat: apply review updates

* feat: apply review updates

---------

Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
  • Loading branch information
ericnordelo and andrew-fleming authored Aug 16, 2023
1 parent 77089c1 commit 3e08fa6
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 181 deletions.
10 changes: 5 additions & 5 deletions src/tests/access/test_accesscontrol.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn STATE() -> AccessControl::ContractState {
fn setup() -> AccessControl::ContractState {
let mut state = STATE();
InternalImpl::_grant_role(ref state, DEFAULT_ADMIN_ROLE, ADMIN());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());
state
}

Expand Down Expand Up @@ -224,7 +224,7 @@ fn test_revoke_role_for_granted_role() {
testing::set_caller_address(ADMIN());

AccessControlImpl::grant_role(ref state, ROLE, AUTHORIZED());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

AccessControlImpl::revoke_role(ref state, ROLE, AUTHORIZED());

Expand All @@ -239,7 +239,7 @@ fn test_revokeRole_for_granted_role() {
testing::set_caller_address(ADMIN());

AccessControlCamelImpl::grantRole(ref state, ROLE, AUTHORIZED());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

AccessControlCamelImpl::revokeRole(ref state, ROLE, AUTHORIZED());

Expand Down Expand Up @@ -320,7 +320,7 @@ fn test_renounce_role_for_granted_role() {
testing::set_caller_address(ADMIN());

AccessControlImpl::grant_role(ref state, ROLE, AUTHORIZED());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

testing::set_caller_address(AUTHORIZED());
AccessControlImpl::renounce_role(ref state, ROLE, AUTHORIZED());
Expand All @@ -336,7 +336,7 @@ fn test_renounceRole_for_granted_role() {
testing::set_caller_address(ADMIN());

AccessControlCamelImpl::grantRole(ref state, ROLE, AUTHORIZED());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

testing::set_caller_address(AUTHORIZED());
AccessControlCamelImpl::renounceRole(ref state, ROLE, AUTHORIZED());
Expand Down
4 changes: 2 additions & 2 deletions src/tests/access/test_dual_accesscontrol.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use array::ArrayTrait;
use openzeppelin::access::accesscontrol::DEFAULT_ADMIN_ROLE;
use openzeppelin::access::accesscontrol::interface::IACCESSCONTROL_ID;
use openzeppelin::access::accesscontrol::interface::IAccessControlDispatcher;
use openzeppelin::access::accesscontrol::interface::IAccessControlCamelDispatcher;
use openzeppelin::access::accesscontrol::interface::IAccessControlDispatcherTrait;
use openzeppelin::access::accesscontrol::interface::IAccessControlDispatcher;
use openzeppelin::access::accesscontrol::interface::IAccessControlCamelDispatcherTrait;
use openzeppelin::access::accesscontrol::interface::IAccessControlCamelDispatcher;
use openzeppelin::access::accesscontrol::dual_accesscontrol::DualCaseAccessControlTrait;
use openzeppelin::access::accesscontrol::dual_accesscontrol::DualCaseAccessControl;
use openzeppelin::tests::mocks::snake_accesscontrol_mock::SnakeAccessControlMock;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/access/test_ownable.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use openzeppelin::access::ownable::Ownable;
use openzeppelin::access::ownable::Ownable::InternalImpl;
use openzeppelin::access::ownable::Ownable::OwnableCamelOnlyImpl;
use openzeppelin::access::ownable::Ownable::OwnableImpl;
use openzeppelin::access::ownable::Ownable::OwnershipTransferred;
use openzeppelin::access::ownable::Ownable::_owner::InternalContractStateTrait;
use openzeppelin::access::ownable::Ownable;
use openzeppelin::tests::utils::constants::{ZERO, OTHER, OWNER};
use openzeppelin::tests::utils;
use option::OptionTrait;
Expand All @@ -23,7 +23,7 @@ fn STATE() -> Ownable::ContractState {
fn setup() -> Ownable::ContractState {
let mut state = STATE();
InternalImpl::initializer(ref state, OWNER());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());
state
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests/security/test_pausable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn test_unpause_when_paused() {
testing::set_caller_address(CALLER());

InternalImpl::_pause(ref state);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

InternalImpl::_unpause(ref state);

Expand All @@ -133,13 +133,13 @@ fn test_unpause_when_unpaused() {
//

fn assert_event_paused(account: ContractAddress) {
let event = testing::pop_log::<Paused>(ZERO()).unwrap();
let event = utils::pop_log::<Paused>(ZERO()).unwrap();
assert(event.account == account, 'Invalid `account`');
utils::assert_no_events_left(ZERO());
}

fn assert_event_unpaused(account: ContractAddress) {
let event = testing::pop_log::<Unpaused>(ZERO()).unwrap();
let event = utils::pop_log::<Unpaused>(ZERO()).unwrap();
assert(event.account == account, 'Invalid `account`');
utils::assert_no_events_left(ZERO());
}
20 changes: 10 additions & 10 deletions src/tests/token/test_erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn STATE() -> ERC20::ContractState {
fn setup() -> ERC20::ContractState {
let mut state = STATE();
ERC20::constructor(ref state, NAME, SYMBOL, SUPPLY, OWNER());
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());
state
}

Expand Down Expand Up @@ -242,7 +242,7 @@ fn test_transfer_from() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

testing::set_caller_address(SPENDER());
assert(ERC20Impl::transfer_from(ref state, OWNER(), RECIPIENT(), VALUE), 'Should return true');
Expand Down Expand Up @@ -311,7 +311,7 @@ fn test_transferFrom() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

testing::set_caller_address(SPENDER());
assert(
Expand Down Expand Up @@ -389,7 +389,7 @@ fn test_increase_allowance() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

assert(ERC20::increase_allowance(ref state, SPENDER(), VALUE), 'Should return true');

Expand Down Expand Up @@ -420,7 +420,7 @@ fn test_increaseAllowance() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

assert(ERC20::increaseAllowance(ref state, SPENDER(), VALUE), 'Should return true');

Expand Down Expand Up @@ -455,7 +455,7 @@ fn test_decrease_allowance() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

assert(ERC20::decrease_allowance(ref state, SPENDER(), VALUE), 'Should return true');

Expand Down Expand Up @@ -486,7 +486,7 @@ fn test_decreaseAllowance() {
let mut state = setup();
testing::set_caller_address(OWNER());
ERC20Impl::approve(ref state, SPENDER(), VALUE);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

assert(ERC20::decreaseAllowance(ref state, SPENDER(), VALUE), 'Should return true');

Expand Down Expand Up @@ -521,7 +521,7 @@ fn test__spend_allowance_not_unlimited() {
let mut state = setup();

InternalImpl::_approve(ref state, OWNER(), SPENDER(), SUPPLY);
testing::pop_log_raw(ZERO());
utils::drop_event(ZERO());

InternalImpl::_spend_allowance(ref state, OWNER(), SPENDER(), VALUE);

Expand Down Expand Up @@ -598,7 +598,7 @@ fn test__burn_from_zero() {
//

fn assert_event_approval(owner: ContractAddress, spender: ContractAddress, value: u256) {
let event = testing::pop_log::<Approval>(ZERO()).unwrap();
let event = utils::pop_log::<Approval>(ZERO()).unwrap();
assert(event.owner == owner, 'Invalid `owner`');
assert(event.spender == spender, 'Invalid `spender`');
assert(event.value == value, 'Invalid `value`');
Expand All @@ -610,7 +610,7 @@ fn assert_only_event_approval(owner: ContractAddress, spender: ContractAddress,
}

fn assert_event_transfer(from: ContractAddress, to: ContractAddress, value: u256) {
let event = testing::pop_log::<Transfer>(ZERO()).unwrap();
let event = utils::pop_log::<Transfer>(ZERO()).unwrap();
assert(event.from == from, 'Invalid `from`');
assert(event.to == to, 'Invalid `to`');
assert(event.value == value, 'Invalid `value`');
Expand Down
Loading

0 comments on commit 3e08fa6

Please sign in to comment.