Repository navigation
Expand file tree
/
Copy pathanalyzer_test.go
More file actions
177 lines (150 loc) · 3.34 KB
/
Copy pathanalyzer_test.go
File metadata and controls
177 lines (150 loc) · 3.34 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 canonicalheader_test
import (
"go/ast"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/lasiar/canonicalheader"
)
const testValue = "hello_world"
func TestAnalyzer(t *testing.T) {
t.Parallel()
testCases := [...]struct {
pkg string
customAnalyzer func(t *testing.T) *analysis.Analyzer
}{
{pkg: "alias"},
{pkg: "assigned"},
{pkg: "common"},
{pkg: "const"},
{pkg: "embedded"},
{
pkg: "exclusions",
customAnalyzer: func(t *testing.T) *analysis.Analyzer {
t.Helper()
a := canonicalheader.New()
err := a.Flags.Set("exclusions", "exclusioN")
require.NoError(t, err)
return a
},
},
{pkg: "generic"},
{pkg: "global"},
{pkg: "initialism"},
{pkg: "responsewriter"},
{pkg: "struct"},
{pkg: "underlying"},
}
for _, tt := range testCases {
t.Run(tt.pkg, func(t *testing.T) {
t.Parallel()
var a *analysis.Analyzer
if tt.customAnalyzer != nil {
a = tt.customAnalyzer(t)
} else {
a = canonicalheader.New()
}
analysistest.RunWithSuggestedFixes(
t,
analysistest.TestData(),
a,
tt.pkg,
)
})
}
t.Run("are_test_cases_complete", func(t *testing.T) {
t.Parallel()
want, err := os.ReadDir(filepath.Join(analysistest.TestData(), "src"))
require.NoError(t, err)
require.Len(t, testCases, len(want))
got := [len(testCases)]string{}
for i, testCase := range testCases {
got[i] = testCase.pkg
}
require.EqualValues(
t,
transform(want, func(d os.DirEntry) string {
return d.Name()
}),
got,
)
})
}
func TestAnalyzerMissingAssignedSelector(t *testing.T) {
t.Parallel()
dir, cleanup, err := analysistest.WriteFiles(map[string]string{
"missingselector/missingselector.go": `package missingselector
import "net/http"
func _() {
h := http.Header{}
get := h.Get
get("X-HEADER")
h.Get("X-HEADER") // want "use \"X-Header\" instead of \"X-HEADER\""
}
`,
})
require.NoError(t, err)
t.Cleanup(cleanup)
a := canonicalheader.New()
run := a.Run
var missingSelectors int
a.Run = func(pass *analysis.Pass) (any, error) {
// A valid selector always has a name. Clear it after type checking to
// exercise the defensive check for an incomplete method assignment.
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
assign, ok := n.(*ast.AssignStmt)
if !ok {
return true
}
for _, rhs := range assign.Rhs {
if selector, ok := rhs.(*ast.SelectorExpr); ok {
selector.Sel = nil
missingSelectors++
}
}
return false
})
}
return run(pass)
}
analysistest.Run(t, dir, a, "missingselector")
require.Equal(t, 1, missingSelectors)
}
func transform[S ~[]E, E any, T any](sl S, f func(E) T) []T {
out := make([]T, len(sl))
for i, t := range sl {
out[i] = f(t)
}
return out
}
func BenchmarkCanonical(b *testing.B) {
v := http.Header{
"Canonical-Header": []string{testValue},
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
s := v.Get("Canonical-Header")
if s != testValue {
b.Fatal()
}
}
}
func BenchmarkNonCanonical(b *testing.B) {
v := http.Header{
"Canonical-Header": []string{testValue},
}
b.ReportAllocs()
b.ResetTimer()
for range b.N {
s := v.Get("CANONICAL-HEADER")
if s != testValue {
b.Fatal()
}
}
}