about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2025-03-21 21:44:08 +0100
committerAlan Pearce2025-03-21 21:44:08 +0100
commit7ada5cf0a0ac21780e8ef5abd4be308446fa3110 (patch)
treee7331cf80b60cca9d8203932ba7ac89cf5410711
parentdce03353aa6f9b7254cb6279ff3bf5d0c7564ff4 (diff)
downloadgomponents-7ada5cf0a0ac21780e8ef5abd4be308446fa3110.tar.lz
gomponents-7ada5cf0a0ac21780e8ef5abd4be308446fa3110.tar.zst
gomponents-7ada5cf0a0ac21780e8ef5abd4be308446fa3110.zip
Add tests for MapWithIndex and MapMap
-rw-r--r--gomponents_test.go38
1 files 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: <ul><li>party hat</li><li>super hat</li></ul>
 }
 
-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, `<ul><li>0: party hat</li><li>1: super hat</li></ul>`, 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: <ul><li>0: party hat</li><li>1: super hat</li></ul>
 }
 
+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, `<ul><li>party: hat</li><li>super: hat</li></ul>`, 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: <ul><li>party: hat</li><li>super: hat</li></ul>
+}
+
 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")}