cover

package
v0.130.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 3 Imported by: 0

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

func CountBodyStatements(fset *token.FileSet, body *ast.BlockStmt) (count int)

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

func CountFileStatements(fset *token.FileSet, file *ast.File) (count int)

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

func CountBlocks(fset *token.FileSet, file *ast.File) (blocks []Block)

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

func CountBodyBlocks(fset *token.FileSet, body *ast.BlockStmt) (blocks []Block)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL