DECANFT-SC/test/DCO2s.t.sol

128 lines
4.3 KiB
Solidity
Raw Normal View History

2023-11-21 22:45:27 +00:00
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;
2023-11-30 06:48:48 +00:00
import {Test, console} from "forge-std/Test.sol";
2023-11-21 22:45:27 +00:00
import {DCO2s} from "../src/DCO2s.sol";
contract DCO2sTest is Test {
DCO2s public dco2sc;
string public name;
2024-04-25 00:21:13 +00:00
string public symbol;
string public baseURI;
string[] public nfts;
2023-11-21 22:45:27 +00:00
function setUp() public {
2024-04-25 00:21:13 +00:00
baseURI = vm.envString("BASE_URI");
name = vm.envString("NAME");
symbol = vm.envString("SYMBOL");
dco2sc = new DCO2s(address(this), address(this), baseURI, name, symbol);
// Import nfts array from .env and split by ' '
nfts = vm.envString("nfts", " ");
2023-11-21 22:45:27 +00:00
}
// @notice this code safe mints an NFT to address 0x1
function testMint() public {
address to = address(0x1);
dco2sc.safeMint(to, nfts[0]);
2023-12-04 23:28:06 +00:00
assertEq(dco2sc.ownerOf(0), to, "Token should be minted to specified address.");
}
// @notice: here we test getting the Base and full URIS
2024-04-25 00:21:13 +00:00
function testGetURI15() public {
dco2sc.safeMint(address(0x1), nfts[15]);
2023-12-04 23:28:06 +00:00
string memory tokenURI = dco2sc.tokenURI(0);
//console.log(tokenURI);
2024-04-25 00:21:13 +00:00
assertEq(tokenURI, string.concat(baseURI, nfts[15]));
2023-12-04 23:28:06 +00:00
}
2023-11-30 06:48:48 +00:00
// @notice: checkFailed token Index
function testFailTokenIndex() public view {
2023-11-30 06:48:48 +00:00
dco2sc.tokenURI(0);
}
// Todo How to verify bytes message error
2023-11-30 06:48:48 +00:00
// @notice: checkFailed token Index
//function testTokenIndex() public {
// vm.expectRevert(bytes("ERC721NonexistentToken(0)"));
// dco2sc.tokenURI(0);
//}
2023-11-21 22:45:27 +00:00
// @notice use basic Enumerable properties totalSupply
// Find a more efficient way mintedNFTs = nfts[:5];?
function testEnumerableTotalSupply() public {
string[5] memory mintedNFTs;
for (uint256 i = 0; i < 5; i++) {
mintedNFTs[i] = nfts[i];
}
// mint 5 nfts
for (uint256 i = 0; i < mintedNFTs.length; i++) {
dco2sc.safeMint(address(1), mintedNFTs[i]);
}
assertEq(mintedNFTs.length, dco2sc.totalSupply());
}
2024-04-25 00:21:13 +00:00
// @notice: use the basic Enumerable property tokenByIndex
// @notice: All nfts in the .env
2023-12-04 23:28:06 +00:00
function testEnumerableTokenByIndex() public {
for (uint256 i; i < nfts.length; i++) {
2024-04-25 00:21:13 +00:00
dco2sc.safeMint(address(1), nfts[i]);
}
2024-04-25 00:21:13 +00:00
assertEq(nfts.length, dco2sc.totalSupply());
2023-12-04 23:28:06 +00:00
// Test for token By Index 4
uint256 tokenID = dco2sc.tokenByIndex(4);
2024-04-25 00:21:13 +00:00
assertEq(nfts[4], nfts[tokenID]);
2023-12-04 23:28:06 +00:00
// Test for token By Index 3
tokenID = dco2sc.tokenByIndex(3);
2024-04-25 00:21:13 +00:00
assertEq(nfts[3], nfts[tokenID]);
}
// @notice use the basic Enumerable property TokenOfOwnerByIndex
2024-04-25 00:21:13 +00:00
// @notice: All nfts in the .env
function testEnumerableTokenOfOwnerByIndex() public {
2024-04-25 00:21:13 +00:00
// mints pair to address 1
for (uint256 i; i < nfts.length; i++) {
if (i % 2 == 0) {
2024-04-25 00:21:13 +00:00
dco2sc.safeMint(address(1), nfts[i]);
} else {
2024-04-25 00:21:13 +00:00
dco2sc.safeMint(address(2), nfts[i]);
}
}
2024-04-25 00:21:13 +00:00
// Get the balance of NFTs of the account 1 pairs are 8
uint256 account1_balance = dco2sc.balanceOf(address(1));
2024-04-25 00:21:13 +00:00
assertEq(account1_balance, 8);
for (uint256 i; i < account1_balance; i++) {
// get the TokenID from account NFTs Index
uint256 token_id = dco2sc.tokenOfOwnerByIndex(address(1), i);
2024-04-25 00:21:13 +00:00
assertEq(dco2sc.tokenURI(token_id), string.concat(baseURI, nfts[token_id]));
}
2023-12-04 23:28:06 +00:00
}
2023-11-21 22:45:27 +00:00
// @notice: testFailMintToZeroAddress test that cannot safe mint to address 0
function testFailMintToZeroAddress() public {
dco2sc.safeMint(address(0), nfts[0]);
2023-11-21 22:45:27 +00:00
}
// @notice testMintAll mints all the NFTs URIS and distribute to wallets
// then it assertEq that it is properly minted to the wallet and the correct URI
function testMintAll() public {
//assign to nfts.lenght addresses each one 1 NFT
for (uint256 i; i < nfts.length; i++) {
dco2sc.safeMint(address(uint160(i + 1)), nfts[i]);
2024-04-25 00:21:13 +00:00
}
// assert that each NFT gets minted with the proper URI
// and address
for (uint256 i; i < nfts.length; i++) {
uint256 token_id = dco2sc.tokenOfOwnerByIndex(address(uint160(i + 1)), 0);
2024-04-25 00:21:13 +00:00
assertEq(string.concat(baseURI, nfts[i]), dco2sc.tokenURI(token_id));
}
}
2023-11-21 22:45:27 +00:00
}