Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: simplify tests #26

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builtin.go → builtins.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package musttag

// builtin is a set of functions supported out of the box.
var builtin = []Func{
// builtins is a set of functions supported out of the box.
var builtins = []Func{
// https://pkg.go.dev/encoding/json
{Name: "encoding/json.Marshal", Tag: "json", ArgPos: 0},
{Name: "encoding/json.MarshalIndent", Tag: "json", ArgPos: 0},
Expand Down
14 changes: 7 additions & 7 deletions musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ func New(funcs ...Func) *analysis.Analyzer {
Flags: flags(&flagFuncs),
Requires: []*analysis.Analyzer{inspect.Analyzer},
Run: func(pass *analysis.Pass) (any, error) {
l := len(builtin) + len(funcs) + len(flagFuncs)
l := len(builtins) + len(funcs) + len(flagFuncs)
m := make(map[string]Func, l)
toMap := func(slice []Func) {
for _, fn := range slice {
m[fn.Name] = fn
}
}
toMap(builtin)
toMap(builtins)
toMap(funcs)
toMap(flagFuncs)
return run(pass, m)
Expand Down Expand Up @@ -84,9 +84,9 @@ var (
}

// HACK(junk1tm): mainModulePackages() does not return packages from `testdata`,
// because it is ignored by the `go list` command.
// For tests to pass we need add these packages manually.
testPackages []string
// because it is ignored by the go tool, and thus, by the `go list` command.
// For tests to pass we need to add the package with tests to the main module manually.
testPackage string
)

// run starts the analysis.
Expand All @@ -95,8 +95,8 @@ func run(pass *analysis.Pass, funcs map[string]Func) (any, error) {
if err != nil {
return nil, err
}
for _, pkg := range testPackages {
mainModule[pkg] = struct{}{}
if testPackage != "" {
mainModule[testPackage] = struct{}{}
}

// store previous reports to prevent reporting
Expand Down
9 changes: 4 additions & 5 deletions musttag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ func TestAnalyzer(t *testing.T) {
// So, to be able to run tests with external dependencies,
// we first need to write a GOPATH-like tree of stubs.
prepareTestFiles(t)

testPackages = []string{"tests", "examples"}
testPackage = "tests"

analyzer := New(
Func{Name: "example.com/custom.Marshal", Tag: "custom", ArgPos: 0},
Func{Name: "example.com/custom.Unmarshal", Tag: "custom", ArgPos: 1},
)

t.Run("examples", func(t *testing.T) {
t.Run("builtins", func(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, analyzer, "examples")
analysistest.Run(t, testdata, analyzer, "builtins")
})

t.Run("tests", func(t *testing.T) {
Expand Down Expand Up @@ -104,7 +103,7 @@ func prepareTestFiles(t *testing.T) {
}
}
hardlink("tests", "tests.go")
hardlink("examples", "examples.go")
hardlink("builtins", "builtins.go")

for file, data := range stubs {
target := filepath.Join(testdata, "src", file)
Expand Down
84 changes: 84 additions & 0 deletions testdata/builtins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tests

import (
"encoding/json"
"encoding/xml"

// these packages are generated before the tests are run.
"example.com/custom"
"github.com/BurntSushi/toml"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"
)

// TODO(junk1tm): drop `reportOnce` and test each builtin function.

func testJSON() {
var user struct { // want `exported fields should be annotated with the "json" tag`
Name string
Email string `json:"email"`
}
json.Marshal(user)
json.MarshalIndent(user, "", "")
json.Unmarshal(nil, &user)
json.NewEncoder(nil).Encode(user)
json.NewDecoder(nil).Decode(&user)
}

func testXML() {
var user struct { // want `exported fields should be annotated with the "xml" tag`
Name string
Email string `xml:"email"`
}
xml.Marshal(user)
xml.MarshalIndent(user, "", "")
xml.Unmarshal(nil, &user)
xml.NewEncoder(nil).Encode(user)
xml.NewDecoder(nil).Decode(&user)
xml.NewEncoder(nil).EncodeElement(user, xml.StartElement{})
xml.NewDecoder(nil).DecodeElement(&user, &xml.StartElement{})
}

func testYAML() {
var user struct { // want `exported fields should be annotated with the "yaml" tag`
Name string
Email string `yaml:"email"`
}
yaml.Marshal(user)
yaml.Unmarshal(nil, &user)
yaml.NewEncoder(nil).Encode(user)
yaml.NewDecoder(nil).Decode(&user)
}

func testTOML() {
var user struct { // want `exported fields should be annotated with the "toml" tag`
Name string
Email string `toml:"email"`
}
toml.Unmarshal(nil, &user)
toml.Decode("", &user)
toml.DecodeFS(nil, "", &user)
toml.DecodeFile("", &user)
toml.NewEncoder(nil).Encode(user)
toml.NewDecoder(nil).Decode(&user)
}

func testMapstructure() {
var user struct { // want `exported fields should be annotated with the "mapstructure" tag`
Name string
Email string `mapstructure:"email"`
}
mapstructure.Decode(nil, &user)
mapstructure.DecodeMetadata(nil, &user, nil)
mapstructure.WeakDecode(nil, &user)
mapstructure.WeakDecodeMetadata(nil, &user, nil)
}

func testCustom() {
var user struct { // want `exported fields should be annotated with the "custom" tag`
Name string
Email string `custom:"email"`
}
custom.Marshal(user)
custom.Unmarshal(nil, &user)
}
67 changes: 0 additions & 67 deletions testdata/examples.go

This file was deleted.

Loading