forked from rillig/gobco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrumenter_test.go
120 lines (111 loc) · 2.23 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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"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"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
base := "testdata/instrumenter/" + test.name
goBytes, err := os.ReadFile(base + ".go")
if err != nil {
panic(err)
}
src := string(goBytes)
gobcoBytes, err := os.ReadFile(base + ".gobco")
if err != nil {
panic(err)
}
expected := string(gobcoBytes)
fset := token.NewFileSet()
mode := parser.ParseComments
f, err := parser.ParseFile(fset, "test.go", src, mode)
if err != nil {
t.Fatal(err)
}
i := instrumenter{
false,
false,
false,
fset,
0,
map[ast.Node]bool{},
map[ast.Expr]*exprSubst{},
map[ast.Stmt]*ast.Stmt{},
map[ast.Stmt]ast.Stmt{},
false,
nil,
}
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, "test.go")
sb.WriteString(fmt.Sprintf("// %s: %q\n",
location, cond.text))
}
actual := sb.String()
if actual != expected {
err := os.WriteFile(base+".gobco", []byte(actual), 0o666)
if err != nil {
t.Fatal(err)
}
t.Errorf("expected:\n%s\nactual:\n%s\n", expected, actual)
}
})
}
}