Skip to content

Commit

Permalink
tests: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 24, 2023
1 parent 90f7aab commit e1fecdc
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 26 deletions.
149 changes: 127 additions & 22 deletions tagliatelle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tagliatelle_test
import (
"os"
"os/exec"
"path"
"path/filepath"
"testing"

Expand All @@ -13,47 +12,153 @@ import (
)

func TestAnalyzer(t *testing.T) {
cfg := tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
"envconfig": "upperSnake",
"env": "upperSnake",
testCases := []struct {
desc string
dir string
patterns []string
cfg tagliatelle.Config
}{
{
desc: "simple",
dir: "one",
patterns: []string{"one"},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
"envconfig": "upperSnake",
"env": "upperSnake",
},
UseFieldName: true,
},
},
},
{
desc: "with non-applicable overrides",
dir: "one",
patterns: []string{"one/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
"envconfig": "upperSnake",
"env": "upperSnake",
},
UseFieldName: true,
},
Overrides: []tagliatelle.Overrides{
{
Package: "one/b/c",
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "upperSnake",
"yaml": "upperSnake",
},
UseFieldName: false,
},
},
},
},
},
{
desc: "with applicable overrides",
dir: "two",
patterns: []string{"two/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
"envconfig": "upperSnake",
"env": "upperSnake",
},
UseFieldName: true,
},
Overrides: []tagliatelle.Overrides{
{
Package: "two/b",
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "upperSnake",
"yaml": "upperSnake",
},
UseFieldName: false,
},
},
},
},
UseFieldName: true,
},
Overrides: []tagliatelle.Overrides{
{
Package: "a/b/c",
{
desc: "ignore",
dir: "three",
patterns: []string{"three/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
"json": "upperSnake",
"yaml": "upperSnake",
"json": "camel",
"yaml": "camel",
"xml": "camel",
"bson": "camel",
"avro": "snake",
"mapstructure": "kebab",
"header": "header",
"envconfig": "upperSnake",
"env": "upperSnake",
},
UseFieldName: true,
},
Overrides: []tagliatelle.Overrides{
{
Package: "three/b",
Base: tagliatelle.Base{
Ignore: true,
},
},
UseFieldName: false,
},
},
},
}

runWithSuggestedFixes(t, tagliatelle.New(cfg), "a", "a")
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
runWithSuggestedFixes(t, tagliatelle.New(test.cfg), test.dir, test.patterns...)
})
}
}

func runWithSuggestedFixes(t *testing.T, a *analysis.Analyzer, dir string, patterns ...string) []*analysistest.Result {
t.Helper()

wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}

defer func() { _ = os.Chdir(wd) }()

testdata := analysistest.TestData()

// NOTE: analysistest does not yet support modules;
// see https://github.com/golang/go/issues/37054 for details.

err := os.Chdir(filepath.Join(testdata, "src", path.Join(dir)))
err = os.Chdir(filepath.Join(testdata, "src", filepath.FromSlash(dir)))
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 0 additions & 3 deletions testdata/src/a/go.mod

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions testdata/src/one/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/one

go 1.19
2 changes: 1 addition & 1 deletion testdata/src/a/sample.go → testdata/src/one/sample.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package a
package one

