Skip to content

Commit

Permalink
adds tests for findField
Browse files Browse the repository at this point in the history
  • Loading branch information
codyleyhan committed Aug 24, 2018
1 parent 0e2aaa9 commit 56768d6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions codegen/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package codegen

import (
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -12,3 +17,78 @@ func TestNormalizeVendor(t *testing.T) {
require.Equal(t, "*bar/baz", normalizeVendor("*foo/vendor/bar/baz"))
require.Equal(t, "*[]*bar/baz", normalizeVendor("*[]*foo/vendor/bar/baz"))
}

func TestFindField(t *testing.T) {
input := `
package test
type Std struct {
Name string
Value int
}
type Anon struct {
Name string
Tags
}
type Tags struct {
Bar string ` + "`" + `gqlgen:"foo"` + "`" + `
Foo int ` + "`" + `gqlgen:"bar"` + "`" + `
}
type Amb struct {
Bar string ` + "`" + `gqlgen:"foo"` + "`" + `
Foo int ` + "`" + `gqlgen:"foo"` + "`" + `
}
`
scope, err := parseScope(input, "test")
require.NoError(t, err)

std := scope.Lookup("Std").Type().Underlying().(*types.Struct)
anon := scope.Lookup("Anon").Type().Underlying().(*types.Struct)
tags := scope.Lookup("Tags").Type().Underlying().(*types.Struct)
amb := scope.Lookup("Amb").Type().Underlying().(*types.Struct)

tests := []struct {
Name string
Struct *types.Struct
Field string
Tag string
Expected string
ShouldError bool
}{
{"Finds a field by name with no tag", std, "name", "", "Name", false},
{"Finds a field by name when passed tag but tag not used", std, "name", "gqlgen", "Name", false},
{"Ignores tags when not passed a tag", tags, "foo", "", "Foo", false},
{"Picks field with tag over field name when passed a tag", tags, "foo", "gqlgen", "Bar", false},
{"Errors when ambigious", amb, "foo", "gqlgen", "", true},
{"Finds a field that is in embedded struct", anon, "bar", "", "Bar", false},
}

for _, tt := range tests {
tt := tt
field, err := findField(tt.Struct, tt.Field, tt.Tag)
if tt.ShouldError {
require.Nil(t, field, tt.Name)
require.Error(t, err, tt.Name)
} else {
require.NoError(t, err, tt.Name)
require.Equal(t, tt.Expected, field.Name(), tt.Name)
}
}
}

func parseScope(input interface{}, packageName string) (*types.Scope, error) {
// test setup to parse the types
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "test.go", input, 0)
if err != nil {
return nil, err
}

conf := types.Config{Importer: importer.Default()}
pkg, err := conf.Check(packageName, fset, []*ast.File{f}, nil)
if err != nil {
return nil, err
}

return pkg.Scope(), nil
}
3 changes: 3 additions & 0 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ resolver:
filename: resolver.go # where to write them
type: Resolver # whats the resolver root implementation type called?

# Optional, turns on binding to field names by tag provided
struct_tag: json

# Tell gqlgen about any existing models you want to reuse for
# graphql. These normally come from the db or a remote api.
models:
Expand Down

0 comments on commit 56768d6

Please sign in to comment.