Testing Time
This commit is contained in:
parent
e4de15d655
commit
8a51ae5cf5
|
@ -0,0 +1,15 @@
|
|||
pragma solidity 0.8.20;
|
||||
|
||||
contract Auction {
|
||||
uint256 public startAt = block.timestamp + 1 days;
|
||||
uint256 public endAt = block.timestamp + 2 days;
|
||||
|
||||
function bid() external {
|
||||
require(block.timestamp >= startAt && block.timestamp < endAt, "cannot bid");
|
||||
}
|
||||
|
||||
function end() external {
|
||||
require(block.timestamp >= endAt, "cannot end");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
pragma solidity ^0.8.20;
|
||||
|
||||
import "forge-std/Test.sol";
|
||||
import {Auction} from "../src/Time.sol";
|
||||
|
||||
contract TimeTest is Test {
|
||||
Auction public auction;
|
||||
uint256 private startAt;
|
||||
|
||||
// vm.warp - set block.timestamp to future timestamp
|
||||
// vm.roll - set block.number
|
||||
// skip - increment current timestamp
|
||||
// rewind - decrement current timestamp
|
||||
|
||||
function setUp() public {
|
||||
auction = new Auction();
|
||||
startAt = block.timestamp;
|
||||
}
|
||||
|
||||
function testBidFailsBeforeStartTime() public {
|
||||
vm.expectRevert(bytes("cannot bid"));
|
||||
auction.bid();
|
||||
}
|
||||
|
||||
function testBid() public {
|
||||
vm.warp(startAt + 1 days);
|
||||
auction.bid();
|
||||
}
|
||||
|
||||
function testBidFailsAfterEndTime() public {
|
||||
vm.expectRevert(bytes("cannot bid"));
|
||||
vm.warp(startAt + 2 days);
|
||||
auction.bid();
|
||||
}
|
||||
|
||||
function testAuctionEnds() public {
|
||||
vm.warp(startAt + 3 days);
|
||||
auction.end();
|
||||
}
|
||||
|
||||
function testTimestamp() public {
|
||||
uint t = block.timestamp;
|
||||
|
||||
// skip - increment current timestamp
|
||||
skip(100);
|
||||
assertEq(block.timestamp, t + 100);
|
||||
|
||||
// rewind - decrement current timestamp
|
||||
rewind(10);
|
||||
assertEq(block.timestamp, t + 100 - 10);
|
||||
}
|
||||
|
||||
function testBlockNumber() public {
|
||||
// vm.roll - set block.number
|
||||
uint b = block.number;
|
||||
vm.roll(999);
|
||||
assertEq(block.number, 999);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue