From ba0d83f4fb1b588f7931b9a31609807adfa11163 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Tue, 8 Jun 2021 18:12:04 +0200 Subject: Add examples in test files (#80) These show up in godoc.--- components/attributes_test.go | 7 ++++++ gomponents.go | 14 +---------- gomponents_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/components/attributes_test.go b/components/attributes_test.go index bf3e93b..9de0604 100644 --- a/components/attributes_test.go +++ b/components/attributes_test.go @@ -1,6 +1,7 @@ package components_test import ( + "os" "testing" g "github.com/maragudk/gomponents" @@ -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:
+} diff --git a/gomponents.go b/gomponents.go index 9cf4297..5ac6939 100644 --- a/gomponents.go +++ b/gomponents.go @@ -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. @@ -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++ { @@ -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 diff --git a/gomponents_test.go b/gomponents_test.go index dffa64c..3064c0f 100644 --- a/gomponents_test.go +++ b/gomponents_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "os" "strings" "testing" @@ -77,6 +78,18 @@ func BenchmarkAttr(b *testing.B) { }) } +func ExampleAttr_bool() { + e := g.El("input", g.Attr("required")) + _ = e.Render(os.Stdout) + // Output: +} + +func ExampleAttr_name_value() { + e := g.El("div", g.Attr("id", "hat")) + _ = e.Render(os.Stdout) + // Output: +} + type outsider struct{} func (o outsider) String() string { @@ -146,6 +159,12 @@ func BenchmarkEl(b *testing.B) { }) } +func ExampleEl() { + e := g.El("div", g.El("span")) + _ = e.Render(os.Stdout) + // Output: