Repository: maragudk/gomponents
Pull request #6 (2020-09-18 12:33:05)

Make NodeFunc and attr implement fmt.Stringer
Also add a testing helper.

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=h1) Report
> Merging [#6](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=desc) into [master](https://codecov.io/gh/maragudk/gomponents/commit/e8faab5207e6ed48aef0a6b1ade710dd8fbfadd3?el=desc) will **decrease** coverage by `5.12%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/maragudk/gomponents/pull/6/graphs/tree.svg?width=650&height=150&src=pr&token=JYN032TYAC)](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##            master       #6      +/-   ##
===========================================
- Coverage   100.00%   94.87%   -5.13%     
===========================================
  Files            1        1              
  Lines           37       39       +2     
===========================================
  Hits            37       37              
- Misses           0        2       +2     
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [gomponents.go](https://codecov.io/gh/maragudk/gomponents/pull/6/diff?src=pr&el=tree#diff-Z29tcG9uZW50cy5nbw==) | `94.87% <0.00%> (-5.13%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=footer). Last update [e8faab5...28b2eb1](https://codecov.io/gh/maragudk/gomponents/pull/6?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/assert/assert.go b/assert/assert.go
new file mode 100644
index 0000000..b2b6002
--- /dev/null
+++ b/assert/assert.go
@@ -0,0 +1,15 @@
+package assert
+
+import (
+	"testing"
+
+	g "github.com/maragudk/gomponents"
+)
+
+// Equal checks for equality between the given expected string and the rendered Node string.
+func Equal(t *testing.T, expected string, actual g.Node) {
+	if expected != actual.Render() {
+		t.Errorf("expected `%v` but got `%v`", expected, actual)
+		t.FailNow()
+	}
+}
diff --git a/gomponents.go b/gomponents.go
index ab85226..beb13f3 100644
--- a/gomponents.go
+++ b/gomponents.go
@@ -25,6 +25,11 @@ func (n NodeFunc) Render() string {
 	return n()
 }
 
+// String satisfies fmt.Stringer.
+func (n NodeFunc) String() string {
+	return n.Render()
+}
+
 // El creates an element DOM Node with a name and child Nodes.
 // Use this if no convenience creator exists.
 func El(name string, children ...Node) NodeFunc {
@@ -92,6 +97,11 @@ func (a attr) Render() string {
 	return fmt.Sprintf(` %v="%v"`, a.name, *a.value)
 }
 
+// String satisfies fmt.Stringer.
+func (a attr) String() string {
+	return a.Render()
+}
+
 // Text creates a text DOM Node that Renders the escaped string t.
 func Text(t string) NodeFunc {
 	return func() string {


Test patch:
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()
-	}
-}