This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
270 lines (233 loc) · 6.19 KB
/
utils_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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"fmt"
"go/parser"
"go/token"
"strings"
"testing"
)
func TestCheckPatterns(t *testing.T) {
t.Run("no patterns", func(t *testing.T) {
err := checkPatterns([]string{})
if err != nil {
t.Errorf("Unexpected error (got '%s')", err)
}
})
t.Run("no invalid patterns", func(t *testing.T) {
err := checkPatterns([]string{"valid pattern"})
if err != nil {
t.Errorf("Unexpected error (got '%s')", err)
}
})
t.Run("some invalid patterns", func(t *testing.T) {
err := checkPatterns([]string{"invalid[pattern"})
if err == nil {
t.Error("Expected an error but got none")
}
})
}
func TestCheckRecursive(t *testing.T) {
t.Run("empty string", func(t *testing.T) {
originalPath := ""
adjustedPath, recursive := checkRecursive(originalPath)
if adjustedPath != originalPath {
t.Errorf("Expected adjusted path to equal original path (was '%s')", adjustedPath)
}
if recursive == true {
t.Error("Expected recursive to be false")
}
})
t.Run("short path", func(t *testing.T) {
originalPath := "./"
adjustedPath, recursive := checkRecursive(originalPath)
if adjustedPath != originalPath {
t.Errorf("Expected adjusted path to equal original path (was '%s')", adjustedPath)
}
if recursive == true {
t.Error("Expected recursive to be false")
}
})
t.Run("long path", func(t *testing.T) {
originalPath := "path/to/directory"
adjustedPath, recursive := checkRecursive(originalPath)
if adjustedPath != originalPath {
t.Errorf("Expected adjusted path to equal original path (was '%s')", adjustedPath)
}
if recursive == true {
t.Error("Expected recursive to be false")
}
})
t.Run("package list wildcard", func(t *testing.T) {
originalPath := "./..."
expectedPath := "./"
adjustedPath, recursive := checkRecursive(originalPath)
if adjustedPath != expectedPath {
t.Errorf("Expected adjusted path to '%s' (was '%s')", expectedPath, adjustedPath)
}
if recursive == false {
t.Error("Expected recursive to be true")
}
})
}
func TestConstructMessage(t *testing.T) {
src := `
package foo
func localFunction(a, b int) int {
return a + b
}
`
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, "", src, 0)
if err != nil {
t.Fatal("Test file could not be parsed")
}
if len(file.Decls) < 1 {
t.Fatal("Test file must contains at least one declaration")
}
funcName := "foobar"
funcParamCount := 3
funcDecl := &funcdecl{
name: funcName,
paramCount: funcParamCount,
pos: file.Decls[0].Pos(),
}
result := constructMessage(fileSet, funcDecl)
if !strings.Contains(result, funcName) {
t.Error("Expected message to contain the function name")
}
if !strings.Contains(result, fmt.Sprintf("%d parameters", funcParamCount)) {
t.Error("Expected message to contain the actual parameter count")
}
}
func TestIncludes(t *testing.T) {
t.Run("empty slice", func(t *testing.T) {
result := includes([]string{}, "foobar")
if result == true {
t.Error("Expected first result to be false")
}
result = includes([]string{}, "")
if result == true {
t.Error("Expected second result to be false")
}
})
t.Run("slice not containing element", func(t *testing.T) {
result := includes([]string{"foo"}, "bar")
if result == true {
t.Error("Expected first result to be false")
}
result = includes([]string{"foo", "bar"}, "baz")
if result == true {
t.Error("Expected second result to be false")
}
})
t.Run("slice containing element", func(t *testing.T) {
result := includes([]string{"foo"}, "foo")
if result == false {
t.Error("Expected first result to be true")
}
result = includes([]string{"foo", "bar"}, "foo")
if result == false {
t.Error("Expected second result to be true")
}
result = includes([]string{"foo", "bar"}, "bar")
if result == false {
t.Error("Expected second result to be true")
}
})
}
func TestMin(t *testing.T) {
t.Run("a < b", func(t *testing.T) {
a, b := 1, 2
if !(a < b) {
t.Fatal("For this test a must be less than b")
}
result := min(a, b)
if result != a {
t.Errorf("Unexpected result (got %d)", result)
}
})
t.Run("a > b", func(t *testing.T) {
a, b := 2, 1
if !(a > b) {
t.Fatal("For this test a must be greater than b")
}
result := min(a, b)
if result != b {
t.Errorf("Unexpected result (got %d)", result)
}
})
t.Run("a == b", func(t *testing.T) {
a, b := 2, 2
if !(a == b) {
t.Fatal("For this test a must be equal to b")
}
result := min(a, b)
if result != a {
t.Errorf("Unexpected result (got %d)", result)
}
})
t.Run("only one value", func(t *testing.T) {
v := 4
result := min(v)
if result != v {
t.Errorf("Unexpected result (got %d)", result)
}
})
t.Run("more than two values", func(t *testing.T) {
a, b, c := 1, 2, 3
if !(a < b) || !(a < c) {
t.Fatal("The value of a must be the lowest for this test")
}
result := min(a, b, c)
if result != a {
t.Errorf("Unexpected result (got %d)", result)
}
})
}
func TestPrintAll(t *testing.T) {
t.Run("no issues", func(t *testing.T) {
var callCount uint
p := mockPrinter{callCount: &callCount}
noIssues := []string{}
printAll(p, noIssues)
if callCount > 0 {
t.Errorf("Expected printer not to be called (called %d times)", callCount)
}
})
t.Run("one issue", func(t *testing.T) {
var calls [][]interface{}
var callCount uint
p := mockPrinter{
callCount: &callCount,
calls: &calls,
}
issues := []string{"Hello world!"}
printAll(p, issues)
if callCount != 1 {
t.Errorf("Expected printer to be called once (called %d times)", callCount)
}
actualCallValue := calls[0][0].(string)
if actualCallValue != issues[0] {
t.Errorf("Unexpected arguments (got '%s')", actualCallValue)
}
})
t.Run("multiple issue", func(t *testing.T) {
var calls [][]interface{}
var callCount uint
p := mockPrinter{
callCount: &callCount,
calls: &calls,
}
issues := []string{"foo", "bar", "baz"}
printAll(p, issues)
if callCount != 3 {
t.Errorf("Expected printer to be called thrice (called %d times)", callCount)
}
for i, expected := range issues {
actual := calls[i][0].(string)
if actual != expected {
t.Errorf("Unexpected arguments in call %d (got '%s')", i, actual)
}
}
})
}