diff --git a/src/Counter.sol b/src/Counter.sol index aded799..f1c987a 100644 --- a/src/Counter.sol +++ b/src/Counter.sol @@ -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; } } diff --git a/test/Counter.t.sol b/test/Counter.t.sol index c0dfa7d..3f25b99 100644 --- a/test/Counter.t.sol +++ b/test/Counter.t.sol @@ -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); } }