From 6373a024a64134a701805aef2301ae95d930730c Mon Sep 17 00:00:00 2001 From: ChrisFernandezVivas Date: Mon, 25 Nov 2024 16:41:55 -0600 Subject: [PATCH] remover blank spaces,divided test in success and error cases --- contracts/src/fund.cairo | 15 ++++--- contracts/tests/test_fund.cairo | 70 ++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/contracts/src/fund.cairo b/contracts/src/fund.cairo index ccae2a9..dab8ea2 100644 --- a/contracts/src/fund.cairo +++ b/contracts/src/fund.cairo @@ -130,15 +130,14 @@ pub mod Fund { } fn set_name(ref self: ContractState, name: ByteArray) { let caller = get_caller_address(); - let valid_address_1 = contract_address_const::< - FundManagerConstants::VALID_ADDRESS_1 - >(); - let valid_address_2 = contract_address_const::< - FundManagerConstants::VALID_ADDRESS_2 - >(); + let valid_address_1 = contract_address_const::(); + let valid_address_2 = contract_address_const::(); assert!( - self.owner.read() == caller || valid_address_1 == caller || valid_address_2 == caller, - "You must be an owner or admin to perform this action"); + self.owner.read() == caller + || valid_address_1 == caller + || valid_address_2 == caller, + "You must be an owner or admin to perform this action" + ); self.name.write(name); } fn get_name(self: @ContractState) -> ByteArray { diff --git a/contracts/tests/test_fund.cairo b/contracts/tests/test_fund.cairo index 890cd5e..8278a94 100644 --- a/contracts/tests/test_fund.cairo +++ b/contracts/tests/test_fund.cairo @@ -491,60 +491,66 @@ fn test_emit_event_donation_withdraw() { dispatcher.withdraw(); - spy.assert_emitted(@array![ - ( - contract_address, - Fund::Event::DonationWithdraw(Fund::DonationWithdraw { - owner_address: OWNER(), - fund_contract_address: contract_address, - withdrawn_amount - }) - ) - ]); + spy + .assert_emitted( + @array![ + ( + contract_address, + Fund::Event::DonationWithdraw( + Fund::DonationWithdraw { + owner_address: OWNER(), + fund_contract_address: contract_address, + withdrawn_amount + } + ) + ) + ] + ); } + #[test] #[should_panic(expected: ("You must be an owner or admin to perform this action",))] -fn test_set_contact_handle(){ +fn test_set_contact_handle_error() { + let contract_address = _setup_(); + let dispatcher = IFundDispatcher { contract_address }; + let contact_handle = dispatcher.get_contact_handle(); + assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); + + start_cheat_caller_address_global(OTHER_USER()); + dispatcher.set_contact_handle(CONTACT_HANDLE_2()) +} + +#[test] +fn test_set_contact_handle_success() { let contract_address = _setup_(); let dispatcher = IFundDispatcher { contract_address }; let contact_handle = dispatcher.get_contact_handle(); assert(contact_handle == CONTACT_HANDLE_1(), 'Invalid contact handle'); - //owner start_cheat_caller_address_global(OWNER()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); - - //valid_address1 + start_cheat_caller_address_global(VALID_ADDRESS_1()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); assert(reverted_contact_handle == CONTACT_HANDLE_1(), 'revert'); - - //valid_address2 + start_cheat_caller_address_global(VALID_ADDRESS_2()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get + dispatcher.set_contact_handle(CONTACT_HANDLE_2()); + let new_contact_handle = dispatcher.get_contact_handle(); assert(new_contact_handle == CONTACT_HANDLE_2(), 'Set contact method not working'); dispatcher.set_contact_handle(CONTACT_HANDLE_1()); let reverted_contact_handle = dispatcher.get_contact_handle(); - assert(reverted_contact_handle == CONTACT_HANDLE_1(),' revert '); - - // set a no valid address - //always should fail - start_cheat_caller_address_global(OTHER_USER()); - dispatcher.set_contact_handle(CONTACT_HANDLE_2());//set - let new_contact_handle = dispatcher.get_contact_handle();//get - assert(new_contact_handle == CONTACT_HANDLE_1(),'check the function'); - + assert(reverted_contact_handle == CONTACT_HANDLE_1(), ' revert ') +} -}