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
|
package spell
import "testing"
func TestSuggestReturnsClosestWord(t *testing.T) {
d := loadT(t)
got := d.Suggest("recieve", 5)
if len(got) == 0 {
t.Fatal("Suggest(recieve) returned nothing")
}
if !contains(got, "receive") {
t.Errorf("Suggest(recieve) = %v, want it to include \"receive\"", got)
}
}
func TestSuggestRanksByDistanceThenFrequency(t *testing.T) {
d := loadT(t)
// "teh" is one edit from "the" (most common word) and from "ten"/"tea"/etc.
// The most frequent close word should rank first.
got := d.Suggest("teh", 5)
if len(got) == 0 {
t.Fatal("Suggest(teh) returned nothing")
}
if got[0] != "the" {
t.Errorf("Suggest(teh)[0] = %q, want \"the\" (closest + most frequent)", got[0])
}
}
func TestSuggestCapsResults(t *testing.T) {
d := loadT(t)
got := d.Suggest("seperate", 3)
if len(got) > 3 {
t.Errorf("Suggest with max=3 returned %d results", len(got))
}
if !contains(got, "separate") {
t.Errorf("Suggest(seperate) = %v, want it to include \"separate\"", got)
}
}
func contains(xs []string, s string) bool {
for _, x := range xs {
if x == s {
return true
}
}
return false
}
|