-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_test.go
68 lines (53 loc) · 1.33 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
package spellingcorrector
import (
"os"
"path"
"testing"
)
func newTestDictionary(t *testing.T) *Dictionary {
currentPath, err := os.Getwd()
if err != nil {
t.Error(err)
}
dictionaryFilePath := path.Join(currentPath, "tmp", "es.dic")
dic, err := NewDictionary(dictionaryFilePath, ESAlphabet)
if err != nil {
t.Error(err)
}
return dic
}
func TestLoadDictionary(t *testing.T) {
currentPath, err := os.Getwd()
if err != nil {
t.Error(err)
}
dictionaryFilePath := path.Join(currentPath, "dictionaries", "wrong.dic")
_, err = LoadDictionary(dictionaryFilePath)
if err == nil {
t.Error("If the dictionary file does not exist, then it should fail")
}
}
func TestTrainDictionary(t *testing.T) {
currentPath, err := os.Getwd()
if err != nil {
t.Error(err)
}
textFilePath := path.Join(currentPath, "examples", "test_sample1.txt")
dic := newTestDictionary(t)
dic.TrainFromTextFile(textFilePath)
if len(dic.Words) != 2 {
t.Error("Expected returned length should be 2")
}
}
func TestTrainDictionaryWithWrongText(t *testing.T) {
currentPath, err := os.Getwd()
if err != nil {
t.Error(err)
}
textFilePath := path.Join(currentPath, "examples", "wrong.txt")
dic := newTestDictionary(t)
err = dic.TrainFromTextFile(textFilePath)
if err == nil {
t.Error("If the text file does not exist, then it should fail")
}
}