forked from trustmaster/go-aspell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aspell_test.go
122 lines (109 loc) · 2.67 KB
/
aspell_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
package aspell
import (
"strings"
"testing"
// "fmt"
)
// This is a test for basic Aspell initialization
// and simple word check.
func TestBasic(t *testing.T) {
// Initialization
opts := map[string]string{
"lang": "en_US",
}
speller, err := NewSpeller(opts)
if err != nil {
t.Errorf("Aspell error: %s", err.Error())
return
}
defer speller.Delete()
// Test config getter
enc := speller.Config("encoding")
if enc != "utf-8" {
t.Errorf("Invalid aspell encoding: %s", enc)
}
// Test Check() against a dictionary
dict := map[string]bool{
"bottom": true,
"xyzzyw": false,
"operation": true,
"rooby": false,
"go": true,
}
for word, res := range dict {
if speller.Check(word) != res {
t.Errorf("Incorrect result for '%s', expected: %t", word, res)
}
}
}
// This is a test for the list of suggestions
func TestSuggestReplace(t *testing.T) {
// Initialization
opts := map[string]string{
"lang": "en_US",
"sug-mode": "slow",
}
speller, err := NewSpeller(opts)
if err != nil {
t.Errorf("Aspell error: %s", err.Error())
return
}
defer speller.Delete()
// A "must have" dictionary
dict := map[string]string{
"choise": "choice",
"soem": "some",
"paerticulaur": "particular",
"unessessay": "unnecessary",
"lauf": "laugh",
"voteing": "voting",
"xiaom": "Xiaomi",
// "juse": "juice", // aspell fails at this
}
speller.AddToPersonal("Xiaomi")
// Search for correct values among suggestions
for incorrect, correct := range dict {
suggs := speller.Suggest(incorrect)
found := false
for _, word := range suggs {
if word == correct {
found = true
break
}
}
if !found {
t.Errorf("Missing suggestion for '%s': expected '%s', suggested '%s'", incorrect, correct, strings.Join(suggs, ", "))
}
}
// Store and test a new replacement
if speller.Replace("juse", "juice") {
sugJuse := speller.Suggest("juse")
found := false
for _, word := range sugJuse {
if word == "juice" {
found = true
break
}
}
if !found {
t.Errorf("Missing replacement for 'juse': expected 'juse', suggested '%s'", strings.Join(sugJuse, ", "))
}
} else {
t.Error("Storing a replacement failed")
}
// // Print dict list
// dicts := Dicts()
// fmt.Printf("Dicts count: %d\n", len(dicts))
// for _, dict := range dicts {
// fmt.Printf("Name: %s\nCode: %s\nJargon: %s\nSize: %s\nModule: %s\n\n", dict.name, dict.code, dict.jargon, dict.size, dict.module)
// }
// // Print main word list
// words, err := speller.MainWordList()
// if err != nil {
// t.Error(err.Error())
// } else {
// for _, word := range words {
// fmt.Printf("%s, ", word)
// }
// }
}