/
realistic_word_test.go
48 lines (45 loc) · 1.12 KB
/
realistic_word_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
package faker
import (
"testing"
)
func TestGetWord(t *testing.T) {
tables := []struct {
context string
mockFunc func()
expectWords []string
}{
{
context: "test",
mockFunc: func() {
consultService = func(context string, max int, w *[]WordInfo) {
*w = []WordInfo{
{Word: "test1", Score: 1, Tags: []string{"tag1"}},
{Word: "test2", Score: 2, Tags: []string{"tag2"}},
{Word: "test3", Score: 3, Tags: []string{"tag3"}},
}
}
},
expectWords: []string{"test1", "test2", "test3"},
},
}
originalResponse := consultService
for _, table := range tables {
table.mockFunc()
resultWords := getWords(table.context)
if len(resultWords) != len(table.expectWords) {
t.Errorf("getWords(%s) was incorrect, got: %d, want: %d.", table.context, len(resultWords), len(table.expectWords))
}
resultWord := getWord(table.context)
valid := false
for _, w := range table.expectWords {
if w == resultWord {
valid = true
break
}
}
if !valid {
t.Errorf("getWord(%s) was incorrect, got: %s.", table.context, resultWord)
}
consultService = originalResponse
}
}