diff --git a/test/unit/Greeter.t.sol b/test/unit/Greeter.t.sol index 82f59c91..2bf094ec 100644 --- a/test/unit/Greeter.t.sol +++ b/test/unit/Greeter.t.sol @@ -1,99 +1,141 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.23; -import {Greeter, IGreeter} from 'contracts/Greeter.sol'; -import {Test} from 'forge-std/Test.sol'; -import {IERC20} from 'forge-std/interfaces/IERC20.sol'; +import {Greeter, IGreeter} from "contracts/Greeter.sol"; +import {Test} from "forge-std/Test.sol"; +import {IERC20} from "forge-std/interfaces/IERC20.sol"; -contract UnitGreeter is Test { - address internal _owner = makeAddr('owner'); - IERC20 internal _token = IERC20(makeAddr('token')); - uint256 internal _initialBalance = 100; - string internal _initialGreeting = 'hola'; +contract Unit_Greeter is Test { + address internal _owner = makeAddr("owner"); + IERC20 internal _token = IERC20(makeAddr("token")); + uint256 internal _initialBalance = 100; + string internal _initialGreeting = "hola"; - Greeter internal _greeter; + Greeter internal _greeter; - event GreetingSet(string _greeting); + event GreetingSet(string _greeting); - function setUp() external { - vm.prank(_owner); - _greeter = new Greeter(_initialGreeting, _token); + function setUp() external { + vm.prank(_owner); + _greeter = new Greeter(_initialGreeting, _token); - vm.etch(address(_token), new bytes(0x1)); - } + vm.etch(address(_token), new bytes(0x1)); + } - function test_EmptyTestExample() external { - // it does nothing - vm.skip(true); - } + function test_EmptyTestExample() external { + // it does nothing + vm.skip(true); + } - function test_ConstructorWhenPassingValidGreetingString() external { - vm.prank(_owner); + function test_ConstructorWhenPassingValidGreetingString() external { + vm.prank(_owner); - // it deploys - _greeter = new Greeter(_initialGreeting, _token); + // it deploys + _greeter = new Greeter(_initialGreeting, _token); - // it sets the greeting string - assertEq(_greeter.greeting(), _initialGreeting); + // it sets the greeting string + assertEq(_greeter.greeting(), _initialGreeting); - // it sets the owner as sender - assertEq(_greeter.OWNER(), _owner); + // it sets the owner as sender + assertEq(_greeter.OWNER(), _owner); - // it sets the token used - assertEq(address(_greeter.token()), address(_token)); - } + // it sets the token used + assertEq(address(_greeter.token()), address(_token)); + } - function test_ConstructorWhenPassingAnEmptyGreetingString() external { - vm.prank(_owner); + function test_ConstructorWhenPassingAnEmptyGreetingString() external { + vm.prank(_owner); - // it reverts - vm.expectRevert(IGreeter.Greeter_InvalidGreeting.selector); - _greeter = new Greeter('', _token); - } + // it reverts + vm.expectRevert(IGreeter.Greeter_InvalidGreeting.selector); + _greeter = new Greeter('', _token); + } - function test_GreetWhenCalled() external { - vm.mockCall(address(_token), abi.encodeWithSelector(IERC20.balanceOf.selector), abi.encode(_initialBalance)); - vm.expectCall(address(_token), abi.encodeWithSelector(IERC20.balanceOf.selector)); - (string memory _greet, uint256 _balance) = _greeter.greet(); + function test_GreetWhenCalled() external { + vm.mockCall(address(_token), abi.encodeWithSelector(IERC20.balanceOf.selector), abi.encode(_initialBalance)); + vm.expectCall(address(_token), abi.encodeWithSelector(IERC20.balanceOf.selector)); + (string memory _greet, uint256 _balance) = _greeter.greet(); - // it returns the greeting string - assertEq(_greet, _initialGreeting); + // it returns the greeting string + assertEq(_greet, _initialGreeting); - // it returns the token balance of the contract - assertEq(_balance, _initialBalance); - } + // it returns the token balance of the contract + assertEq(_balance, _initialBalance); + } - modifier whenCalledByTheOwner() { - vm.startPrank(_owner); - _; - vm.stopPrank(); - } + modifier whenCalledByTheOwner() { + vm.startPrank(_owner); + _; + vm.stopPrank(); + } - function test_SetGreetingWhenPassingAValidGreetingString() external whenCalledByTheOwner { - string memory _newGreeting = 'hello'; + function test_SetGreetingWhenPassingAValidGreetingString() external whenCalledByTheOwner { + string memory _newGreeting = "hello"; - // it emit GreetingSet - vm.expectEmit(true, true, true, true, address(_greeter)); - emit GreetingSet(_newGreeting); + // it emit GreetingSet + vm.expectEmit(true, true, true, true, address(_greeter)); + emit GreetingSet(_newGreeting); - _greeter.setGreeting(_newGreeting); + _greeter.setGreeting(_newGreeting); - // it sets the greeting string - assertEq(_greeter.greeting(), _newGreeting); - } + // it sets the greeting string + assertEq(_greeter.greeting(), _newGreeting); + } - function test_SetGreetingWhenPassingAnEmptyGreetingString() external whenCalledByTheOwner { - // it reverts - vm.expectRevert(IGreeter.Greeter_InvalidGreeting.selector); - _greeter.setGreeting(''); - } + function test_SetGreetingWhenPassingAnEmptyGreetingString() external whenCalledByTheOwner { + // it reverts + vm.expectRevert(IGreeter.Greeter_InvalidGreeting.selector); + _greeter.setGreeting(""); + } - function test_SetGreetingWhenCalledByANon_owner(address _caller) external { - vm.assume(_caller != _owner); - vm.prank(_caller); + function test_SetGreetingWhenCalledByANon_owner(address _caller) external { + vm.assume(_caller != _owner); + vm.prank(_caller); - // it reverts - vm.expectRevert(IGreeter.Greeter_OnlyOwner.selector); - _greeter.setGreeting('new greeting'); - } + // it reverts + vm.expectRevert(IGreeter.Greeter_OnlyOwner.selector); + _greeter.setGreeting("new greeting"); + } + + function test_SomeMethodRevertWhen_X() external { + // it should revert + } + + modifier whenNotX() { + _; + } + + function test_SomeMethodRevertWhen_Y() external whenNotX { + // it should revert + } + + modifier whenNotY() { + _; + } + + function test_SomeMethodRevertWhen_Z() external whenNotX whenNotY { + // it should revert + } + + function test_SomeMethodWhenNotZ() external whenNotX whenNotY { + // it should do A + // it should do B + } + + function test_SomeOtherMethodRevertWhen_X() external { + // it should revert + } + + function test_SomeOtherMethodRevertWhen_Y() external { + // it should revert + } + + function test_SomeOtherMethodRevertWhen_Z() external { + // it should revert + } + + function test_SomeOtherMethodWhenNotXYZ() external { + // it should do A + // it should do B + } } diff --git a/test/unit/Greeter.tree b/test/unit/Greeter.tree index bbc74ea5..97f3f3d0 100644 --- a/test/unit/Greeter.tree +++ b/test/unit/Greeter.tree @@ -1,25 +1,24 @@ -Greeter::constructor -├── when passing valid greeting string -│ ├── it deploys -│ ├── it sets the greeting string -│ ├── it sets the owner as sender -│ └── it sets the token used -└── when passing an empty greeting string - └── it reverts - - -Greeter::greet -└── when called - ├── it returns the greeting string - └── it returns the token balance of the contract - - -Greeter::setGreeting -├── when called by the owner -│ ├── when passing a valid greeting string -│ │ ├── it sets the greeting string -│ │ └── it emit GreetingSet -│ └── when passing an empty greeting string -│ └── it reverts -└── when called by a non-owner - └── it reverts \ No newline at end of file +// A +Unit_Greeter::someMethod +├── when X +│ └── it should revert +└── when not X + ├── when Y + │ └── it should revert + └── when not Y + ├── when Z + │ └── it should revert + └── when not Z + ├── it should do A + └── it should do B +// B +Unit_Greeter::someOtherMethod +├── when X +│ └── it should revert +├── when Y +│ └── it should revert +├── when Z +│ └── it should revert +└── when not XYZ + ├── it should do A + └── it should do B \ No newline at end of file