Skip to content

Commit 196ff4e

Browse files
committed
tests: rewrite analyzer tests
1 parent 6e7cd01 commit 196ff4e

File tree

8 files changed

+47
-52
lines changed

8 files changed

+47
-52
lines changed

tagalign_test.go

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,62 @@
11
package tagalign
22

33
import (
4-
"path/filepath"
54
"testing"
65

76
"github.com/fatih/structtag"
87
"github.com/stretchr/testify/assert"
98
"golang.org/x/tools/go/analysis/analysistest"
109
)
1110

12-
func Test_alignOnly(t *testing.T) {
13-
// only align
14-
a := NewAnalyzer()
15-
unsort, err := filepath.Abs("testdata/align")
16-
assert.NoError(t, err)
17-
analysistest.Run(t, unsort, a)
18-
}
11+
func TestAnalyzer(t *testing.T) {
12+
testCases := []struct {
13+
desc string
14+
dir string
15+
opts []Option
16+
}{
17+
{
18+
desc: "only align",
19+
dir: "align_only",
20+
},
21+
{
22+
desc: "sort only",
23+
dir: "sort_only",
24+
opts: []Option{WithAlign(false), WithSort(nil...)},
25+
},
26+
{
27+
desc: "sort with order",
28+
dir: "sortorder",
29+
opts: []Option{WithAlign(false), WithSort("xml", "json", "yaml")},
30+
},
31+
{
32+
desc: "align and sort with fixed order",
33+
dir: "alignsortorder",
34+
opts: []Option{WithSort("json", "yaml", "xml")},
35+
},
36+
{
37+
desc: "strict style",
38+
dir: "strict",
39+
opts: []Option{WithSort("json", "yaml", "xml"), WithStrictStyle()},
40+
},
41+
{
42+
desc: "align single field",
43+
dir: "single_field",
44+
},
45+
{
46+
desc: "bad syntax tag",
47+
dir: "bad_syntax_tag",
48+
},
49+
}
1950

20-
func Test_sortOnly(t *testing.T) {
21-
a := NewAnalyzer(WithAlign(false), WithSort(nil...))
22-
sort, err := filepath.Abs("testdata/sort")
23-
assert.NoError(t, err)
24-
analysistest.Run(t, sort, a)
25-
}
51+
for _, test := range testCases {
52+
t.Run(test.desc, func(t *testing.T) {
53+
t.Parallel()
2654

27-
func Test_sortWithOrder(t *testing.T) {
28-
// test disable align but enable sort
29-
a := NewAnalyzer(WithAlign(false), WithSort("xml", "json", "yaml"))
30-
sort, err := filepath.Abs("testdata/sortorder")
31-
assert.NoError(t, err)
32-
analysistest.Run(t, sort, a)
33-
}
55+
a := NewAnalyzer(test.opts...)
3456

35-
func Test_alignAndSortWithOrder(t *testing.T) {
36-
// align and sort with fixed order
37-
a := NewAnalyzer(WithSort("json", "yaml", "xml"))
38-
sort, err := filepath.Abs("testdata/alignsortorder")
39-
assert.NoError(t, err)
40-
analysistest.Run(t, sort, a)
57+
analysistest.Run(t, analysistest.TestData(), a, test.dir)
58+
})
59+
}
4160
}
4261

4362
func Test_alignFormat(t *testing.T) {
@@ -57,27 +76,3 @@ func Test_sortTags(t *testing.T) {
5776
assert.Equal(t, "gorm", tags.Tags()[4].Key)
5877
assert.Equal(t, "zip", tags.Tags()[5].Key)
5978
}
60-
61-
func Test_strictStyle(t *testing.T) {
62-
// align and sort with fixed order
63-
a := NewAnalyzer(WithSort("json", "yaml", "xml"), WithStrictStyle())
64-
sort, err := filepath.Abs("testdata/strict")
65-
assert.NoError(t, err)
66-
analysistest.Run(t, sort, a)
67-
}
68-
69-
func Test_alignSingleField(t *testing.T) {
70-
// only align
71-
a := NewAnalyzer()
72-
unsort, err := filepath.Abs("testdata/single_field")
73-
assert.NoError(t, err)
74-
analysistest.Run(t, unsort, a)
75-
}
76-
77-
func Test_badSyntaxTag(t *testing.T) {
78-
// only align
79-
a := NewAnalyzer()
80-
unsort, err := filepath.Abs("testdata/bad_syntax_tag")
81-
assert.NoError(t, err)
82-
analysistest.Run(t, unsort, a)
83-
}
File renamed without changes.

testdata/sort/example.go renamed to testdata/src/sort_only/example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sort
1+
package sort_only
22

33
import "time"
44

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)