diff options
author | Markus Wüstenberg | 2020-09-18 14:38:09 +0200 |
---|---|---|
committer | GitHub | 2020-09-18 14:38:09 +0200 |
commit | f9d584c9889276000171046bcc2e32177514f552 (patch) | |
tree | a07b241aef821b40b01cb46919966e90b1452d5c | |
parent | e8faab5207e6ed48aef0a6b1ade710dd8fbfadd3 (diff) | |
download | gomponents-f9d584c9889276000171046bcc2e32177514f552.tar.lz gomponents-f9d584c9889276000171046bcc2e32177514f552.tar.zst gomponents-f9d584c9889276000171046bcc2e32177514f552.zip |
Make NodeFunc and attr implement fmt.Stringer (#6)
-rw-r--r-- | assert/assert.go | 15 | ||||
-rw-r--r-- | gomponents.go | 10 | ||||
-rw-r--r-- | gomponents_test.go | 33 |
3 files changed, 43 insertions, 15 deletions
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 { 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, "<div/>", e.Render()) + assert.Equal(t, "<div/>", 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() - } -} |