Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch coverage not returning correct value for branch coverage #6609

Closed
2 tasks done
jatZama opened this issue Dec 16, 2023 · 1 comment
Closed
2 tasks done

Branch coverage not returning correct value for branch coverage #6609

jatZama opened this issue Dec 16, 2023 · 1 comment
Labels
T-bug Type: bug

Comments

@jatZama
Copy link

jatZama commented Dec 16, 2023

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (3d6bfdd 2023-12-16T00:20:53.761034000Z)

What command(s) is the bug in?

forge coverage

Operating System

macOS (Apple Silicon)

Describe the bug

I tested this contract :

// SPDX-License-Identifier: BSD-3-Clause-Clear

pragma solidity 0.8.22;

contract Branch {

    function branch(bool lhs, bool rhs) external returns (bool){
        bool result;
        if (lhs || rhs){
            result = true;
        }
        return result;
    }
}

With this simple test:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.22;

import {Test, console2} from "forge-std/Test.sol";
import {Branch} from "../src/Branch.sol";

contract BranchTest is Test {
    Branch public branch;

    function setUp() public {
        branch = new Branch();
    }

    function test_branch() public {
        branch.branch(true,false);
    }
}

forge coverage (latest forge version, with default params) returns 100% coverage for % Branches, which is obviously incorrect. On the other hand, the saùe test returns 50% coverage with hardhat coverage which seems more correct.

@zerosnacks
Copy link
Member

zerosnacks commented Jul 5, 2024

Related: #4314

I think there is a logical error in your test setup as you run the test once and there is no branching occuring as lhs evaluates, skipping over rhs.

if (lhs || rhs){
    result = true;
}

Rewriting the test as

contract Branch {
   function branch(bool foo) external returns (bool){
       bool result;
       if (foo) {
           result = true;
       } else {
           result = false;
       }
       return result;
   }
}
contract BranchTest is Test {
   Branch public branch;

   function setUp() public {
       branch = new Branch();
   }

   function test_branch() public {
       branch.branch(true);
   }
}

Correctly yields:

File % Lines % Statements % Branches % Funcs
src/Branch.sol 80.00% (4/5) 75.00% (3/4) 50.00% (1/2) 100.00% (1/1)
Total 80.00% (4/5) 75.00% (3/4) 50.00% (1/2) 100.00% (1/1)

Screenshot from 2024-07-05 15-38-24

Marking as resolved, #4314 is still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Status: Completed
Development

No branches or pull requests

2 participants