extensions/spellcheck/hunspell/tests/unit/test_hunspell.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7
michael@0 8 const tests = [
michael@0 9 ["affixes", "iso-8859-1"],
michael@0 10 ["condition", "iso-8859-1"],
michael@0 11 ["condition_utf", "UTF-8"],
michael@0 12 ["base", "iso-8859-1"],
michael@0 13 ["base_utf", "UTF-8"],
michael@0 14 ["allcaps", "iso-8859-1"],
michael@0 15 ["allcaps_utf", "UTF-8"],
michael@0 16 ["allcaps2", "iso-8859-1"],
michael@0 17 ["allcaps3", "iso-8859-1"],
michael@0 18 ["keepcase", "iso-8859-1"],
michael@0 19 ["i58202", "iso-8859-1"],
michael@0 20 ["map", "iso-8859-1"],
michael@0 21 ["rep", "iso-8859-1"],
michael@0 22 ["sug", "iso-8859-1"],
michael@0 23 ["sugutf", "UTF-8"],
michael@0 24 ["phone", "iso-8859-1"],
michael@0 25 ["flag", "iso-8859-1"],
michael@0 26 ["flaglong", "iso-8859-1"],
michael@0 27 ["flagnum", "iso-8859-1"],
michael@0 28 ["flagutf8", "UTF-8"],
michael@0 29 ["slash", "iso-8859-1"],
michael@0 30 ["forbiddenword", "iso-8859-1"],
michael@0 31 ["nosuggest", "iso-8859-1"],
michael@0 32 ["alias", "iso-8859-1"],
michael@0 33 ["alias2", "iso-8859-1"],
michael@0 34 ["alias3", "iso-8859-1"],
michael@0 35 ["breakdefault", "iso-8859-1"],
michael@0 36 ["break", "UTF-8"],
michael@0 37 ["needaffix", "iso-8859-1"],
michael@0 38 ["needaffix2", "iso-8859-1"],
michael@0 39 ["needaffix3", "iso-8859-1"],
michael@0 40 ["needaffix4", "iso-8859-1"],
michael@0 41 ["needaffix5", "iso-8859-1"],
michael@0 42 ["circumfix", "iso-8859-1"],
michael@0 43 ["fogemorpheme", "iso-8859-1"],
michael@0 44 ["onlyincompound", "iso-8859-1"],
michael@0 45 ["complexprefixes", "iso-8859-1"],
michael@0 46 ["complexprefixes2", "iso-8859-1"],
michael@0 47 ["complexprefixesutf", "UTF-8"],
michael@0 48 ["conditionalprefix", "iso-8859-1"],
michael@0 49 ["zeroaffix", "iso-8859-1"],
michael@0 50 ["utf8", "UTF-8"],
michael@0 51 ["utf8_bom", "UTF-8", {1: "todo"}],
michael@0 52 ["utf8_bom2", "UTF-8", {1: "todo"}],
michael@0 53 ["utf8_nonbmp", "UTF-8", {1: "todo", 2: "todo", 3: "todo", 4: "todo"}],
michael@0 54 ["compoundflag", "iso-8859-1"],
michael@0 55 ["compoundrule", "iso-8859-1"],
michael@0 56 ["compoundrule2", "iso-8859-1"],
michael@0 57 ["compoundrule3", "iso-8859-1"],
michael@0 58 ["compoundrule4", "iso-8859-1"],
michael@0 59 ["compoundrule5", "UTF-8"],
michael@0 60 ["compoundrule6", "iso-8859-1"],
michael@0 61 ["compoundrule7", "iso-8859-1"],
michael@0 62 ["compoundrule8", "iso-8859-1"],
michael@0 63 ["compoundaffix", "iso-8859-1"],
michael@0 64 ["compoundaffix2", "iso-8859-1"],
michael@0 65 ["compoundaffix3", "iso-8859-1"],
michael@0 66 ["checkcompounddup", "iso-8859-1"],
michael@0 67 ["checkcompoundtriple", "iso-8859-1"],
michael@0 68 ["simplifiedtriple", "iso-8859-1"],
michael@0 69 ["checkcompoundrep", "iso-8859-1"],
michael@0 70 ["checkcompoundcase2", "iso-8859-1"],
michael@0 71 ["checkcompoundcaseutf", "UTF-8"],
michael@0 72 ["checkcompoundpattern", "iso-8859-1"],
michael@0 73 ["checkcompoundpattern2", "iso-8859-1"],
michael@0 74 ["checkcompoundpattern3", "iso-8859-1"],
michael@0 75 ["checkcompoundpattern4", "iso-8859-1"],
michael@0 76 ["utfcompound", "UTF-8"],
michael@0 77 ["checksharps", "iso-8859-1"],
michael@0 78 ["checksharpsutf", "UTF-8"],
michael@0 79 ["germancompounding", "iso-8859-1"],
michael@0 80 ["germancompoundingold", "iso-8859-1"],
michael@0 81 ["i35725", "iso-8859-1"],
michael@0 82 ["i53643", "iso-8859-1"],
michael@0 83 ["i54633", "iso-8859-1"],
michael@0 84 ["i54980", "iso-8859-1", {1: "todo", 3: "todo"}],
michael@0 85 ["maputf", "UTF-8"],
michael@0 86 ["reputf", "UTF-8"],
michael@0 87 ["ignore", "iso-8859-1"],
michael@0 88 ["ignoreutf", "UTF-8",
michael@0 89 {1: "todo", 2: "todo", 3: "todo", 4: "todo", 5: "todo", 6: "todo",
michael@0 90 7: "todo", 8: "todo"}],
michael@0 91 ["1592880", "iso-8859-1"],
michael@0 92 ["1695964", "iso-8859-1"],
michael@0 93 ["1463589", "iso-8859-1"],
michael@0 94 ["1463589_utf", "UTF-8"],
michael@0 95 ["IJ", "iso-8859-1"],
michael@0 96 ["i68568", "iso-8859-1"],
michael@0 97 ["i68568utf", "UTF-8"],
michael@0 98 ["1706659", "iso-8859-1"],
michael@0 99 ["digits_in_words", "iso-8859-1"],
michael@0 100 // ["colons_in_words", "iso-8859-1"], Suggestion test only
michael@0 101 ["ngram_utf_fix", "UTF-8"],
michael@0 102 ["morph", "us-ascii",
michael@0 103 {11: "todo", 12: "todo", 13: "todo", 14: "todo", 15: "todo", 16: "todo",
michael@0 104 17: "todo", 18: "todo", 19: "todo", 20: "todo", 21: "todo", 22: "todo",
michael@0 105 23: "todo", 24: "todo", 25: "todo", 26: "todo", 27: "todo"}],
michael@0 106 ["1975530", "UTF-8"],
michael@0 107 ["fullstrip", "iso-8859-1"],
michael@0 108 ["iconv", "UTF-8"],
michael@0 109 ["oconv", "UTF-8"],
michael@0 110 ["encoding", "iso-8859-1", {1: "todo", 3: "todo"}],
michael@0 111 ["korean", "UTF-8"],
michael@0 112 ["opentaal_forbiddenword1", "UTF-8"],
michael@0 113 ["opentaal_forbiddenword2", "UTF-8"],
michael@0 114 ["opentaal_keepcase", "UTF-8"],
michael@0 115 ["arabic", "UTF-8"],
michael@0 116 ["2970240", "iso-8859-1"],
michael@0 117 ["2970242", "iso-8859-1"],
michael@0 118 ["breakoff", "iso-8859-1"],
michael@0 119 ["opentaal_cpdpat", "iso-8859-1"],
michael@0 120 ["opentaal_cpdpat2", "iso-8859-1"],
michael@0 121 ["2999225", "iso-8859-1"],
michael@0 122 ["onlyincompound2", "iso-8859-1"],
michael@0 123 ["forceucase", "iso-8859-1"],
michael@0 124 ["warn", "iso-8859-1"]
michael@0 125 ];
michael@0 126
michael@0 127 function do_get_file_by_line(file, charset) {
michael@0 128 dump("getting file by line for file " + file.path + "\n");
michael@0 129 dump("using charset " + charset +"\n");
michael@0 130 let fis = Cc["@mozilla.org/network/file-input-stream;1"].
michael@0 131 createInstance(Ci.nsIFileInputStream);
michael@0 132 fis.init(file, 0x1 /* READONLY */,
michael@0 133 0444, Ci.nsIFileInputStream.CLOSE_ON_EOF);
michael@0 134
michael@0 135 let lis = Cc["@mozilla.org/intl/converter-input-stream;1"].
michael@0 136 createInstance(Ci.nsIConverterInputStream);
michael@0 137 lis.init(fis, charset, 1024, 0);
michael@0 138 lis.QueryInterface(Ci.nsIUnicharLineInputStream);
michael@0 139
michael@0 140 var val = {};
michael@0 141 while (lis.readLine(val)) {
michael@0 142 yield val.value;
michael@0 143 val = {};
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 function do_run_test(checker, name, charset, todo_good, todo_bad) {
michael@0 148 dump("\n\n\n\n");
michael@0 149 dump("running test for " + name + "\n");
michael@0 150 if (!checker) {
michael@0 151 do_throw("Need spell checker here!");
michael@0 152 }
michael@0 153
michael@0 154 let good = do_get_file("data/" + name + ".good", true);
michael@0 155 let bad = do_get_file("data/" + name + ".wrong", true);
michael@0 156 let sug = do_get_file("data/" + name + ".sug", true);
michael@0 157
michael@0 158 dump("Need some expected output\n")
michael@0 159 do_check_true(good.exists() || bad.exists() || sug.exists());
michael@0 160
michael@0 161 dump("Setting dictionary to " + name + "\n");
michael@0 162 checker.dictionary = name;
michael@0 163
michael@0 164 if (good.exists()) {
michael@0 165 var good_counter = 0;
michael@0 166 for (val in do_get_file_by_line(good, charset)) {
michael@0 167 let todo = false;
michael@0 168 good_counter++;
michael@0 169 if (todo_good && todo_good[good_counter]) {
michael@0 170 todo = true;
michael@0 171 dump("TODO\n");
michael@0 172 }
michael@0 173
michael@0 174 dump("Expect word " + val + " is spelled correctly\n");
michael@0 175 if (todo) {
michael@0 176 todo_check_true(checker.check(val));
michael@0 177 } else {
michael@0 178 do_check_true(checker.check(val));
michael@0 179 }
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 if (bad.exists()) {
michael@0 184 var bad_counter = 0;
michael@0 185 for (val in do_get_file_by_line(bad, charset)) {
michael@0 186 let todo = false;
michael@0 187 bad_counter++;
michael@0 188 if (todo_bad && todo_bad[bad_counter]) {
michael@0 189 todo = true;
michael@0 190 dump("TODO\n");
michael@0 191 }
michael@0 192
michael@0 193 dump("Expect word " + val + " is spelled wrong\n");
michael@0 194 if (todo) {
michael@0 195 todo_check_false(checker.check(val));
michael@0 196 } else {
michael@0 197 do_check_false(checker.check(val));
michael@0 198 }
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 // XXXkhuey test suggestions
michael@0 203 }
michael@0 204
michael@0 205 function run_test() {
michael@0 206 let spellChecker = Cc["@mozilla.org/spellchecker/engine;1"].
michael@0 207 getService(Ci.mozISpellCheckingEngine);
michael@0 208
michael@0 209 do_check_true(!!spellChecker, "Should have a spell checker");
michael@0 210 spellChecker.QueryInterface(Ci.mozISpellCheckingEngine);
michael@0 211 let testdir = do_get_file("data/", false);
michael@0 212 spellChecker.loadDictionariesFromDir(testdir);
michael@0 213
michael@0 214 function do_run_test_closure(test) {
michael@0 215 [name, charset, todo_good, todo_bad] = test;
michael@0 216 do_run_test(spellChecker, name, charset, todo_good, todo_bad);
michael@0 217 }
michael@0 218
michael@0 219 tests.forEach(do_run_test_closure);
michael@0 220 }

mercurial