From 287d0036f33d0b949a950b7b02fbd58a15231408 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sat, 31 Aug 2019 09:47:37 -0600 Subject: [PATCH] Created SpaceList type --- attribute/generated.go | 14 +++++++------- attribute/generated_test.go | 9 --------- attribute/value_types.go | 9 +++++++++ attribute/value_types_test.go | 24 ++++++++++++++++++++++++ internal/codegen/codegen.go | 3 +-- 5 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 attribute/value_types.go create mode 100644 attribute/value_types_test.go diff --git a/attribute/generated.go b/attribute/generated.go index 892284a..9b5b083 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -19,7 +19,7 @@ func Accept(value ...string) hatmill.Attrib { func AcceptCharset(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "accept-charset", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -103,7 +103,7 @@ func Cite(value string) hatmill.Attrib { func Class(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "class", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -243,7 +243,7 @@ func Enctype(value string) hatmill.Attrib { func For(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "for", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -275,7 +275,7 @@ func Formmethod(value string) hatmill.Attrib { func Headers(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "headers", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -500,7 +500,7 @@ func Pattern(value string) hatmill.Attrib { func Ping(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "ping", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -547,7 +547,7 @@ func Referrerpolicy(value string) hatmill.Attrib { func Rel(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "rel", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } @@ -585,7 +585,7 @@ func Rowspan(value int) hatmill.Attrib { func Sandbox(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "sandbox", - Value: String(strings.Join(value, " ")), + Value: String(SpaceList(value).String()), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index bf7b096..34b06fc 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -14,15 +14,6 @@ func testAttribValue(t *testing.T, attrib hatmill.Attrib, expectedValue string) } } -func TestSpaceList(t *testing.T) { - t.Run("empty", func(t *testing.T) { - testAttribValue(t, attribute.Class(), "") - }) - t.Run("nonempty", func(t *testing.T) { - testAttribValue(t, attribute.Class("alpha", "bravo", "charlie"), "alpha bravo charlie") - }) -} - func TestCommaList(t *testing.T) { t.Run("empty", func(t *testing.T) { testAttribValue(t, attribute.Class(), "") diff --git a/attribute/value_types.go b/attribute/value_types.go new file mode 100644 index 0000000..761f331 --- /dev/null +++ b/attribute/value_types.go @@ -0,0 +1,9 @@ +package attribute + +import "strings" + +type SpaceList []string + +func (l SpaceList) String() string { + return strings.Join(l, " ") +} diff --git a/attribute/value_types_test.go b/attribute/value_types_test.go new file mode 100644 index 0000000..cef5d75 --- /dev/null +++ b/attribute/value_types_test.go @@ -0,0 +1,24 @@ +package attribute_test + +import ( + "gitlab.codemonkeysoftware.net/b/hatmill/attribute" + "testing" +) + +func expectEqualStrings(t *testing.T, actual, expected string) { + if actual != expected { + t.Fatalf("expected %q, got %q", expected, actual) + } +} + +func TestSpaceList(t *testing.T) { + t.Run("empty", func(t *testing.T) { + expectEqualStrings(t, attribute.SpaceList{}.String(), "") + }) + + t.Run("nonempty", func(t *testing.T) { + expected := "alpha bravo charlie" + actual := attribute.SpaceList{"alpha", "bravo", "charlie"}.String() + expectEqualStrings(t, actual, expected) + }) +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 80081f7..3db45dd 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -68,8 +68,7 @@ var attribTypes = map[string]AttribTypeInfo{ Imports: []string{"strconv"}, }, "space list": { - Template: listTemplate(" "), - Imports: []string{"strings"}, + Template: simpleTemplate("...string", "SpaceList(%s).String()"), }, "comma list": { Template: listTemplate(","),