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(erc721): add unit tests #142

Merged
merged 36 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
853c7e1
ref: remove unused imports in tests
bidzyyys Jun 19, 2024
af916e9
test(erc721): balance_of tests (#69)
bidzyyys Jun 19, 2024
ba56082
test(erc721): owner_of unit tests (#69)
bidzyyys Jun 19, 2024
e400f74
test(erc721): transfer_from unit test (#69)
bidzyyys Jun 19, 2024
d312f2d
chore: implement Display trait for PanicCodes
bidzyyys Jun 20, 2024
85b80d6
fix: failing test
bidzyyys Jun 20, 2024
57b7a58
test(motsu): add shims for account_codehash (#69)
bidzyyys Jun 20, 2024
4a4f49d
test(erc721): safe_transfer_from unit tests (#69)
bidzyyys Jun 20, 2024
aada7cc
test(erc721): safe_transfer_from_with_data unit tests (#69)
bidzyyys Jun 20, 2024
86941a2
test(erc721): approve unit tests (#69)
bidzyyys Jun 21, 2024
c21b031
test(erc721): set_approval_for_all unit tests (#69)
bidzyyys Jun 21, 2024
4e17778
test(erc721): get_approved unit tests (#69)
bidzyyys Jun 21, 2024
433bcf8
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 21, 2024
420dc39
ref: fmt
bidzyyys Jun 21, 2024
3f8ecf5
test(erc721): _owner_of_inner unit tests (#69)
bidzyyys Jun 21, 2024
91162e1
test(erc721): _get_approved_inner unit tests (#69)
bidzyyys Jun 21, 2024
d63a6b6
test(erc721): _is_authorized unit tests (#69)
bidzyyys Jun 21, 2024
489e1db
test(erc721): _check_authorized unit tests (#69)
bidzyyys Jun 21, 2024
7bc9e5b
test(erc721): _safe_mint unit tests (#69)
bidzyyys Jun 21, 2024
7482719
test(erc721): _burn unit tests (#69)
bidzyyys Jun 21, 2024
ae017c3
test(erc721): _transfer unit tests (#69)
bidzyyys Jun 21, 2024
7e73ae2
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 21, 2024
0d6eb5f
test(erc721): _safe_transfer unit tests (#69)
bidzyyys Jun 21, 2024
f20ec5a
test(erc721): _approve unit tests (#69)
bidzyyys Jun 21, 2024
c7a3ce1
test(erc721): _set_approval_for_all unit tests (#69)
bidzyyys Jun 21, 2024
a94ae28
test(erc721): _require_owned unit tests (#69)
bidzyyys Jun 21, 2024
3b16acb
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 21, 2024
fd02dab
ref: revert docs
bidzyyys Jun 21, 2024
3de8f56
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 24, 2024
ba797df
chore: add dummy shims for motsu (#69)
bidzyyys Jun 24, 2024
42aace9
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 24, 2024
b51e3df
ref(erc721): unit tests naming convention for error checks (#69)
bidzyyys Jun 24, 2024
be831aa
chore: apply CR comments (#69)
bidzyyys Jun 24, 2024
4d9a2f1
Merge branch 'main' into test/erc721-unit-tests
bidzyyys Jun 24, 2024
34620f3
use uint! macro
qalisander Jun 24, 2024
ed1da07
use uint! macro
qalisander Jun 24, 2024
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
7 changes: 2 additions & 5 deletions contracts/src/access/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,8 @@ impl AccessControl {

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{address, Address, U256};
use stylus_sdk::{
msg,
storage::{StorageMap, StorageType},
};
use alloy_primitives::{address, Address};
use stylus_sdk::msg;

use super::{AccessControl, Error};

Expand Down
3 changes: 1 addition & 2 deletions contracts/src/token/erc20/extensions/capped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ impl Capped {

#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{uint, U256};
use stylus_sdk::storage::{StorageType, StorageU256};
use alloy_primitives::uint;

use super::Capped;

Expand Down
2 changes: 1 addition & 1 deletion contracts/src/token/erc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl Erc20 {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{address, uint, Address, U256};
use stylus_sdk::{msg, storage::StorageType};
use stylus_sdk::msg;

use super::{Erc20, Error, IErc20};

Expand Down
127 changes: 110 additions & 17 deletions contracts/src/token/erc721/extensions/burnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ mod tests {

use super::IErc721Burnable;
use crate::token::erc721::{
tests::random_token_id, Erc721, Error, IErc721,
tests::random_token_id, ERC721InsufficientApproval,
ERC721NonexistentToken, Erc721, Error, IErc721,
};

const BOB: Address = address!("F4EaCDAbEf3c8f1EdE91b6f2A6840bc2E4DD3526");
Expand All @@ -70,21 +71,94 @@ mod tests {
.expect("should return the balance of Alice");

let result = contract.burn(token_id);
assert!(result.is_ok());

let balance = contract
.balance_of(alice)
.expect("should return the balance of Alice");

let err = contract.owner_of(token_id).unwrap_err();
assert_eq!(initial_balance - one, balance);

let err = contract
.owner_of(token_id)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
));
}

#[motsu::test]
fn burns_with_approval(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");

let initial_balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

assert!(matches!(err, Error::NonexistentToken(_)));
contract._token_approvals.setter(token_id).set(alice);

let result = contract.burn(token_id);
assert!(result.is_ok());

assert_eq!(initial_balance - one, balance);
let err = contract
.owner_of(token_id)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
));

let balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

assert_eq!(initial_balance - uint!(1_U256), balance);
}

#[motsu::test]
fn get_approved_errors_when_previous_approval_burned(contract: Erc721) {
fn burns_with_approval_for_all(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");

let initial_balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

// As we cannot change `msg::sender()`, we need to use this workaround.
contract._operator_approvals.setter(BOB).setter(alice).set(true);

let result = contract.burn(token_id);

assert!(result.is_ok());

let err = contract
.owner_of(token_id)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
));

let balance =
contract.balance_of(BOB).expect("should return the balance of Bob");

assert_eq!(initial_balance - uint!(1_U256), balance);
}

#[motsu::test]
fn error_when_get_approved_of_previous_approval_burned(contract: Erc721) {
let alice = msg::sender();
let token_id = random_token_id();

Expand All @@ -95,31 +169,50 @@ mod tests {

contract.burn(token_id).expect("should burn previously minted token");

let err = contract.get_approved(token_id).unwrap_err();
let err = contract
.get_approved(token_id)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(err, Error::NonexistentToken(_)));
assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
));
}

#[motsu::test]
fn burn_errors_when_no_previous_approval(contract: Erc721) {
fn error_when_burn_without_approval(contract: Erc721) {
let token_id = random_token_id();

contract._mint(BOB, token_id).expect("should mint a token for Bob");

let err = contract.burn(token_id).unwrap_err();

assert!(matches!(err, Error::InsufficientApproval(_)));
let err = contract
.burn(token_id)
.expect_err("should not burn unapproved token");

assert!(matches!(
err,
Error::InsufficientApproval(ERC721InsufficientApproval {
operator,
token_id: t_id,
}) if operator == msg::sender() && t_id == token_id
));
}

#[motsu::test]
fn burn_errors_when_unknown_token(contract: Erc721) {
let alice = msg::sender();
fn error_when_burn_nonexistent_token(contract: Erc721) {
let token_id = random_token_id();

contract._mint(alice, token_id).expect("should mint a token for Alice");

let err = contract.burn(token_id + uint!(1_U256)).unwrap_err();
let err = contract
.burn(token_id)
.expect_err("should return Error::NonexistentToken");

assert!(matches!(err, Error::NonexistentToken(_)));
assert!(matches!(
err,
Error::NonexistentToken (ERC721NonexistentToken{
token_id: t_id
}) if t_id == token_id
));
}
}
16 changes: 6 additions & 10 deletions contracts/src/token/erc721/extensions/enumerable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,7 @@ impl Erc721Enumerable {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::{address, uint, Address, U256};
use stylus_sdk::{
msg,
prelude::StorageType,
storage::{StorageMap, StorageVec},
};
use stylus_sdk::msg;

use super::{Erc721Enumerable, Error, IErc721Enumerable};
use crate::token::erc721::{tests::random_token_id, Erc721, IErc721};
Expand All @@ -326,9 +322,7 @@ mod tests {
}

#[motsu::test]
fn token_by_index_errors_when_index_out_of_bound(
contract: Erc721Enumerable,
) {
fn error_when_token_by_index_is_out_of_bound(contract: Erc721Enumerable) {
assert_eq!(U256::ZERO, contract.total_supply());

let token_idx = uint!(2024_U256);
Expand Down Expand Up @@ -453,7 +447,9 @@ mod tests {
}

#[motsu::test]
fn token_of_owner_errors_index_out_of_bound(contract: Erc721Enumerable) {
fn error_when_token_of_owner_for_index_out_of_bound(
contract: Erc721Enumerable,
) {
let alice = msg::sender();
let mut erc721 = Erc721::default();
assert_eq!(
Expand All @@ -478,7 +474,7 @@ mod tests {
}

#[motsu::test]
fn token_of_owner_errors_owner_does_not_own_any_token(
fn error_when_token_of_owner_does_not_own_any_token(
contract: Erc721Enumerable,
) {
let erc721 = Erc721::default();
Expand Down
1 change: 0 additions & 1 deletion contracts/src/token/erc721/extensions/uri_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl Erc721UriStorage {
#[cfg(all(test, feature = "std"))]
mod tests {
use alloy_primitives::U256;
use stylus_sdk::{prelude::StorageType, storage::StorageMap};

use super::Erc721UriStorage;

Expand Down
Loading
Loading