-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer_test.go
104 lines (94 loc) · 1.93 KB
/
tokenizer_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
package tokenizer
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
type args struct {
stopWords []string
}
tests := []struct {
name string
args args
want *Tokenizer
}{
{
name: "Given a set of stop words, a new Tokenizer should be created",
args: args{
stopWords: []string{"foo", "bar", "baz"},
},
want: &Tokenizer{
stopWords: []string{"foo", "bar", "baz"},
stopWordsMap: map[string]struct{}{
"foo": {},
"bar": {},
"baz": {},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ObjectsAreEqual(tt.want, New(tt.args.stopWords...))
})
}
}
func TestTokenizer_Tokenize(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
tokenizer *Tokenizer
args args
want []string
}{
{
name: "Given a text Tokenizer.Tokenize should return a set of string tokens, ignoring any stopwords",
tokenizer: New("foo", "bar", "baz"),
args: args{
text: "123 foo 456 bar 789 baz qux",
},
want: []string{"123", "456", "789", "qux"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.ElementsMatch(t, tt.want, tt.tokenizer.Tokenize(tt.args.text))
})
}
}
func Test_mapWords(t *testing.T) {
type args struct {
words []string
}
tests := []struct {
name string
args args
want map[string]struct{}
}{
{
name: "mapWords should create a map where the keys are each one of the input words",
args: args{
words: []string{"foo", "bar", "baz"},
},
want: map[string]struct{}{
"foo": {},
"bar": {},
"baz": {},
},
},
{
name: "If no words are provided, an empty map should be returned",
args: args{
words: []string{},
},
want: map[string]struct{}{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, mapWords(tt.args.words))
})
}
}