-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,386 additions
and
1,827 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package coverage | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
) | ||
|
||
// detectExecutableLines analyzes the given source code content and returns a map | ||
// of line numbers to boolean values indicating whether each line is executable. | ||
func DetectExecutableLines(content string) (map[int]bool, error) { | ||
fset := token.NewFileSet() | ||
node, err := parser.ParseFile(fset, "", content, parser.ParseComments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
executableLines := make(map[int]bool) | ||
|
||
ast.Inspect(node, func(n ast.Node) bool { | ||
if n == nil { | ||
return true | ||
} | ||
|
||
if isExecutableLine(n) { | ||
line := fset.Position(n.Pos()).Line | ||
executableLines[line] = true | ||
} | ||
|
||
return true | ||
}) | ||
|
||
return executableLines, nil | ||
} | ||
|
||
// countCodeLines counts the number of executable lines in the given source code content. | ||
func CountCodeLines(content string) int { | ||
lines, err := DetectExecutableLines(content) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
return len(lines) | ||
} | ||
|
||
// isExecutableLine determines whether a given AST node represents an | ||
// executable line of code for the purpose of code coverage measurement. | ||
// | ||
// It returns true for statement nodes that typically contain executable code, | ||
// such as assignments, expressions, return statements, and control flow statements. | ||
// | ||
// It returns false for nodes that represent non-executable lines, such as | ||
// declarations, blocks, and function definitions. | ||
func isExecutableLine(node ast.Node) bool { | ||
switch n := node.(type) { | ||
case *ast.AssignStmt, *ast.ExprStmt, *ast.ReturnStmt, *ast.BranchStmt, | ||
*ast.IncDecStmt, *ast.GoStmt, *ast.DeferStmt, *ast.SendStmt: | ||
return true | ||
case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt, | ||
*ast.TypeSwitchStmt, *ast.SelectStmt: | ||
return true | ||
case *ast.CaseClause: | ||
// Even if a `case` condition (e.g., `case 1:`) in a `switch` statement is executed, | ||
// the condition itself is not included in the coverage; coverage only recorded for the | ||
// code block inside the corresponding `case` clause. | ||
return false | ||
case *ast.LabeledStmt: | ||
return isExecutableLine(n.Stmt) | ||
case *ast.FuncDecl: | ||
return false | ||
case *ast.BlockStmt: | ||
return false | ||
case *ast.DeclStmt: | ||
// check inner declarations in the DeclStmt (e.g. `var a, b = 1, 2`) | ||
// if there is a value initialization, then the line is executable | ||
genDecl, ok := n.Decl.(*ast.GenDecl) | ||
if ok && (genDecl.Tok == token.VAR || genDecl.Tok == token.CONST) { | ||
for _, spec := range genDecl.Specs { | ||
valueSpec, ok := spec.(*ast.ValueSpec) | ||
if ok && len(valueSpec.Values) > 0 { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
case *ast.ImportSpec, *ast.TypeSpec, *ast.ValueSpec: | ||
return false | ||
case *ast.InterfaceType: | ||
return false | ||
case *ast.GenDecl: | ||
switch n.Tok { | ||
case token.VAR, token.CONST: | ||
for _, spec := range n.Specs { | ||
valueSpec, ok := spec.(*ast.ValueSpec) | ||
if ok && len(valueSpec.Values) > 0 { | ||
return true | ||
} | ||
} | ||
return false | ||
case token.TYPE, token.IMPORT: | ||
return false | ||
default: | ||
return true | ||
} | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package coverage | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDetectExecutableLines(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
content string | ||
want map[int]bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Simple function", | ||
content: ` | ||
package main | ||
func main() { | ||
x := 5 | ||
if x > 3 { | ||
println("Greater") | ||
} | ||
}`, | ||
want: map[int]bool{ | ||
5: true, // x := 5 | ||
6: true, // if x > 3 | ||
7: true, // println("Greater") | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Function with loop", | ||
content: ` | ||
package main | ||
func loopFunction() { | ||
for i := 0; i < 5; i++ { | ||
if i%2 == 0 { | ||
continue | ||
} | ||
println(i) | ||
} | ||
}`, | ||
want: map[int]bool{ | ||
5: true, // for i := 0; i < 5; i++ | ||
6: true, // if i%2 == 0 | ||
7: true, // continue | ||
9: true, // println(i) | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Only declarations", | ||
content: ` | ||
package main | ||
import "fmt" | ||
var x int | ||
type MyStruct struct { | ||
field int | ||
}`, | ||
want: map[int]bool{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "Invalid gno code", | ||
content: ` | ||
This is not valid Go code | ||
It should result in an error`, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
got, err := DetectExecutableLines(tt.content) | ||
assert.Equal(t, tt.wantErr, err != nil) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.