-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathabuse_loader_test.go
More file actions
171 lines (137 loc) · 3.63 KB
/
abuse_loader_test.go
File metadata and controls
171 lines (137 loc) · 3.63 KB
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
package masker
import (
"strings"
"testing"
)
func TestAbuseWordLoaderLoadFromString(t *testing.T) {
loader := NewAbuseWordLoader()
content := `# This is a comment
bad
terrible
# Another comment
awful
horrible
`
words, err := loader.LoadFromString(content)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
expected := []string{"bad", "terrible", "awful", "horrible"}
if len(words) != len(expected) {
t.Errorf("Expected %d words, got %d", len(expected), len(words))
}
for i, word := range expected {
if words[i] != word {
t.Errorf("Expected word %s at position %d, got %s", word, i, words[i])
}
}
}
func TestAbuseWordLoaderLoadFromSlice(t *testing.T) {
loader := NewAbuseWordLoader()
input := []string{"bad", " terrible ", "", "awful", " "}
words := loader.LoadFromSlice(input)
expected := []string{"bad", "terrible", "awful"}
if len(words) != len(expected) {
t.Errorf("Expected %d words, got %d", len(expected), len(words))
}
for i, word := range expected {
if words[i] != word {
t.Errorf("Expected word %s at position %d, got %s", word, i, words[i])
}
}
}
func TestAbuseWordLoaderLoadFromReader(t *testing.T) {
loader := NewAbuseWordLoader()
content := "bad\nterrible\nawful"
reader := strings.NewReader(content)
words, err := loader.LoadFromReader(reader)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
expected := []string{"bad", "terrible", "awful"}
if len(words) != len(expected) {
t.Errorf("Expected %d words, got %d", len(expected), len(words))
}
for i, word := range expected {
if words[i] != word {
t.Errorf("Expected word %s at position %d, got %s", word, i, words[i])
}
}
}
func TestAbuseWordLoaderEmptyInput(t *testing.T) {
loader := NewAbuseWordLoader()
// Test empty string
words, err := loader.LoadFromString("")
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if len(words) != 0 {
t.Errorf("Expected 0 words, got %d", len(words))
}
// Test empty slice
words = loader.LoadFromSlice([]string{})
if len(words) != 0 {
t.Errorf("Expected 0 words, got %d", len(words))
}
// Test slice with only empty strings
words = loader.LoadFromSlice([]string{"", " ", "\t", "\n"})
if len(words) != 0 {
t.Errorf("Expected 0 words, got %d", len(words))
}
}
func TestAbuseWordLoaderOnlyComments(t *testing.T) {
loader := NewAbuseWordLoader()
content := `# This is a comment
# Another comment
# Yet another comment
`
words, err := loader.LoadFromString(content)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
if len(words) != 0 {
t.Errorf("Expected 0 words, got %d", len(words))
}
}
func TestAbuseWordLoaderMixedContent(t *testing.T) {
loader := NewAbuseWordLoader()
content := `# Header comment
bad
# Middle comment
terrible
# End comment
awful
`
words, err := loader.LoadFromString(content)
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
expected := []string{"bad", "terrible", "awful"}
if len(words) != len(expected) {
t.Errorf("Expected %d words, got %d", len(expected), len(words))
}
for i, word := range expected {
if words[i] != word {
t.Errorf("Expected word %s at position %d, got %s", word, i, words[i])
}
}
}
func BenchmarkAbuseWordLoaderLoadFromString(b *testing.B) {
loader := NewAbuseWordLoader()
content := strings.Repeat("word\n", 1000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
loader.LoadFromString(content)
}
}
func BenchmarkAbuseWordLoaderLoadFromSlice(b *testing.B) {
loader := NewAbuseWordLoader()
words := make([]string, 5000)
for i := 0; i < 5000; i++ {
words[i] = "word"
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
loader.LoadFromSlice(words)
}
}