forked from albrow/vdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
587 lines (579 loc) · 16.8 KB
/
parse_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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package vdom
import (
"fmt"
"testing"
)
// TestParse tests the tree returned from the Parse function for various different
// inputs.
func TestParse(t *testing.T) {
// We'll use table-driven testing here.
testCases := []struct {
// A human-readable name describing this test case
name string
// The src html to be parsed
src []byte
// The expected tree to be returned from the Parse function
expectedTree *Tree
}{
{
name: "Element root",
src: []byte("<div></div>"),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "div",
},
},
},
},
{
name: "Text root",
src: []byte("Hello"),
expectedTree: &Tree{
Children: []Node{
&Text{
Value: []byte("Hello"),
},
},
},
},
{
name: "Comment root",
src: []byte("<!--comment-->"),
expectedTree: &Tree{
Children: []Node{
&Comment{
Value: []byte("comment"),
},
},
},
},
{
name: "ul with nested li's",
src: []byte("<ul><li>one</li><li>two</li><li>three</li></ul>"),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "ul",
children: []Node{
&Element{
Name: "li",
children: []Node{
&Text{
Value: []byte("one"),
},
},
},
&Element{
Name: "li",
children: []Node{
&Text{
Value: []byte("two"),
},
},
},
&Element{
Name: "li",
children: []Node{
&Text{
Value: []byte("three"),
},
},
},
},
},
},
},
},
{
name: "Element with attrs",
src: []byte(`<div class="container" id="main" data-custom-attr="foo"></div>`),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "div",
Attrs: []Attr{
{Name: "class", Value: "container"},
{Name: "id", Value: "main"},
{Name: "data-custom-attr", Value: "foo"},
},
},
},
},
},
{
name: "Script tag with escaped characters",
src: []byte(`<script type="text/javascript">function((){console.log("<Hello brackets>")})()</script>`),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "script",
Attrs: []Attr{
{Name: "type", Value: "text/javascript"},
},
children: []Node{
&Text{
Value: []byte(`function((){console.log("<Hello brackets>")})()`),
},
},
},
},
},
},
{
name: "Form with autoclosed tags",
src: []byte(`<form method="post"><input type="text" name="firstName"><input type="text" name="lastName"></form>`),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "form",
Attrs: []Attr{
{Name: "method", Value: "post"},
},
children: []Node{
&Element{
Name: "input",
Attrs: []Attr{
{Name: "type", Value: "text"},
{Name: "name", Value: "firstName"},
},
},
&Element{
Name: "input",
Attrs: []Attr{
{Name: "type", Value: "text"},
{Name: "name", Value: "lastName"},
},
},
},
},
},
},
},
{
name: "Multiple Children",
src: []byte("<div></div>Hello<!--comment-->"),
expectedTree: &Tree{
Children: []Node{
&Element{
Name: "div",
},
&Text{
Value: []byte("Hello"),
},
&Comment{
Value: []byte("comment"),
},
},
},
},
}
// Iterate through each test case
for i, tc := range testCases {
// Parse the input from tc.src
gotTree, err := Parse(tc.src)
if err != nil {
t.Errorf("Unexpected error in Parse: %s", err.Error())
}
// Check that the resulting tree matches what we expect
if match, msg := tc.expectedTree.Compare(gotTree, true); !match {
t.Errorf("Error in test case %d (%s): HTML was not parsed correctly.\n%s", i, tc.name, msg)
}
}
}
// TestHTML tests the HTML method for each node in a parsed tree for various different
// inputs.
func TestHTML(t *testing.T) {
// We'll use table-driven testing here.
testCases := []struct {
// A human-readable name describing this test case
name string
// The src html to be parsed
src []byte
// A function which should check the results of the HTML method of each
// node in the parsed tree, and return an error if any results are incorrect.
testFunc func(*Tree) error
}{
{
name: "Element root",
src: []byte("<div></div>"),
testFunc: func(tree *Tree) error {
expectedHTML := []byte("<div></div>")
return expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root element")
},
},
{
name: "Text root",
src: []byte("Hello"),
testFunc: func(tree *Tree) error {
expectedHTML := []byte("Hello")
return expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root text node")
},
},
{
name: "Comment root",
src: []byte("<!--comment-->"),
testFunc: func(tree *Tree) error {
expectedHTML := []byte("<!--comment-->")
return expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root comment node")
},
},
{
name: "ul with nested li's",
src: []byte("<ul><li>one</li><li>two</li><li>three</li></ul>"),
testFunc: func(tree *Tree) error {
{
// Test the root of the tree, the ul element
expectedHTML := []byte("<ul><li>one</li><li>two</li><li>three</li></ul>")
if err := expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "the root ul element"); err != nil {
return err
}
}
lis := tree.Children[0].Children()
{
// Test each li element
expectedHTML := [][]byte{
[]byte("<li>one</li>"),
[]byte("<li>two</li>"),
[]byte("<li>three</li>"),
}
for i, li := range lis {
desc := fmt.Sprintf("li element %d", i)
if err := expectHTMLEquals(expectedHTML[i], li.HTML(), desc); err != nil {
return err
}
}
}
{
// Test each text node inside the li elements
expectedHTML := [][]byte{
[]byte("one"),
[]byte("two"),
[]byte("three"),
}
for i, li := range lis {
gotHTML := li.Children()[0].HTML()
desc := fmt.Sprintf("the text inside li element %d", i)
if err := expectHTMLEquals(expectedHTML[i], gotHTML, desc); err != nil {
return err
}
}
}
return nil
},
},
{
name: "Element with attrs",
src: []byte(`<div class="container" id="main" data-custom-attr="foo"></div>`),
testFunc: func(tree *Tree) error {
expectedHTML := []byte(`<div class="container" id="main" data-custom-attr="foo"></div>`)
return expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root element")
},
},
{
name: "Script tag with escaped characters",
src: []byte(`<script type="text/javascript">function((){console.log("<Hello brackets>")})()</script>`),
testFunc: func(tree *Tree) error {
{
// Test the root element
expectedHTML := []byte(`<script type="text/javascript">function((){console.log("<Hello brackets>")})()</script>`)
if err := expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root script element"); err != nil {
return err
}
}
{
// Test the text node inside the root element
expectedHTML := []byte(`function((){console.log("<Hello brackets>")})()`)
if err := expectHTMLEquals(expectedHTML, tree.Children[0].Children()[0].HTML(), "text node inside script element"); err != nil {
return err
}
}
return nil
},
},
{
name: "Form with autoclosed tags",
src: []byte(`<form method="post"><input type="text" name="firstName"><input type="text" name="lastName"></form>`),
testFunc: func(tree *Tree) error {
{
// Test the root element
expectedHTML := []byte(`<form method="post"><input type="text" name="firstName"><input type="text" name="lastName"></form>`)
if err := expectHTMLEquals(expectedHTML, tree.Children[0].HTML(), "root script element"); err != nil {
return err
}
}
{
inputs := tree.Children[0].Children()
// Test each child input element
expectedHTML := [][]byte{
[]byte(`<input type="text" name="firstName">`),
[]byte(`<input type="text" name="lastName">`),
}
for i, input := range inputs {
desc := fmt.Sprintf("input element %d", i)
if err := expectHTMLEquals(expectedHTML[i], input.HTML(), desc); err != nil {
return err
}
}
}
return nil
},
},
{
name: "Multiple Children",
src: []byte("<div></div>Hello<!--comment-->"),
testFunc: func(tree *Tree) error {
expectedHTML := [][]byte{
[]byte(`<div></div>`),
[]byte(`Hello`),
[]byte(`<!--comment-->`),
}
for i, root := range tree.Children {
desc := fmt.Sprintf("root node %d of type %T", i, root)
if err := expectHTMLEquals(expectedHTML[i], root.HTML(), desc); err != nil {
return err
}
}
return nil
},
},
}
// Iterate through each test case
for i, tc := range testCases {
// Parse the input from tc.src
gotTree, err := Parse(tc.src)
if err != nil {
t.Errorf("Unexpected error in Parse: %s", err.Error())
}
// Use the testFunc to test for certain conditions
if err := tc.testFunc(gotTree); err != nil {
t.Errorf("Error in test case %d (%s):\n%s", i, tc.name, err.Error())
}
}
}
// expectHTMLEquals returns an error if expected does not equal got. description should be
// a human-readable description of the element that was tested.
func expectHTMLEquals(expected []byte, got []byte, description string) error {
if string(expected) != string(got) {
return fmt.Errorf("HTML for %s was not correct.\n\tExpected: %s\n\tBut got: %s", description, string(expected), string(got))
}
return nil
}
// TestInnerHTML tests the InnerHTML method for each element in a parsed tree for various different
// inputs.
func TestInnerHTML(t *testing.T) {
// We'll use table-driven testing here.
testCases := []struct {
// A human-readable name describing this test case
name string
// The src html to be parsed
src []byte
// A function which should check the results of the InnerHTML method of each
// node in the parsed tree, and return an error if any results are incorrect.
testFunc func(*Tree) error
}{
{
name: "Element root",
src: []byte("<div></div>"),
testFunc: func(tree *Tree) error {
expectedInner := []byte("")
el := tree.Children[0].(*Element)
return expectInnerHTMLEquals(expectedInner, el.InnerHTML(), "root element")
},
},
{
name: "ul with nested li's",
src: []byte("<ul><li>one</li><li>two</li><li>three</li></ul>"),
testFunc: func(tree *Tree) error {
{
// Test the root of the tree, the ul element
expectedInner := []byte("<li>one</li><li>two</li><li>three</li>")
el := tree.Children[0].(*Element)
if err := expectInnerHTMLEquals(expectedInner, el.InnerHTML(), "the root ul element"); err != nil {
return err
}
}
lis := tree.Children[0].Children()
{
// Test each li element
expectedInner := [][]byte{
[]byte("one"),
[]byte("two"),
[]byte("three"),
}
for i, li := range lis {
el := li.(*Element)
desc := fmt.Sprintf("li element %d", i)
if err := expectInnerHTMLEquals(expectedInner[i], el.InnerHTML(), desc); err != nil {
return err
}
}
}
return nil
},
},
{
name: "Inner element with attrs",
src: []byte(`<div><div class="container" id="main" data-custom-attr="foo"></div></div>`),
testFunc: func(tree *Tree) error {
expectedInner := []byte(`<div class="container" id="main" data-custom-attr="foo"></div>`)
el := tree.Children[0].(*Element)
return expectInnerHTMLEquals(expectedInner, el.InnerHTML(), "root element")
},
},
{
name: "Script tag with escaped characters",
src: []byte(`<script type="text/javascript">function((){console.log("<Hello brackets>")})()</script>`),
testFunc: func(tree *Tree) error {
expectedInner := []byte(`function((){console.log("<Hello brackets>")})()`)
el := tree.Children[0].(*Element)
if err := expectInnerHTMLEquals(expectedInner, el.InnerHTML(), "root script element"); err != nil {
return err
}
return nil
},
},
{
name: "Form with autoclosed tags",
src: []byte(`<form method="post"><input type="text" name="firstName"><input type="text" name="lastName"></form>`),
testFunc: func(tree *Tree) error {
{
// Test the root element
expectedInner := []byte(`<input type="text" name="firstName"><input type="text" name="lastName">`)
el := tree.Children[0].(*Element)
if err := expectInnerHTMLEquals(expectedInner, el.InnerHTML(), "root script element"); err != nil {
return err
}
}
{
inputs := tree.Children[0].Children()
// Test each child input element
for i, input := range inputs {
el := input.(*Element)
desc := fmt.Sprintf("input element %d", i)
if err := expectInnerHTMLEquals([]byte{}, el.InnerHTML(), desc); err != nil {
return err
}
}
}
return nil
},
},
}
// Iterate through each test case
for i, tc := range testCases {
// Parse the input from tc.src
gotTree, err := Parse(tc.src)
if err != nil {
t.Errorf("Unexpected error in Parse: %s", err.Error())
}
// Use the testFunc to test for certain conditions
if err := tc.testFunc(gotTree); err != nil {
t.Errorf("Error in test case %d (%s):\n%s", i, tc.name, err.Error())
}
}
}
// expectInnerHTMLEquals returns an error if expected does not equal got. description should be
// a human-readable description of the element that was tested.
func expectInnerHTMLEquals(expected []byte, got []byte, description string) error {
if string(expected) != string(got) {
return fmt.Errorf("InnerHTML for %s was not correct.\n\tExpected: %s\n\tBut got: %s", description, string(expected), string(got))
}
return nil
}
// TestSelector tests the Selector method for each element in the virtual tree for various
// different inputs.
func TestSelector(t *testing.T) {
// We'll use table-driven testing here.
testCases := []struct {
// A human-readable name describing this test case
name string
// The src html to be parsed
src []byte
// A function which should check the results of the Selector method of each
// element in the parsed tree, and return an error if any results are incorrect.
testFunc func(*Tree) error
}{
{
name: "Element root",
src: []byte("<div></div>"),
testFunc: func(tree *Tree) error {
el := tree.Children[0].(*Element)
return expectSelectorEquals(el, "*:nth-child(1)", "root element")
},
},
{
name: "ul with nested li's",
src: []byte("<ul><li>one</li><li>two</li><li>three</li></ul>"),
testFunc: func(tree *Tree) error {
{
// Test the root of the tree, the ul element
el := tree.Children[0].(*Element)
if err := expectSelectorEquals(el, "*:nth-child(1)", "the root ul element"); err != nil {
return err
}
}
lis := tree.Children[0].Children()
{
// Test each child li element
for i, li := range lis {
el := li.(*Element)
expected := fmt.Sprintf("*:nth-child(1) > *:nth-child(%d)", i+1)
desc := fmt.Sprintf("li element %d", i)
if err := expectSelectorEquals(el, expected, desc); err != nil {
return err
}
}
}
return nil
},
},
{
name: "Form with autoclosed tags",
src: []byte(`<form method="post"><input type="text" name="firstName"><input type="text" name="lastName"></form>`),
testFunc: func(tree *Tree) error {
{
// Test the root element
el := tree.Children[0].(*Element)
if err := expectSelectorEquals(el, "*:nth-child(1)", "the root ul element"); err != nil {
return err
}
}
{
inputs := tree.Children[0].Children()
// Test each child input element
for i, input := range inputs {
el := input.(*Element)
expected := fmt.Sprintf("*:nth-child(1) > *:nth-child(%d)", i+1)
desc := fmt.Sprintf("input element %d", i)
if err := expectSelectorEquals(el, expected, desc); err != nil {
return err
}
}
}
return nil
},
},
}
// Iterate through each test case
for i, tc := range testCases {
// Parse the input from tc.src
gotTree, err := Parse(tc.src)
if err != nil {
t.Errorf("Unexpected error in Parse: %s", err.Error())
}
// Use the testFunc to test for certain conditions
if err := tc.testFunc(gotTree); err != nil {
t.Errorf("Error in test case %d (%s):\n%s", i, tc.name, err.Error())
}
}
}
// expectSelectorEquals returns an error if el.Selector() does not equal expected. description
// should be a human-readable description of the element that was tested, e.g. "the root ul element"
func expectSelectorEquals(el *Element, expected, description string) error {
got := el.Selector()
if expected != got {
return fmt.Errorf("Selector for %s was not correct. Expected `%s` but got `%s`", description, expected, got)
}
return nil
}