And the following tests (hidden by default for brevity reasons, click the toggle below to collapse them):
Click me to toggle the tests
```solidity
contract FooTest is Test {
Foo internal foo = new Foo();
uint256[] internal arr;
function setUp() public virtual {
arr.push(1);
}
function test_RevertWhen_Gt() external {
uint256 number = 2;
vm.expectRevert(abi.encodeWithSelector(Foo.Gte1.selector, number, arr[0]));
foo.checkLt(number, arr);
}
function test_RevertWhen_Eq() external {
uint256 number = 1;
vm.expectRevert(abi.encodeWithSelector(Foo.Gte1.selector, number, arr[0]));
foo.checkLt(number, arr);
}
function test_Lt() external {
uint256 number = 0;
bool result = foo.checkLt(number, arr);
assertTrue(result);
}
}
```
Now, run forge coverage. You will get this report:
Notice that the branch coverage is 50% instead of 100%, even if we have tests that account for the full gamut of possibilities (>, ==, and <).
Now, here's the strangest thing - just by getting rid of the the firstElement argument of the custom error, branch coverage increases to 100%. Also, passing the number instead of arr[0] achieves the same 100% coverage, like this:
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.2.0 (249538f 2023-02-08T00:12:05.805004Z)
What command(s) is the bug in?
forge coverage
Operating System
macOS (Apple Silicon)
Describe the bug
This is a strange bug. Reproducing it in a repo with a clean slate took me hours, but I did eventually manage to get to the bottom of it.
Take the following unsuspecting contract:
And the following tests (hidden by default for brevity reasons, click the toggle below to collapse them):
Click me to toggle the tests
```solidity contract FooTest is Test { Foo internal foo = new Foo(); uint256[] internal arr; function setUp() public virtual { arr.push(1); } function test_RevertWhen_Gt() external { uint256 number = 2; vm.expectRevert(abi.encodeWithSelector(Foo.Gte1.selector, number, arr[0])); foo.checkLt(number, arr); } function test_RevertWhen_Eq() external { uint256 number = 1; vm.expectRevert(abi.encodeWithSelector(Foo.Gte1.selector, number, arr[0])); foo.checkLt(number, arr); } function test_Lt() external { uint256 number = 0; bool result = foo.checkLt(number, arr); assertTrue(result); } } ```Now, run
forge coverage
. You will get this report:Notice that the branch coverage is 50% instead of 100%, even if we have tests that account for the full gamut of possibilities (
>
,==
, and<
).Now, here's the strangest thing - just by getting rid of the the
firstElement
argument of the custom error, branch coverage increases to 100%. Also, passing thenumber
instead ofarr[0]
achieves the same 100% coverage, like this: