Skip to content

Commit

Permalink
Add examples in test files (#80)
Browse files Browse the repository at this point in the history
These show up in godoc.
  • Loading branch information
markuswustenberg authored Jun 8, 2021
1 parent 7c0f2e4 commit ba0d83f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
7 changes: 7 additions & 0 deletions components/attributes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components_test

import (
"os"
"testing"

g "github.com/maragudk/gomponents"
Expand Down Expand Up @@ -30,3 +31,9 @@ func TestClasses(t *testing.T) {
}
})
}

func ExampleClasses() {
e := g.El("div", c.Classes{"party-hat": true, "boring-hat": false})
_ = e.Render(os.Stdout)
// Output: <div class="party-hat"></div>
}
14 changes: 1 addition & 13 deletions gomponents.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package gomponents provides declarative view components in Go, that can render to HTML5.
// Package gomponents provides view components in Go, that render to HTML 5.
// The primary interface is a Node, which has a single function Render, which should render
// the Node to a string. Furthermore, NodeFunc is a function which implements the Node interface
// by calling itself on Render.
Expand Down Expand Up @@ -219,14 +219,6 @@ func Group(children []Node) Node {
}

// Map something enumerable to a list of Nodes.
// Example:
// items := []string{"hat", "partyhat"}
//
// lis := Map(len(items), func(i int) Node {
// return El("li", Text(items[i]))
// })
//
// list := El("ul", lis...)
func Map(length int, cb func(i int) Node) []Node {
var nodes []Node
for i := 0; i < length; i++ {
Expand All @@ -237,10 +229,6 @@ func Map(length int, cb func(i int) Node) []Node {

// If condition is true, return the given Node. Otherwise, return nil.
// This helper function is good for inlining elements conditionally.
// Example:
// El("div",
// If(showMessage, El("span", "You lost your hat.")),
// )
func If(condition bool, n Node) Node {
if condition {
return n
Expand Down
56 changes: 56 additions & 0 deletions gomponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -77,6 +78,18 @@ func BenchmarkAttr(b *testing.B) {
})
}

func ExampleAttr_bool() {
e := g.El("input", g.Attr("required"))
_ = e.Render(os.Stdout)
// Output: <input required>
}

func ExampleAttr_name_value() {
e := g.El("div", g.Attr("id", "hat"))
_ = e.Render(os.Stdout)
// Output: <div id="hat"></div>
}

type outsider struct{}

func (o outsider) String() string {
Expand Down Expand Up @@ -146,6 +159,12 @@ func BenchmarkEl(b *testing.B) {
})
}

func ExampleEl() {
e := g.El("div", g.El("span"))
_ = e.Render(os.Stdout)
// Output: <div><span></span></div>
}

type erroringWriter struct{}

func (w *erroringWriter) Write(p []byte) (n int, err error) {
Expand All @@ -159,20 +178,38 @@ func TestText(t *testing.T) {
})
}

func ExampleText() {
e := g.El("span", g.Text("Party hats > normal hats."))
_ = e.Render(os.Stdout)
// Output: <span>Party hats &gt; normal hats.</span>
}

func TestTextf(t *testing.T) {
t.Run("renders interpolated and escaped text", func(t *testing.T) {
e := g.Textf("<%v>", "div")
assert.Equal(t, "&lt;div&gt;", e)
})
}

func ExampleTextf() {
e := g.El("span", g.Textf("%v party hats > %v normal hats.", 2, 3))
_ = e.Render(os.Stdout)
// Output: <span>2 party hats &gt; 3 normal hats.</span>
}

func TestRaw(t *testing.T) {
t.Run("renders raw text", func(t *testing.T) {
e := g.Raw("<div>")
assert.Equal(t, "<div>", e)
})
}

func ExampleRaw() {
e := g.El("span", g.Raw("<strong>Party</strong> hats &gt; normal hats."))
_ = e.Render(os.Stdout)
// Output: <span><strong>Party</strong> hats &gt; normal hats.</span>
}

func TestGroup(t *testing.T) {
t.Run("groups multiple nodes into one", func(t *testing.T) {
children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}
Expand Down Expand Up @@ -222,6 +259,15 @@ func TestMap(t *testing.T) {
})
}

func ExampleMap() {
items := []string{"party hat", "super hat"}
e := g.El("ul", g.Group(g.Map(len(items), func(i int) g.Node {
return g.El("li", g.Text(items[i]))
})))
_ = e.Render(os.Stdout)
// Output: <ul><li>party hat</li><li>super hat</li></ul>
}

func TestIf(t *testing.T) {
t.Run("returns node if condition is true", func(t *testing.T) {
n := g.El("div", g.If(true, g.El("span")))
Expand All @@ -233,3 +279,13 @@ func TestIf(t *testing.T) {
assert.Equal(t, "<div></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."))),
)
_ = e.Render(os.Stdout)
// Output: <div><span>You lost your hat!</span></div>
}

0 comments on commit ba0d83f

Please sign in to comment.