Skip to content

Commit

Permalink
fix: add some unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
huyhuynh3103 committed Nov 14, 2024
1 parent 7ae9072 commit c79dd17
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
9 changes: 4 additions & 5 deletions src/ERC1155Common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ contract ERC1155Common is
onlyRole(MINTER_ROLE)
{
uint256 length = tos.length;
require(length == amounts.length, "ERC1155: invalid array lengths");
require(length == datas.length, "ERC1155: invalid array lengths");
require(length != 0 && length == amounts.length && length == datas.length, "ERC1155: invalid array lengths");

for (uint256 i; i < length; ++i) {
_mint(tos[i], id, amounts[i], datas[i]);
Expand All @@ -101,22 +100,22 @@ contract ERC1155Common is
/**
* @dev See {ERC1155-uri}.
*/
function uri(uint256 tokenId) public view override returns (string memory) {
function uri(uint256 tokenId) public view virtual override returns (string memory) {
string memory uri_ = super.uri(tokenId);
return string.concat(uri_, tokenId.toString());
}

/**
* @dev Collection name.
*/
function name() public view returns (string memory) {
function name() public view virtual returns (string memory) {
return _name;
}

/**
* @dev Collection symbol.
*/
function symbol() public view returns (string memory) {
function symbol() public view virtual returns (string memory) {
return _symbol;
}

Expand Down
18 changes: 7 additions & 11 deletions src/mock/launchpad/SampleNFT1155Launchpad.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { ERC1155 } from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";
import { NFTLaunchpadCommon } from "../../launchpad/NFTLaunchpadCommon.sol";
import { SampleERC1155, ERC1155Common } from "../SampleERC1155.sol";

contract SampleNFT1155Launchpad is ERC1155, AccessControl, NFTLaunchpadCommon {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

constructor(address admin, address minter, string memory uri_) ERC1155(uri_) {
_setupRole(DEFAULT_ADMIN_ROLE, admin);
_setupRole(MINTER_ROLE, minter);
}
contract SampleNFT1155Launchpad is SampleERC1155, NFTLaunchpadCommon {
constructor(address admin, string memory name, string memory symbol, string memory uri)
SampleERC1155(admin, name, symbol, uri)
{ }

/// @dev Mint NFTs for the launchpad.
function mintLaunchpad(address to, uint256 quantity, bytes calldata /* extraData */ )
Expand All @@ -35,9 +31,9 @@ contract SampleNFT1155Launchpad is ERC1155, AccessControl, NFTLaunchpadCommon {
public
view
virtual
override(ERC1155, AccessControl, NFTLaunchpadCommon)
override(ERC1155Common, NFTLaunchpadCommon)
returns (bool)
{
return super.supportsInterface(interfaceId);
return ERC1155Common.supportsInterface(interfaceId) || NFTLaunchpadCommon.supportsInterface(interfaceId);
}
}
2 changes: 1 addition & 1 deletion src/mock/launchpad/SampleNFT721Launchpad.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ contract SampleNFT721Launchpad is SampleERC721, NFTLaunchpadCommon {
override(ERC721Common, NFTLaunchpadCommon)
returns (bool)
{
return super.supportsInterface(interfaceId);
return ERC721Common.supportsInterface(interfaceId) || NFTLaunchpadCommon.supportsInterface(interfaceId);
}
}
32 changes: 32 additions & 0 deletions test/foundry/SampleNFT1155Launchpad.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { SampleNFT1155Launchpad, SampleERC1155 } from "../../src/mock/launchpad/SampleNFT1155Launchpad.sol";
import { INFTLaunchpad } from "src/interfaces/launchpad/INFTLaunchpad.sol";
import { IERC1155Common } from "src/interfaces/IERC1155Common.sol";

contract SampleERC1155LaunchpadTest is Test {
using Strings for uint256;

address admin = makeAddr("admin");
string public constant NAME = "SampleERC721";
string public constant SYMBOL = "NFT";
string public constant BASE_URI = "http://example.com/";

SampleNFT1155Launchpad internal _t;

function setUp() public virtual {
_t = new SampleNFT1155Launchpad(admin, NAME, SYMBOL, BASE_URI);
}

function testSupportInterface() public {
assertEq(_token().supportsInterface(type(INFTLaunchpad).interfaceId), true);
assertEq(_token().supportsInterface(type(IERC1155Common).interfaceId), true);
}

function _token() internal view virtual returns (SampleNFT1155Launchpad) {
return _t;
}
}
31 changes: 31 additions & 0 deletions test/foundry/SampleNFT721Launchpad.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { SampleNFT721Launchpad } from "../../src/mock/launchpad/SampleNFT721Launchpad.sol";
import { INFTLaunchpad } from "src/interfaces/launchpad/INFTLaunchpad.sol";
import { IERC721Common } from "src/interfaces/IERC721Common.sol";

contract SampleNFT721LaunchpadTest is Test {
using Strings for uint256;

string public constant NAME = "SampleERC721";
string public constant SYMBOL = "NFT";
string public constant BASE_URI = "http://example.com/";

SampleNFT721Launchpad internal _t;

function setUp() public virtual {
_t = new SampleNFT721Launchpad(NAME, SYMBOL, BASE_URI);
}

function testSupportInterface() public {
assertEq(_token().supportsInterface(type(INFTLaunchpad).interfaceId), true);
assertEq(_token().supportsInterface(type(IERC721Common).interfaceId), true);
}

function _token() internal view virtual returns (SampleNFT721Launchpad) {
return _t;
}
}

0 comments on commit c79dd17

Please sign in to comment.