Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: standardize test function names across codebase #504

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,26 +412,26 @@ mod tests {
}

#[motsu::test]
fn default_role_is_default_admin(contract: AccessControl) {
fn default_admin_role_success(contract: AccessControl) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn default_admin_role_success(contract: AccessControl) {
fn get_role_admin_returns_default_admin_for_regular_role(
contract: AccessControl,
) {

Don't forget the requirements:

function_name_description for successful tests.

function_name is the name of the function that is being tested, and this should apply to getters as well.

Double check that all the other test names follow this convention.

let role_admin = contract.get_role_admin(ROLE.into());
assert_eq!(role_admin, AccessControl::DEFAULT_ADMIN_ROLE);
}

#[motsu::test]
fn default_admin_roles_admin_is_itself(contract: AccessControl) {
fn default_admin_role_admin_success(contract: AccessControl) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn default_admin_role_admin_success(contract: AccessControl) {
fn get_role_admin_returns_default_admin_for_default_admin_role(
contract: AccessControl,
) {

const DEFAULT_ADMIN_ROLE: [u8; 32] = AccessControl::DEFAULT_ADMIN_ROLE;
let role_admin = contract.get_role_admin(DEFAULT_ADMIN_ROLE.into());
assert_eq!(role_admin, DEFAULT_ADMIN_ROLE);
}

#[motsu::test]
fn non_admin_cannot_grant_role_to_others(contract: AccessControl) {
fn grant_role_reverts_when_not_admin(contract: AccessControl) {
let err = contract.grant_role(ROLE.into(), ALICE).unwrap_err();
assert!(matches!(err, Error::UnauthorizedAccount(_)));
}

#[motsu::test]
fn accounts_can_be_granted_roles_multiple_times(contract: AccessControl) {
fn grant_role_success_with_multiple_grants(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

contract.grant_role(ROLE.into(), ALICE).unwrap();
Expand All @@ -441,7 +441,7 @@ mod tests {
}

#[motsu::test]
fn not_granted_roles_can_be_revoked(contract: AccessControl) {
fn revoke_role_success_with_ungranted_role(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

let has_role = contract.has_role(ROLE.into(), ALICE);
Expand All @@ -452,7 +452,7 @@ mod tests {
}

#[motsu::test]
fn admin_can_revoke_role(contract: AccessControl) {
fn revoke_role_success(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);

Expand All @@ -464,7 +464,7 @@ mod tests {
}

#[motsu::test]
fn non_admin_cannot_revoke_role(contract: AccessControl) {
fn revoke_role_reverts_when_not_admin(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);

let has_role = contract.has_role(ROLE.into(), ALICE);
Expand All @@ -474,7 +474,7 @@ mod tests {
}

#[motsu::test]
fn roles_can_be_revoked_multiple_times(contract: AccessControl) {
fn revoke_role_success_with_multiple_revokes(contract: AccessControl) {
_grant_role_to_msg_sender(contract, AccessControl::DEFAULT_ADMIN_ROLE);

contract.revoke_role(ROLE.into(), ALICE).unwrap();
Expand All @@ -484,7 +484,7 @@ mod tests {
}

#[motsu::test]
fn bearer_can_renounce_role(contract: AccessControl) {
fn renounce_role_success(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);

let has_role = contract.has_role(ROLE.into(), msg::sender());
Expand All @@ -495,14 +495,14 @@ mod tests {
}

#[motsu::test]
fn only_sender_can_renounce(contract: AccessControl) {
fn renounce_role_reverts_when_not_sender(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);
let err = contract.renounce_role(ROLE.into(), ALICE).unwrap_err();
assert!(matches!(err, Error::BadConfirmation(_)));
}

#[motsu::test]
fn roles_can_be_renounced_multiple_times(contract: AccessControl) {
fn renounce_role_success_with_multiple_renounces(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);

let sender = msg::sender();
Expand All @@ -513,7 +513,7 @@ mod tests {
}

#[motsu::test]
fn a_roles_admin_role_can_change(contract: AccessControl) {
fn set_role_admin_success(contract: AccessControl) {
contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());
_grant_role_to_msg_sender(contract, OTHER_ROLE);

Expand All @@ -522,7 +522,7 @@ mod tests {
}

#[motsu::test]
fn the_new_admin_can_grant_roles(contract: AccessControl) {
fn role_admin_change_success_with_new_grant(contract: AccessControl) {
Copy link
Collaborator

@0xNeshi 0xNeshi Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn role_admin_change_success_with_new_grant(contract: AccessControl) {
fn grant_role_succeeds_when_called_by_new_admin(contract: AccessControl) {
  • grant_role is actually being tested here
  • related to the comment above, make sure that all test case names follow the expected convention

contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());
_grant_role_to_msg_sender(contract, OTHER_ROLE);

Expand All @@ -532,7 +532,7 @@ mod tests {
}

#[motsu::test]
fn the_new_admin_can_revoke_roles(contract: AccessControl) {
fn role_admin_change_success_with_new_revoke(contract: AccessControl) {
contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());
_grant_role_to_msg_sender(contract, OTHER_ROLE);

Expand All @@ -543,7 +543,9 @@ mod tests {
}

#[motsu::test]
fn previous_admins_no_longer_grant_roles(contract: AccessControl) {
fn role_admin_change_reverts_when_old_admin_grants(
contract: AccessControl,
) {
_grant_role_to_msg_sender(contract, ROLE);
contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());

Expand All @@ -552,7 +554,9 @@ mod tests {
}

#[motsu::test]
fn previous_admins_no_longer_revoke_roles(contract: AccessControl) {
fn role_admin_change_reverts_when_old_admin_revokes(
contract: AccessControl,
) {
_grant_role_to_msg_sender(contract, ROLE);
contract._set_role_admin(ROLE.into(), OTHER_ROLE.into());

Expand All @@ -561,13 +565,13 @@ mod tests {
}

#[motsu::test]
fn does_not_revert_if_sender_has_role(contract: AccessControl) {
fn check_role_success(contract: AccessControl) {
_grant_role_to_msg_sender(contract, ROLE);
contract._check_role(ROLE.into(), msg::sender()).unwrap();
}

#[motsu::test]
fn reverts_if_sender_doesnt_have_role(contract: AccessControl) {
fn check_role_reverts_when_no_role(contract: AccessControl) {
let err = contract._check_role(ROLE.into(), msg::sender()).unwrap_err();
assert!(matches!(err, Error::UnauthorizedAccount(_)));
let err =
Expand All @@ -576,27 +580,27 @@ mod tests {
}

#[motsu::test]
fn internal_grant_role_true_if_no_role(contract: AccessControl) {
fn internal_grant_role_success_when_no_role(contract: AccessControl) {
let role_granted = contract._grant_role(ROLE.into(), ALICE);
assert!(role_granted);
}

#[motsu::test]
fn internal_grant_role_false_if_role(contract: AccessControl) {
fn internal_grant_role_success_when_has_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
let role_granted = contract._grant_role(ROLE.into(), ALICE);
assert!(!role_granted);
}

#[motsu::test]
fn internal_revoke_role_true_if_role(contract: AccessControl) {
fn internal_revoke_role_success_when_has_role(contract: AccessControl) {
contract._roles.setter(ROLE.into()).has_role.insert(ALICE, true);
let role_revoked = contract._revoke_role(ROLE.into(), ALICE);
assert!(role_revoked);
}

#[motsu::test]
fn internal_revoke_role_false_if_no_role(contract: AccessControl) {
fn internal_revoke_role_success_when_no_role(contract: AccessControl) {
let role_revoked = contract._revoke_role(ROLE.into(), ALICE);
assert!(!role_revoked);
}
Expand Down
14 changes: 7 additions & 7 deletions contracts/src/access/ownable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ mod tests {
const ALICE: Address = address!("A11CEacF9aa32246d767FCCD72e02d6bCbcC375d");

#[motsu::test]
fn reads_owner(contract: Ownable) {
fn owner_read_success(contract: Ownable) {
contract._owner.set(msg::sender());
let owner = contract.owner();
assert_eq!(owner, msg::sender());
}

#[motsu::test]
fn transfers_ownership(contract: Ownable) {
fn transfer_ownership_success(contract: Ownable) {
contract._owner.set(msg::sender());

contract.transfer_ownership(ALICE).expect("should transfer ownership");
Expand All @@ -227,7 +227,7 @@ mod tests {
}

#[motsu::test]
fn prevents_non_onwers_from_transferring(contract: Ownable) {
fn transfer_ownership_reverts_when_not_owner(contract: Ownable) {
// Alice must be set as owner, because we can't set the
// `msg::sender` yet.
contract._owner.set(ALICE);
Expand All @@ -238,15 +238,15 @@ mod tests {
}

#[motsu::test]
fn prevents_reaching_stuck_state(contract: Ownable) {
fn transfer_ownership_reverts_when_zero_address(contract: Ownable) {
contract._owner.set(msg::sender());

let err = contract.transfer_ownership(Address::ZERO).unwrap_err();
assert!(matches!(err, Error::InvalidOwner(_)));
}

#[motsu::test]
fn loses_ownership_after_renouncing(contract: Ownable) {
fn renounce_ownership_success(contract: Ownable) {
contract._owner.set(msg::sender());

let _ = contract.renounce_ownership();
Expand All @@ -255,7 +255,7 @@ mod tests {
}

#[motsu::test]
fn prevents_non_owners_from_renouncing(contract: Ownable) {
fn renounce_ownership_reverts_when_not_owner(contract: Ownable) {
// Alice must be set as owner, because we can't set the
// `msg::sender` yet.
contract._owner.set(ALICE);
Expand All @@ -265,7 +265,7 @@ mod tests {
}

#[motsu::test]
fn recovers_access_using_internal_transfer(contract: Ownable) {
fn internal_transfer_ownership_success(contract: Ownable) {
contract._owner.set(ALICE);

contract._transfer_ownership(ALICE);
Expand Down
26 changes: 14 additions & 12 deletions contracts/src/access/ownable_two_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,21 @@ mod tests {
const BOB: Address = address!("B0B0cB49ec2e96DF5F5fFB081acaE66A2cBBc2e2");

#[motsu::test]
fn reads_owner(contract: Ownable2Step) {
fn owner_read_success(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());
let owner = contract.owner();
assert_eq!(owner, msg::sender());
}

#[motsu::test]
fn reads_pending_owner(contract: Ownable2Step) {
fn pending_owner_read_success(contract: Ownable2Step) {
contract._pending_owner.set(ALICE);
let pending_owner = contract.pending_owner();
assert_eq!(pending_owner, ALICE);
}

#[motsu::test]
fn initiates_ownership_transfer(contract: Ownable2Step) {
fn transfer_ownership_success(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());

contract
Expand All @@ -256,7 +256,7 @@ mod tests {
}

#[motsu::test]
fn prevents_non_owners_from_initiating_transfer(contract: Ownable2Step) {
fn transfer_ownership_reverts_when_not_owner(contract: Ownable2Step) {
contract._ownable._owner.set(ALICE);

let err = contract.transfer_ownership(BOB).unwrap_err();
Expand All @@ -267,7 +267,7 @@ mod tests {
}

#[motsu::test]
fn accepts_ownership(contract: Ownable2Step) {
fn accept_ownership_success(contract: Ownable2Step) {
contract._ownable._owner.set(ALICE);
contract._pending_owner.set(msg::sender());

Expand All @@ -277,7 +277,7 @@ mod tests {
}

#[motsu::test]
fn prevents_non_pending_owner_from_accepting(contract: Ownable2Step) {
fn accept_ownership_reverts_when_not_pending_owner(contract: Ownable2Step) {
contract._ownable._owner.set(ALICE);
contract._pending_owner.set(BOB);

Expand All @@ -289,7 +289,7 @@ mod tests {
}

#[motsu::test]
fn completes_two_step_ownership_transfer(contract: Ownable2Step) {
fn ownership_transfer_success_with_two_steps(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());

contract
Expand All @@ -307,15 +307,15 @@ mod tests {
}

#[motsu::test]
fn renounces_ownership(contract: Ownable2Step) {
fn renounce_ownership_success(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());

contract.renounce_ownership().expect("should renounce ownership");
assert_eq!(contract.owner(), Address::ZERO);
}

#[motsu::test]
fn prevents_non_owners_from_renouncing(contract: Ownable2Step) {
fn renounce_ownership_reverts_when_not_owner(contract: Ownable2Step) {
contract._ownable._owner.set(ALICE);

let err = contract.renounce_ownership().unwrap_err();
Expand All @@ -326,7 +326,9 @@ mod tests {
}

#[motsu::test]
fn cancels_transfer_on_renounce(contract: Ownable2Step) {
fn renounce_ownership_success_with_pending_transfer(
contract: Ownable2Step,
) {
contract._ownable._owner.set(msg::sender());
contract._pending_owner.set(ALICE);

Expand All @@ -336,7 +338,7 @@ mod tests {
}

#[motsu::test]
fn allows_owner_to_cancel_transfer(contract: Ownable2Step) {
fn transfer_ownership_success_with_cancel(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());
contract._pending_owner.set(ALICE);

Expand All @@ -348,7 +350,7 @@ mod tests {
}

#[motsu::test]
fn allows_owner_to_overwrite_transfer(contract: Ownable2Step) {
fn transfer_ownership_success_with_overwrite(contract: Ownable2Step) {
contract._ownable._owner.set(msg::sender());

contract
Expand Down
Loading
Loading