-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shamoji_test.go
109 lines (87 loc) · 1.86 KB
/
shamoji_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
package shamoji_test
import (
"context"
"strings"
"testing"
"github.com/osamingo/shamoji"
)
type example struct {
DenyList [][]byte
}
func newExample(deny ...[]byte) *shamoji.Serve {
e := &example{
DenyList: deny,
}
return &shamoji.Serve{
Tokenizer: e,
Filer: e,
}
}
func (e *example) Tokenize(sentence string) [][]byte {
fs := strings.Fields(sentence)
ts := make([][]byte, len(fs))
for i := range fs {
ts[i] = []byte(fs[i])
}
return ts
}
func (e *example) Test(src []byte) bool {
for i := range e.DenyList {
if string(src) == string(e.DenyList[i]) {
return true
}
}
return false
}
func TestServe_Do(t *testing.T) {
t.Parallel()
s := newExample([]byte("fuck"), []byte("fucker"))
cases := map[string]struct {
sentence string
result bool
expect string
}{
"Empty sentence": {"", false, ""},
"Sentence with deny word": {"I'm a student.", false, ""},
"Sentence without deny word": {"fuck you.", true, "fuck"},
}
for n, c := range cases {
c := c
t.Run(n, func(t *testing.T) {
t.Parallel()
ret, token := s.Do(c.sentence)
if ret != c.result {
t.Error("shoud be", c.result)
}
if token != c.expect {
t.Error("shoud be", c.result)
}
})
}
}
func TestServe_DoAsync(t *testing.T) {
t.Parallel()
s := newExample([]byte("fuck"), []byte("fucker"))
cases := map[string]struct {
sentence string
result bool
expect string
}{
"Empty sentence": {"", false, ""},
"Sentence with deny word": {"I'm a student.", false, ""},
"Sentence without deny word": {"fuck you.", true, "fuck"},
}
for n, c := range cases {
c := c
t.Run(n, func(t *testing.T) {
t.Parallel()
ret, token := s.DoAsync(context.Background(), c.sentence)
if ret != c.result {
t.Error("shoud be ", c.result)
}
if token != c.expect {
t.Error("shoud be ", c.result)
}
})
}
}