-
Notifications
You must be signed in to change notification settings - Fork 13
/
readability_test.go
227 lines (197 loc) · 5.4 KB
/
readability_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
package readability
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/net/html"
)
func TestMaxElemsToParse(t *testing.T) {
input := strings.NewReader(`<html>
<head>
<title>hello world</title>
</head>
<body>
<p>lorem ipsum</p>
</body>
</html>`)
parser := New()
parser.MaxElemsToParse = 3
_, err := parser.Parse(input, "https://cixtor.com/blog")
if err.Error() != "too many elements: 5" {
t.Fatalf("expecting failure due to MaxElemsToParse: %s", err)
}
}
func TestRemoveScripts(t *testing.T) {
input := strings.NewReader(`<html>
<head>
<title>hello world</title>
</head>
<body>
<script src="/js/scripts.min.js" type="text/javascript"></script>
<p>lorem ipsum</p>
<script type="text/javascript" src="/js/scripts.min.js"></script>
<script type="text/javascript">
window.alert('Hello World');
</script>
</body>
</html>`)
a, err := New().Parse(input, "https://cixtor.com/blog")
if err != nil {
t.Fatalf("parser failure: %s", err)
}
if a.TextContent != "lorem ipsum" {
t.Fatalf("scripts were not removed: %s", a.TextContent)
}
}
func getNodeExcerpt(node *html.Node) string {
outer := outerHTML(node)
outer = strings.Join(strings.Fields(outer), "\x20")
if len(outer) < 500 {
return outer
}
return outer[:500]
}
func errColorDiff(label string, a string, b string) error {
coloredA := ""
coloredB := ""
for i := 0; i < len(a); i++ {
if b[i] == a[i] {
coloredA += a[i : i+1]
coloredB += b[i : i+1]
continue
}
coloredA += "\x1b[0;92m" + a[i:] + "\x1b[0m"
coloredB += "\x1b[0;91m" + b[i:] + "\x1b[0m"
break
}
return fmt.Errorf("%s\n- %s\n+ %s", label, coloredA, coloredB)
}
func compareArticleContent(result *html.Node, expected *html.Node) error {
// Make sure number of nodes is same
resultNodesCount := len(children(result))
expectedNodesCount := len(children(expected))
if resultNodesCount != expectedNodesCount {
return fmt.Errorf(
"number of nodes is different, want %d got %d",
expectedNodesCount,
resultNodesCount,
)
}
resultNode := result
expectedNode := expected
for resultNode != nil && expectedNode != nil {
// Get node excerpt
resultExcerpt := getNodeExcerpt(resultNode)
expectedExcerpt := getNodeExcerpt(expectedNode)
// Compare tag name
resultTagName := tagName(resultNode)
expectedTagName := tagName(expectedNode)
if resultTagName != expectedTagName {
return fmt.Errorf(
"tag name is different\nwant: %s (%s)\ngot : %s (%s)",
expectedTagName,
expectedExcerpt,
resultTagName,
resultExcerpt,
)
}
// Compare attributes
resultAttrCount := len(resultNode.Attr)
expectedAttrCount := len(expectedNode.Attr)
if resultAttrCount != expectedAttrCount {
return fmt.Errorf(
"number of attributes is different\nwant: %d (%s)\ngot : %d (%s)",
expectedAttrCount,
expectedExcerpt,
resultAttrCount,
resultExcerpt,
)
}
for _, resultAttr := range resultNode.Attr {
expectedAttrVal := getAttribute(expectedNode, resultAttr.Key)
switch resultAttr.Key {
case "href", "src":
resultAttr.Val = strings.TrimSuffix(resultAttr.Val, "/")
expectedAttrVal = strings.TrimSuffix(expectedAttrVal, "/")
}
if resultAttr.Val != expectedAttrVal {
return fmt.Errorf(
"attribute %s is different\nwant: %s (%s)\ngot : %s (%s)",
resultAttr.Key,
expectedAttrVal,
expectedExcerpt,
resultAttr.Val,
resultExcerpt,
)
}
}
// Compare text content
resultText := strings.TrimSpace(textContent(resultNode))
expectedText := strings.TrimSpace(textContent(expectedNode))
resultText = strings.Join(strings.Fields(resultText), "\x20")
expectedText = strings.Join(strings.Fields(expectedText), "\x20")
if resultText != expectedText {
return errColorDiff(
"text content is different",
expectedExcerpt,
resultExcerpt,
)
}
// Move to next node
r := Readability{}
resultNode = r.getNextNode(resultNode, false)
expectedNode = r.getNextNode(expectedNode, false)
}
return nil
}
func TestParse(t *testing.T) {
testDir := "scenarios"
testItems, err := ioutil.ReadDir(testDir)
if err != nil {
t.Errorf("\nfailed to read test directory")
}
for _, item := range testItems {
if !item.IsDir() {
continue
}
t.Run(item.Name(), func(t1 *testing.T) {
// Open test file
testFilePath := filepath.Join(testDir, item.Name(), "source.html")
testFile, err := os.Open(testFilePath)
if err != nil {
t1.Errorf("\nfailed to open test file")
}
defer testFile.Close()
// Open expected result file
expectedFilePath := filepath.Join(testDir, item.Name(), "expected.html")
expectedFile, err := os.Open(expectedFilePath)
if err != nil {
t1.Errorf("\nfailed to open expected result file")
}
defer expectedFile.Close()
// Parse expected result
expectedHTML, err := html.Parse(expectedFile)
if err != nil {
t1.Errorf("\nfailed to parse expected result file")
}
// Get article from test file
resultArticle, err := New().Parse(testFile, "http://fakehost/test/page.html")
if err != nil {
t1.Errorf("\nfailed to parse test file")
}
// Parse article into HTML
resultHTML, err := html.Parse(strings.NewReader(resultArticle.Content))
if err != nil {
t1.Errorf("\nfailed to parse test article into HTML")
}
// Compare article
err = compareArticleContent(resultHTML, expectedHTML)
if err != nil {
t1.Errorf("\n%v", err)
}
})
}
}