-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate tests/unit/test_slot_approvable.cairo to snforge 0.27.0
- Loading branch information
1 parent
844c03d
commit 9ce5333
Showing
3 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ mod unit { | |
mod test_initialization; | ||
mod test_metadata; | ||
mod test_mint_burn; | ||
mod test_slot_approvable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Core imports | ||
use debug::PrintTrait; | ||
|
||
// snforge deps | ||
use snforge_std::{ | ||
declare, ContractClassTrait, start_cheat_caller_address, stop_cheat_caller_address, | ||
test_address, spy_events, EventSpyAssertionsTrait | ||
}; | ||
|
||
// External imports | ||
use openzeppelin::token::erc721::ERC721Component; | ||
|
||
// Local imports | ||
use cairo_erc_3525::module::ERC3525Component::{InternalTrait, ERC3525Impl}; | ||
use cairo_erc_3525::module::ERC3525Component; | ||
use cairo_erc_3525::extensions::slotapprovable::module::ERC3525SlotApprovableComponent::{ | ||
ERC3525SlotApprovableImpl, InternalTrait as ERC3525SlotApprovableInternalTrait, | ||
ExternalTrait as ERC3525SlotApprovableExternalTrait | ||
}; | ||
use cairo_erc_3525::extensions::slotapprovable::module::ERC3525SlotApprovableComponent; | ||
use super::constants::{ | ||
ERC3525SlotApprovableComponentState, CONTRACT_STATE, COMPONENT_STATE_SLOT_APPROVABLE, | ||
VALUE_DECIMALS, TOKEN_ID_1, TOKEN_ID_2, SLOT_1, SLOT_2, VALUE, ZERO, OWNER, OPERATOR, SOMEONE | ||
}; | ||
|
||
// Settings | ||
|
||
fn setup() -> ERC3525SlotApprovableComponentState { | ||
let mut state = COMPONENT_STATE_SLOT_APPROVABLE(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
mock_state.erc3525.initializer(VALUE_DECIMALS); | ||
state.initializer(); | ||
state | ||
} | ||
// fn setup() -> (ERC3525::ContractState, ERC3525SlotApprovable::ContractState) { | ||
// let mut state = STATE(); | ||
// mock_state.initializer(VALUE_DECIMALS); | ||
// let mut state_slot_approvable = STATE_SLOT_APPROVABLE(); | ||
// state.initializer(ref state_slot_approvable); | ||
// (state, state_slot_approvable) | ||
// } | ||
|
||
// Tests approvals | ||
#[test] | ||
#[available_gas(20000000)] | ||
fn test_slot_approvable_owner_can_approve_slot() { | ||
let mut state = setup(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), true); | ||
stop_cheat_caller_address(OWNER()); | ||
} | ||
|
||
#[test] | ||
#[available_gas(20000000)] | ||
fn test_slot_approvable_operator_can_approve_value() { | ||
let mut state = setup(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
mock_state.erc3525._mint(OWNER(), TOKEN_ID_1, SLOT_1, VALUE); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), true); | ||
stop_cheat_caller_address(OWNER()); | ||
start_cheat_caller_address(test_address(), OPERATOR()); | ||
state.approve_value(TOKEN_ID_1, OPERATOR(), VALUE); | ||
stop_cheat_caller_address(OPERATOR()); | ||
} | ||
|
||
#[test] | ||
#[available_gas(20000000)] | ||
#[should_panic(expected: ('ERC3525: caller not allowed',))] | ||
fn test_slot_approvable_operator_cannot_approve_any_token() { | ||
let mut state = setup(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
mock_state.erc3525._mint(OWNER(), TOKEN_ID_1, SLOT_1, VALUE); | ||
state.set_approval_for_slot(OWNER(), SLOT_2, OPERATOR(), true); | ||
stop_cheat_caller_address(OWNER()); | ||
start_cheat_caller_address(test_address(), OPERATOR()); | ||
state.approve_value(TOKEN_ID_1, OPERATOR(), VALUE); | ||
stop_cheat_caller_address(OPERATOR()); | ||
} | ||
|
||
#[test] | ||
#[available_gas(20000000)] | ||
#[should_panic(expected: ('ERC3525: insufficient allowance',))] | ||
fn test_slot_approvable_operator_cannot_transfer_anyones_value() { | ||
let mut state = setup(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
mock_state.erc3525._mint(OWNER(), TOKEN_ID_1, SLOT_1, VALUE); | ||
mock_state.erc3525._mint(SOMEONE(), TOKEN_ID_2, SLOT_1, VALUE); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), true); | ||
stop_cheat_caller_address(OWNER()); | ||
start_cheat_caller_address(test_address(), OPERATOR()); | ||
state.transfer_value_from(TOKEN_ID_2, 0, OPERATOR(), VALUE); | ||
stop_cheat_caller_address(OPERATOR()); | ||
} | ||
|
||
#[test] | ||
#[available_gas(20000000)] | ||
#[should_panic(expected: ('ERC3525: slot mismatch',))] | ||
fn test_slot_approvable_operator_transfer_value_revert_slot_mismatch() { | ||
let mut state = setup(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
mock_state.erc3525._mint(OWNER(), TOKEN_ID_1, SLOT_1, VALUE); | ||
mock_state.erc3525._mint(SOMEONE(), TOKEN_ID_2, SLOT_2, VALUE); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), true); | ||
stop_cheat_caller_address(OWNER()); | ||
start_cheat_caller_address(test_address(), OPERATOR()); | ||
state.transfer_value_from(TOKEN_ID_1, TOKEN_ID_2, ZERO(), VALUE); | ||
stop_cheat_caller_address(OPERATOR()); | ||
} | ||
|
||
#[test] | ||
#[available_gas(20000000)] | ||
#[should_panic(expected: ('ERC3525: caller not allowed',))] | ||
fn test_slot_approvable_revoked_slot_operator_cannot_approve() { | ||
let mut state = setup(); | ||
let mut mock_state = CONTRACT_STATE(); | ||
start_cheat_caller_address(test_address(), OWNER()); | ||
mock_state.erc3525._mint(OWNER(), TOKEN_ID_1, SLOT_1, VALUE); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), true); | ||
state.set_approval_for_slot(OWNER(), SLOT_1, OPERATOR(), false); | ||
stop_cheat_caller_address(OWNER()); | ||
start_cheat_caller_address(test_address(), OPERATOR()); | ||
state.approve_value(TOKEN_ID_1, OPERATOR(), VALUE); | ||
stop_cheat_caller_address(OPERATOR()); | ||
} |