1+ package main
2+
3+ import (
4+ "go/token"
5+ "path"
6+ "reflect"
7+ "sort"
8+ "testing"
9+ )
10+
11+ const testDir = "./testdata"
12+
13+ // TestCheckIssues attempts to see if issues are ignored or not and annotates the issues if they are ignored
14+ func TestCheckIssues (t * testing.T ) {
15+ tests := map [string ]struct {
16+ tokens []token.Position
17+ expected []Issue
18+ }{
19+ "all_ignored" : {
20+ tokens : []token.Position {
21+ token.Position {Filename :"main.go" , Line : 23 , Column : 5 },
22+ token.Position {Filename :"main.go" , Line : 29 , Column : 5 },
23+ },
24+ expected : []Issue {
25+ Issue {statement : token.Position {Filename :"main.go" , Line : 23 , Column : 5 }, ignored : true },
26+ Issue {statement : token.Position {Filename :"main.go" , Line : 29 , Column : 5 }, ignored : true },
27+ },
28+ },
29+ "ignored_back_to_back" : {
30+ tokens : []token.Position {
31+ token.Position {Filename :"main.go" , Line : 22 , Column : 5 },
32+ token.Position {Filename :"main.go" , Line : 23 , Column : 5 },
33+ },
34+ expected : []Issue {
35+ Issue {statement : token.Position {Filename :"main.go" , Line : 22 , Column : 5 }, ignored : true },
36+ Issue {statement : token.Position {Filename :"main.go" , Line : 23 , Column : 5 }, ignored : false },
37+ },
38+ },
39+ "single_ignored" : {
40+ tokens : []token.Position {
41+ token.Position {Filename :"main.go" , Line : 23 , Column : 5 },
42+ token.Position {Filename :"main.go" , Line : 29 , Column : 5 },
43+ },
44+ expected : []Issue {
45+ Issue {statement : token.Position {Filename :"main.go" , Line : 23 , Column : 5 }, ignored : true },
46+ Issue {statement : token.Position {Filename :"main.go" , Line : 29 , Column : 5 }, ignored : false },
47+ },
48+ },
49+ "multiple_files" : {
50+ tokens : []token.Position {
51+ token.Position {Filename :"main.go" , Line : 23 , Column : 5 },
52+ token.Position {Filename :"main.go" , Line : 24 , Column : 5 },
53+ token.Position {Filename :"helpers.go" , Line : 16 , Column : 5 },
54+ token.Position {Filename :"helpers.go" , Line : 17 , Column : 5 },
55+ },
56+ expected : []Issue {
57+ Issue {statement : token.Position {Filename :"main.go" , Line : 23 , Column : 5 }, ignored : true },
58+ Issue {statement : token.Position {Filename :"main.go" , Line : 24 , Column : 5 }, ignored : true },
59+ Issue {statement : token.Position {Filename :"helpers.go" , Line : 16 , Column : 5 }, ignored : true },
60+ Issue {statement : token.Position {Filename :"helpers.go" , Line : 17 , Column : 5 }, ignored : true },
61+ },
62+ },
63+ }
64+
65+ for name , expectations := range tests {
66+ t .Run (name , func (t * testing.T ) {
67+ for idx , pos := range expectations .tokens {
68+ expectations .tokens [idx ].Filename = path .Join (testDir , name , pos .Filename )
69+ }
70+ for idx , issue := range expectations .expected {
71+ expectations .expected [idx ].statement .Filename = path .Join (testDir , name , issue .statement .Filename )
72+ }
73+
74+ issues , err := CheckIssues (expectations .tokens )
75+ if err != nil {
76+ t .Fatal (err )
77+ }
78+
79+ if len (issues ) != len (expectations .expected ) {
80+ t .Fatal ("The expected number of issues was not found" )
81+ }
82+
83+ // sort to ensure we have the same issue order
84+ sort .Slice (expectations .expected , func (i , j int ) bool {return expectations .expected [i ].statement .Filename < expectations .expected [j ].statement .Filename })
85+ sort .Slice (issues , func (i , j int ) bool {return issues [i ].statement .Filename < issues [j ].statement .Filename })
86+
87+ for idx , expected := range expectations .expected {
88+ actual := issues [idx ]
89+ if ! reflect .DeepEqual (actual , expected ) {
90+ t .Errorf ("The actual value of %v did not match the expected %v" , actual , expected )
91+ }
92+ }
93+ })
94+ }
95+ }
0 commit comments