forked from client9/misspell
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
replace_test.go
159 lines (138 loc) · 3.47 KB
/
replace_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package misspell
import (
"strings"
"testing"
)
func TestReplaceIgnore(t *testing.T) {
cases := []struct {
ignore string
text string
}{
{"knwo,gae", "https://github.com/Unknwon, github.com/hnakamur/gaesessions"},
}
for line, tt := range cases {
r := New()
r.RemoveRule(strings.Split(tt.ignore, ","))
r.Compile()
got, _ := r.Replace(tt.text)
if got != tt.text {
t.Errorf("%d: Replace files want %q got %q", line, tt.text, got)
}
}
}
func TestReplaceLocale(t *testing.T) {
cases := []struct {
orig string
want string
}{
{orig: "The colours are pretty", want: "The colors are pretty"},
{orig: "summaries", want: "summaries"},
}
r := New()
r.AddRuleList(DictAmerican)
r.Compile()
for _, test := range cases {
test := test
t.Run(test.orig, func(t *testing.T) {
t.Parallel()
got, _ := r.Replace(test.orig)
if got != test.want {
t.Errorf("ReplaceLocale want %q got %q", test.orig, got)
}
})
}
}
func TestReplace(t *testing.T) {
cases := []struct {
orig string
want string
}{
{orig: "I live in Amercia", want: "I live in America"},
{orig: "grill brocoli now", want: "grill broccoli now"},
{orig: "There is a zeebra", want: "There is a zebra"},
{orig: "foo other bar", want: "foo other bar"},
{orig: "ten fiels", want: "ten fields"},
{orig: "Closeing Time", want: "Closing Time"},
{orig: "closeing Time", want: "closing Time"},
{orig: " TOOD: foobar", want: " TODO: foobar"},
{orig: " preceed ", want: " precede "},
{orig: "preceeding", want: "preceding"},
{orig: "functionallity", want: "functionality"},
}
r := New()
for _, test := range cases {
test := test
t.Run(test.orig, func(t *testing.T) {
t.Parallel()
got, _ := r.Replace(test.orig)
if got != test.want {
t.Errorf("Replace files want %q got %q", test.orig, got)
}
})
}
}
func TestCheckReplace(t *testing.T) {
t.Run("Nothing at all", func(t *testing.T) {
t.Parallel()
r := Replacer{
engine: NewStringReplacer("foo", "foobar"),
corrected: map[string]string{"foo": "foobar"},
}
s := "nothing at all"
news, diffs := r.Replace(s)
if s != news || len(diffs) != 0 {
t.Errorf("Basic recheck failed: %q vs %q", s, news)
}
})
t.Run("Single, correct,.Correctedacements", func(t *testing.T) {
t.Parallel()
testCases := []struct {
orig string
expected string
}{
{
orig: "foo",
expected: "foobar",
},
{
orig: "foo junk",
expected: "foobar junk",
},
{
orig: "junk foo",
expected: "junk foobar",
},
{
orig: "junk foo junk",
expected: "junk foobar junk",
},
}
for _, test := range testCases {
orig := test.orig
expected := test.expected
t.Run(orig, func(t *testing.T) {
t.Parallel()
r := Replacer{
engine: NewStringReplacer("foo", "foobar", "runing", "running"),
corrected: map[string]string{"foo": "foobar", "runing": "running"},
}
news, diffs := r.Replace(orig)
if news != expected || len(diffs) != 1 || diffs[0].Original != "foo" && diffs[0].Corrected != expected && diffs[0].Column != 0 {
t.Errorf("basic recheck failed %q vs %q", expected, news)
}
})
}
})
t.Run("Incorrect.Correctedacements", func(t *testing.T) {
t.Parallel()
r := Replacer{
engine: NewStringReplacer("foo", "foobar"),
corrected: map[string]string{"foo": "foobar"},
}
s := "food pruning"
news, _ := r.Replace(s)
if news != s {
t.Errorf("incorrect.Correctedacement failed: %q vs %q", s, news)
}
})
}