@@ -179,20 +179,63 @@ func ExampleFoo() {
179
179
}` , "use of `fmt2.Printf` forbidden by pattern `^fmt\\ .Printf` at testing.go:7:2" )
180
180
})
181
181
182
+ t .Run ("it ignores function names but checks return type" , func (t * testing.T ) {
183
+ linter , _ := NewLinter ([]string {`Foo` }, OptionAnalyzeTypes (true ))
184
+ expectIssues (t , linter , true , `
185
+ package bar
186
+
187
+ func Foo() {}
188
+ func Bad() Foo {}
189
+ }` , "use of `Foo` forbidden by pattern `Foo` at testing.go:5:12" )
190
+ })
191
+
192
+ t .Run ("it ignores type names but checks type" , func (t * testing.T ) {
193
+ linter , _ := NewLinter ([]string {`Foo` }, OptionAnalyzeTypes (true ))
194
+ expectIssues (t , linter , true , `
195
+ package bar
196
+
197
+ type Foo struct {
198
+ Ok int
199
+ Bad Foo
200
+ }` , "use of `Foo` forbidden by pattern `Foo` at testing.go:6:7" )
201
+ })
202
+
203
+ t .Run ("it ignores constant names but checks type" , func (t * testing.T ) {
204
+ linter , _ := NewLinter ([]string {`Foo` }, OptionAnalyzeTypes (true ))
205
+ expectIssues (t , linter , true , `
206
+ package bar
207
+
208
+ const Foo = 1;
209
+ const Bad Foo = 1;
210
+ ` , "use of `Foo` forbidden by pattern `Foo` at testing.go:5:11" )
211
+ })
212
+
213
+ t .Run ("it ignores import alises" , func (t * testing.T ) {
214
+ linter , _ := NewLinter ([]string {`Foo` }, OptionAnalyzeTypes (true ))
215
+ expectIssues (t , linter , true , `
216
+ package bar
217
+
218
+ import Foo "foo"
219
+ ` )
220
+ })
182
221
}
183
222
184
223
// sourcePath matches "at /tmp/TestForbiddenIdentifiersdisplays_custom_messages4260088387/001/testing.go".
185
224
var sourcePath = regexp .MustCompile (`at .*/([[:alnum:]]+.go)` )
186
225
187
226
func expectIssues (t * testing.T , linter * Linter , expand bool , contents string , issues ... string ) {
227
+ t .Helper ()
188
228
actualIssues := parseFile (t , linter , expand , "testing.go" , contents )
189
229
actualIssueStrs := make ([]string , 0 , len (actualIssues ))
190
230
for _ , i := range actualIssues {
191
231
str := i .String ()
192
232
str = sourcePath .ReplaceAllString (str , "at $1" )
193
233
actualIssueStrs = append (actualIssueStrs , str )
194
234
}
195
- assert .ElementsMatch (t , issues , actualIssueStrs )
235
+ if ! assert .ElementsMatch (t , issues , actualIssueStrs ) {
236
+ t .Logf ("Expected: %v" , issues )
237
+ t .Logf ("Got: %v" , actualIssueStrs )
238
+ }
196
239
}
197
240
198
241
func parseFile (t * testing.T , linter * Linter , expand bool , fileName , contents string ) []Issue {
0 commit comments