From 7ada5cf0a0ac21780e8ef5abd4be308446fa3110 Mon Sep 17 00:00:00 2001
From: Alan Pearce
Date: Fri, 21 Mar 2025 21:44:08 +0100
Subject: Add tests for MapWithIndex and MapMap
---
gomponents_test.go | 38 ++++++++++++++++++++++++++++++++------
1 file changed, 32 insertions(+), 6 deletions(-)
diff --git a/gomponents_test.go b/gomponents_test.go
index 7ea31c0..0d1c478 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -264,18 +264,44 @@ func ExampleMap() {
// Output:
}
-func ExampleMap_index() {
+func TestMapWithIndex(t *testing.T) {
+ t.Run("maps items with index", func(t *testing.T) {
+ items := []string{"party hat", "super hat"}
+ e := g.El("ul", g.MapWithIndex(items, func(index int, item string) g.Node {
+ return g.El("li", g.Textf("%v: %v", index, item))
+ }))
+ assert.Equal(t, ``, e)
+ })
+}
+
+func ExampleMapWithIndex() {
items := []string{"party hat", "super hat"}
- var index int
- e := g.El("ul", g.Map(items, func(i string) g.Node {
- e := g.El("li", g.Textf("%v: %v", index, i))
- index++
- return e
+ e := g.El("ul", g.MapWithIndex(items, func(index int, item string) g.Node {
+ return g.El("li", g.Textf("%v: %v", index, item))
}))
_ = e.Render(os.Stdout)
// Output:
}
+func TestMapMap(t *testing.T) {
+ t.Run("maps a map of items", func(t *testing.T) {
+ items := map[string]string{"party": "hat", "super": "hat"}
+ e := g.El("ul", g.MapMap(items, func(key string, value string) g.Node {
+ return g.El("li", g.Textf("%v: %v", key, value))
+ }))
+ assert.Equal(t, ``, e)
+ })
+}
+
+func ExampleMapMap() {
+ items := map[string]string{"party": "hat", "super": "hat"}
+ e := g.El("ul", g.MapMap(items, func(key string, value string) g.Node {
+ return g.El("li", g.Textf("%v: %v", key, value))
+ }))
+ _ = e.Render(os.Stdout)
+ // Output:
+}
+
func TestGroup(t *testing.T) {
t.Run("groups multiple nodes into one", func(t *testing.T) {
children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}
--
cgit 1.4.1