-
Notifications
You must be signed in to change notification settings - Fork 6
/
backcompat_test.go
185 lines (164 loc) · 4.57 KB
/
backcompat_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
// Copyright (c) The microformats project authors.
// SPDX-License-Identifier: MIT
package microformats
import (
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
)
func Test_BackcompatRootClasses(t *testing.T) {
tests := []struct {
classes, want []string
}{
{nil, nil},
{[]string{""}, nil},
{[]string{"foo"}, nil},
{[]string{"adr"}, []string{"h-adr"}},
{[]string{"adr", "foo"}, []string{"h-adr"}},
{[]string{"adr", "vcard"}, []string{"h-adr", "h-card"}},
}
for _, tt := range tests {
got := backcompatRootClasses(tt.classes, nil)
if want := tt.want; !cmp.Equal(got, want) {
t.Errorf("backcompatRootClasses(%q) returned %q, want %q)", tt.classes, got, want)
}
}
}
func Test_BackcompatPropertyClasses(t *testing.T) {
tests := []struct {
classes []string
rels []string
context []string // microformat type that property appears in
want []string
}{
{nil, nil, nil, nil},
{[]string{""}, nil, nil, nil},
{[]string{"foo"}, nil, nil, nil},
{[]string{"fn"}, nil, []string{"h-card"}, []string{"p-name"}},
{[]string{"fn", "foo"}, nil, []string{"h-card"}, []string{"p-name"}},
{[]string{"fn", "email"}, nil, []string{"h-card"}, []string{"p-name", "u-email"}},
// itemtype-specific property mappings
{[]string{"summary"}, nil, []string{"h-entry"}, []string{"p-summary"}},
{[]string{"summary"}, nil, []string{"h-event"}, []string{"p-name"}},
// duplicate properties
{[]string{"summary"}, nil, []string{"h-entry", "h-resume"}, []string{"p-summary"}},
{[]string{"summary"}, nil, []string{"h-entry", "h-event"}, []string{"p-summary", "p-name"}},
// rels
{nil, []string{"bookmark"}, nil, nil},
{nil, []string{"bookmark"}, []string{"h-entry"}, []string{"u-url"}},
{[]string{"category"}, []string{"tag"}, []string{"h-card"}, []string{"u-category"}},
}
for _, tt := range tests {
got := backcompatPropertyClasses(tt.classes, tt.rels, tt.context)
sort.Strings(got)
sort.Strings(tt.want)
if want := tt.want; !cmp.Equal(got, want) {
t.Errorf("backcompatPropertyClasses(%q) returned %q, want %q)", tt.classes, got, want)
}
}
}
func Test_BackcompatURLCategory(t *testing.T) {
tests := []struct {
url string
want string
}{
{"", ""},
{"a", "a"},
{"a/b", "b"},
{"/a/b", "b"},
{"/a/b/", "b"},
{"http://example.com/a/b", "b"},
{"%", "%"}, // invalid URL
}
for _, tt := range tests {
got := backcompatURLCategory(tt.url)
if want := tt.want; got != want {
t.Errorf("backcompatURLCategory(%q) returned %q, want %q)", tt.url, got, want)
}
}
}
func Test_BackcompatIncludeRefs(t *testing.T) {
tests := []struct {
html string
wantRefs []string
wantReplace bool
}{
{`<a></a>`, nil, false},
{`<a href="#foo"></a>`, nil, false},
{`<a class="include" href="foo"></a>`, nil, false},
{`<a class="include" href="#"></a>`, nil, false},
{
`<object class="include" data="#foo">`,
[]string{"foo"},
true,
},
{
`<a class="include" href="#foo"></a>`,
[]string{"foo"},
true,
},
{
`<a itemref="foo"></a>`,
[]string{"foo"},
false,
},
{
`<a itemref="foo bar"></a>`,
[]string{"foo", "bar"},
false,
},
}
for _, tt := range tests {
p := &parser{}
node, _ := parseNode(tt.html)
refs, replace := p.backcompatIncludeRefs(node)
if !cmp.Equal(refs, tt.wantRefs) {
t.Errorf("backcompatIncludeRefs(%v) returned refs %v, want %v", tt.html, refs, tt.wantRefs)
}
if replace != tt.wantReplace {
t.Errorf("backcompatIncludeRefs(%v) returned replace %t, want %t", tt.html, replace, tt.wantReplace)
}
}
}
func Test_BackcompatIncludeNode(t *testing.T) {
n, _ := parseNode("<p></p>")
tests := []struct {
node *html.Node
refs []string
replace bool
want *html.Node
}{
{n, []string{}, false, n},
}
for _, tt := range tests {
p := &parser{}
got := p.backcompatIncludeNode(tt.node, tt.refs, tt.replace)
if want := tt.want; !cmp.Equal(got, want) {
t.Errorf("backcompatIncludeNode(%v, %v, %v) returned %v, want %v", tt.node, tt.refs, tt.replace, got, want)
}
}
}
func Test_IsAncestorNode(t *testing.T) {
a1, _ := parseNode("<p><b></b></p>")
a2 := a1.FirstChild
b1, _ := parseNode("<div></div>")
tests := []struct {
parent, child *html.Node
want bool // expected return from from isAncestorNode
}{
{nil, nil, false},
{a1, nil, false},
{nil, a1, false},
{a1, a1, true},
{a1, a2, true},
{a2, a1, false},
{a1, b1, false},
}
for _, tt := range tests {
got := isAncestorNode(tt.child, tt.parent)
if got != tt.want {
t.Errorf("isAncestorNode(%v, %v) returned %v, want %v", tt.child, tt.parent, got, tt.want)
}
}
}