Repository: maragudk/gomponents
Pull request #11 (2020-09-21 09:24:54)

Add a Write function

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/11?src=pr&el=h1) Report
> Merging [#11](https://codecov.io/gh/maragudk/gomponents/pull/11?src=pr&el=desc) into [master](https://codecov.io/gh/maragudk/gomponents/commit/8de5a685efbc6f13e606dcd70d15b757e3f9ad9a?el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master      #11      +/-   ##
==========================================
+ Coverage   98.71%   98.75%   +0.03%     
==========================================
  Files           3        3              
  Lines          78       80       +2     
==========================================
+ Hits           77       79       +2     
  Misses          1        1              
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/11?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [gomponents.go](https://codecov.io/gh/maragudk/gomponents/pull/11/diff?src=pr&el=tree#diff-Z29tcG9uZW50cy5nbw==) | `97.50% <100.00%> (+0.13%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/11?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/11?src=pr&el=footer). Last update [8de5a68...c81e808](https://codecov.io/gh/maragudk/gomponents/pull/11?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/gomponents.go b/gomponents.go
index beaf1dd..d160b54 100644
--- a/gomponents.go
+++ b/gomponents.go
@@ -10,6 +10,7 @@ package gomponents
 import (
 	"fmt"
 	"html/template"
+	"io"
 	"strings"
 )
 
@@ -114,3 +115,9 @@ func Raw(t string) NodeFunc {
 		return t
 	}
 }
+
+// Write to the given io.Writer, returning any error.
+func Write(w io.Writer, n Node) error {
+	_, err := w.Write([]byte(n.Render()))
+	return err
+}


Test patch:
diff --git a/gomponents_test.go b/gomponents_test.go
index fdc8c52..ef5b17a 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -1,6 +1,8 @@
 package gomponents_test
 
 import (
+	"errors"
+	"strings"
 	"testing"
 
 	g "github.com/maragudk/gomponents"
@@ -76,3 +78,31 @@ func TestRaw(t *testing.T) {
 		assert.Equal(t, "<div/>", e)
 	})
 }
+
+type erroringWriter struct{}
+
+func (w *erroringWriter) Write(p []byte) (n int, err error) {
+	return 0, errors.New("don't want to write")
+}
+
+func TestWrite(t *testing.T) {
+	t.Run("writes to the writer", func(t *testing.T) {
+		e := g.El("div")
+		var b strings.Builder
+		err := g.Write(&b, e)
+		if err != nil {
+			t.FailNow()
+		}
+		if b.String() != e.Render() {
+			t.FailNow()
+		}
+	})
+
+	t.Run("errors on write error", func(t *testing.T) {
+		e := g.El("div")
+		err := g.Write(&erroringWriter{}, e)
+		if err == nil {
+			t.FailNow()
+		}
+	})
+}