-
Notifications
You must be signed in to change notification settings - Fork 90
/
utils_test.go
72 lines (63 loc) · 2.08 KB
/
utils_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
package readability
import (
nurl "net/url"
"strings"
"testing"
)
func Test_indexOf(t *testing.T) {
sample := strings.Fields("" +
"hello this is a simple sentence and we try " +
"to repeat some simple word like this")
scenarios := map[string]int{
"hello": 0,
"this": 1,
"simple": 4,
"we": 7,
"repeat": 10,
}
for word, expected := range scenarios {
if idx := indexOf(sample, word); idx != expected {
t.Errorf("\n"+
"word : \"%s\"\n"+
"want : %d\n"+
"got : %d", word, expected, idx)
}
}
}
func Test_wordCount(t *testing.T) {
scenarios := map[string]int{
"German fashion designer Karl Lagerfeld, best known for his creative work at Chanel, dies at the age of 85.": 19,
"A suicide bombing attack near Pulwama, in Indian administered Kashmir, kills 40 security personnel.": 14,
"NASA concludes the 15 year Opportunity Mars rover mission after being unable to wake the rover from hibernation.": 18,
}
for sentence, expected := range scenarios {
if count := wordCount(sentence); count != expected {
t.Errorf("\n"+
"sentence : \"%s\"\n"+
"want : %d\n"+
"got : %d", sentence, expected, count)
}
}
}
func Test_toAbsoluteURI(t *testing.T) {
baseURL, _ := nurl.ParseRequestURI("http://localhost:8080/absolute/")
scenarios := map[string]string{
"#here": "#here",
"/test/123": "http://localhost:8080/test/123",
"test/123": "http://localhost:8080/absolute/test/123",
"//www.google.com": "http://www.google.com",
"https://www.google.com": "https://www.google.com",
"ftp://ftp.server.com": "ftp://ftp.server.com",
"www.google.com": "http://localhost:8080/absolute/www.google.com",
"http//www.google.com": "http://localhost:8080/absolute/http//www.google.com",
"../hello/relative": "http://localhost:8080/hello/relative",
}
for url, expected := range scenarios {
if result := toAbsoluteURI(url, baseURL); result != expected {
t.Errorf("\n"+
"url : \"%s\"\n"+
"want : \"%s\"\n"+
"got : \"%s\"", url, expected, result)
}
}
}