From 8a51ae5cf585cf614c039f0974db0877af2e8ac6 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Sun, 24 Sep 2023 22:08:50 -0600 Subject: [PATCH] Testing Time --- src/Time.sol | 15 +++++++++++++ test/Time.t.sol | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/Time.sol create mode 100644 test/Time.t.sol diff --git a/src/Time.sol b/src/Time.sol new file mode 100644 index 0000000..858d43b --- /dev/null +++ b/src/Time.sol @@ -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"); + } +} + diff --git a/test/Time.t.sol b/test/Time.t.sol new file mode 100644 index 0000000..25d5aa1 --- /dev/null +++ b/test/Time.t.sol @@ -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); + } + +}