1
1
package tagalign
2
2
3
3
import (
4
- "path/filepath"
5
4
"testing"
6
5
7
6
"github.com/fatih/structtag"
8
7
"github.com/stretchr/testify/assert"
9
8
"golang.org/x/tools/go/analysis/analysistest"
10
9
)
11
10
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
+ }
19
50
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 ()
26
54
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 ... )
34
56
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
+ }
41
60
}
42
61
43
62
func Test_alignFormat (t * testing.T ) {
@@ -57,27 +76,3 @@ func Test_sortTags(t *testing.T) {
57
76
assert .Equal (t , "gorm" , tags .Tags ()[4 ].Key )
58
77
assert .Equal (t , "zip" , tags .Tags ()[5 ].Key )
59
78
}
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
- }
0 commit comments