-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_test.go
138 lines (108 loc) · 2.7 KB
/
dictionary_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
package main
import (
"fmt"
"os"
"testing"
)
func TestDictionary(t *testing.T) {
data := []byte(`["Foo","Bar","baz","QUUX"]`)
dict, err := ParseDictionary("test_dict", data)
if err != nil {
t.Error(err)
return
}
if dict == nil {
t.Error("got nil dictionary back")
return
}
if dict.Name != "test_dict" {
t.Error("dictionary name not correct")
}
if len(dict.Words) != 4 {
t.Error("Didn't parse proper number of words")
}
if dict.Words[3] != "QUUX" {
t.Error("Didn't ready words in order")
}
otherDict, _ := ParseDictionary("other", []byte(`["Fie", "Fy", "Foe", "Foo"]`))
combined := MergeDictionaries(make([]string, 0), dict, otherDict)
if len(combined.Words) == len(dict.Words)+len(otherDict.Words) {
t.Error("Merge didn't deduplicate")
} else if len(combined.Words) != len(dict.Words)+len(otherDict.Words)-1 {
t.Error("Merge didn't deduplicate")
}
if len(combined.Words) == len(dict.Words)+len(otherDict.Words) {
t.Error("Merge didn't deduplicate")
}
if combined.Name != "test_dict + other" {
t.Error("Merge didn't combine names properly")
}
}
func TestDictionaryParserFailure(t *testing.T) {
data := []byte(`{"key": "value"}`)
dict, err := ParseDictionary("bad_dictionary", data)
if err == nil {
t.Error("Didn't return error on malformed dictionary")
}
if dict != nil {
t.Error("Parsing malformed data didn't return nil dict")
}
}
func TestBigDictionary(t *testing.T) {
data, rerr := os.ReadFile("json/full-dict.json")
if rerr != nil {
t.Error(rerr)
return
}
dict, perr := ParseDictionary("Full", data)
if perr != nil {
t.Error(perr)
return
}
if len(dict.Words) < 20000 {
t.Error("Got truncated dictionary")
}
}
func TestReadConfigs(t *testing.T) {
mainConfigs, addedConfigs, err := ReadConfigs()
if err != nil {
t.Error(err)
return
}
if mainConfigs == nil || addedConfigs == nil {
t.Error("One config list is nil")
return
}
if len(mainConfigs) == 0 || len(addedConfigs) == 0 {
t.Error("One of the lists is 0 length")
}
for i, mc := range mainConfigs {
if mc.File == "" {
t.Error(fmt.Sprintf("main config %d has blank filename", i))
}
if mc.Description == "" {
t.Error(fmt.Sprintf("main config %d has blank description", i))
}
}
for i, ac := range addedConfigs {
if ac.File == "" {
t.Error(fmt.Sprintf("added config %d has blank filename", i))
}
if ac.Description == "" {
t.Error(fmt.Sprintf("added config %d has blank description", i))
}
}
}
func TestReadDictionaries(t *testing.T) {
mainDicts, addedDicts, err := ReadDictionaries()
if err != nil {
t.Error(err)
return
}
if len(mainDicts) == 0 {
t.Error("No main dictionaries")
}
if len(addedDicts) == 0 {
t.Error("No added dictionaries")
}
}