/
remote_test.go
134 lines (119 loc) · 3.14 KB
/
remote_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
package goGradeLevel
import (
"log"
"testing"
"code.google.com/p/go-html-transform/h5"
)
func Test_getRemoteText(t *testing.T) {
// fmt.Println(getRemoteText("http://apod.nasa.gov/apod/ap130220.html"))
// log.Print(getRemoteText("http://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_test"))
// fmt.Println(getRemoteText("http://www.wired.com/wiredscience/2013/02/how-mathematical-research-is-making-the-life-of-pi-tiger-even-better/"))
}
func Test_AnalyzeUrl(t *testing.T) {
var d = []struct{
in string
want *Result
} {
{"http://apod.nasa.gov/apod/ap130220.html", &Result{ReadingEase:41.01510209042297,FleschKincaidGradeLevel:11.074987846378221,ColemanLiauIndex:13.415867768595039}},
}
for _, v := range d {
b := AnalyzeUrl(v.in)
if !compareResult(b, v.want) {
t.Errorf("url: %s wanted %v got %v", v.in, v.want, b)
}
}
}
func Test_AnalyzeUrls(t *testing.T) {
var d = []struct{
in []string
want map[string]*Result
} {
{
[]string{
"http://apod.nasa.gov/apod/ap130220.html",
"http://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_test",
},
map[string]*Result{
"http://apod.nasa.gov/apod/ap130220.html":
&Result{ReadingEase:41.01510209042297,FleschKincaidGradeLevel:11.074987846378221,ColemanLiauIndex:13.415867768595039},
"http://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_test":
&Result{ReadingEase:39.94790088638197,FleschKincaidGradeLevel:10.4305418057634,ColemanLiauIndex:13.429741248097411},
},
},
}
for _, v := range d {
b := AnalyzeUrls(v.in)
for kk, vv := range v.want {
if !compareResult(b[kk], vv) {
t.Errorf("url: %s, wanted %v,\n got %v", kk, vv, b[kk])
}
}
}
}
func Test_endSentenceWithNode(t *testing.T) {
var d = []struct{
in string
want bool
} {
{"<li><a>Privacy policy</a></li>", true},
{"<a>Privacy policy</a>", false},
{"<p>Text <i><a>Anchor</a></i> </p>", true},
{"<i><a>Anchor</a></i>", false},
}
for _, v := range d {
tree := parseString(v.in)
if endSentenceWithNode(tree) != v.want {
t.Errorf("%s != %t", v.in, v.want)
}
}
}
func Test_collectText(t *testing.T) {
var d = []struct{
in string
want []string
} {
{"<li><a>A</a></li>", []string{"A", eos}},
{"<a>A</a>", []string{"A"}},
{"<p>P<i><a>A</a></i></p>", []string{"P", "A", eos}},
{"<i><a>A</a></i>", []string{"A"}},
}
for _, v := range d {
text := collectText(parseString(v.in))
if !compareStringArray(text, v.want) {
t.Errorf("%s != %v, got: %v", v.in, v.want, text)
}
}
}
/*
* utility functions
*/
func parseString(s string) *h5.Node {
p := h5.NewParserFromString(s)
err := p.Parse()
if err != nil {
log.Fatal(err.Error())
}
return p.Tree()
}
func compareStringArray(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
func compareResult(a, b *Result) bool {
if a == nil || b == nil {
return false
}
if compareFloats(a.ReadingEase, b.ReadingEase) &&
compareFloats(a.FleschKincaidGradeLevel, b.FleschKincaidGradeLevel) &&
compareFloats(a.ColemanLiauIndex, b.ColemanLiauIndex) {
return true
}
return false
}