From a5c434b4604cdd21b5cb5f16544200d7a48be6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wei=C3=9Fer=20Hase?= Date: Mon, 22 Jul 2024 10:52:41 +0200 Subject: [PATCH] feat: adding more checks to bToken tests --- test/unit/BToken.t.sol | 29 +++++++++++++++++++++++++++++ test/unit/BToken.tree | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/test/unit/BToken.t.sol b/test/unit/BToken.t.sol index 68810bd8..1207a294 100644 --- a/test/unit/BToken.t.sol +++ b/test/unit/BToken.t.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.25; +import {IERC20} from '@openzeppelin/contracts/interfaces/IERC20.sol'; import {IERC20Errors} from '@openzeppelin/contracts/interfaces/draft-IERC6093.sol'; import {Test} from 'forge-std/Test.sol'; import {MockBToken} from 'test/smock/MockBToken.sol'; @@ -44,6 +45,10 @@ contract BToken is Test { } function test_IncreaseApprovalWhenCalled() external { + // it emits Approval event + vm.expectEmit(); + emit IERC20.Approval(caller, spender, 200e18); + bToken.increaseApproval(spender, 100e18); // it increases spender approval assertEq(bToken.allowance(caller, spender), 200e18); @@ -69,13 +74,27 @@ contract BToken is Test { } function test_DecreaseApprovalWhenCalled() external { + // it emits Approval event + vm.expectEmit(); + emit IERC20.Approval(caller, spender, 50e18); + bToken.decreaseApproval(spender, 50e18); // it decreases spender approval assertEq(bToken.allowance(caller, spender), 50e18); } + function test__pushRevertWhen_ContractDoesNotHaveEnoughBalance() external { + // it should revert + vm.expectRevert(abi.encodeWithSelector(IERC20Errors.ERC20InsufficientBalance.selector, address(bToken), 0, 50e18)); + bToken.call__push(target, 50e18); + } + function test__pushWhenCalled() external { deal(address(bToken), address(bToken), initialBalance); + // it emits Transfer event + vm.expectEmit(); + emit IERC20.Transfer(address(bToken), target, 50e18); + bToken.call__push(target, 50e18); // it transfers tokens to recipient @@ -83,8 +102,18 @@ contract BToken is Test { assertEq(bToken.balanceOf(target), 50e18); } + function test__pullRevertWhen_TargetDoesNotHaveEnoughBalance() external { + // it should revert + vm.expectRevert(abi.encodeWithSelector(IERC20Errors.ERC20InsufficientBalance.selector, target, 0, 50e18)); + bToken.call__pull(target, 50e18); + } + function test__pullWhenCalled() external { deal(address(bToken), address(target), initialBalance); + // it emits Transfer event + vm.expectEmit(); + emit IERC20.Transfer(target, address(bToken), 50e18); + bToken.call__pull(target, 50e18); // it transfers tokens from sender diff --git a/test/unit/BToken.tree b/test/unit/BToken.tree index 0682a9c7..35f85bec 100644 --- a/test/unit/BToken.tree +++ b/test/unit/BToken.tree @@ -9,6 +9,7 @@ BToken::increaseApproval ├── when spender is address zero │ └── it should revert └── when called + ├── it emits Approval event └── it increases spender approval BToken::decreaseApproval @@ -19,12 +20,19 @@ BToken::decreaseApproval ├── when decrement is bigger than current approval │ └── it decreases spender approval to 0 └── when called + ├── it emits Approval event └── it decreases spender approval BToken::_push +├── when contract does not have enough balance +│ └── it should revert └── when called + ├── it emits Transfer event └── it transfers tokens to recipient BToken::_pull +├── when target does not have enough balance +│ └── it should revert └── when called + ├── it emits Transfer event └── it transfers tokens from sender