From 5b98b623524d2d3767f67ffd38be856efed2604f Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Sat, 28 Jun 2014 17:32:30 -0400 Subject: [PATCH] improve Validate & test --- typewriters/container/container.go | 24 +++++++-- typewriters/container/container_test.go | 71 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 typewriters/container/container_test.go diff --git a/typewriters/container/container.go b/typewriters/container/container.go index 95caa02..b23fa79 100644 --- a/typewriters/container/container.go +++ b/typewriters/container/container.go @@ -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) { diff --git a/typewriters/container/container_test.go b/typewriters/container/container_test.go new file mode 100644 index 0000000..ce3a361 --- /dev/null +++ b/typewriters/container/container_test.go @@ -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) + } +}