Skip to content

Commit 9ee6005

Browse files
authored
fix(coverage): assert should not be branch (#9467)
1 parent 9af381f commit 9ee6005

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

crates/evm/coverage/src/analysis.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,9 @@ impl<'a> ContractVisitor<'a> {
372372
let expr: Option<Node> = node.attribute("expression");
373373
if let Some(NodeType::Identifier) = expr.as_ref().map(|expr| &expr.node_type) {
374374
// Might be a require call, add branch coverage.
375+
// Asserts should not be considered branches: <https://github.com/foundry-rs/foundry/issues/9460>.
375376
let name: Option<String> = expr.and_then(|expr| expr.attribute("name"));
376-
if let Some("require" | "assert") = name.as_deref() {
377+
if let Some("require") = name.as_deref() {
377378
let branch_id = self.branch_id;
378379
self.branch_id += 1;
379380
self.push_item_kind(

crates/forge/tests/cli/coverage.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -368,39 +368,29 @@ contract AContractTest is DSTest {
368368
)
369369
.unwrap();
370370

371-
// Assert 50% branch coverage for assert failure.
371+
// Assert 50% statement coverage for assert failure (assert not considered a branch).
372372
cmd.arg("coverage").args(["--mt", "testAssertRevertBranch"]).assert_success().stdout_eq(str![
373373
[r#"
374374
...
375-
| File | % Lines | % Statements | % Branches | % Funcs |
376-
|-------------------|--------------|--------------|--------------|---------------|
377-
| src/AContract.sol | 66.67% (2/3) | 50.00% (1/2) | 50.00% (1/2) | 100.00% (1/1) |
378-
| Total | 66.67% (2/3) | 50.00% (1/2) | 50.00% (1/2) | 100.00% (1/1) |
375+
| File | % Lines | % Statements | % Branches | % Funcs |
376+
|-------------------|--------------|--------------|---------------|---------------|
377+
| src/AContract.sol | 66.67% (2/3) | 50.00% (1/2) | 100.00% (0/0) | 100.00% (1/1) |
378+
| Total | 66.67% (2/3) | 50.00% (1/2) | 100.00% (0/0) | 100.00% (1/1) |
379379
380380
"#]
381381
]);
382382

383-
// Assert 50% branch coverage for proper assert.
383+
// Assert 100% statement coverage for proper assert (assert not considered a branch).
384384
cmd.forge_fuse().arg("coverage").args(["--mt", "testAssertBranch"]).assert_success().stdout_eq(
385385
str![[r#"
386386
...
387-
| File | % Lines | % Statements | % Branches | % Funcs |
388-
|-------------------|---------------|---------------|--------------|---------------|
389-
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 50.00% (1/2) | 100.00% (1/1) |
390-
| Total | 100.00% (3/3) | 100.00% (2/2) | 50.00% (1/2) | 100.00% (1/1) |
391-
392-
"#]],
393-
);
394-
395-
// Assert 100% coverage (assert properly covered).
396-
cmd.forge_fuse().arg("coverage").assert_success().stdout_eq(str![[r#"
397-
...
398387
| File | % Lines | % Statements | % Branches | % Funcs |
399388
|-------------------|---------------|---------------|---------------|---------------|
400-
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 100.00% (2/2) | 100.00% (1/1) |
401-
| Total | 100.00% (3/3) | 100.00% (2/2) | 100.00% (2/2) | 100.00% (1/1) |
389+
| src/AContract.sol | 100.00% (3/3) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (1/1) |
390+
| Total | 100.00% (3/3) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (1/1) |
402391
403-
"#]]);
392+
"#]],
393+
);
404394
});
405395

406396
forgetest!(require, |prj, cmd| {
@@ -753,10 +743,10 @@ contract FooTest is DSTest {
753743
.assert_success()
754744
.stdout_eq(str![[r#"
755745
...
756-
| File | % Lines | % Statements | % Branches | % Funcs |
757-
|-------------|----------------|----------------|----------------|---------------|
758-
| src/Foo.sol | 91.67% (33/36) | 90.00% (27/30) | 87.50% (14/16) | 100.00% (9/9) |
759-
| Total | 91.67% (33/36) | 90.00% (27/30) | 87.50% (14/16) | 100.00% (9/9) |
746+
| File | % Lines | % Statements | % Branches | % Funcs |
747+
|-------------|----------------|----------------|---------------|---------------|
748+
| src/Foo.sol | 91.67% (33/36) | 90.00% (27/30) | 80.00% (8/10) | 100.00% (9/9) |
749+
| Total | 91.67% (33/36) | 90.00% (27/30) | 80.00% (8/10) | 100.00% (9/9) |
760750
761751
"#]]);
762752

@@ -767,10 +757,10 @@ contract FooTest is DSTest {
767757
.assert_success()
768758
.stdout_eq(str![[r#"
769759
...
770-
| File | % Lines | % Statements | % Branches | % Funcs |
771-
|-------------|----------------|----------------|----------------|---------------|
772-
| src/Foo.sol | 97.22% (35/36) | 96.67% (29/30) | 93.75% (15/16) | 100.00% (9/9) |
773-
| Total | 97.22% (35/36) | 96.67% (29/30) | 93.75% (15/16) | 100.00% (9/9) |
760+
| File | % Lines | % Statements | % Branches | % Funcs |
761+
|-------------|----------------|----------------|---------------|---------------|
762+
| src/Foo.sol | 97.22% (35/36) | 96.67% (29/30) | 90.00% (9/10) | 100.00% (9/9) |
763+
| Total | 97.22% (35/36) | 96.67% (29/30) | 90.00% (9/10) | 100.00% (9/9) |
774764
775765
"#]]);
776766

@@ -779,8 +769,8 @@ contract FooTest is DSTest {
779769
...
780770
| File | % Lines | % Statements | % Branches | % Funcs |
781771
|-------------|-----------------|-----------------|-----------------|---------------|
782-
| src/Foo.sol | 100.00% (36/36) | 100.00% (30/30) | 100.00% (16/16) | 100.00% (9/9) |
783-
| Total | 100.00% (36/36) | 100.00% (30/30) | 100.00% (16/16) | 100.00% (9/9) |
772+
| src/Foo.sol | 100.00% (36/36) | 100.00% (30/30) | 100.00% (10/10) | 100.00% (9/9) |
773+
| Total | 100.00% (36/36) | 100.00% (30/30) | 100.00% (10/10) | 100.00% (9/9) |
784774
785775
"#]]);
786776
});
@@ -949,8 +939,8 @@ contract FooTest is DSTest {
949939
...
950940
| File | % Lines | % Statements | % Branches | % Funcs |
951941
|-------------|----------------|----------------|--------------|---------------|
952-
| src/Foo.sol | 75.00% (15/20) | 66.67% (14/21) | 83.33% (5/6) | 100.00% (5/5) |
953-
| Total | 75.00% (15/20) | 66.67% (14/21) | 83.33% (5/6) | 100.00% (5/5) |
942+
| src/Foo.sol | 75.00% (15/20) | 66.67% (14/21) | 75.00% (3/4) | 100.00% (5/5) |
943+
| Total | 75.00% (15/20) | 66.67% (14/21) | 75.00% (3/4) | 100.00% (5/5) |
954944
955945
"#]]);
956946

@@ -959,8 +949,8 @@ contract FooTest is DSTest {
959949
...
960950
| File | % Lines | % Statements | % Branches | % Funcs |
961951
|-------------|-----------------|-----------------|---------------|---------------|
962-
| src/Foo.sol | 100.00% (20/20) | 100.00% (21/21) | 100.00% (6/6) | 100.00% (5/5) |
963-
| Total | 100.00% (20/20) | 100.00% (21/21) | 100.00% (6/6) | 100.00% (5/5) |
952+
| src/Foo.sol | 100.00% (20/20) | 100.00% (21/21) | 100.00% (4/4) | 100.00% (5/5) |
953+
| Total | 100.00% (20/20) | 100.00% (21/21) | 100.00% (4/4) | 100.00% (5/5) |
964954
965955
"#]]);
966956
});

0 commit comments

Comments
 (0)