-
Notifications
You must be signed in to change notification settings - Fork 11
/
exhaustive_test.go
94 lines (78 loc) · 2.81 KB
/
exhaustive_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
package exhaustive
import (
"regexp"
"testing"
"golang.org/x/tools/go/analysis/analysistest"
)
func TestExhaustive(t *testing.T) {
runTest := func(t *testing.T, pattern string, setup ...func()) {
t.Helper()
t.Run(pattern, func(t *testing.T) {
resetFlags()
// default to checking switch and map for test.
fCheck = stringsFlag{
[]string{
string(elementSwitch),
string(elementMap),
},
nil,
}
for _, f := range setup {
f()
}
analysistest.Run(t, analysistest.TestData(), Analyzer, pattern)
})
}
if !testing.Short() {
// Analysis of code that uses complex packages, such as package os and
// package reflect, should not fail.
runTest(t, "complexpkg/...")
}
// Enum discovery, enum types.
runTest(t, "enum/...")
// Tests for the -check-generated flag.
runTest(t, "generated-file/check-generated-off/...")
runTest(t, "generated-file/check-generated-on/...", func() { fCheckGenerated = true })
// Tests for the -default-signifies-exhaustive flag.
// (For tests with this flag off, see other testdata packages
// such as "general/...".)
runTest(t, "default-signifies-exhaustive/default-absent/...", func() { fDefaultSignifiesExhaustive = true })
runTest(t, "default-signifies-exhaustive/default-present/...", func() { fDefaultSignifiesExhaustive = true })
// These tests exercise the default-case-required flag and its escape comment
runTest(t, "default-case-required/default-required/...", func() { fDefaultCaseRequired = true })
runTest(t, "default-case-required/default-not-required/...", func() { fDefaultCaseRequired = false })
// Tests for -ignore-enum-members and -ignore-enum-types flags.
runTest(t, "ignore-pattern/...", func() {
fIgnoreEnumMembers = regexpFlag{
regexp.MustCompile(`_UNSPECIFIED$|^general/y\.Echinodermata$|^ignore-pattern\.User$`),
}
fIgnoreEnumTypes = regexpFlag{
regexp.MustCompile(`label|^reflect\.Kind$|^time\.Duration$`),
}
})
// Tests for -package-scope-only flag.
runTest(t, "scope/allscope/...")
runTest(t, "scope/pkgscope/...", func() { fPackageScopeOnly = true })
// Program elements with ignore comment should not be
// checked during implicitly exhaustive mode.
runTest(t, "ignore-comment/...")
// Program elements without enforce comment should not be
// checked in explicitly exhaustive mode.
runTest(t, "enforce-comment/...", func() {
fExplicitExhaustiveSwitch = true
fExplicitExhaustiveMap = true
})
// To satisfy exhaustiveness, it is sufficient for each unique constant
// value of the members to be listed, not each member by name.
runTest(t, "duplicate-enum-value/...")
runTest(t, "typealias/...")
runTest(t, "typeparam/...")
// mixture of general tests.
runTest(t, "general/...")
}
func assertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatalf("got %s, want nil error", err)
}
}