Skip to content

Commit

Permalink
consolidate initialize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Jun 6, 2024
1 parent 562a7b4 commit 28e0c95
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 94 deletions.
64 changes: 17 additions & 47 deletions token/src/presets/erc721/tests_enumerable_mintable_burnable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use token::components::tests::token::erc721::test_erc721_balance::{
// Setup
//

fn setup() -> (IWorldDispatcher, IERC721EnumMintBurnPresetDispatcher) {
fn setup_uninitialized() -> (IWorldDispatcher, IERC721EnumMintBurnPresetDispatcher) {
let world = spawn_test_world(
array![
erc_721_token_approval_model::TEST_CLASS_HASH,
Expand Down Expand Up @@ -109,13 +109,8 @@ fn setup() -> (IWorldDispatcher, IERC721EnumMintBurnPresetDispatcher) {
(world, erc721_enum_mint_burn_dispatcher)
}

//
// initializer
//

#[test]
fn test_initializer() {
let (world, mut enum_mint_burn) = setup();
fn setup() -> (IWorldDispatcher, IERC721EnumMintBurnPresetDispatcher) {
let (world, mut enum_mint_burn) = setup_uninitialized();

// initialize contracts
enum_mint_burn
Expand All @@ -125,6 +120,17 @@ fn test_initializer() {
utils::drop_all_events(enum_mint_burn.contract_address);
utils::drop_all_events(world.contract_address);

(world, enum_mint_burn)
}

//
// initializer
//

#[test]
fn test_initializer() {
let (_world, mut enum_mint_burn) = setup();

assert(enum_mint_burn.balance_of(OWNER()) == 2, 'Should eq 2');
assert(enum_mint_burn.name() == "NAME", 'Name should be NAME');
assert(enum_mint_burn.symbol() == "SYMBOL", 'Symbol should be SYMBOL');
Expand All @@ -134,7 +140,7 @@ fn test_initializer() {
#[test]
#[should_panic(expected: ('ERC721: caller is not owner', 'ENTRYPOINT_FAILED'))]
fn test_initialize_not_world_owner() {
let (_world, mut enum_mint_burn) = setup();
let (_world, mut enum_mint_burn) = setup_uninitialized();

utils::impersonate(OWNER());

Expand All @@ -148,10 +154,6 @@ fn test_initialize_not_world_owner() {
fn test_initialize_multiple() {
let (_world, mut enum_mint_burn) = setup();

// initialize contracts
enum_mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

enum_mint_burn.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID_3].span());
}

Expand All @@ -163,14 +165,6 @@ fn test_initialize_multiple() {
fn test_approve() {
let (world, mut enum_mint_burn) = setup();

// initialize contracts
enum_mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(enum_mint_burn.contract_address);
utils::drop_all_events(world.contract_address);

utils::impersonate(OWNER());

enum_mint_burn.approve(SPENDER(), TOKEN_ID);
Expand All @@ -191,14 +185,6 @@ fn test_approve() {
fn test_transfer_from() {
let (world, mut enum_mint_burn) = setup();

// initialize contracts
enum_mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(enum_mint_burn.contract_address);
utils::drop_all_events(world.contract_address);

utils::impersonate(OWNER());
enum_mint_burn.approve(SPENDER(), TOKEN_ID);

Expand Down Expand Up @@ -227,15 +213,7 @@ fn test_transfer_from() {

#[test]
fn test_mint() {
let (world, mut enum_mint_burn) = setup();

// initialize contracts
enum_mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(enum_mint_burn.contract_address);
utils::drop_all_events(world.contract_address);
let (_world, mut enum_mint_burn) = setup();

enum_mint_burn.mint(RECIPIENT(), 3);
assert(enum_mint_burn.balance_of(RECIPIENT()) == 1, 'invalid balance_of');
Expand All @@ -253,15 +231,7 @@ fn test_mint() {

#[test]
fn test_burn() {
let (world, mut enum_mint_burn) = setup();

// initialize contracts
enum_mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(enum_mint_burn.contract_address);
utils::drop_all_events(world.contract_address);
let (_world, mut enum_mint_burn) = setup();

enum_mint_burn.burn(TOKEN_ID_2);
assert(enum_mint_burn.balance_of(OWNER()) == 1, 'invalid balance_of');
Expand Down
64 changes: 17 additions & 47 deletions token/src/presets/erc721/tests_mintable_burnable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use token::components::tests::token::erc721::test_erc721_balance::{
// Setup
//

fn setup() -> (IWorldDispatcher, IERC721MintableBurnablePresetDispatcher) {
fn setup_uninitialized() -> (IWorldDispatcher, IERC721MintableBurnablePresetDispatcher) {
let world = spawn_test_world(
array![
erc_721_token_approval_model::TEST_CLASS_HASH,
Expand Down Expand Up @@ -87,22 +87,28 @@ fn setup() -> (IWorldDispatcher, IERC721MintableBurnablePresetDispatcher) {
(world, erc721_mintable_burnable_dispatcher)
}

//
// initializer
//

#[test]
fn test_initializer() {
let (world, mut mintable_burnable) = setup();
fn setup() -> (IWorldDispatcher, IERC721MintableBurnablePresetDispatcher) {
let (world, mut mint_burn) = setup_uninitialized();

// initialize contracts
mintable_burnable
mint_burn
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(mintable_burnable.contract_address);
utils::drop_all_events(mint_burn.contract_address);
utils::drop_all_events(world.contract_address);

(world, mint_burn)
}

//
// initializer
//

#[test]
fn test_initializer() {
let (_world, mut mintable_burnable) = setup();

assert(mintable_burnable.balance_of(OWNER()) == 2, 'Should eq 2');
assert(mintable_burnable.name() == "NAME", 'Name should be NAME');
assert(mintable_burnable.symbol() == "SYMBOL", 'Symbol should be SYMBOL');
Expand All @@ -112,7 +118,7 @@ fn test_initializer() {
#[test]
#[should_panic(expected: ('ERC721: caller is not owner', 'ENTRYPOINT_FAILED'))]
fn test_initialize_not_world_owner() {
let (_world, mut mintable_burnable) = setup();
let (_world, mut mintable_burnable) = setup_uninitialized();

utils::impersonate(OWNER());

Expand All @@ -126,10 +132,6 @@ fn test_initialize_not_world_owner() {
fn test_initialize_multiple() {
let (_world, mut mintable_burnable) = setup();

// initialize contracts
mintable_burnable
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

mintable_burnable.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID_3].span());
}

Expand All @@ -141,14 +143,6 @@ fn test_initialize_multiple() {
fn test_approve() {
let (world, mut mintable_burnable) = setup();

// initialize contracts
mintable_burnable
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(mintable_burnable.contract_address);
utils::drop_all_events(world.contract_address);

utils::impersonate(OWNER());

mintable_burnable.approve(SPENDER(), TOKEN_ID);
Expand All @@ -169,14 +163,6 @@ fn test_approve() {
fn test_transfer_from() {
let (world, mut mintable_burnable) = setup();

// initialize contracts
mintable_burnable
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(mintable_burnable.contract_address);
utils::drop_all_events(world.contract_address);

utils::impersonate(OWNER());
mintable_burnable.approve(SPENDER(), TOKEN_ID);

Expand All @@ -202,14 +188,6 @@ fn test_transfer_from() {
fn test_mint() {
let (world, mut mintable_burnable) = setup();

// initialize contracts
mintable_burnable
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(mintable_burnable.contract_address);
utils::drop_all_events(world.contract_address);

mintable_burnable.mint(RECIPIENT(), 3);
assert(mintable_burnable.balance_of(RECIPIENT()) == 1, 'invalid balance_of');
utils::drop_event(world.contract_address);
Expand All @@ -226,14 +204,6 @@ fn test_mint() {
fn test_burn() {
let (world, mut mintable_burnable) = setup();

// initialize contracts
mintable_burnable
.initializer("NAME", "SYMBOL", "URI", OWNER(), array![TOKEN_ID, TOKEN_ID_2].span());

// drop all events
utils::drop_all_events(mintable_burnable.contract_address);
utils::drop_all_events(world.contract_address);

mintable_burnable.burn(TOKEN_ID);
assert(mintable_burnable.balance_of(OWNER()) == 1, 'invalid balance_of');
utils::drop_event(world.contract_address);
Expand Down

0 comments on commit 28e0c95

Please sign in to comment.