From f58a0660823bc88c901eba4a130f1d4996729c73 Mon Sep 17 00:00:00 2001
From: Markus Wüstenberg
Date: Tue, 24 Sep 2024 09:46:45 +0200
Subject: Make `Group` a type (#202)
This changes `Group` to be a type instead of a function, which means it
can support both grouping `[]Node` as well as doing variadic-ish slice things
like `Group{n1, n2}`.
This also means that `Map` can just return a `Group`.
Special thanks to @deitrix for this simple and elegant solution that,
for some reason or another, has had me perplexed for a long time.
Fixes #201.---
gomponents_test.go | 39 ++++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 13 deletions(-)
(limited to 'gomponents_test.go')
diff --git a/gomponents_test.go b/gomponents_test.go
index 27fb534..c8dab1b 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -228,7 +228,7 @@ func ExampleRawf() {
}
func TestMap(t *testing.T) {
- t.Run("maps slices to nodes", func(t *testing.T) {
+ t.Run("maps a slice to a group", func(t *testing.T) {
items := []string{"hat", "partyhat", "turtlehat"}
lis := g.Map(items, func(i string) g.Node {
return g.El("li", g.Text(i))
@@ -237,14 +237,20 @@ func TestMap(t *testing.T) {
list := g.El("ul", lis...)
assert.Equal(t, `
`, list)
+ if len(lis) != 3 {
+ t.FailNow()
+ }
+ assert.Equal(t, `hat`, lis[0])
+ assert.Equal(t, `partyhat`, lis[1])
+ assert.Equal(t, `turtlehat`, lis[2])
})
}
func ExampleMap() {
items := []string{"party hat", "super hat"}
- e := g.El("ul", g.Group(g.Map(items, func(i string) g.Node {
+ e := g.El("ul", g.Map(items, func(i string) g.Node {
return g.El("li", g.Text(i))
- })))
+ }))
_ = e.Render(os.Stdout)
// Output:
}
@@ -262,25 +268,26 @@ func TestGroup(t *testing.T) {
assert.Equal(t, "", e)
})
- t.Run("does not ignore attributes at the second level", func(t *testing.T) {
- children := []g.Node{g.El("div", g.Attr("class", "hat")), g.El("span")}
+ t.Run("does not ignore attributes at the second level and below", func(t *testing.T) {
+ children := []g.Node{g.El("div", g.Attr("class", "hat"), g.El("hr", g.Attr("id", "partyhat"))), g.El("span")}
e := g.Group(children)
- assert.Equal(t, ``, e)
- })
-
- t.Run("can render a group child node including attributes", func(t *testing.T) {
- children := []g.Node{g.Attr("id", "hat"), g.El("div"), g.El("span")}
- e := g.El("div", g.Group(children))
- assert.Equal(t, ``, e)
+ assert.Equal(t, `
`, e)
})
t.Run("implements fmt.Stringer", func(t *testing.T) {
children := []g.Node{g.El("div"), g.El("span")}
e := g.Group(children)
- if e, ok := e.(fmt.Stringer); !ok || e.String() != "" {
+ if e, ok := any(e).(fmt.Stringer); !ok || e.String() != "" {
t.FailNow()
}
})
+
+ t.Run("can be used like a regular slice", func(t *testing.T) {
+ e := g.Group{g.El("div"), g.El("span")}
+ assert.Equal(t, "", e)
+ assert.Equal(t, "", e[0])
+ assert.Equal(t, "", e[1])
+ })
}
func ExampleGroup() {
@@ -290,6 +297,12 @@ func ExampleGroup() {
// Output:
}
+func ExampleGroup_slice() {
+ e := g.Group{g.El("div"), g.El("span")}
+ _ = e.Render(os.Stdout)
+ // Output:
+}
+
func TestIf(t *testing.T) {
t.Run("returns node if condition is true", func(t *testing.T) {
n := g.El("div", g.If(true, g.El("span")))
--
cgit 1.4.1