Skip to content

Commit

Permalink
improve Validate & test
Browse files Browse the repository at this point in the history
  • Loading branch information
clipperhouse committed Jun 28, 2014
1 parent b6e4c61 commit 5b98b62
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
24 changes: 21 additions & 3 deletions typewriters/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,28 @@ func (c *ContainerWriter) Name() string {

func (c *ContainerWriter) Validate(t typewriter.Type) (bool, error) {
tag, found, err := t.Tags.ByName("containers")
if found && err == nil {
c.tagsByType[t.String()] = tag

if !found || err != nil {
return false, err
}

// must include at least one item that we recognize
any := false
for _, item := range tag.Items {
any = any || containerTemplates.Contains(item)
if any {
// found one, move on
break
}
}
return found, err

if !any {
// not an error, but irrelevant
return false, nil
}

c.tagsByType[t.String()] = tag
return true, nil
}

func (c *ContainerWriter) WriteHeader(w io.Writer, t typewriter.Type) {
Expand Down
71 changes: 71 additions & 0 deletions typewriters/container/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package container

import (
"testing"

"github.com/clipperhouse/gen/typewriter"
)

func TestValidate(t *testing.T) {
g := NewContainerWriter()

pkg := typewriter.NewPackage("dummy", "SomePackage")

typ := typewriter.Type{
Package: pkg,
Name: "SomeType",
Tags: typewriter.Tags{},
}

write, err := g.Validate(typ)

if write {
t.Errorf("no 'containers' tag should not write")
}

if err != nil {
t.Error(err)
}

typ2 := typewriter.Type{
Package: pkg,
Name: "SomeType2",
Tags: typewriter.Tags{
typewriter.Tag{
Name: "containers",
Items: []string{},
},
},
}

write2, err2 := g.Validate(typ2)

if write2 {
t.Errorf("empty 'containers' tag should not write")
}

if err2 != nil {
t.Error(err)
}

typ3 := typewriter.Type{
Package: pkg,
Name: "SomeType3",
Tags: typewriter.Tags{
typewriter.Tag{
Name: "containers",
Items: []string{"List", "Foo"},
},
},
}

write3, err3 := g.Validate(typ3)

if !write3 {
t.Errorf("'containers' tag with List should write (and ignore others)")
}

if err3 != nil {
t.Error(err)
}
}

0 comments on commit 5b98b62

Please sign in to comment.