type Foo struct {
ID string `json:"ID"` // want `json\(camel\): got 'ID' want 'id'`
Expand Down
56 changes: 56 additions & 0 deletions testdata/src/three/b/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package b

type Foo struct {
ID string `json:"ID"`
UserID string `json:"UserID"`
Name string `json:"NAME"`
Value string `json:"VALUE,omitempty"`
Bar Bar `json:"BAR"`
Bur `json:"BUR"`

Qiix Quux `json:",inline"`
Quux `json:",inline"`
}

type Bar struct {
Name string `json:"-"`
Value string `json:"VALUE"`
CommonServiceFooItem *Bir `json:"COMMON_SERVICE_ITEM,omitempty"`
}

type Bir struct {
Name string `json:"-"`
Value string `json:"VALUE"`
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
}

type Bur struct {
Name string
Value string `yaml:"Value"`
More string `json:"-"`
Also string `json:",omitempty"`
ReqPerS string `avro:"req_per_s"`
HeaderValue string `header:"Header-Value"`
WrongHeaderValue string `header:"Header_Value"`
EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"`
WrongEnvConfigValue string `envconfig:"env_config_value"`
EnvValue string `env:"ENV_VALUE"`
WrongEnvValue string `env:"env_value"`
}

type Quux struct {
Data []byte `json:"data"`
}

// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags.
// Please read the readme about this choice.
type MessedUpTags struct {
// an invalid tag cannot be validated.
Bad string `json:"bad`

// a tag not supported by the rules is not validated.
Whatever string `foo:whatever`

// a tag supported by the rule cannot be validated because foo tag breaks the whole tags block
Mixed string `json:"mixed" foo:mixed`
}
3 changes: 3 additions & 0 deletions testdata/src/three/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/two

go 1.19
56 changes: 56 additions & 0 deletions testdata/src/three/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package two

type Foo struct {
ID string `json:"ID"` // want `json\(camel\): got 'ID' want 'id'`
UserID string `json:"UserID"` // want `json\(camel\): got 'UserID' want 'userId'`
Name string `json:"name"`
Value string `json:"value,omitempty"`
Bar Bar `json:"bar"`
Bur `json:"bur"`

Qiix Quux `json:",inline"`
Quux `json:",inline"`
}

type Bar struct {
Name string `json:"-"`
Value string `json:"value"`
CommonServiceFooItem *Bir `json:"CommonServiceItem,omitempty"` // want `json\(camel\): got 'CommonServiceItem' want 'commonServiceFooItem'`
}

type Bir struct {
Name string `json:"-"`
Value string `json:"value"`
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
}

type Bur struct {
Name string
Value string `yaml:"Value"` // want `yaml\(camel\): got 'Value' want 'value'`
More string `json:"-"`
Also string `json:",omitempty"` // want `json\(camel\): got 'Also' want 'also'`
ReqPerS string `avro:"req_per_s"`
HeaderValue string `header:"Header-Value"`
WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Wrong-Header-Value'`
EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"`
WrongEnvConfigValue string `envconfig:"env_config_value"` // want `envconfig\(upperSnake\): got 'env_config_value' want 'WRONG_ENV_CONFIG_VALUE'`
EnvValue string `env:"ENV_VALUE"`
WrongEnvValue string `env:"env_value"` // want `env\(upperSnake\): got 'env_value' want 'WRONG_ENV_VALUE'`
}

type Quux struct {
Data []byte `json:"data"`
}

// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags.
// Please read the readme about this choice.
type MessedUpTags struct {
// an invalid tag cannot be validated.
Bad string `json:"bad`

// a tag not supported by the rules is not validated.
Whatever string `foo:whatever`

// a tag supported by the rule cannot be validated because foo tag breaks the whole tags block
Mixed string `json:"mixed" foo:mixed`
}
56 changes: 56 additions & 0 deletions testdata/src/two/b/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package b

type Foo struct {
ID string `json:"ID"`
UserID string `json:"UserID"` // want `json\(upperSnake\): got 'UserID' want 'USER_ID'`
Name string `json:"NAME"`
Value string `json:"VALUE,omitempty"`
Bar Bar `json:"BAR"`
Bur `json:"BUR"`

Qiix Quux `json:",inline"`
Quux `json:",inline"`
}

type Bar struct {
Name string `json:"-"`
Value string `json:"VALUE"`
CommonServiceFooItem *Bir `json:"COMMON_SERVICE_ITEM,omitempty"`
}

type Bir struct {
Name string `json:"-"`
Value string `json:"VALUE"`
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
}

type Bur struct {
Name string
Value string `yaml:"Value"` // want `yaml\(upperSnake\): got 'Value' want 'VALUE'`
More string `json:"-"`
Also string `json:",omitempty"` // want `json\(upperSnake\): got 'Also' want 'ALSO'`
ReqPerS string `avro:"req_per_s"`
HeaderValue string `header:"Header-Value"`
WrongHeaderValue string `header:"Header_Value"` // want `header\(header\): got 'Header_Value' want 'Header-Value'`
EnvConfigValue string `envconfig:"ENV_CONFIG_VALUE"`
WrongEnvConfigValue string `envconfig:"env_config_value"` // want `envconfig\(upperSnake\): got 'env_config_value' want 'ENV_CONFIG_VALUE'`
EnvValue string `env:"ENV_VALUE"`
WrongEnvValue string `env:"env_value"` // want `env\(upperSnake\): got 'env_value' want 'ENV_VALUE'`
}

type Quux struct {
Data []byte `json:"data"` // want `json\(upperSnake\): got 'data' want 'DATA'`
}

// MessedUpTags struct is to validate the tool is not doing any validation about invalid tags.
// Please read the readme about this choice.
type MessedUpTags struct {
// an invalid tag cannot be validated.
Bad string `json:"bad`

// a tag not supported by the rules is not validated.
Whatever string `foo:whatever`

// a tag supported by the rule cannot be validated because foo tag breaks the whole tags block
Mixed string `json:"mixed" foo:mixed` // want `json\(upperSnake\): got 'mixed' want 'MIXED'`
}
3 changes: 3 additions & 0 deletions testdata/src/two/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/three

go 1.19
Loading

0 comments on commit e1fecdc

Please sign in to comment.