2020-09-13 13:50:19 -07:00
|
|
|
package gomponents_test
|
|
|
|
|
|
|
|
import (
|
2020-09-21 02:27:37 -07:00
|
|
|
"errors"
|
2020-09-21 07:07:39 -07:00
|
|
|
"fmt"
|
2020-09-21 02:27:37 -07:00
|
|
|
"strings"
|
2020-09-13 13:50:19 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
g "github.com/maragudk/gomponents"
|
2020-09-18 05:38:09 -07:00
|
|
|
"github.com/maragudk/gomponents/assert"
|
2020-09-13 13:50:19 -07:00
|
|
|
)
|
|
|
|
|
2020-09-18 05:38:09 -07:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-13 13:50:19 -07:00
|
|
|
func TestAttr(t *testing.T) {
|
|
|
|
t.Run("renders just the local name with one argument", func(t *testing.T) {
|
|
|
|
a := g.Attr("required")
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, " required", a)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("renders the name and value when given two arguments", func(t *testing.T) {
|
|
|
|
a := g.Attr("id", "hat")
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, ` id="hat"`, a)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("panics with more than two arguments", func(t *testing.T) {
|
|
|
|
called := false
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
called = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
g.Attr("name", "value", "what is this?")
|
|
|
|
if !called {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
2020-09-21 07:07:39 -07:00
|
|
|
|
|
|
|
t.Run("implements fmt.Stringer", func(t *testing.T) {
|
|
|
|
a := g.Attr("required")
|
|
|
|
s := fmt.Sprintf("%v", a)
|
|
|
|
if s != " required" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
2020-09-13 13:50:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEl(t *testing.T) {
|
|
|
|
t.Run("renders an empty element if no children given", func(t *testing.T) {
|
|
|
|
e := g.El("div")
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, "<div/>", e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("renders an empty element if only attributes given as children", func(t *testing.T) {
|
|
|
|
e := g.El("div", g.Attr("class", "hat"))
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, `<div class="hat"/>`, e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("renders an element, attributes, and element children", func(t *testing.T) {
|
|
|
|
e := g.El("div", g.Attr("class", "hat"), g.El("span"))
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, `<div class="hat"><span/></div>`, e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
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"))
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, `<div class="hat"><span/></div>`, e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestText(t *testing.T) {
|
|
|
|
t.Run("renders escaped text", func(t *testing.T) {
|
|
|
|
e := g.Text("<div/>")
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, "<div/>", e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:10:35 -07:00
|
|
|
func TestTextf(t *testing.T) {
|
|
|
|
t.Run("renders interpolated and escaped text", func(t *testing.T) {
|
|
|
|
e := g.Textf("<%v/>", "div")
|
|
|
|
assert.Equal(t, "<div/>", e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-13 13:50:19 -07:00
|
|
|
func TestRaw(t *testing.T) {
|
|
|
|
t.Run("renders raw text", func(t *testing.T) {
|
|
|
|
e := g.Raw("<div/>")
|
2020-09-18 05:38:09 -07:00
|
|
|
assert.Equal(t, "<div/>", e)
|
2020-09-13 13:50:19 -07:00
|
|
|
})
|
|
|
|
}
|
2020-09-21 02:27:37 -07:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|