about summary refs log tree commit diff stats
path: root/gomponents_non_generic_test.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2022-09-22 09:41:06 +0200
committerGitHub2022-09-22 09:41:06 +0200
commitf387a7123040d9414350bf783bdeff17ac0572c7 (patch)
tree89859a54e3cd6bf3e6169c384529af98aeee4dc5 /gomponents_non_generic_test.go
parent3334d6c7d049cec57ad36f9d5cb0d491faf54a5d (diff)
downloadgomponents-f387a7123040d9414350bf783bdeff17ac0572c7.tar.lz
gomponents-f387a7123040d9414350bf783bdeff17ac0572c7.tar.zst
gomponents-f387a7123040d9414350bf783bdeff17ac0572c7.zip
Add generic Map implementation (#108)
Instead of waiting for a good time to switch the `Map` implementation,
I've decided to just offer two implementations: one for Go versions
before 1.18, and one for 1.18 and onwards. This is achieved using build
tags in the source files.

This is obviously a breaking change for consumers of this library that
use Go 1.18 and onwards.

See #88
Diffstat (limited to 'gomponents_non_generic_test.go')
-rw-r--r--gomponents_non_generic_test.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/gomponents_non_generic_test.go b/gomponents_non_generic_test.go
new file mode 100644
index 0000000..4186808
--- /dev/null
+++ b/gomponents_non_generic_test.go
@@ -0,0 +1,34 @@
+//go:build !go1.18
+// +build !go1.18
+
+package gomponents_test
+
+import (
+	"os"
+	"testing"
+
+	g "github.com/maragudk/gomponents"
+	"github.com/maragudk/gomponents/internal/assert"
+)
+
+func TestMap(t *testing.T) {
+	t.Run("maps slices to nodes", func(t *testing.T) {
+		items := []string{"hat", "partyhat", "turtlehat"}
+		lis := g.Map(len(items), func(i int) g.Node {
+			return g.El("li", g.Text(items[i]))
+		})
+
+		list := g.El("ul", lis...)
+
+		assert.Equal(t, `<ul><li>hat</li><li>partyhat</li><li>turtlehat</li></ul>`, list)
+	})
+}
+
+func ExampleMap() {
+	items := []string{"party hat", "super hat"}
+	e := g.El("ul", g.Group(g.Map(len(items), func(i int) g.Node {
+		return g.El("li", g.Text(items[i]))
+	})))
+	_ = e.Render(os.Stdout)
+	// Output: <ul><li>party hat</li><li>super hat</li></ul>
+}