You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have identified an anomalous behavior in the testing process when it comes to testing libraries, and I would like to provide a more detailed explanation using the given code example:
pragma solidity ^0.8.19;
import "forge-std/Test.sol";
library Lib {
struct Item {
uint A;
uint B;
uint C;
}
function _isValid(Item memory item) internal view {
console.log("_isValid IN");
require(item.A > 10, "A need to be higher than 10");
console.log("A ok");
require(item.B > 100, "B need to be higher than 100");
console.log("B ok");
require(item.C > 1000, "A need to be higher than 1000");
console.log("C ok");
console.log("_isValid OUT");
}
}
contract Contract {
function _isValid(Lib.Item memory item) public view {
console.log("_isValid IN");
require(item.A > 10, "A need to be higher than 10");
console.log("A ok");
require(item.B > 100, "B need to be higher than 100");
console.log("B ok");
require(item.C > 1000, "A need to be higher than 1000");
console.log("C ok");
console.log("_isValid OUT");
}
}
contract testLib is Test {
using Lib for Lib.Item;
function testIsValidLib() public {
Lib.Item memory item = Lib.Item({A: 0, B: 0, C: 0});
vm.expectRevert("A need to be higher than 10");
item._isValid();
// This is NOT executed
console.log("This is NOT shown");
item.A = 11;
vm.expectRevert("B need to be higher than 100");
item._isValid();
}
function testIsValidContract() public {
Contract c = new Contract();
Lib.Item memory item = Lib.Item({A: 0, B: 0, C: 0});
vm.expectRevert("A need to be higher than 10");
c._isValid(item);
console.log("This is shown");
item.A = 11;
vm.expectRevert("B need to be higher than 100");
c._isValid(item);
}
}
The library, named Lib, defines a struct called Item, along with an internal function _isValid that performs specific validations on an Item object. The contract, named Contract, also includes a function _isValid that performs the same validations as the library's _isValid function.
In the testing process, there is a discrepancy between the behavior of the testIsValidContract function and the testIsValidLib function when encountering a revert caused by the _isValid call. Specifically.
The issue is that the testIsValidLib function does not proceed with its execution beyond the point of the revert, despite the presence of the vm.expectRevert() statement in the code, while testIsValidContract is successfully executed until the end.
I think this inconsistency is unexpected considering the usage of vm.expectRevert() in both scenarios.
This is the outcome that illustrates this behaviour:
❯ forge test -vvv
[⠢] Compiling...
[⠘] Compiling 1 files with 0.8.19
[⠊] Solc 0.8.19 finished in 490.66ms
Compiler run successful!
Running 2 tests for test/Bug.t.sol:testLib
[PASS] testIsValidContract() (gas: 217389)
Logs:
_isValid IN
This is shown
_isValid IN
A ok
[PASS] testIsValidLib() (gas: 6631)
Logs:
_isValid IN
Test result: ok. 2 passed; 0 failed; finished in 311.58µs
The text was updated successfully, but these errors were encountered:
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.2.0 (db96393 2023-05-11T00:10:51.773583000Z)
What command(s) is the bug in?
forge test -vvv
Operating System
macOS (Apple Silicon)
Describe the bug
I have identified an anomalous behavior in the testing process when it comes to testing libraries, and I would like to provide a more detailed explanation using the given code example:
The library, named
Lib
, defines a struct called Item, along with an internal function_isValid
that performs specific validations on an Item object. The contract, namedContract
, also includes a function _isValid that performs the same validations as the library's_isValid
function.In the testing process, there is a discrepancy between the behavior of the
testIsValidContract
function and thetestIsValidLib
function when encountering a revert caused by the_isValid
call. Specifically.The issue is that the
testIsValidLib
function does not proceed with its execution beyond the point of the revert, despite the presence of the vm.expectRevert() statement in the code, whiletestIsValidContract
is successfully executed until the end.I think this inconsistency is unexpected considering the usage of vm.expectRevert() in both scenarios.
This is the outcome that illustrates this behaviour:
The text was updated successfully, but these errors were encountered: