From 28e0c95c617e93879e231a5dd8009ce1f94bf2d2 Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Thu, 6 Jun 2024 16:06:06 +0100 Subject: [PATCH] consolidate initialize logic --- .../tests_enumerable_mintable_burnable.cairo | 64 +++++-------------- .../erc721/tests_mintable_burnable.cairo | 64 +++++-------------- 2 files changed, 34 insertions(+), 94 deletions(-) diff --git a/token/src/presets/erc721/tests_enumerable_mintable_burnable.cairo b/token/src/presets/erc721/tests_enumerable_mintable_burnable.cairo index 5845bae5..df2d92d5 100644 --- a/token/src/presets/erc721/tests_enumerable_mintable_burnable.cairo +++ b/token/src/presets/erc721/tests_enumerable_mintable_burnable.cairo @@ -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, @@ -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 @@ -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'); @@ -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()); @@ -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()); } @@ -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); @@ -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); @@ -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'); @@ -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'); diff --git a/token/src/presets/erc721/tests_mintable_burnable.cairo b/token/src/presets/erc721/tests_mintable_burnable.cairo index 8c9dbc5d..ef127b92 100644 --- a/token/src/presets/erc721/tests_mintable_burnable.cairo +++ b/token/src/presets/erc721/tests_mintable_burnable.cairo @@ -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, @@ -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'); @@ -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()); @@ -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()); } @@ -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); @@ -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); @@ -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); @@ -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);