all repos — gomponents @ 5eae1eef0f0a090ae569a23c0c52db356a109cfc

HTML components in pure Go

Add else part to If/Iff

Alan Pearce
commit

5eae1eef0f0a090ae569a23c0c52db356a109cfc

parent

6aea6d0965f9030d598eb87f5bfca36b638035f6

1 file changed, 61 insertions(+), 23 deletions(-)

changed files
M gomponents_test.gogomponents_test.go
@@ -103,16 +103,19 @@ e := g.El("div")
assert.Equal(t, "<div></div>", e) }) - t.Run("renders an empty element without closing tag if it's a void kind element", func(t *testing.T) { - e := g.El("hr") - assert.Equal(t, "<hr>", e) + t.Run( + "renders an empty element without closing tag if it's a void kind element", + func(t *testing.T) { + e := g.El("hr") + assert.Equal(t, "<hr>", e) - e = g.El("br") - assert.Equal(t, "<br>", e) + e = g.El("br") + assert.Equal(t, "<br>", e) - e = g.El("img") - assert.Equal(t, "<img>", e) - }) + e = g.El("img") + assert.Equal(t, "<img>", 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"))
@@ -124,10 +127,13 @@ e := g.El("div", g.Attr("class", "hat"), g.El("br"))
assert.Equal(t, `<div class="hat"><br></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("br"), g.Attr("class", "hat")) - assert.Equal(t, `<div class="hat"><br></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("br"), g.Attr("class", "hat")) + assert.Equal(t, `<div class="hat"><br></div>`, e) + }, + ) t.Run("renders outside if node does not implement nodeTypeDescriber", func(t *testing.T) { e := g.El("div", outsider{})
@@ -201,8 +207,11 @@ })
} func ExampleRaw() { - e := g.El("span", - g.Raw(`<button onclick="javascript:alert('Party time!')">Party hats</button> &gt; normal hats.`), + e := g.El( + "span", + g.Raw( + `<button onclick="javascript:alert('Party time!')">Party hats</button> &gt; normal hats.`, + ), ) _ = e.Render(os.Stdout) // Output: <span><button onclick="javascript:alert('Party time!')">Party hats</button> &gt; normal hats.</span>
@@ -216,8 +225,12 @@ })
} func ExampleRawf() { - e := g.El("span", - g.Rawf(`<button onclick="javascript:alert('%v')">Party hats</button> &gt; normal hats.`, "Party time!"), + e := g.El( + "span", + g.Rawf( + `<button onclick="javascript:alert('%v')">Party hats</button> &gt; normal hats.`, + "Party time!", + ), ) _ = e.Render(os.Stdout) // Output: <span><button onclick="javascript:alert('Party time!')">Party hats</button> &gt; normal hats.</span>
@@ -277,7 +290,10 @@ assert.Equal(t, "<div></div><span></span>", e)
}) t.Run("does not ignore attributes at the second level and below", func(t *testing.T) { - children := []g.Node{g.El("div", g.Attr("class", "hat"), g.El("hr", g.Attr("id", "partyhat"))), g.El("span")} + children := []g.Node{ + g.El("div", g.Attr("class", "hat"), g.El("hr", g.Attr("id", "partyhat"))), + g.El("span"), + } e := g.Group(children) assert.Equal(t, `<div class="hat"><hr id="partyhat"></div><span></span>`, e) })
@@ -321,14 +337,21 @@ t.Run("returns nil if condition is false", func(t *testing.T) {
n := g.El("div", g.If(false, g.El("span"))) assert.Equal(t, "<div></div>", n) }) + + t.Run("returns second node if given and condition is false", func(t *testing.T) { + n := g.El("div", g.If(false, g.Text("first"), g.Text("second"))) + assert.Equal(t, "<div>second</div>", n) + }) } func ExampleIf() { showMessage := true e := g.El("div", - g.If(showMessage, g.El("span", g.Text("You lost your hat!"))), - g.If(!showMessage, g.El("span", g.Text("No messages."))), + g.If(showMessage, + g.El("span", g.Text("You lost your hat!")), + g.El("span", g.Text("No messages.")), + ), ) _ = e.Render(os.Stdout)
@@ -349,6 +372,15 @@ return g.El("span")
})) assert.Equal(t, "<div></div>", n) }) + + t.Run("returns second node if given and condition is false", func(t *testing.T) { + n := g.El("div", g.Iff(false, func() g.Node { + return g.Text("first") + }, func() g.Node { + return g.Text("second") + })) + assert.Equal(t, "<div>second</div>", n) + }) } func ExampleIff() {
@@ -359,11 +391,17 @@ var user *User
e := g.El("div", // This would panic using just If - g.Iff(user != nil, func() g.Node { - return g.Text(user.Name) - }), + g.Iff( + user != nil, + func() g.Node { + return g.Text(user.Name) + }, + func() g.Node { + return g.Text("No user") + }, + ), ) _ = e.Render(os.Stdout) - // Output: <div></div> + // Output: <div>No user</div> }