-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathswearfilter_test.go
218 lines (205 loc) · 6.4 KB
/
swearfilter_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
package swearfilter
import (
"testing"
)
func TestNew(t *testing.T) {
filter := NewSwearFilter(true, "fuck", "hell")
if filter.DisableNormalize {
t.Errorf("Filter option DisableNormalize was incorrect, got: %t, want: %t", filter.DisableNormalize, false)
}
if filter.DisableSpacedTab {
t.Errorf("Filter option DisableSpacedTab was incorrect, got: %t, want: %t", filter.DisableSpacedTab, false)
}
if filter.DisableMultiWhitespaceStripping {
t.Errorf("Filter option DisableMultiWhitespaceStripping was incorrect, got: %t, want: %t", filter.DisableMultiWhitespaceStripping, false)
}
if filter.DisableZeroWidthStripping {
t.Errorf("Filter option DisableZeroWidthStripping was incorrect, got: %t, want: %t", filter.DisableZeroWidthStripping, false)
}
if !filter.EnableSpacedBypass {
t.Errorf("Filter option EnableSpacedBypass was incorrect, got: %t, want: %t", filter.EnableSpacedBypass, true)
}
if filter.DisableLeetSpeak {
t.Errorf("Filter option DisableLeetSpeak was incorrect, got: %t, want: %t", filter.EnableSpacedBypass, false)
}
if len(filter.BadWords) != 2 {
t.Errorf("Filter option BadWords was incorrect, got length: %d, want length: %d", len(filter.BadWords), 2)
}
}
func TestCheckAndAddDelete(t *testing.T) {
filter := NewSwearFilter(true)
var prev []string
tests := []struct {
name string
input string
expected []string
}{
// Basic cases
{"basic match", "fucking", []string{"fuck"}},
{"unicode chars", "fûçk", []string{"fuck"}},
{"clean text", "asdf", nil},
{"spaced out", "f u c k", []string{"fuck"}},
// Multi-char leet speak cases (from multiCharLeet map)
{"ph substitution", "phuck", []string{"fuck"}},
// Single-char leet speak cases (from leetChars map)
{"basic a4", "b4st4rd", []string{"bastard"}},
{"at symbol", "@sshole", []string{"asshole"}},
{"euro sign", `p€nis`, []string{"penis"}},
{"multi char w", "vvhor3", []string{"whore"}},
{"spaced chars", "s h ! t", []string{"shit"}},
{"multiple mappings", "fvv|<", []string{"fwk"}},
{"o variants", "b()()bs", []string{"boobs"}},
{"ph mapping", "ph@rt", []string{"fart"}},
{"double v w", `\/\/@nk3r`, []string{"wanker"}},
{"pure numbers", "5417", []string{"salt"}},
{"mixed special", "@$$h0l3", []string{"asshole"}},
{"x variant", "><xx", []string{"xxx"}},
{"dollar sign", "$hit", []string{"shit"}},
{"number mix", "8!7ch", []string{"bitch"}},
{"brackets o", "c[]c|<", []string{"cock"}},
{"uu variant", "uuank", []string{"wank"}},
{"hash symbol", "#0rny", []string{"horny"}},
{"g variants", "6i6", []string{"gig"}},
{"plus sign", "+i+s", []string{"tits"}},
{"j and z", "j2j2", []string{"izi"}},
{"alt k test", "1<un7", []string{"kunt"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Remove previous words
if len(prev) > 0 {
filter.Delete(prev...)
}
// Add new words from expected
if tt.expected != nil {
filter.Add(tt.expected...)
}
// Store current words for next iteration
prev = tt.expected
// Verify current word list
currentWords := filter.Words()
t.Logf("Current filter words: %v", currentWords)
// Run the check
trippers, err := filter.Check(tt.input)
if err != nil {
t.Errorf("Check failed: %v", err)
}
// Verify results
if (trippers == nil && tt.expected != nil) || (trippers != nil && tt.expected == nil) {
t.Errorf("got trippers %v, want %v ", trippers, tt.expected)
return
}
if len(trippers) != len(tt.expected) {
t.Errorf("got trippers length %d, want %d", len(trippers), len(tt.expected))
return
}
// Check each expected word is in trippers
for _, expected := range tt.expected {
found := false
for _, got := range trippers {
if got == expected {
found = true
break
}
}
if !found {
t.Errorf("expected word %s not found in trippers %v", expected, trippers)
}
}
})
}
}
func TestCheck(t *testing.T) {
//filter := NewSwearFilter(true, "fuck", "hell")
filter := NewSwearFilter(true,
"fuck",
"fwk",
"hell",
"asshole",
"bastard",
"bitch",
"boobs",
"cock",
"fart",
"fuck",
"gig",
"horny",
"izi",
"kk",
"kunt",
"penis",
"salt",
"shit",
"tits",
"wank",
"wanker",
"whore",
"xxx",
)
tests := []struct {
name string
input string
expected []string
}{
// Basic cases
{"basic match", "fucking", []string{"fuck"}},
{"unicode chars", "fûçk", []string{"fuck"}},
{"clean text", "asdf", []string{}},
{"spaced out", "f u c k", []string{"fuck"}},
//
// // Multi-char leet speak cases (from multiCharLeet map)
{"ph substitution", "phuck", []string{"fuck"}},
//
// // Single-char leet speak cases (from leetChars map)
{"basic a4", "b4st4rd", []string{"bastard"}},
{"at symbol", "@sshole", []string{"asshole"}},
{"euro sign", "p€nis", []string{"penis"}},
{"multi char w", "vvhor3", []string{"whore"}},
{"spaced chars", "s h ! t", []string{"shit"}},
{"multiple mappings", "fvv|<", []string{"fwk"}},
{"o variants", "b()()bs", []string{"boobs"}},
{"ph mapping", "ph@rt", []string{"fart"}},
{"double v w", `\/\/@nk3r`, []string{"wanker", "wank"}},
{"pure numbers", "5417", []string{"salt"}},
{"mixed special", "@$$h0l3", []string{"asshole"}},
{"x variant", "><xx", []string{"xxx"}},
{"dollar sign", "$hit", []string{"shit"}},
{"number mix", "8!7ch", []string{"bitch"}},
{"brackets o", "c[]c|<", []string{"cock"}},
{"uu variant", "uuank", []string{"wank"}},
{"hash symbol", "#0rny", []string{"horny"}},
{"g variants", "616", []string{"gig"}},
{"plus sign", "+i+s", []string{"tits"}},
{"j and z", "j2j2", []string{"izi"}},
{"alt k test", "1<un7", []string{"kunt"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
trippers, err := filter.Check(tt.input)
if err != nil {
t.Errorf("Check failed: %v", err)
}
if (trippers == nil && tt.expected != nil) || (trippers != nil && tt.expected == nil) {
t.Errorf("got trippers %v , want %v ", trippers, tt.expected)
return
}
if len(trippers) != len(tt.expected) {
t.Errorf("got trippers length %d, want %d", len(trippers), len(tt.expected))
return
}
// Check each expected word is in trippers
for _, expected := range tt.expected {
found := false
for _, got := range trippers {
if got == expected {
found = true
break
}
}
if !found {
t.Errorf("expected word %s not found in trippers %v", expected, trippers)
}
}
})
}
}