Documentation
¶
Overview ¶
Package cover provides Go-compatible statement block counting for coverage analysis. It replicates the block-counting algorithm from Go's cmd/cover tool without source rewriting, allowing pure AST-based coverage analysis.
The algorithm is extracted from Go 1.26 cmd/cover/cover.go and adapted to only count blocks (no instrumentation). This produces statement counts identical to `go test -cover`.
To update for a new Go version, see boundary.go and visitor.go headers.
Package cover provides Go-compatible statement block counting for coverage analysis. It replicates the block-counting algorithm from Go's cmd/cover tool without source rewriting, allowing pure AST-based coverage analysis.
Package cover provides Go-compatible statement block counting for coverage analysis.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountBodyStatements ¶
CountBodyStatements returns the total coverable statements in a single function body. This is useful for per-function coverage analysis.
Params:
- fset: token file set for position information
- body: function body block statement
Returns:
- int: coverable statement count for this function body
func CountFileStatements ¶
CountFileStatements returns the total number of coverable statements in a file. This is the sum of NumStmt across all blocks, matching the denominator that `go test -cover` uses for percentage calculation.
Params:
- fset: token file set for position information
- file: parsed AST file
Returns:
- int: total coverable statement count
Types ¶
type Block ¶
type Block struct {
// StartPos is the start position of the block in the source.
StartPos token.Pos
// EndPos is the end position of the block in the source.
EndPos token.Pos
// NumStmt is the number of statements in this block.
// This matches the NumStmt field in Go's coverage profile format.
NumStmt int
}
Block represents a coverable code block with the same semantics as Go's coverage tool. Each block is a contiguous sequence of statements between control flow boundaries, and NumStmt is the number of top-level statements in that block.
func CountBlocks ¶
CountBlocks returns all coverable blocks in a Go source file. It walks every FuncDecl with a body and applies Go's block-counting algorithm. The returned blocks have the same boundaries and statement counts as those produced by `go tool cover -mode=set`.
Params:
- fset: token file set for position information
- file: parsed AST file
Returns:
- []Block: coverable blocks with positions and statement counts
func CountBodyBlocks ¶
CountBodyBlocks returns all coverable blocks in a single function body. Unlike CountBodyStatements which returns the total, this returns individual blocks for per-block coverage analysis (CFG construction, branch resolution).
Params:
- fset: token file set for position information
- body: function body block statement
Returns:
- []Block: coverable blocks with positions and statement counts