Skip to content

Commit

Permalink
fix(coverage): assert should not be branch (#9467)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Dec 3, 2024
1 parent 9af381f commit 9ee6005
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
3 changes: 2 additions & 1 deletion crates/evm/coverage/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ impl<'a> ContractVisitor<'a> {
let expr: Option<Node> = node.attribute("expression");
if let Some(NodeType::Identifier) = expr.as_ref().map(|expr| &expr.node_type) {
// Might be a require call, add branch coverage.
// Asserts should not be considered branches: <https://github.com/foundry-rs/foundry/issues/9460>.
let name: Option<String> = expr.and_then(|expr| expr.attribute("name"));
if let Some("require" | "assert") = name.as_deref() {
if let Some("require") = name.as_deref() {
let branch_id = self.branch_id;
self.branch_id += 1;
self.push_item_kind(
Expand Down
58 changes: 24 additions & 34 deletions crates/forge/tests/cli/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,39 +368,29 @@ contract AContractTest is DSTest {
)
.unwrap();

// Assert 50% branch coverage for assert failure.
// Assert 50% statement coverage for assert failure (assert not considered a branch).
cmd.arg("coverage").args(["--mt", "testAssertRevertBranch"]).assert_success().stdout_eq(str![
[r#"
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------------|--------------|--------------|--------------|---------------|
| src/AContract.sol | 66.67% (2/3) | 50.00% (1/2) | 50.00% (1/2) | 100.00% (1/1) |
| Total | 66.67% (2/3) | 50.00% (1/2) | 50.00% (1/2) | 100.00% (1/1) |
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------------|--------------|--------------|---------------|---------------|
| src/AContract.sol | 66.67% (2/3) | 50.00% (1/2) | 100.00% (0/0) | 100.00% (1/1) |
| Total | 66.67% (2/3) | 50.00% (1/2) | 100.00% (0/0) | 100.00% (1/1) |
"#]
]);

// Assert 50% branch coverage for proper assert.
// Assert 100% statement coverage for proper assert (assert not considered a branch).
cmd.forge_fuse().arg("coverage").args(["--mt", "testAssertBranch"]).assert_success().stdout_eq(
str![[r#"
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------------|---------------|---------------|--------------|---------------|
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 50.00% (1/2) | 100.00% (1/1) |
| Total | 100.00% (3/3) | 100.00% (2/2) | 50.00% (1/2) | 100.00% (1/1) |
"#]],
);

// Assert 100% coverage (assert properly covered).
cmd.forge_fuse().arg("coverage").assert_success().stdout_eq(str![[r#"
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------------|---------------|---------------|---------------|---------------|
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 100.00% (2/2) | 100.00% (1/1) |
| Total | 100.00% (3/3) | 100.00% (2/2) | 100.00% (2/2) | 100.00% (1/1) |
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (1/1) |
| Total | 100.00% (3/3) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (1/1) |
"#]]);
"#]],
);
});

forgetest!(require, |prj, cmd| {
Expand Down Expand Up @@ -753,10 +743,10 @@ contract FooTest is DSTest {
.assert_success()
.stdout_eq(str![[r#"
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|----------------|----------------|----------------|---------------|
| src/Foo.sol | 91.67% (33/36) | 90.00% (27/30) | 87.50% (14/16) | 100.00% (9/9) |
| Total | 91.67% (33/36) | 90.00% (27/30) | 87.50% (14/16) | 100.00% (9/9) |
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|----------------|----------------|---------------|---------------|
| src/Foo.sol | 91.67% (33/36) | 90.00% (27/30) | 80.00% (8/10) | 100.00% (9/9) |
| Total | 91.67% (33/36) | 90.00% (27/30) | 80.00% (8/10) | 100.00% (9/9) |
"#]]);

Expand All @@ -767,10 +757,10 @@ contract FooTest is DSTest {
.assert_success()
.stdout_eq(str![[r#"
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|----------------|----------------|----------------|---------------|
| src/Foo.sol | 97.22% (35/36) | 96.67% (29/30) | 93.75% (15/16) | 100.00% (9/9) |
| Total | 97.22% (35/36) | 96.67% (29/30) | 93.75% (15/16) | 100.00% (9/9) |
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|----------------|----------------|---------------|---------------|
| src/Foo.sol | 97.22% (35/36) | 96.67% (29/30) | 90.00% (9/10) | 100.00% (9/9) |
| Total | 97.22% (35/36) | 96.67% (29/30) | 90.00% (9/10) | 100.00% (9/9) |
"#]]);

Expand All @@ -779,8 +769,8 @@ contract FooTest is DSTest {
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|-----------------|-----------------|-----------------|---------------|
| src/Foo.sol | 100.00% (36/36) | 100.00% (30/30) | 100.00% (16/16) | 100.00% (9/9) |
| Total | 100.00% (36/36) | 100.00% (30/30) | 100.00% (16/16) | 100.00% (9/9) |
| src/Foo.sol | 100.00% (36/36) | 100.00% (30/30) | 100.00% (10/10) | 100.00% (9/9) |
| Total | 100.00% (36/36) | 100.00% (30/30) | 100.00% (10/10) | 100.00% (9/9) |
"#]]);
});
Expand Down Expand Up @@ -949,8 +939,8 @@ contract FooTest is DSTest {
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|----------------|----------------|--------------|---------------|
| src/Foo.sol | 75.00% (15/20) | 66.67% (14/21) | 83.33% (5/6) | 100.00% (5/5) |
| Total | 75.00% (15/20) | 66.67% (14/21) | 83.33% (5/6) | 100.00% (5/5) |
| src/Foo.sol | 75.00% (15/20) | 66.67% (14/21) | 75.00% (3/4) | 100.00% (5/5) |
| Total | 75.00% (15/20) | 66.67% (14/21) | 75.00% (3/4) | 100.00% (5/5) |
"#]]);

Expand All @@ -959,8 +949,8 @@ contract FooTest is DSTest {
...
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------|-----------------|-----------------|---------------|---------------|
| src/Foo.sol | 100.00% (20/20) | 100.00% (21/21) | 100.00% (6/6) | 100.00% (5/5) |
| Total | 100.00% (20/20) | 100.00% (21/21) | 100.00% (6/6) | 100.00% (5/5) |
| src/Foo.sol | 100.00% (20/20) | 100.00% (21/21) | 100.00% (4/4) | 100.00% (5/5) |
| Total | 100.00% (20/20) | 100.00% (21/21) | 100.00% (4/4) | 100.00% (5/5) |
"#]]);
});
Expand Down

0 comments on commit 9ee6005

Please sign in to comment.