contract TimeManipulationTest is Test { TimeManipulation public nft;
// Computes address for a given private key address alice = vm.addr(1);
functionsetUp() public { nft = newTimeManipulation(); }
// forge test -vv --match-test testMint functiontestMint() public { console.log("Condition 1: block.timestamp % 170 != 0"); // Set block.timestamp to 169 vm.warp(169); console.log("block.timestamp: %s", block.timestamp); // Sets all subsequent calls' msg.sender to be the input address // until `stopPrank` is called vm.startPrank(alice); console.log("alice balance before mint: %s", nft.balanceOf(alice)); nft.luckyMint(); console.log("alice balance after mint: %s", nft.balanceOf(alice));
// Set block.timestamp to 17000 console.log("Condition 2: block.timestamp % 170 == 0"); vm.warp(17000); console.log("block.timestamp: %s", block.timestamp); console.log("alice balance before mint: %s", nft.balanceOf(alice)); nft.luckyMint(); console.log("alice balance after mint: %s", nft.balanceOf(alice)); vm.stopPrank(); } }
forge init TimeMnipulation cd TimeMnipulation forge install Openzeppelin/openzeppelin-contracts
将这一讲的代码分别复制到src和test目录下,然后使用下列命令启动测试用例:
1
forge test -vv --match-test testMint
输出如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
Running 1 test for test/TimeManipulation.t.sol:TimeManipulationTest [PASS] testMint() (gas: 94666) Logs: Condition 1: block.timestamp % 170 != 0 block.timestamp: 169 alice balance before mint: 0 alice balance after mint: 0 Condition 2: block.timestamp % 170 == 0 block.timestamp: 17000 alice balance before mint: 0 alice balance after mint: 1
Test result: ok. 1 passed; 0 failed; finished in 7.64ms