-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
757 lines (725 loc) · 18.8 KB
/
parser_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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
package netscape
import (
"encoding/xml"
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
func TestParse(t *testing.T) {
cases := []struct {
tname string
input string
want FileNode
wantErr error
}{
// nominal cases
{
tname: "flat document",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><A HREF="https://domain.tld">Test Domain</A>
<DT><A HREF="https://desc.domain.tld">Test Domain (with description)</A>
<DD>Look! A short description for this bookmark.
<DT><A HREF="https://emptydesc.domain.tld">Test Domain (with empty description)</A>
<DD>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Href: "https://domain.tld",
Title: "Test Domain",
},
{
Href: "https://desc.domain.tld",
Title: "Test Domain (with description)",
Description: "Look! A short description for this bookmark.",
},
{
Href: "https://emptydesc.domain.tld",
Title: "Test Domain (with empty description)",
},
},
},
},
},
{
tname: "empty document with UTF-8 BOM",
input: string(utf8bom) + `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
},
},
},
{
tname: "bookmark with attributes",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><A HREF="https://domain.tld" ADD_DATE="151637044" PRIVATE="1" TAGS="test tags">Test Domain</A>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Href: "https://domain.tld",
Title: "Test Domain",
Attributes: map[string]string{
"ADD_DATE": "151637044",
"PRIVATE": "1",
"TAGS": "test tags",
},
},
},
},
},
},
{
tname: "bookmark with empty description",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><A HREF="https://domain.tld">Test Domain</A>
<DD>
<DT><A HREF="https://domain.tld">Test Domain</A>
<DD>
<DT><A HREF="https://emptydesc.domain.tld">Test Domain (with empty description)</A>
<DD>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Href: "https://domain.tld",
Title: "Test Domain",
},
{
Href: "https://domain.tld",
Title: "Test Domain",
},
{
Href: "https://emptydesc.domain.tld",
Title: "Test Domain (with empty description)",
},
},
},
},
},
{
tname: "bookmark with multi-line description",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><A HREF="https://domain.tld">Test Domain</A>
<DD>Description:
- item 1
- item 1.1
- item 1.2
- item 2
- item 3
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Description: "Description:\n\n- item 1\n - item 1.1\n - item 1.2\n- item 2\n- item 3",
Href: "https://domain.tld",
Title: "Test Domain",
},
},
},
},
},
{
tname: "bookmark with description containing HTML markup",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><A HREF="https://domain.tld">Test Domain</A>
<DD>Markup:
<a href="http://localhost:8080"><img src="http://localhost:8080/splash.png"/></a>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Description: `Markup:
<a href="http://localhost:8080"><img src="http://localhost:8080/splash.png"/></a>`,
Href: "https://domain.tld",
Title: "Test Domain",
},
},
},
},
},
{
tname: "nested folders",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Level 0</H1>
<DL><p>
<DT><H3>Level 1A</H3>
<DL><p>
<DT><H3>Level 2A</H3>
<DL><p>
</DL><p>
</DL><p>
<DT><H3>Level 1B</H3>
<DL><p>
</DL><p>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Level 0",
Subfolders: []FolderNode{
{
Name: "Level 1A",
Subfolders: []FolderNode{
{Name: "Level 2A"},
},
},
{Name: "Level 1B"},
},
},
},
},
{
tname: "nested folders with bookmarks",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Level 0</H1>
<DL><p>
<DT><A HREF="https://l0.domain.tld">Level 0</A>
<DT><A HREF="https://l0.domain.tld">Level 0</A>
<DT><H3>Level 1A</H3>
<DL><p>
<DT><A HREF="https://l1a.domain.tld">Level 1A</A>
<DT><H3>Level 2A</H3>
<DL><p>
<DT><A HREF="https://l2a.domain.tld">Level 2A</A>
</DL><p>
</DL><p>
<DT><H3>Level 1B</H3>
<DL><p>
</DL><p>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Level 0",
Bookmarks: []BookmarkNode{
{Href: "https://l0.domain.tld", Title: "Level 0"},
{Href: "https://l0.domain.tld", Title: "Level 0"},
},
Subfolders: []FolderNode{
{
Name: "Level 1A",
Bookmarks: []BookmarkNode{
{Href: "https://l1a.domain.tld", Title: "Level 1A"},
},
Subfolders: []FolderNode{
{
Name: "Level 2A",
Bookmarks: []BookmarkNode{
{Href: "https://l2a.domain.tld", Title: "Level 2A"},
},
},
},
},
{Name: "Level 1B"},
},
},
},
},
{
tname: "nested folder with attributes",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 ADD_DATE="1460294955" LAST_MODIFIED="1460294956" PERSONAL_TOOLBAR_FOLDER="true">Personal toolbar</H3>
<DD>Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar
<DL><p>
</DL><p>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Subfolders: []FolderNode{
{
Name: "Personal toolbar",
Description: "Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar",
Attributes: map[string]string{
"ADD_DATE": "1460294955",
"LAST_MODIFIED": "1460294956",
"PERSONAL_TOOLBAR_FOLDER": "true",
},
},
},
},
},
},
{
tname: "nested folder with description and bookmarks",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Level 0</H1>
<DL><p>
<DT><H3>Level 1</H3>
<DD>Folder with description
<DL><p>
<DT><A HREF="https://domain.tld">Test Domain</A>
</DL><p>
</DL><p>
`,
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Level 0",
Subfolders: []FolderNode{
{
Name: "Level 1",
Description: "Folder with description",
Bookmarks: []BookmarkNode{
{
Href: "https://domain.tld",
Title: "Test Domain",
},
},
},
},
},
},
},
// error cases
{
tname: "empty document",
wantErr: ErrDoctypeMissing,
},
{
tname: "missing DOCTYPE",
input: `<!-- No DOCTYPE\n -->\n`,
wantErr: ErrDoctypeMissing,
},
{
tname: "invalid DOCTYPE",
input: `<!DOCTYPE dummy SYSTEM "dummy.dtd">`,
wantErr: ErrDoctypeInvalid,
},
{
tname: "incomplete TITLE",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks`,
wantErr: wrapWitParseError("failed to parse title", 35, &xml.SyntaxError{Msg: "unexpected EOF", Line: 2}),
},
{
tname: "incomplete H1",
input: `<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookma`,
wantErr: wrapWitParseError("failed to parse folder", 60, &xml.SyntaxError{Msg: "unexpected EOF", Line: 2}),
},
}
for _, tc := range cases {
t.Run(tc.tname, func(t *testing.T) {
r := strings.NewReader(tc.input)
got, err := Parse(r)
if tc.wantErr != nil {
if err == nil {
t.Error("expected an error, got none")
} else if !errors.Is(err, tc.wantErr) {
t.Errorf("want error %q, got %q", tc.wantErr, err)
}
return
}
if err != nil {
t.Fatalf("expected no error, got %q", err)
}
if got.Title != tc.want.Title {
t.Errorf("want title %q, got %q", tc.want.Title, got.Title)
}
assertFolderNodesEqual(t, got.Root, tc.want.Root)
})
}
}
func TestParseFile(t *testing.T) {
cases := []struct {
tname string
inputFilename string
want FileNode
}{
{
tname: "Netscape (basic)",
inputFilename: "netscape_basic.htm",
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Description: "Super-secret stuff you're not supposed to know about",
Href: "https://private.tld",
Title: "Secret stuff",
Attributes: map[string]string{
"ADD_DATE": "10/Oct/2000:13:55:36 +0300",
"PRIVATE": "1",
"TAGS": "private secret",
},
},
{
Href: "http://public.tld",
Title: "Public stuff",
Attributes: map[string]string{
"ADD_DATE": "1456433748",
"PRIVATE": "0",
"TAGS": "public hello world",
},
},
},
},
},
},
{
tname: "Netscape (extended markup)",
inputFilename: "netscape_extended.htm",
want: FileNode{
Title: "My local links",
Root: FolderNode{
Name: "Shaarli export of all bookmarks on Sat, 06 Jun 20 15:50:59 +0200",
Bookmarks: []BookmarkNode{
{
Description: `For 10 years, a rogue fishing vessel and its crew plundered the world’s oceans, escaping repeated attempts of capture. Then a dramatic pursuit finally netted the one that got away.
<a href="http://localhost.localdomain:8083/Shaarli/?JVvqCA"><img src="http://localhost.localdomain:8083/Shaarli/cache/thumb/290ccda0deea6083ee613d358446103e/c975558ad43acdbd982ffafd8c01163d6c9ec5ca125901.jpg"/></a>`,
Href: "https://www.bbc.com/future/article/20190213-the-dramatic-hunt-for-the-fish-pirates-exploiting-our-seas",
Title: "The hunt for the fish pirates who exploit the sea - BBC Future",
Attributes: map[string]string{
"ADD_DATE": "1591451445",
"PRIVATE": "1",
"TAGS": "story,oceans",
},
},
},
},
},
},
{
tname: "Netscape (multiline descriptions)",
inputFilename: "netscape_multiline.htm",
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Description: "List:\n- item1\n- item2\n- item3",
Href: "http://multi.li.ne/1",
Title: "Multiline desc",
Attributes: map[string]string{
"ADD_DATE": "1456433741",
"PRIVATE": "0",
"TAGS": "multi",
},
},
{
Description: "Nested lists:\n- list1\n - item1.1\n - item1.2\n - item1.3\n- list2\n - item2.1",
Href: "http://multi.li.ne/2",
Title: "Multiline desc",
Attributes: map[string]string{
"ADD_DATE": "1456433742",
"PRIVATE": "0",
"TAGS": "multi",
},
},
{
Description: "List:\n- item1\n- item2\n\nParagraph number one.\n\nParagraph\nnumber\ntwo.",
Href: "http://multi.li.ne/3",
Title: "Multiline desc",
Attributes: map[string]string{
"ADD_DATE": "1456433747",
"PRIVATE": "0",
"TAGS": "multi",
},
},
},
},
},
},
{
tname: "Netscape (nested)",
inputFilename: "netscape_nested.htm",
want: FileNode{
Title: "Bookmarks",
Root: FolderNode{
Name: "Bookmarks",
Bookmarks: []BookmarkNode{
{
Href: "http://nest.ed/1",
Title: "Nested 1",
Attributes: map[string]string{
"ADD_DATE": "1456433741",
"PRIVATE": "0",
"TAGS": "tag1,tag2, multi word",
},
},
{
Href: "http://nest.ed/2",
Title: "Nested 2",
Attributes: map[string]string{
"ADD_DATE": "1456733741",
"PRIVATE": "0",
"TAGS": "tag4",
},
},
},
Subfolders: []FolderNode{
{
Name: "Folder1, the first,folder to encounter",
Attributes: map[string]string{
"ADD_DATE": "1456433722",
"LAST_MODIFIED": "1456433739",
},
Bookmarks: []BookmarkNode{
{
Href: "http://nest.ed/1-1",
Title: "Nested 1-1",
Attributes: map[string]string{
"ADD_DATE": "1456433742",
"PRIVATE": "0",
"TAGS": "tag1,tag2,multi word",
},
},
{
Href: "http://nest.ed/1-2",
Title: "Nested 1-2",
Attributes: map[string]string{
"ADD_DATE": "1456433747",
"PRIVATE": "0",
"TAGS": "tag3,tag4, leaf multi word",
},
},
},
},
{
Name: "Folder2",
Description: "This second folder contains wonderful links!",
Attributes: map[string]string{
"ADD_DATE": "1456433722",
},
Bookmarks: []BookmarkNode{
{
Description: "First link of the second section",
Href: "http://nest.ed/2-1",
Title: "Nested 2-1",
Attributes: map[string]string{
"ADD_DATE": "1454433742",
"PRIVATE": "0",
},
},
{
Description: "Second link of the second section",
Href: "http://nest.ed/2-2",
Title: "Nested 2-2",
Attributes: map[string]string{
"ADD_DATE": "1453233747",
"PRIVATE": "0",
},
},
},
},
{
Name: "Folder3",
Subfolders: []FolderNode{
{
Name: "Folder3-1",
Bookmarks: []BookmarkNode{
{
Href: "http://nest.ed/3-1",
Title: "Nested 3-1",
Attributes: map[string]string{
"ADD_DATE": "1454433742",
"PRIVATE": "0",
"TAGS": "tag3",
},
},
{
Href: "http://nest.ed/3-2",
Title: "Nested 3-2",
Attributes: map[string]string{
"ADD_DATE": "1453233747",
"PRIVATE": "0",
},
},
},
},
},
},
},
},
},
},
{
tname: "Safari (folded)",
inputFilename: "safari_folded.htm",
want: FileNode{
Title: "Signets",
Root: FolderNode{
Name: "Signets",
Bookmarks: []BookmarkNode{},
Subfolders: []FolderNode{
{
Name: "Favoris",
Attributes: map[string]string{
"FOLDED": "FOLDED",
},
Bookmarks: []BookmarkNode{
{
Href: "https://github.com/",
Title: "GitHub",
},
},
},
{
Name: "Menu Signets",
Attributes: map[string]string{
"FOLDED": "FOLDED",
},
},
{
Name: "GitHub - Go",
Attributes: map[string]string{
"FOLDED": "FOLDED",
},
Bookmarks: []BookmarkNode{
{
Href: "https://github.com/golang/go",
Title: "golang/go: The Go programming language",
},
},
},
{
Name: "Misc",
Attributes: map[string]string{
"FOLDED": "FOLDED",
},
Subfolders: []FolderNode{
{
Name: "Wiki",
Attributes: map[string]string{
"FOLDED": "FOLDED",
},
Bookmarks: []BookmarkNode{
{
Href: "https://en.wikipedia.org/wiki/Main_Page",
Title: "Wikipedia, the free encyclopedia",
},
},
},
},
},
},
},
},
},
}
for _, tc := range cases {
t.Run(tc.tname, func(t *testing.T) {
file, err := os.Open(filepath.Join("testdata", tc.inputFilename))
if err != nil {
t.Fatalf("failed to open input file %s: %s", tc.inputFilename, err)
}
defer file.Close()
got, err := Parse(file)
if err != nil {
t.Fatalf("expected no error, got %q", err)
}
if got.Title != tc.want.Title {
t.Errorf("want title %q, got %q", tc.want.Title, got.Title)
}
assertFolderNodesEqual(t, got.Root, tc.want.Root)
})
}
}
func assertFolderNodesEqual(t *testing.T, got FolderNode, want FolderNode) {
t.Helper()
if got.Name != want.Name {
t.Errorf("want folder name %q, got %q", want.Name, got.Name)
}
if got.Description != want.Description {
t.Errorf("want folder description %q, got %q", want.Description, got.Description)
}
if len(got.Attributes) != len(want.Attributes) {
t.Fatalf("want %d attributes for folder %q, got %d", len(want.Attributes), want.Name, len(got.Attributes))
}
for name, wantValue := range want.Attributes {
if got.Attributes[name] != wantValue {
t.Errorf("want folder %q attribute %q value %q, got %q", want.Name, name, wantValue, got.Attributes[name])
}
}
if len(got.Bookmarks) != len(want.Bookmarks) {
t.Fatalf("want %d bookmarks in folder %q, got %d", len(want.Bookmarks), want.Name, len(got.Bookmarks))
}
for index, wantBookmark := range want.Bookmarks {
if got.Bookmarks[index].Description != wantBookmark.Description {
t.Errorf("want bookmark %d description %q, got %q", index, wantBookmark.Description, got.Bookmarks[index].Description)
}
if got.Bookmarks[index].Href != wantBookmark.Href {
t.Errorf("want bookmark %d href %q, got %q", index, wantBookmark.Href, got.Bookmarks[index].Href)
}
if got.Bookmarks[index].Title != wantBookmark.Title {
t.Errorf("want bookmark %d title %q, got %q", index, wantBookmark.Title, got.Bookmarks[index].Title)
}
if len(got.Bookmarks[index].Attributes) != len(wantBookmark.Attributes) {
t.Errorf("want %d attributes for bookmark %d, got %d", len(wantBookmark.Attributes), index, len(got.Bookmarks[index].Attributes))
}
for name, wantValue := range wantBookmark.Attributes {
if got.Bookmarks[index].Attributes[name] != wantValue {
t.Errorf("want bookmark %d attribute %q value %q, got %q", index, name, wantValue, got.Bookmarks[index].Attributes[name])
}
}
}
if len(got.Subfolders) != len(want.Subfolders) {
t.Fatalf("want %d subfolders for folder %q, got %d", len(want.Subfolders), want.Name, len(got.Subfolders))
}
for index, wantSubfolder := range want.Subfolders {
assertFolderNodesEqual(t, got.Subfolders[index], wantSubfolder)
}
}