forked from chasefleming/elem-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elements_test.go
319 lines (261 loc) · 9.95 KB
/
elements_test.go
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package elem
import (
"testing"
"github.com/chasefleming/elem-go/attrs"
"github.com/stretchr/testify/assert"
)
// ========== Document Structure ==========
func TestBody(t *testing.T) {
expected := `<body class="page-body"><p>Welcome to Elem!</p></body>`
el := Body(Attrs{attrs.Class: "page-body"}, P(nil, Text("Welcome to Elem!")))
assert.Equal(t, expected, el.Render())
}
func TestHead(t *testing.T) {
// ... [Code for the Head test here]
}
func TestHtml(t *testing.T) {
expected := `<html lang="en"><head><meta charset="UTF-8"><title>Elem Page</title></head><body><p>Welcome to Elem!</p></body></html>`
el := Html(Attrs{attrs.Lang: "en"},
Head(nil,
Meta(Attrs{attrs.Charset: "UTF-8"}),
Title(nil, Text("Elem Page")),
),
Body(nil, P(nil, Text("Welcome to Elem!"))),
)
assert.Equal(t, expected, el.Render())
}
// ========== Text Formatting and Structure ==========
func TestA(t *testing.T) {
expected := `<a href="https://example.com">Visit Example</a>`
el := A(Attrs{attrs.Href: "https://example.com"}, Text("Visit Example"))
assert.Equal(t, expected, el.Render())
}
func TestBlockquote(t *testing.T) {
expected := `<blockquote>Quote text</blockquote>`
el := Blockquote(nil, Text("Quote text"))
assert.Equal(t, expected, el.Render())
}
func TestBr(t *testing.T) {
expected := `<br>`
el := Br(nil)
assert.Equal(t, expected, el.Render())
}
func TestCode(t *testing.T) {
expected := `<code>Code snippet</code>`
el := Code(nil, Text("Code snippet"))
assert.Equal(t, expected, el.Render())
}
func TestDiv(t *testing.T) {
expected := `<div class="container">Hello, Elem!</div>`
el := Div(Attrs{attrs.Class: "container"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestEm(t *testing.T) {
expected := `<em>Italic text</em>`
el := Em(nil, Text("Italic text"))
assert.Equal(t, expected, el.Render())
}
func TestH1(t *testing.T) {
expected := `<h1 class="title">Hello, Elem!</h1>`
el := H1(Attrs{attrs.Class: "title"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestH2(t *testing.T) {
expected := `<h2 class="subtitle">Hello, Elem!</h2>`
el := H2(Attrs{attrs.Class: "subtitle"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestH3(t *testing.T) {
expected := `<h3>Hello, Elem!</h3>`
el := H3(nil, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestHr(t *testing.T) {
expected := `<hr>`
el := Hr(nil)
assert.Equal(t, expected, el.Render())
}
func TestI(t *testing.T) {
expected1 := `<i>Idiomatic Text</i>`
expected2 := `<i class="fa-regular fa-face-smile"></i>`
el := I(nil, Text("Idiomatic Text"))
assert.Equal(t, expected1, el.Render())
el = I(Attrs{attrs.Class: "fa-regular fa-face-smile"})
assert.Equal(t, expected2, el.Render())
}
func TestP(t *testing.T) {
expected := `<p>Hello, Elem!</p>`
el := P(nil, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestPre(t *testing.T) {
expected := `<pre>Preformatted text</pre>`
el := Pre(nil, Text("Preformatted text"))
assert.Equal(t, expected, el.Render())
}
func TestSpan(t *testing.T) {
expected := `<span class="highlight">Hello, Elem!</span>`
el := Span(Attrs{attrs.Class: "highlight"}, Text("Hello, Elem!"))
assert.Equal(t, expected, el.Render())
}
func TestStrong(t *testing.T) {
expected := `<strong>Bold text</strong>`
el := Strong(nil, Text("Bold text"))
assert.Equal(t, expected, el.Render())
}
// ========== Lists ==========
func TestLi(t *testing.T) {
expected := `<li>Item 1</li>`
el := Li(nil, Text("Item 1"))
assert.Equal(t, expected, el.Render())
}
func TestUl(t *testing.T) {
expected := `<ul><li>Item 1</li><li>Item 2</li></ul>`
el := Ul(nil, Li(nil, Text("Item 1")), Li(nil, Text("Item 2")))
assert.Equal(t, expected, el.Render())
}
func TestOl(t *testing.T) {
expected := `<ol><li>Item 1</li><li>Item 2</li></ol>`
el := Ol(nil, Li(nil, Text("Item 1")), Li(nil, Text("Item 2")))
assert.Equal(t, expected, el.Render())
}
func TestDl(t *testing.T) {
expected := `<dl><dt>Term 1</dt><dd>Description 1</dd><dt>Term 2</dt><dd>Description 2</dd></dl>`
el := Dl(nil, Dt(nil, Text("Term 1")), Dd(nil, Text("Description 1")), Dt(nil, Text("Term 2")), Dd(nil, Text("Description 2")))
assert.Equal(t, expected, el.Render())
}
func TestDt(t *testing.T) {
expected := `<dt>Term 1</dt>`
el := Dt(nil, Text("Term 1"))
assert.Equal(t, expected, el.Render())
}
func TestDd(t *testing.T) {
expected := `<dd>Description 1</dd>`
el := Dd(nil, Text("Description 1"))
assert.Equal(t, expected, el.Render())
}
// ========== Forms ==========
func TestButton(t *testing.T) {
expected := `<button class="btn">Click Me</button>`
el := Button(Attrs{attrs.Class: "btn"}, Text("Click Me"))
assert.Equal(t, expected, el.Render())
}
func TestForm(t *testing.T) {
expected := `<form action="/submit" method="post"><input name="username" type="text"></form>`
el := Form(Attrs{attrs.Action: "/submit", attrs.Method: "post"}, Input(Attrs{attrs.Type: "text", attrs.Name: "username"}))
assert.Equal(t, expected, el.Render())
}
func TestInput(t *testing.T) {
expected := `<input name="username" placeholder="Enter your username" type="text">`
el := Input(Attrs{attrs.Type: "text", attrs.Name: "username", attrs.Placeholder: "Enter your username"})
assert.Equal(t, expected, el.Render())
}
func TestLabel(t *testing.T) {
expected := `<label for="username">Username</label>`
el := Label(Attrs{attrs.For: "username"}, Text("Username"))
assert.Equal(t, expected, el.Render())
}
func TestSelectAndOption(t *testing.T) {
expected := `<select name="color"><option value="red">Red</option><option value="blue">Blue</option></select>`
el := Select(Attrs{attrs.Name: "color"}, Option(Attrs{attrs.Value: "red"}, Text("Red")), Option(Attrs{attrs.Value: "blue"}, Text("Blue")))
assert.Equal(t, expected, el.Render())
}
func TestTextarea(t *testing.T) {
expected := `<textarea name="comment" rows="5">Leave a comment...</textarea>`
el := Textarea(Attrs{attrs.Name: "comment", attrs.Rows: "5"}, Text("Leave a comment..."))
assert.Equal(t, expected, el.Render())
}
// ========== Hyperlinks and Multimedia ==========
func TestImg(t *testing.T) {
expected := `<img alt="An image" src="image.jpg">`
el := Img(Attrs{attrs.Src: "image.jpg", attrs.Alt: "An image"})
assert.Equal(t, expected, el.Render())
}
// ========== Meta Elements ==========
func TestLink(t *testing.T) {
expected := `<link href="https://example.com/styles.css" rel="stylesheet">`
el := Link(Attrs{attrs.Rel: "stylesheet", attrs.Href: "https://example.com/styles.css"})
assert.Equal(t, expected, el.Render())
}
func TestMeta(t *testing.T) {
expected := `<meta charset="UTF-8">`
el := Meta(Attrs{attrs.Charset: "UTF-8"})
assert.Equal(t, expected, el.Render())
}
func TestScript(t *testing.T) {
expected := `<script src="https://example.com/script.js"></script>`
el := Script(Attrs{attrs.Src: "https://example.com/script.js"})
assert.Equal(t, expected, el.Render())
}
// ========== Semantic Elements ==========
func TestArticle(t *testing.T) {
expected := `<article><h2>Article Title</h2><p>Article content.</p></article>`
el := Article(nil, H2(nil, Text("Article Title")), P(nil, Text("Article content.")))
assert.Equal(t, expected, el.Render())
}
func TestAside(t *testing.T) {
expected := `<aside><p>Sidebar content.</p></aside>`
el := Aside(nil, P(nil, Text("Sidebar content.")))
assert.Equal(t, expected, el.Render())
}
func TestFooter(t *testing.T) {
expected := `<footer><p>Footer content.</p></footer>`
el := Footer(nil, P(nil, Text("Footer content.")))
assert.Equal(t, expected, el.Render())
}
func TestHeader(t *testing.T) {
expected := `<header class="site-header"><h1>Welcome to Elem!</h1></header>`
el := Header(Attrs{attrs.Class: "site-header"}, H1(nil, Text("Welcome to Elem!")))
assert.Equal(t, expected, el.Render())
}
func TestMainElem(t *testing.T) {
expected := `<main><p>Main content goes here.</p></main>`
el := Main(nil, P(nil, Text("Main content goes here.")))
assert.Equal(t, expected, el.Render())
}
func TestNav(t *testing.T) {
expected := `<nav><a href="/home">Home</a><a href="/about">About</a></nav>`
el := Nav(nil, A(Attrs{attrs.Href: "/home"}, Text("Home")), A(Attrs{attrs.Href: "/about"}, Text("About")))
assert.Equal(t, expected, el.Render())
}
func TestSection(t *testing.T) {
expected := `<section><h3>Section Title</h3><p>Section content.</p></section>`
el := Section(nil, H3(nil, Text("Section Title")), P(nil, Text("Section content.")))
assert.Equal(t, expected, el.Render())
}
// ========== Tables ==========
func TestTr(t *testing.T) {
expected := `<tr>Row content.</tr>`
el := Tr(nil, Text("Row content."))
assert.Equal(t, expected, el.Render())
}
func TestTd(t *testing.T) {
expected := `<tr><td><h1>Cell one.</h1></td><td>Cell two.</td></tr>`
el := Tr(nil, Td(nil, H1(nil, Text("Cell one."))), Td(nil, Text("Cell two.")))
assert.Equal(t, expected, el.Render())
}
func TestTh(t *testing.T) {
expected := `<tr><th>First name</th><th>Last name</th><th>Age</th></tr>`
el := Tr(nil, Th(nil, Text("First name")), Th(nil, Text("Last name")), Th(nil, Text("Age")))
assert.Equal(t, expected, el.Render())
}
func TestTHead(t *testing.T) {
expected := `<thead><tr><td>Text</td><td><a href="/link">Link</a></td></tr></thead>`
el := THead(nil, Tr(nil, Td(nil, Text("Text")), Td(nil, A(Attrs{attrs.Href: "/link"}, Text("Link")))))
assert.Equal(t, expected, el.Render())
}
func TestTBody(t *testing.T) {
expected := `<tbody><tr><td>Table body</td></tr></tbody>`
el := TBody(nil, Tr(nil, Td(nil, Text("Table body"))))
assert.Equal(t, expected, el.Render())
}
func TestTFoot(t *testing.T) {
expected := `<tfoot><tr><td><a href="/footer">Table footer</a></td></tr></tfoot>`
el := TFoot(nil, Tr(nil, Td(nil, A(Attrs{attrs.Href: "/footer"}, Text("Table footer")))))
assert.Equal(t, expected, el.Render())
}
func TestTable(t *testing.T) {
expected := `<table><tr><th>Table header</th></tr><tr><td>Table content</td></tr></table>`
el := Table(nil, Tr(nil, Th(nil, Text("Table header"))), Tr(nil, Td(nil, Text("Table content"))))
assert.Equal(t, expected, el.Render())
}