From 197a7a28c5dc5e855c3b11a4a0b2b63ce4e793c1 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 20 Feb 2023 11:46:46 -0300 Subject: [PATCH] test: add and change tests to fuzz test (#128) --- contracts/test/foundry/FleekERC721/Burn.t.sol | 18 ++++--- .../test/foundry/FleekERC721/Deploy.t.sol | 8 +++ .../test/foundry/FleekERC721/GetToken.t.sol | 49 +++++++++++-------- contracts/test/foundry/FleekERC721/Mint.t.sol | 18 +++++++ .../test/foundry/FleekERC721/TokenURI.t.sol | 14 +++--- 5 files changed, 70 insertions(+), 37 deletions(-) diff --git a/contracts/test/foundry/FleekERC721/Burn.t.sol b/contracts/test/foundry/FleekERC721/Burn.t.sol index 7c448ea..bd35e4b 100644 --- a/contracts/test/foundry/FleekERC721/Burn.t.sol +++ b/contracts/test/foundry/FleekERC721/Burn.t.sol @@ -17,22 +17,24 @@ contract Test_FleekERC721_Burn is Test_FleekERC721_Base { CuT.burn(tokenId); } - function test_cannotBurnAsNotOwner() public { - vm.prank(address(1)); + function testFuzz_cannotBurnAsNotOwner(address account) public { + vm.assume(account != deployer); + vm.prank(account); expectRevertWithTokenRole(); CuT.burn(tokenId); } - function test_cannotBurnAsController() public { - address user = address(1); - CuT.grantTokenRole(tokenId, FleekAccessControl.Roles.Controller, user); - vm.prank(user); + function testFuzz_cannotBurnAsController(address account) public { + vm.assume(account != deployer); + CuT.grantTokenRole(tokenId, FleekAccessControl.Roles.Controller, account); + vm.prank(account); expectRevertWithTokenRole(); CuT.burn(tokenId); } - function test_cannotBurnInexistentToken() public { + function testFuzz_cannotBurnInexistentToken(uint256 _tokenId) public { + vm.assume(_tokenId != tokenId); expectRevertWithTokenRole(); // Token role is tested first before if token exists - CuT.burn(1); + CuT.burn(_tokenId); } } diff --git a/contracts/test/foundry/FleekERC721/Deploy.t.sol b/contracts/test/foundry/FleekERC721/Deploy.t.sol index cf646c5..67ceee4 100644 --- a/contracts/test/foundry/FleekERC721/Deploy.t.sol +++ b/contracts/test/foundry/FleekERC721/Deploy.t.sol @@ -21,4 +21,12 @@ contract Test_FleekERC721_Deploy is Test_FleekERC721_Base { function test_deployerShouldBeCollectionOwner() public { assertTrue(CuT.hasCollectionRole(FleekAccessControl.Roles.Owner, deployer)); } + + function testFuzz_nameAndSymbol(string memory _name, string memory _symbol) public { + CuT = new FleekERC721(); + CuT.initialize(_name, _symbol); + + assertEq(CuT.name(), _name); + assertEq(CuT.symbol(), _symbol); + } } diff --git a/contracts/test/foundry/FleekERC721/GetToken.t.sol b/contracts/test/foundry/FleekERC721/GetToken.t.sol index 0410be1..4cd2234 100644 --- a/contracts/test/foundry/FleekERC721/GetToken.t.sol +++ b/contracts/test/foundry/FleekERC721/GetToken.t.sol @@ -32,13 +32,22 @@ contract Test_FleekERC721_GetToken is Test_FleekERC721_Base { assertEq(currentBuild, 0); } - function test_getTokenAfterUpdate() public { - CuT.setTokenName(tokenId, "New App Name"); - CuT.setTokenDescription(tokenId, "New description for the app."); - CuT.setTokenExternalURL(tokenId, "https://new-url.com"); - CuT.setTokenENS(tokenId, "new-ens.eth"); - CuT.setTokenBuild(tokenId, "ce1a3fc141e29f8e1d00a654e156c4982d7711bf", "https://github.com/other/repo"); - CuT.setTokenLogoAndColor(tokenId, TestConstants.LOGO_1, 0x654321); + function testFuzz_getTokenAfterUpdate( + string memory newAppName, + string memory newDescription, + string memory newExternalURL, + string memory newENS, + string memory newCommitHash, + string memory newRepository, + string memory newLogo, + uint24 newColor + ) public { + CuT.setTokenName(tokenId, newAppName); + CuT.setTokenDescription(tokenId, newDescription); + CuT.setTokenExternalURL(tokenId, newExternalURL); + CuT.setTokenENS(tokenId, newENS); + CuT.setTokenBuild(tokenId, newCommitHash, newRepository); + CuT.setTokenLogoAndColor(tokenId, newLogo, newColor); ( string memory name, @@ -49,25 +58,23 @@ contract Test_FleekERC721_GetToken is Test_FleekERC721_Base { string memory logo, uint24 color ) = CuT.getToken(tokenId); - assertEq(name, "New App Name"); - assertEq(description, "New description for the app."); - assertEq(externalURL, "https://new-url.com"); - assertEq(logo, TestConstants.LOGO_1); - assertEq(color, 0x654321); - assertEq(ENS, "new-ens.eth"); + assertEq(name, newAppName); + assertEq(description, newDescription); + assertEq(externalURL, newExternalURL); + assertEq(logo, newLogo); + assertEq(color, newColor); + assertEq(ENS, newENS); assertEq(currentBuild, 1); } - function test_getTokenForDifferentAddresses() public { - vm.prank(address(1)); - CuT.getToken(tokenId); - vm.prank(address(2)); - CuT.getToken(tokenId); - vm.prank(address(3)); + function testFuzz_getTokenForDifferentAddresses(address account) public { + vm.prank(account); CuT.getToken(tokenId); } - function testFail_tokenURIForNonExistentId() public view { - CuT.getToken(1); + function testFuzz_tokenURIForNonExistentId(uint256 _tokenId) public { + vm.assume(_tokenId != tokenId); + expectRevertWithInvalidTokenId(); + CuT.getToken(_tokenId); } } diff --git a/contracts/test/foundry/FleekERC721/Mint.t.sol b/contracts/test/foundry/FleekERC721/Mint.t.sol index cb017bd..940a736 100644 --- a/contracts/test/foundry/FleekERC721/Mint.t.sol +++ b/contracts/test/foundry/FleekERC721/Mint.t.sol @@ -49,4 +49,22 @@ contract Test_FleekERC721_Mint is Test_FleekERC721_Base { assertEq(CuT.balanceOf(deployer), 1); } + + function testFuzz_mint( + address to, + string memory appName, + string memory description, + string memory externalURL, + string memory ens, + string memory commitHash, + string memory gitRepository, + string memory logo, + uint24 color + ) public { + vm.assume(to != address(0)); + uint256 tokenId = CuT.mint(to, appName, description, externalURL, ens, commitHash, gitRepository, logo, color); + + assertEq(tokenId, 0); + assertEq(CuT.ownerOf(tokenId), to); + } } diff --git a/contracts/test/foundry/FleekERC721/TokenURI.t.sol b/contracts/test/foundry/FleekERC721/TokenURI.t.sol index c19c6ce..5ed6a2f 100644 --- a/contracts/test/foundry/FleekERC721/TokenURI.t.sol +++ b/contracts/test/foundry/FleekERC721/TokenURI.t.sol @@ -66,17 +66,15 @@ contract Test_FleekERC721_TokenURI is Test_FleekERC721_Base, Test_FleekERC721_To ); } - function test_tokenURIForDifferentAddresses() public { - vm.prank(address(1)); - CuT.tokenURI(tokenId); - vm.prank(address(2)); - CuT.tokenURI(tokenId); - vm.prank(address(3)); + function testFuzz_tokenURIForDifferentAddresses(address account) public { + vm.prank(account); CuT.tokenURI(tokenId); } - function testFail_tokenURIForInexistentId() public view { - CuT.tokenURI(1); + function testFuzz_tokenURIForInexistentId(uint256 _tokenId) public { + vm.assume(_tokenId != tokenId); + expectRevertWithInvalidTokenId(); + CuT.tokenURI(_tokenId); } function test_shouldEmitEventForMetadataChanges() public {