Skip to content

Commit

Permalink
remover blank spaces,divided test in success and error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisFernandezVivas committed Nov 25, 2024
1 parent 452bc59 commit 6373a02
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
15 changes: 7 additions & 8 deletions contracts/src/fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FundManagerConstants::VALID_ADDRESS_1>();
let valid_address_2 = contract_address_const::<FundManagerConstants::VALID_ADDRESS_2>();
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 {
Expand Down
70 changes: 38 additions & 32 deletions contracts/tests/test_fund.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ')
}


}

0 comments on commit 6373a02

Please sign in to comment.