about summary refs log tree commit diff stats
path: root/gomponents_test.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2020-09-18 14:38:09 +0200
committerGitHub2020-09-18 14:38:09 +0200
commitf9d584c9889276000171046bcc2e32177514f552 (patch)
treea07b241aef821b40b01cb46919966e90b1452d5c /gomponents_test.go
parente8faab5207e6ed48aef0a6b1ade710dd8fbfadd3 (diff)
downloadgomponents-f9d584c9889276000171046bcc2e32177514f552.tar.lz
gomponents-f9d584c9889276000171046bcc2e32177514f552.tar.zst
gomponents-f9d584c9889276000171046bcc2e32177514f552.zip
Make NodeFunc and attr implement fmt.Stringer (#6)
Diffstat (limited to 'gomponents_test.go')
-rw-r--r--gomponents_test.go33
1 files changed, 18 insertions, 15 deletions
diff --git a/gomponents_test.go b/gomponents_test.go
index 21ab30a..fdc8c52 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -4,17 +4,27 @@ import (
 	"testing"
 
 	g "github.com/maragudk/gomponents"
+	"github.com/maragudk/gomponents/assert"
 )
 
+func TestNodeFunc(t *testing.T) {
+	t.Run("implements fmt.Stringer", func(t *testing.T) {
+		fn := g.NodeFunc(func() string { return "hat" })
+		if fn.String() != "hat" {
+			t.FailNow()
+		}
+	})
+}
+
 func TestAttr(t *testing.T) {
 	t.Run("renders just the local name with one argument", func(t *testing.T) {
 		a := g.Attr("required")
-		equal(t, " required", a.Render())
+		assert.Equal(t, " required", a)
 	})
 
 	t.Run("renders the name and value when given two arguments", func(t *testing.T) {
 		a := g.Attr("id", "hat")
-		equal(t, ` id="hat"`, a.Render())
+		assert.Equal(t, ` id="hat"`, a)
 	})
 
 	t.Run("panics with more than two arguments", func(t *testing.T) {
@@ -34,42 +44,35 @@ func TestAttr(t *testing.T) {
 func TestEl(t *testing.T) {
 	t.Run("renders an empty element if no children given", func(t *testing.T) {
 		e := g.El("div")
-		equal(t, "<div/>", e.Render())
+		assert.Equal(t, "<div/>", e)
 	})
 
 	t.Run("renders an empty element if only attributes given as children", func(t *testing.T) {
 		e := g.El("div", g.Attr("class", "hat"))
-		equal(t, `<div class="hat"/>`, e.Render())
+		assert.Equal(t, `<div class="hat"/>`, e)
 	})
 
 	t.Run("renders an element, attributes, and element children", func(t *testing.T) {
 		e := g.El("div", g.Attr("class", "hat"), g.El("span"))
-		equal(t, `<div class="hat"><span/></div>`, e.Render())
+		assert.Equal(t, `<div class="hat"><span/></div>`, e)
 	})
 
 	t.Run("renders attributes at the correct place regardless of placement in parameter list", func(t *testing.T) {
 		e := g.El("div", g.El("span"), g.Attr("class", "hat"))
-		equal(t, `<div class="hat"><span/></div>`, e.Render())
+		assert.Equal(t, `<div class="hat"><span/></div>`, e)
 	})
 }
 
 func TestText(t *testing.T) {
 	t.Run("renders escaped text", func(t *testing.T) {
 		e := g.Text("<div/>")
-		equal(t, "&lt;div/&gt;", e.Render())
+		assert.Equal(t, "&lt;div/&gt;", e)
 	})
 }
 
 func TestRaw(t *testing.T) {
 	t.Run("renders raw text", func(t *testing.T) {
 		e := g.Raw("<div/>")
-		equal(t, "<div/>", e.Render())
+		assert.Equal(t, "<div/>", e)
 	})
 }
-
-func equal(t *testing.T, expected, actual string) {
-	if expected != actual {
-		t.Errorf("expected %v but got %v", expected, actual)
-		t.FailNow()
-	}
-}