-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_test.go
More file actions
177 lines (165 loc) · 4.03 KB
/
load_test.go
File metadata and controls
177 lines (165 loc) · 4.03 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package congo
import (
"fmt"
"testing"
)
func strSetEqual(xs, ys []string) bool {
if len(xs) != len(ys) {
return false
}
m := make(map[string]uint)
for _, x := range xs {
m[x] = m[x] + 1
}
for _, y := range ys {
c, ok := m[y]
if !ok {
return false
}
if c == 1 {
delete(m, y)
} else {
m[y] = c - 1
}
}
return len(m) == 0
}
func TestLoadTargetPackage(t *testing.T) {
packagePaths := []string{
"github.com/ajalab/congo/testdata",
"testdata/pointer.go",
}
for i, packagePath := range packagePaths {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
_, err := loadTargetPackage(packagePath)
if err != nil {
t.Fatal(err)
}
})
}
}
func TestLoadTargetFuncs(t *testing.T) {
zeroExecuteOption := &ExecuteOption{}
myExecuteOption := &ExecuteOption{MaxExec: 100}
fooExecuteOption := &ExecuteOption{MaxExec: 10, MinCoverage: 0.75}
barExecuteOption := &ExecuteOption{MaxExec: 50, MinCoverage: defaultExecuteOption.MinCoverage}
tcs := []struct {
packagePath string
funcNames []string
argEO *ExecuteOption
ans map[string]*ExecuteOption
}{
{
"testdata/load/foo.go",
nil,
zeroExecuteOption,
map[string]*ExecuteOption{"AnnotatedFoo": fooExecuteOption},
},
{
"testdata/load/foo.go",
nil,
myExecuteOption,
map[string]*ExecuteOption{
"AnnotatedFoo": {
MaxExec: myExecuteOption.MaxExec, MinCoverage: fooExecuteOption.MinCoverage,
},
},
},
{
"testdata/load/foo.go",
[]string{"AnnotatedFoo", "NonAnnotatedFoo"},
zeroExecuteOption,
map[string]*ExecuteOption{
"AnnotatedFoo": fooExecuteOption,
"NonAnnotatedFoo": defaultExecuteOption,
},
},
{
"testdata/load/foo.go",
[]string{"AnnotatedFoo", "NonAnnotatedFoo"},
myExecuteOption,
map[string]*ExecuteOption{
"AnnotatedFoo": {
MaxExec: myExecuteOption.MaxExec, MinCoverage: fooExecuteOption.MinCoverage,
},
"NonAnnotatedFoo": {
MaxExec: myExecuteOption.MaxExec, MinCoverage: defaultExecuteOption.MinCoverage,
},
},
},
{
"github.com/ajalab/congo/testdata/load",
nil,
zeroExecuteOption,
map[string]*ExecuteOption{
"AnnotatedFoo": fooExecuteOption,
"AnnotatedBar": barExecuteOption,
},
},
{
"github.com/ajalab/congo/testdata/load",
[]string{"AnnotatedBar"},
zeroExecuteOption,
map[string]*ExecuteOption{
"AnnotatedBar": barExecuteOption,
},
},
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
targetPackage, err := loadTargetPackage(tc.packagePath)
if err != nil {
t.Fatalf("%s: %v", tc.packagePath, err)
}
targets, err := loadTargetFuncs(
tc.packagePath,
targetPackage,
tc.funcNames,
tc.argEO,
)
if err != nil {
t.Fatalf("%s: %v", tc.packagePath, err)
}
if len(targets) != len(tc.ans) {
t.Fatalf("expected: %v, actual: %v", tc.ans, targets)
}
for k, a := range targets {
e, ok := tc.ans[k]
if !ok {
t.Fatalf("function \"%s\" is an unexpected target", k)
}
if a.MaxExec != e.MaxExec || a.MinCoverage != e.MinCoverage {
t.Errorf("execute options are wrong for function %s: expected %+v, actual %+v", k, e, a.ExecuteOption)
}
}
})
}
}
func TestParseAnnotationDirective(t *testing.T) {
tcs := []struct {
s string
key string
value string
parsed bool
}{
{"congo:skip", "skip", "", true},
{"congo :skip", "skip", "", false},
{"congo: skip", "skip", "", false},
{"congo:skipkip", "skip", "", false},
{"congo:hoge x", "hoge", "x", true},
{"congo:hoge x y", "hoge", "x y", true},
{"congo:hoge x", "hoge", "x", true},
{"congo:hogehoge x", "hoge", "", false},
}
for _, tc := range tcs {
t.Run(fmt.Sprintf("parse \"%s\" from \"%s\"", tc.key, tc.s), func(t *testing.T) {
value, parsed := parseAnnotationDirective(tc.s, tc.key)
if parsed != tc.parsed {
t.Errorf("assertion failed on parsed: expected %t, actual %t", tc.parsed, parsed)
}
if value != tc.value {
t.Errorf("assertion failed on value: expected \"%s\", actual \"%s\"", tc.value, value)
}
})
}
}