forked from atom/node-spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspellchecker-spec.coffee
181 lines (141 loc) · 6.81 KB
/
spellchecker-spec.coffee
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
{Spellchecker} = require '../lib/spellchecker'
fs = require 'fs'
path = require 'path'
enUS = "A robot is a mechanical or virtual artificial agent, usually an electronic machine"
deDE = "ein Roboter ist eine technische Apparatur, die üblicherweise dazu dient, dem Menschen mechanische Arbeit abzunehmen."
frFR = "les robots les plus évolués sont capables de se déplacer et de se recharger par eux-mêmes"
defaultLanguage = if process.platform is 'darwin' then '' else 'en_US'
dictionaryDirectory = path.join(__dirname, 'dictionaries')
readDictionaryForLang = (lang) ->
return new Buffer([]) unless lang
fs.readFileSync(path.join(dictionaryDirectory, "#{lang.replace(/_/g, '-')}.bdic"))
describe "SpellChecker", ->
describe ".isMisspelled(word)", ->
beforeEach ->
@fixture = new Spellchecker()
@fixture.setDictionary defaultLanguage, readDictionaryForLang(defaultLanguage)
it "returns true if the word is mispelled", ->
@fixture.setDictionary('en_US', readDictionaryForLang('en_US'))
expect(@fixture.isMisspelled('wwoorrddd')).toBe true
it "returns false if the word isn't mispelled", ->
@fixture.setDictionary('en_US', readDictionaryForLang('en_US'))
expect(@fixture.isMisspelled('word')).toBe false
it "throws an exception when no word specified", ->
expect(-> @fixture.isMisspelled()).toThrow()
it "automatically detects languages on OS X", ->
return unless process.platform is 'darwin'
expect(@fixture.checkSpelling(enUS)).toEqual []
expect(@fixture.checkSpelling(deDE)).toEqual []
expect(@fixture.checkSpelling(frFR)).toEqual []
it "correctly switches languages", ->
expect(@fixture.setDictionary('en_US', readDictionaryForLang('en_US'))).toBe true
expect(@fixture.checkSpelling(enUS)).toEqual []
expect(@fixture.checkSpelling(deDE)).not.toEqual []
expect(@fixture.checkSpelling(frFR)).not.toEqual []
if @fixture.setDictionary('de_DE', readDictionaryForLang('de_DE'))
expect(@fixture.checkSpelling(enUS)).not.toEqual []
expect(@fixture.checkSpelling(deDE)).toEqual []
expect(@fixture.checkSpelling(frFR)).not.toEqual []
@fixture = new Spellchecker()
if @fixture.setDictionary('fr_FR', readDictionaryForLang('fr_FR'))
expect(@fixture.checkSpelling(enUS)).not.toEqual []
expect(@fixture.checkSpelling(deDE)).not.toEqual []
expect(@fixture.checkSpelling(frFR)).toEqual []
describe ".checkSpelling(string)", ->
beforeEach ->
@fixture = new Spellchecker()
@fixture.setDictionary defaultLanguage, readDictionaryForLang(defaultLanguage)
it "returns an array of character ranges of misspelled words", ->
string = "cat caat dog dooog"
expect(@fixture.checkSpelling(string)).toEqual [
{start: 4, end: 8},
{start: 13, end: 18},
]
it "accounts for UTF16 pairs", ->
string = "😎 cat caat dog dooog"
expect(@fixture.checkSpelling(string)).toEqual [
{start: 7, end: 11},
{start: 16, end: 21},
]
it "accounts for other non-word characters", ->
string = "'cat' (caat. <dog> :dooog)"
expect(@fixture.checkSpelling(string)).toEqual [
{start: 7, end: 11},
{start: 20, end: 25},
]
it "does not treat non-english letters as word boundaries", ->
@fixture.add("cliché")
expect(@fixture.checkSpelling("what cliché nonsense")).toEqual []
@fixture.remove("cliché")
it "handles words with apostrophes", ->
string = "doesn't isn't aint hasn't"
expect(@fixture.checkSpelling(string)).toEqual [
{start: string.indexOf("aint"), end: string.indexOf("aint") + 4}
]
string = "you say you're 'certain', but are you really?"
expect(@fixture.checkSpelling(string)).toEqual []
string = "you say you're 'sertan', but are you really?"
expect(@fixture.checkSpelling(string)).toEqual [
{start: string.indexOf("sertan"), end: string.indexOf("',")}
]
it "handles invalid inputs", ->
fixture = @fixture
expect(fixture.checkSpelling("")).toEqual []
expect(-> fixture.checkSpelling()).toThrow("Bad argument")
expect(-> fixture.checkSpelling(null)).toThrow("Bad argument")
expect(-> fixture.checkSpelling({})).toThrow("Bad argument")
describe ".getCorrectionsForMisspelling(word)", ->
beforeEach ->
@fixture = new Spellchecker()
@fixture.setDictionary defaultLanguage, readDictionaryForLang(defaultLanguage)
it "returns an array of possible corrections", ->
corrections = @fixture.getCorrectionsForMisspelling('worrd')
expect(corrections.length).toBeGreaterThan 0
expect(corrections.indexOf('word')).toBeGreaterThan -1
it "throws an exception when no word specified", ->
expect(-> @fixture.getCorrectionsForMisspelling()).toThrow()
describe ".add(word) and .remove(word)", ->
beforeEach ->
@fixture = new Spellchecker()
@fixture.setDictionary defaultLanguage, readDictionaryForLang(defaultLanguage)
it "allows words to be added and removed to the dictionary", ->
# NB: Windows spellchecker cannot remove words, and since it holds onto
# words, rerunning this test >1 time causes it to incorrectly fail
return if process.platform is 'win32'
expect(@fixture.isMisspelled('wwoorrdd')).toBe true
@fixture.add('wwoorrdd')
expect(@fixture.isMisspelled('wwoorrdd')).toBe false
@fixture.remove('wwoorrdd')
expect(@fixture.isMisspelled('wwoorrdd')).toBe true
it "add throws an error if no word is specified", ->
errorOccurred = false
try
@fixture.add()
catch
errorOccurred = true
expect(errorOccurred).toBe true
it "remove throws an error if no word is specified", ->
errorOccurred = false
try
@fixture.remove()
catch
errorOccurred = true
expect(errorOccurred).toBe true
describe ".getAvailableDictionaries()", ->
beforeEach ->
@fixture = new Spellchecker()
@fixture.setDictionary defaultLanguage, readDictionaryForLang(defaultLanguage)
it "returns an array of string dictionary names", ->
# NB: getAvailableDictionaries is nop'ped in hunspell and it also doesn't
# work inside Appveyor's CI environment
return if process.platform is 'linux' or process.env.CI or process.env.SPELLCHECKER_PREFER_HUNSPELL
dictionaries = @fixture.getAvailableDictionaries()
expect(Array.isArray(dictionaries)).toBe true
expect(dictionaries.length).toBeGreaterThan 0
for dictionary in dictionaries.length
expect(typeof dictionary).toBe 'string'
expect(diction.length).toBeGreaterThan 0
describe ".setDictionary(lang, dictDirectory)", ->
it "sets the spell checker's language, and dictionary directory", ->
awesome = true
expect(awesome).toBe true