spellcheck_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. goog.provide('goog.spell.SpellCheckTest');
  15. goog.setTestOnly('goog.spell.SpellCheckTest');
  16. goog.require('goog.spell.SpellCheck');
  17. goog.require('goog.testing.jsunit');
  18. var TEST_DATA = {
  19. 'Test': [goog.spell.SpellCheck.WordStatus.VALID, []],
  20. 'strnig': [goog.spell.SpellCheck.WordStatus.INVALID, []],
  21. 'wtih': [goog.spell.SpellCheck.WordStatus.INVALID, []],
  22. 'a': [goog.spell.SpellCheck.WordStatus.VALID, []],
  23. 'few': [goog.spell.SpellCheck.WordStatus.VALID, []],
  24. 'misspeled': [
  25. goog.spell.SpellCheck.WordStatus.INVALID,
  26. ['misspelled', 'misapplied', 'misspell']
  27. ],
  28. 'words': [goog.spell.SpellCheck.WordStatus.VALID, []],
  29. 'Testing': [goog.spell.SpellCheck.WordStatus.VALID, []],
  30. 'set': [goog.spell.SpellCheck.WordStatus.VALID, []],
  31. 'status': [goog.spell.SpellCheck.WordStatus.VALID, []],
  32. 'vaild': [goog.spell.SpellCheck.WordStatus.INVALID, []],
  33. 'invalid': [goog.spell.SpellCheck.WordStatus.VALID, []],
  34. 'ignoerd': [goog.spell.SpellCheck.WordStatus.INVALID, []]
  35. };
  36. function mockSpellCheckingFunction(words, spellChecker, callback) {
  37. var len = words.length;
  38. var data = [];
  39. for (var i = 0; i < len; i++) {
  40. var word = words[i];
  41. var status = TEST_DATA[word][0];
  42. var suggestions = TEST_DATA[word][1];
  43. data.push([word, status, suggestions]);
  44. }
  45. callback.call(spellChecker, data);
  46. }
  47. function testWordMatching() {
  48. var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction);
  49. var valid = goog.spell.SpellCheck.WordStatus.VALID;
  50. var invalid = goog.spell.SpellCheck.WordStatus.INVALID;
  51. spell.checkBlock('Test strnig wtih a few misspeled words.');
  52. assertEquals(valid, spell.checkWord('Test'));
  53. assertEquals(invalid, spell.checkWord('strnig'));
  54. assertEquals(invalid, spell.checkWord('wtih'));
  55. assertEquals(valid, spell.checkWord('a'));
  56. assertEquals(valid, spell.checkWord('few'));
  57. assertEquals(invalid, spell.checkWord('misspeled'));
  58. assertEquals(valid, spell.checkWord('words'));
  59. }
  60. function testSetWordStatusValid() {
  61. var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction);
  62. var valid = goog.spell.SpellCheck.WordStatus.VALID;
  63. spell.checkBlock('Testing set status vaild.');
  64. spell.setWordStatus('vaild', valid);
  65. assertEquals(valid, spell.checkWord('vaild'));
  66. }
  67. function testSetWordStatusInvalid() {
  68. var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction);
  69. var valid = goog.spell.SpellCheck.WordStatus.VALID;
  70. var invalid = goog.spell.SpellCheck.WordStatus.INVALID;
  71. spell.checkBlock('Testing set status invalid.');
  72. spell.setWordStatus('invalid', invalid);
  73. assertEquals(invalid, spell.checkWord('invalid'));
  74. }
  75. function testSetWordStatusIgnored() {
  76. var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction);
  77. var ignored = goog.spell.SpellCheck.WordStatus.IGNORED;
  78. spell.checkBlock('Testing set status ignoerd.');
  79. spell.setWordStatus('ignoerd', ignored);
  80. assertEquals(ignored, spell.checkWord('ignoerd'));
  81. }
  82. function testGetSuggestions() {
  83. var spell = new goog.spell.SpellCheck(mockSpellCheckingFunction);
  84. spell.checkBlock('Test strnig wtih a few misspeled words.');
  85. var suggestions = spell.getSuggestions('misspeled');
  86. assertEquals(3, suggestions.length);
  87. }