| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.spell.SpellCheckTest'); |
| 16 |
goog.setTestOnly('goog.spell.SpellCheckTest'); |
| 17 |
|
| 18 |
goog.require('goog.spell.SpellCheck'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
var TEST_DATA = { |
| 22 |
'Test': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 23 |
'strnig': [goog.spell.SpellCheck.WordStatus.INVALID, []], |
| 24 |
'wtih': [goog.spell.SpellCheck.WordStatus.INVALID, []], |
| 25 |
'a': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 26 |
'few': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 27 |
'misspeled': [ |
| 28 |
goog.spell.SpellCheck.WordStatus.INVALID, |
| 29 |
['misspelled', 'misapplied', 'misspell'] |
| 30 |
], |
| 31 |
'words': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 32 |
'Testing': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 33 |
'set': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 34 |
'status': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 35 |
'vaild': [goog.spell.SpellCheck.WordStatus.INVALID, []], |
| 36 |
'invalid': [goog.spell.SpellCheck.WordStatus.VALID, []], |
| 37 |
'ignoerd': [goog.spell.SpellCheck.WordStatus.INVALID, []] |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* @param {!Array<string>} words |
| 42 |
* @param {!Object} spellChecker |
| 43 |
* @param {!Function} callback |
| 44 |
*/ |
| 45 |
function mockSpellCheckingFunction(words, spellChecker, callback) { |
| 46 |
var len = words.length; |
| 47 |
var data = []; |
| 48 |
for (var i = 0; i < len; i++) { |
| 49 |
var word = words[i]; |
| 50 |
var status = TEST_DATA[word][0]; |
| 51 |
var suggestions = TEST_DATA[word][1]; |
| 52 |
data.push([word, status, suggestions]); |
| 53 |
} |
| 54 |
callback.call(spellChecker, data); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
function testWordMatching() { |
| 59 |
var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction); |
| 60 |
|
| 61 |
var valid = goog.spell.SpellCheck.WordStatus.VALID; |
| 62 |
var invalid = goog.spell.SpellCheck.WordStatus.INVALID; |
| 63 |
|
| 64 |
spell.checkBlock('Test strnig wtih a few misspeled words.'); |
| 65 |
assertEquals(valid, spell.checkWord('Test')); |
| 66 |
assertEquals(invalid, spell.checkWord('strnig')); |
| 67 |
assertEquals(invalid, spell.checkWord('wtih')); |
| 68 |
assertEquals(valid, spell.checkWord('a')); |
| 69 |
assertEquals(valid, spell.checkWord('few')); |
| 70 |
assertEquals(invalid, spell.checkWord('misspeled')); |
| 71 |
assertEquals(valid, spell.checkWord('words')); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
function testSetWordStatusValid() { |
| 76 |
var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction); |
| 77 |
|
| 78 |
var valid = goog.spell.SpellCheck.WordStatus.VALID; |
| 79 |
|
| 80 |
spell.checkBlock('Testing set status vaild.'); |
| 81 |
spell.setWordStatus('vaild', valid); |
| 82 |
|
| 83 |
assertEquals(valid, spell.checkWord('vaild')); |
| 84 |
} |
| 85 |
|
| 86 |
function testSetWordStatusInvalid() { |
| 87 |
var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction); |
| 88 |
|
| 89 |
var invalid = goog.spell.SpellCheck.WordStatus.INVALID; |
| 90 |
|
| 91 |
spell.checkBlock('Testing set status invalid.'); |
| 92 |
spell.setWordStatus('invalid', invalid); |
| 93 |
|
| 94 |
assertEquals(invalid, spell.checkWord('invalid')); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
function testSetWordStatusIgnored() { |
| 99 |
var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction); |
| 100 |
|
| 101 |
var ignored = goog.spell.SpellCheck.WordStatus.IGNORED; |
| 102 |
|
| 103 |
spell.checkBlock('Testing set status ignoerd.'); |
| 104 |
spell.setWordStatus('ignoerd', ignored); |
| 105 |
|
| 106 |
assertEquals(ignored, spell.checkWord('ignoerd')); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
function testGetSuggestions() { |
| 111 |
var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction); |
| 112 |
|
| 113 |
spell.checkBlock('Test strnig wtih a few misspeled words.'); |
| 114 |
var suggestions = spell.getSuggestions('misspeled'); |
| 115 |
assertEquals(3, suggestions.length); |
| 116 |
} |
| 117 |
|
| 118 |
function testWordBoundaryRegex() { |
| 119 |
var regex = goog.spell.SpellCheck.WORD_BOUNDARY_REGEX; |
| 120 |
assertEquals(3, 'one two three'.split(regex).length); |
| 121 |
assertEquals(3, 'one/two/three'.split(regex).length); |
| 122 |
assertEquals(3, 'one-two-three'.split(regex).length); |
| 123 |
assertEquals(3, 'one.two.three'.split(regex).length); |
| 124 |
assertEquals(3, 'one,two,three'.split(regex).length); |
| 125 |
assertEquals(3, 'one[two]three'.split(regex).length); |
| 126 |
assertEquals(3, 'one\\two\\three'.split(regex).length); |
| 127 |
assertEquals(3, 'one\rtwo\nthree'.split(regex).length); |
| 128 |
assertEquals(3, 'one\ttwo\tthree'.split(regex).length); |
| 129 |
assertEquals(3, 'one!two~three'.split(regex).length); |
| 130 |
assertEquals(18, 'A"1#B$2%C!3(D)4*E+5:F;6<G>7=H?8@I^9'.split(regex).length); |
| 131 |
assertEquals(6, 'a_two`z{four|Z}six'.split(regex).length); |
| 132 |
} |