Foundry test: How to Write Basic Tests
This commit is contained in:
parent
3208f88328
commit
fe866cb658
|
@ -1,14 +1,22 @@
|
|||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.13;
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
contract Counter {
|
||||
uint256 public number;
|
||||
uint public count;
|
||||
|
||||
function setNumber(uint256 newNumber) public {
|
||||
number = newNumber;
|
||||
// Funcion to get the current count
|
||||
function get() public view returns (uint) {
|
||||
return count;
|
||||
}
|
||||
|
||||
function increment() public {
|
||||
number++;
|
||||
// Function to increment count by 1
|
||||
function inc() public {
|
||||
count += 1;
|
||||
}
|
||||
|
||||
// Function to decrement count by 1
|
||||
function dec() public {
|
||||
// thid function will fail if count = 0
|
||||
count -= 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.13;
|
||||
|
||||
import {Test, console2} from "forge-std/Test.sol";
|
||||
import {Test, stdError} from "forge-std/Test.sol";
|
||||
import {Counter} from "../src/Counter.sol";
|
||||
|
||||
contract CounterTest is Test {
|
||||
|
@ -9,16 +9,26 @@ contract CounterTest is Test {
|
|||
|
||||
function setUp() public {
|
||||
counter = new Counter();
|
||||
counter.setNumber(0);
|
||||
}
|
||||
|
||||
function testIncrement() public {
|
||||
counter.increment();
|
||||
assertEq(counter.number(), 1);
|
||||
function testInc() public {
|
||||
counter.inc();
|
||||
assertEq(counter.count(), 1);
|
||||
}
|
||||
|
||||
function testSetNumber(uint256 x) public {
|
||||
counter.setNumber(x);
|
||||
assertEq(counter.number(), x);
|
||||
function testFailDec() public {
|
||||
counter.dec();
|
||||
}
|
||||
|
||||
function testDecUnderFlow() public {
|
||||
vm.expectRevert(stdError.arithmeticError);
|
||||
counter.dec();
|
||||
}
|
||||
|
||||
function testDec() public {
|
||||
counter.inc();
|
||||
counter.inc();
|
||||
counter.dec();
|
||||
assertEq(counter.count(), 1);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue