Testing Time

This commit is contained in:
David E. Perez Negron R. 2023-09-24 22:08:50 -06:00
parent e4de15d655
commit 8a51ae5cf5
2 changed files with 75 additions and 0 deletions

15
src/Time.sol Normal file
View File

@ -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");
}
}

60
test/Time.t.sol Normal file
View File

@ -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);
}
}