Skip to content

Commit 36263ea

Browse files
committed
tests: add suggested fixes
1 parent 196ff4e commit 36263ea

File tree

8 files changed

+107
-3
lines changed

8 files changed

+107
-3
lines changed

tagalign_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ func TestAnalyzer(t *testing.T) {
5050

5151
for _, test := range testCases {
5252
t.Run(test.desc, func(t *testing.T) {
53-
t.Parallel()
54-
5553
a := NewAnalyzer(test.opts...)
5654

57-
analysistest.Run(t, analysistest.TestData(), a, test.dir)
55+
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, test.dir)
5856
})
5957
}
6058
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package testdata
2+
3+
type FooBar struct {
4+
Foo int `json:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo"`
5+
Bar string `json:"___bar___,omitempty" validate:"required"` // want `json:"___bar___,omitempty" validate:"required"`
6+
FooFoo int8 `json:"foo_foo" validate:"required" yaml:"fooFoo"` // want `tag is not aligned, should be: json:"foo_foo"`
7+
BarBar int `json:"bar_bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar"`
8+
FooBar struct {
9+
Foo int `json:"foo" yaml:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo" yaml:"foo" validate:"required"`
10+
Bar222 string `json:"bar222" validate:"required" yaml:"bar"` // want `tag is not aligned, should be: json:"bar222" validate:"required" yaml:"bar"`
11+
} `json:"foo_bar" validate:"required"`
12+
FooFooFoo struct {
13+
BarBarBar struct {
14+
BarBarBarBar string `json:"bar_bar_bar_bar" validate:"required"` // want `json:"bar_bar_bar_bar" validate:"required"`
15+
BarBarBarFooBar string `json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"`
16+
} `json:"bar_bar_bar" validate:"required"`
17+
}
18+
BarFooBarFoo struct{}
19+
// test comment
20+
// test commnet 2
21+
BarFoo string `json:"bar_foo" validate:"required"` // want `tag is not aligned, should be: json:"bar_foo" validate:"required"`
22+
BarFooBar string `json:"bar_foo_bar" validate:"required"`
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package alignsortorder
2+
3+
type AlignAndSortWithOrderExample struct {
4+
Foo int `json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"` // want `tag is not aligned, should be: json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
5+
Bar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
6+
FooBar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
7+
}
8+
9+
type AlignAndSortWithOrderExample2 struct {
10+
Foo int `json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"` // want `tag is not aligned , should be: json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
11+
12+
Bar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned , should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package issues6
2+
3+
type FooBar struct {
4+
Foo int `json: "foo" validate:"required"` // want `bad syntax for struct tag value`
5+
Bar string `json:bar` // want `bad syntax for struct tag value`
6+
FooFoo int8 `json:"foo_foo" validate:"required"`
7+
BarBar int `json:"bar_bar" validate:"required"`
8+
}
9+
10+
type FooBar2 struct {
11+
Foo int `json:"foo" validate:"required"`
12+
13+
FooFoo int8 `json:"foo_foo"`
14+
BarBar int `json:"bar_bar" validate:"required"`
15+
XXX int `json:"xxx" validate:"required"`
16+
Bar string `json:bar` // want `bad syntax for struct tag value`
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package singlefield
2+
3+
type FooBar struct {
4+
Foo int `json:"foo" validate:"required"`
5+
Bar string `json:"bar" validate:"required"`
6+
7+
FooFoo int8 `json:"foo_foo" validate:"required"` // want `json:"foo_foo" validate:"required"`
8+
9+
BarBar int `json:"bar_bar" validate:"required"`
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sort_only
2+
3+
import "time"
4+
5+
type TagAlignExampleSortOnlyKO struct {
6+
Foo time.Time `gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"` // want `gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"`
7+
FooBar struct{} `gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"` // want `gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"`
8+
}
9+
10+
type TagAlignExampleSortOnlyOK struct {
11+
Foo time.Time `gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"`
12+
FooBar struct{} `gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"`
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package sortorder
2+
3+
type SortWithOrderExample struct {
4+
// not aligned but sorted, should not be reported
5+
Foo int `xml:"baz" json:"foo,omitempty" yaml:"bar" binding:"required" gorm:"column:foo" validate:"required" zip:"foo" `
6+
Bar int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar" `
7+
FooBar int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" `
8+
// aligned but not sorted, should be reported
9+
BarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"`
10+
// not aligned but sorted, should trim spaces between tags
11+
FooBarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"`
12+
}

testdata/src/strict/example.go.golden

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package strict
2+
3+
type AlignAndSortWithOrderExample struct {
4+
Foo int `json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"` // want `tag is not aligned, should be: json:"foo,omitempty" yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
5+
Bar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
6+
FooBar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
7+
}
8+
9+
type AlignAndSortWithOrderExample2 struct {
10+
Foo int ` yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"` // want `tag is not aligned, should be: yaml:"bar" xml:"baz" binding:"required" gorm:"column:foo" validate:"required" zip:"foo"`
11+
Bar int `json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"bar" binding:"required" gorm:"column:bar" validate:"required"`
12+
}
13+
14+
type AlignAndSortWithOrderExample3 struct {
15+
Foo int ` gorm:"column:foo" zip:"foo"` // want `tag is not aligned, should be: gorm:"column:foo" zip:"foo"`
16+
Bar int `json:"bar,omitempty" yaml:"foo" xml:"barxxxxxxxxxxxx" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" xml:"barxxxxxxxxxxxx" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
17+
FooBar int `json:"bar,omitempty" yaml:"foo" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"` // want `tag is not aligned, should be: json:"bar,omitempty" yaml:"foo" binding:"required" gorm:"column:bar" validate:"required" zip:"bar"`
18+
}

0 commit comments

Comments
 (0)