-
Notifications
You must be signed in to change notification settings - Fork 13
/
instrumenter_test.go
138 lines (129 loc) · 2.67 KB
/
instrumenter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"go/types"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
// Test_instrumenter ensures that a piece of code is properly instrumented by
// sprinkling calls to GobcoCover around each interesting expression.
func Test_instrumenter(t *testing.T) {
tests := []struct {
name string
}{
{"AssignStmt"},
{"BinaryExpr"},
{"BlockStmt"},
{"CallExpr"},
{"CaseClause"},
{"CommClause"},
{"Comment"},
{"CompositeLit"},
{"DeclStmt"},
{"DeferStmt"},
{"Ellipsis"},
{"ExprStmt"},
{"ForStmt"},
{"FuncDecl"},
{"FuncLit"},
{"GenDecl"},
{"GoStmt"},
{"IfStmt"},
{"IncDecStmt"},
{"IndexExpr"},
{"KeyValueExpr"},
{"LabeledStmt"},
{"ListExpr"},
{"ParenExpr"},
{"RangeStmt"},
{"ReturnStmt"},
{"SelectorExpr"},
{"SelectStmt"},
{"SendStmt"},
{"SliceExpr"},
{"StarExpr"},
{"SwitchStmt"},
{"TypeAssertExpr"},
{"TypeSwitchStmt"},
{"UnaryExpr"},
{"ValueSpec"},
}
testInstrumenter := func(name string, branch bool, ext string) {
dir := "testdata/instrumenter"
base := dir + "/" + name
expectedBytes, err := os.ReadFile(base + ext)
if err != nil {
expectedBytes = nil
}
expected := string(expectedBytes)
fset := token.NewFileSet()
mode := parser.ParseComments
relevant := func(info fs.FileInfo) bool {
n := info.Name()
return strings.HasPrefix(n, name) ||
strings.HasPrefix(n, "zzz")
}
pkgs, err := parser.ParseDir(fset, dir, relevant, mode)
if err != nil {
t.Fatal(err)
}
i := instrumenter{
branch,
false,
false,
false,
false,
fset,
map[*ast.Package]*types.Package{},
map[ast.Expr]types.Type{},
nil,
0,
map[ast.Expr]bool{},
map[ast.Expr]*exprSubst{},
map[ast.Stmt]*ast.Stmt{},
map[ast.Stmt]ast.Stmt{},
false,
nil,
}
fileName := filepath.Clean(base + ".go")
f := pkgs["instrumenter"].Files[fileName]
assert(f != nil, fileName)
i.resolveTypes(pkgs)
i.typePkg = i.pkg[pkgs["instrumenter"]]
i.instrumentFileNode(f)
var sb strings.Builder
err = printer.Fprint(&sb, fset, f)
if err != nil {
t.Fatal(err)
}
if len(i.conds) > 0 {
sb.WriteString("\n")
}
for _, cond := range i.conds {
location := strings.TrimPrefix(cond.pos, fileName)
sb.WriteString(fmt.Sprintf("// %s: %q\n",
location, cond.text))
}
actual := sb.String()
if actual != expected {
err := os.WriteFile(base+ext, []byte(actual), 0o666)
if err != nil {
t.Fatal(err)
}
t.Errorf("updated %s", base+ext)
}
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
testInstrumenter(test.name, true, ".branch")
testInstrumenter(test.name, false, ".cond")
})
}
}