Skip to content

Commit

Permalink
tests: rewrite analyzer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 22, 2024
1 parent 6e7cd01 commit 196ff4e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 52 deletions.
97 changes: 46 additions & 51 deletions tagalign_test.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
package tagalign

import (
"path/filepath"
"testing"

"github.com/fatih/structtag"
"github.com/stretchr/testify/assert"
"golang.org/x/tools/go/analysis/analysistest"
)

func Test_alignOnly(t *testing.T) {
// only align
a := NewAnalyzer()
unsort, err := filepath.Abs("testdata/align")
assert.NoError(t, err)
analysistest.Run(t, unsort, a)
}
func TestAnalyzer(t *testing.T) {
testCases := []struct {
desc string
dir string
opts []Option
}{
{
desc: "only align",
dir: "align_only",
},
{
desc: "sort only",
dir: "sort_only",
opts: []Option{WithAlign(false), WithSort(nil...)},
},
{
desc: "sort with order",
dir: "sortorder",
opts: []Option{WithAlign(false), WithSort("xml", "json", "yaml")},
},
{
desc: "align and sort with fixed order",
dir: "alignsortorder",
opts: []Option{WithSort("json", "yaml", "xml")},
},
{
desc: "strict style",
dir: "strict",
opts: []Option{WithSort("json", "yaml", "xml"), WithStrictStyle()},
},
{
desc: "align single field",
dir: "single_field",
},
{
desc: "bad syntax tag",
dir: "bad_syntax_tag",
},
}

func Test_sortOnly(t *testing.T) {
a := NewAnalyzer(WithAlign(false), WithSort(nil...))
sort, err := filepath.Abs("testdata/sort")
assert.NoError(t, err)
analysistest.Run(t, sort, a)
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

func Test_sortWithOrder(t *testing.T) {
// test disable align but enable sort
a := NewAnalyzer(WithAlign(false), WithSort("xml", "json", "yaml"))
sort, err := filepath.Abs("testdata/sortorder")
assert.NoError(t, err)
analysistest.Run(t, sort, a)
}
a := NewAnalyzer(test.opts...)

func Test_alignAndSortWithOrder(t *testing.T) {
// align and sort with fixed order
a := NewAnalyzer(WithSort("json", "yaml", "xml"))
sort, err := filepath.Abs("testdata/alignsortorder")
assert.NoError(t, err)
analysistest.Run(t, sort, a)
analysistest.Run(t, analysistest.TestData(), a, test.dir)
})
}
}

func Test_alignFormat(t *testing.T) {
Expand All @@ -57,27 +76,3 @@ func Test_sortTags(t *testing.T) {
assert.Equal(t, "gorm", tags.Tags()[4].Key)
assert.Equal(t, "zip", tags.Tags()[5].Key)
}

func Test_strictStyle(t *testing.T) {
// align and sort with fixed order
a := NewAnalyzer(WithSort("json", "yaml", "xml"), WithStrictStyle())
sort, err := filepath.Abs("testdata/strict")
assert.NoError(t, err)
analysistest.Run(t, sort, a)
}

func Test_alignSingleField(t *testing.T) {
// only align
a := NewAnalyzer()
unsort, err := filepath.Abs("testdata/single_field")
assert.NoError(t, err)
analysistest.Run(t, unsort, a)
}

func Test_badSyntaxTag(t *testing.T) {
// only align
a := NewAnalyzer()
unsort, err := filepath.Abs("testdata/bad_syntax_tag")
assert.NoError(t, err)
analysistest.Run(t, unsort, a)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sort
package sort_only

import "time"

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 196ff4e

Please sign in to comment.