1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package gomponents_test
import (
"testing"
g "github.com/maragudk/gomponents"
)
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())
})
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())
})
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()
}
})
}
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())
})
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())
})
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())
})
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())
})
}
func TestText(t *testing.T) {
t.Run("renders escaped text", func(t *testing.T) {
e := g.Text("<div/>")
equal(t, "<div/>", e.Render())
})
}
func TestRaw(t *testing.T) {
t.Run("renders raw text", func(t *testing.T) {
e := g.Raw("<div/>")
equal(t, "<div/>", e.Render())
})
}
func equal(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("expected %v but got %v", expected, actual)
t.FailNow()
}
}
|