Skip to content

Commit

Permalink
Merge branch 'pr/56'
Browse files Browse the repository at this point in the history
  • Loading branch information
clipperhouse committed Jul 7, 2014
2 parents e27372d + b947368 commit 89b9dc2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 37 deletions.
4 changes: 2 additions & 2 deletions typewriter/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func TestNewApp(t *testing.T) {
}

// app and dummy types
if len(a1.Types) != 2 {
t.Errorf("should have found 2 types, found %v", len(a1.Types))
if len(a1.Types) != 4 {
t.Errorf("should have found 4 types, found %v", len(a1.Types))
}

// this merely tests that they've been assigned to the app
Expand Down
8 changes: 8 additions & 0 deletions typewriter/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@ package typewriter

// +test foo:"bar"
type dummy int

type (
// +test foo:"bar"
dummy2 map[string]dummy

// +test foo:"bar"
dummy3 string
)
68 changes: 35 additions & 33 deletions typewriter/parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package typewriter

import (
"errors"
"fmt"
"go/ast"
"go/parser"
Expand Down Expand Up @@ -29,13 +28,13 @@ func getTypes(directive string, filter func(os.FileInfo) bool) ([]Type, error) {
for name, astPackage := range astPackages {

// collect type nodes
var nodes []ast.Node
var decls []*ast.GenDecl

ast.Inspect(astPackage, func(n ast.Node) bool {
// is it a type?
// http://golang.org/pkg/go/ast/#GenDecl
if d, ok := n.(*ast.GenDecl); ok && d.Tok == token.TYPE {
nodes = append(nodes, n)
decls = append(decls, d)

// no need to keep walking, we don't care about TypeSpec's children
return false
Expand All @@ -57,45 +56,48 @@ func getTypes(directive string, filter func(os.FileInfo) bool) ([]Type, error) {

pkg := &Package{typesPkg}

for _, node := range nodes {
// these assertions should be safe, see ast.Inspect above
decl := node.(*ast.GenDecl)
// the first spec in this Decl should be the type spec, see ast.Inspect above
spec := decl.Specs[0].(*ast.TypeSpec)
for _, decl := range decls {
if decl.Lparen == 0 {
// not parenthesized, copy GenDecl.Doc into TypeSpec.Doc
decl.Specs[0].(*ast.TypeSpec).Doc = decl.Doc
}
for _, gspec := range decl.Specs {
spec := gspec.(*ast.TypeSpec)

pointer, tags, found, err := parseTags(directive, decl.Doc.Text())
pointer, tags, found, err := parseTags(directive, spec.Doc.Text())

if err != nil {
return typs, err
}
if err != nil {
return typs, err
}

if !found {
continue
}
if !found {
continue
}

typ := Type{
Package: pkg,
Pointer: pointer,
Name: spec.Name.Name,
Tags: tags,
}
typ := Type{
Package: pkg,
Pointer: pointer,
Name: spec.Name.Name,
Tags: tags,
}

t, _, err := types.Eval(typ.LocalName(), typesPkg, typesPkg.Scope())
t, _, err := types.Eval(typ.LocalName(), typesPkg, typesPkg.Scope())

known := err == nil
known := err == nil

if !known {
err = errors.New(fmt.Sprintf("failed to evaluate type %s (%s)", typ.Name, err))
continue
}
if !known {
err = fmt.Errorf("failed to evaluate type %s (%s)", typ.Name, err)
continue
}

typ.comparable = isComparable(t)
typ.numeric = isNumeric(t)
typ.ordered = isOrdered(t)
typ.test = test(strings.HasSuffix(fset.Position(spec.Pos()).Filename, "_test.go"))
typ.Type = t
typ.comparable = isComparable(t)
typ.numeric = isNumeric(t)
typ.ordered = isOrdered(t)
typ.test = test(strings.HasSuffix(fset.Position(spec.Pos()).Filename, "_test.go"))
typ.Type = t

typs = append(typs, typ)
typs = append(typs, typ)
}
}
}

Expand Down
40 changes: 38 additions & 2 deletions typewriter/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func TestGetTypes(t *testing.T) {
t.Error(err)
}

if len(typs) != 2 {
t.Errorf("should have found the 2 marked-up types, found %v", len(typs))
if len(typs) != 4 {
t.Errorf("should have found the 4 marked-up types, found %v", len(typs))
}

// put 'em into a map for convenience
Expand All @@ -29,6 +29,14 @@ func TestGetTypes(t *testing.T) {
t.Errorf("should have found the dummy type")
}

if _, found := m["dummy2"]; !found {
t.Errorf("should have found the dummy2 type")
}

if _, found := m["dummy3"]; !found {
t.Errorf("should have found the dummy3 type")
}

dummy := m["dummy"]

if !dummy.Comparable() {
Expand All @@ -43,6 +51,34 @@ func TestGetTypes(t *testing.T) {
t.Errorf("dummy type should be numeric")
}

dummy2 := m["dummy2"]

if dummy2.Comparable() {
t.Errorf("dummy2 type should not be comparable")
}

if dummy2.Ordered() {
t.Errorf("dummy2 type should not be ordered")
}

if dummy2.Numeric() {
t.Errorf("dummy2 type should not be numeric")
}

dummy3 := m["dummy3"]

if !dummy3.Comparable() {
t.Errorf("dummy3 type should be comparable")
}

if !dummy3.Ordered() {
t.Errorf("dummy3 type should be ordered")
}

if dummy3.Numeric() {
t.Errorf("dummy3 type should not be numeric")
}

// check tag existence at a high level here, see also tag parsing tests

if len(m["app"].Tags) != 2 {
Expand Down

0 comments on commit 89b9dc2

Please sign in to comment.