-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter_test.go
149 lines (135 loc) · 3.12 KB
/
filter_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
package wikipedia
import (
"testing"
)
func pagesToChan(pages []Page) chan Page {
c := make(chan Page, len(pages))
for _, page := range pages {
c <- page
}
close(c)
return c
}
func chanToPages(c <-chan Page) []Page {
pages := []Page{}
for page := range c {
pages = append(pages, page)
}
return pages
}
func isPagesEqual(c1 []Page, c2 []Page, t *testing.T) {
if len(c1) != len(c2) {
t.Fatalf(
"Length for output is incorrect got %d expected %d \n\tTest case: `%#v`\n\tOutput: `%#v`",
len(c1),
len(c2),
c1,
c2,
)
return
}
for k := range c1 {
if c1[k] != c2[k] {
t.Fatalf("Output doesn't match input: \nExpected %#v\nGot: %#v", c1[k], c2[k])
}
}
}
func TestFilterByCategory(t *testing.T) {
tests := []struct {
filter CategoryFilter
input []Page
output []Page
}{
{
filter: CategoryFilter{
Pattern: "\\[\\[Category:(.+)]\\]",
Allowed: []string{},
},
input: []Page{Page{Text: "[[Category:hello]]"}},
output: []Page{},
},
{
filter: CategoryFilter{
Pattern: "\\[\\[Category:(.+)]\\]",
Allowed: []string{"hello"},
},
input: []Page{Page{Text: "[[Category:hello]]"}},
output: []Page{Page{Text: "[[Category:hello]]"}},
},
{
filter: CategoryFilter{
Pattern: "\\[\\[Category:(.+)]\\]",
Allowed: []string{"hello"},
Denied: []string{"hello world"},
},
input: []Page{
Page{Text: "[[Category:hello world]]"},
Page{Text: "[[Category:hello there]]"},
},
output: []Page{Page{Text: "[[Category:hello there]]"}},
},
}
for _, test := range tests {
inputChan := pagesToChan(test.input)
outputChan := make(chan Page, len(test.input))
FilterByCategory(inputChan, outputChan, test.filter)
output := chanToPages(outputChan)
isPagesEqual(test.output, output, t)
}
}
func TestFilterByTitleLength(t *testing.T) {
tests := []struct {
filter TitleLengthFilter
input []Page
output []Page
}{
{
filter: TitleLengthFilter{Min: 0, Max: 5},
input: []Page{Page{Title: "1234567890"}},
output: []Page{},
},
{
filter: TitleLengthFilter{Min: 5, Max: 10},
input: []Page{Page{Title: "1234567890"}},
output: []Page{Page{Title: "1234567890"}},
},
{
filter: TitleLengthFilter{Min: 5, Max: 10},
input: []Page{Page{Title: "1234"}},
output: []Page{},
},
}
for _, test := range tests {
inputChan := pagesToChan(test.input)
outputChan := make(chan Page, len(test.input))
FilterByTitleLength(inputChan, outputChan, test.filter)
output := chanToPages(outputChan)
isPagesEqual(test.output, output, t)
}
}
func TestFilterByRedirect(t *testing.T) {
tests := []struct {
input []Page
output []Page
}{
{
input: []Page{Page{Redir: Redirect{Title: "another page"}}},
output: []Page{},
},
{
input: []Page{Page{Redir: Redirect{Title: ""}}},
output: []Page{Page{Redir: Redirect{Title: ""}}},
},
{
input: []Page{Page{}},
output: []Page{Page{}},
},
}
for _, test := range tests {
inputChan := pagesToChan(test.input)
outputChan := make(chan Page, len(test.input))
FilterByRedirect(inputChan, outputChan)
output := chanToPages(outputChan)
isPagesEqual(test.output, output, t)
}
}