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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
package html_test
import (
"errors"
"fmt"
"testing"
g "github.com/maragudk/gomponents"
"github.com/maragudk/gomponents/assert"
. "github.com/maragudk/gomponents/html"
)
type erroringWriter struct{}
func (w *erroringWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("don't want to write")
}
func TestDocument(t *testing.T) {
t.Run("returns doctype and children", func(t *testing.T) {
assert.Equal(t, `<!doctype html><html></html>`, Document(g.El("html")))
})
t.Run("errors on write error in Render", func(t *testing.T) {
err := Document(g.El("html")).Render(&erroringWriter{})
assert.Error(t, err)
})
}
func TestFormEl(t *testing.T) {
t.Run("returns a form element with action and method attributes", func(t *testing.T) {
assert.Equal(t, `<form action="/" method="post"></form>`, FormEl("/", "post"))
})
}
func TestInput(t *testing.T) {
t.Run("returns an input element with attributes type and name", func(t *testing.T) {
assert.Equal(t, `<input type="text" name="hat">`, Input("text", "hat"))
})
}
func TestLabel(t *testing.T) {
t.Run("returns a label element with attribute for", func(t *testing.T) {
assert.Equal(t, `<label for="hat">Hat</label>`, Label("hat", g.Text("Hat")))
})
}
func TestOption(t *testing.T) {
t.Run("returns an option element with attribute label and content", func(t *testing.T) {
assert.Equal(t, `<option value="hat">Hat</option>`, Option("Hat", "hat"))
})
}
func TestProgress(t *testing.T) {
t.Run("returns a progress element with attributes value and max", func(t *testing.T) {
assert.Equal(t, `<progress value="5.5" max="10"></progress>`, Progress(5.5, 10))
})
}
func TestSelect(t *testing.T) {
t.Run("returns a select element with attribute name", func(t *testing.T) {
assert.Equal(t, `<select name="hat"><option value="partyhat">Partyhat</option></select>`,
Select("hat", Option("Partyhat", "partyhat")))
})
}
func TestTextarea(t *testing.T) {
t.Run("returns a textarea element with attribute name", func(t *testing.T) {
assert.Equal(t, `<textarea name="hat"></textarea>`, Textarea("hat"))
})
}
func TestA(t *testing.T) {
t.Run("returns an a element with a href attribute", func(t *testing.T) {
assert.Equal(t, `<a href="#">hat</a>`, A("#", g.Text("hat")))
})
}
func TestImg(t *testing.T) {
t.Run("returns an img element with href and alt attributes", func(t *testing.T) {
assert.Equal(t, `<img src="hat.png" alt="hat" id="image">`, Img("hat.png", "hat", g.Attr("id", "image")))
})
}
func TestSimpleElements(t *testing.T) {
cases := map[string]func(...g.Node) g.NodeFunc{
"address": Address,
"article": Article,
"aside": Aside,
"audio": Audio,
"blockquote": BlockQuote,
"body": Body,
"button": Button,
"canvas": Canvas,
"cite": Cite,
"code": Code,
"colgroup": ColGroup,
"data": Data,
"datalist": DataList,
"details": Details,
"dialog": Dialog,
"div": Div,
"dl": Dl,
"fieldset": FieldSet,
"figure": Figure,
"footer": Footer,
"head": Head,
"header": Header,
"hgroup": HGroup,
"html": HTML,
"iframe": IFrame,
"legend": Legend,
"li": Li,
"main": Main,
"menu": Menu,
"meter": Meter,
"nav": Nav,
"noscript": NoScript,
"object": Object,
"ol": Ol,
"optgroup": OptGroup,
"p": P,
"picture": Picture,
"pre": Pre,
"script": Script,
"section": Section,
"span": Span,
"style": StyleEl,
"summary": Summary,
"svg": SVG,
"table": Table,
"tbody": TBody,
"td": Td,
"tfoot": TFoot,
"th": Th,
"thead": THead,
"tr": Tr,
"ul": Ul,
}
for name, fn := range cases {
t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
n := fn(g.Attr("id", "hat"))
assert.Equal(t, fmt.Sprintf(`<%v id="hat"></%v>`, name, name), n)
})
}
}
func TestSimpleVoidKindElements(t *testing.T) {
cases := map[string]func(...g.Node) g.NodeFunc{
"area": Area,
"base": Base,
"br": Br,
"col": Col,
"embed": Embed,
"hr": Hr,
"link": Link,
"meta": Meta,
"param": Param,
"source": Source,
"wbr": Wbr,
}
for name, fn := range cases {
t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
n := fn(g.Attr("id", "hat"))
assert.Equal(t, fmt.Sprintf(`<%v id="hat">`, name), n)
})
}
}
func TestTextElements(t *testing.T) {
cases := map[string]func(string, ...g.Node) g.NodeFunc{
"abbr": Abbr,
"b": B,
"caption": Caption,
"dd": Dd,
"del": Del,
"dfn": Dfn,
"dt": Dt,
"em": Em,
"figcaption": FigCaption,
"h1": H1,
"h2": H2,
"h3": H3,
"h4": H4,
"h5": H5,
"h6": H6,
"i": I,
"ins": Ins,
"kbd": Kbd,
"mark": Mark,
"q": Q,
"s": S,
"samp": Samp,
"small": Small,
"strong": Strong,
"sub": Sub,
"sup": Sup,
"time": Time,
"title": TitleEl,
"u": U,
"var": Var,
}
for name, fn := range cases {
t.Run(fmt.Sprintf("should output %v", name), func(t *testing.T) {
n := fn("hat", g.Attr("id", "hat"))
assert.Equal(t, fmt.Sprintf(`<%v id="hat">hat</%v>`, name, name), n)
})
}
}
|