1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/spellcheck/hunspell/tests/unit/test_hunspell.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,220 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +const Cc = Components.classes; 1.9 +const Ci = Components.interfaces; 1.10 + 1.11 +const tests = [ 1.12 + ["affixes", "iso-8859-1"], 1.13 + ["condition", "iso-8859-1"], 1.14 + ["condition_utf", "UTF-8"], 1.15 + ["base", "iso-8859-1"], 1.16 + ["base_utf", "UTF-8"], 1.17 + ["allcaps", "iso-8859-1"], 1.18 + ["allcaps_utf", "UTF-8"], 1.19 + ["allcaps2", "iso-8859-1"], 1.20 + ["allcaps3", "iso-8859-1"], 1.21 + ["keepcase", "iso-8859-1"], 1.22 + ["i58202", "iso-8859-1"], 1.23 + ["map", "iso-8859-1"], 1.24 + ["rep", "iso-8859-1"], 1.25 + ["sug", "iso-8859-1"], 1.26 + ["sugutf", "UTF-8"], 1.27 + ["phone", "iso-8859-1"], 1.28 + ["flag", "iso-8859-1"], 1.29 + ["flaglong", "iso-8859-1"], 1.30 + ["flagnum", "iso-8859-1"], 1.31 + ["flagutf8", "UTF-8"], 1.32 + ["slash", "iso-8859-1"], 1.33 + ["forbiddenword", "iso-8859-1"], 1.34 + ["nosuggest", "iso-8859-1"], 1.35 + ["alias", "iso-8859-1"], 1.36 + ["alias2", "iso-8859-1"], 1.37 + ["alias3", "iso-8859-1"], 1.38 + ["breakdefault", "iso-8859-1"], 1.39 + ["break", "UTF-8"], 1.40 + ["needaffix", "iso-8859-1"], 1.41 + ["needaffix2", "iso-8859-1"], 1.42 + ["needaffix3", "iso-8859-1"], 1.43 + ["needaffix4", "iso-8859-1"], 1.44 + ["needaffix5", "iso-8859-1"], 1.45 + ["circumfix", "iso-8859-1"], 1.46 + ["fogemorpheme", "iso-8859-1"], 1.47 + ["onlyincompound", "iso-8859-1"], 1.48 + ["complexprefixes", "iso-8859-1"], 1.49 + ["complexprefixes2", "iso-8859-1"], 1.50 + ["complexprefixesutf", "UTF-8"], 1.51 + ["conditionalprefix", "iso-8859-1"], 1.52 + ["zeroaffix", "iso-8859-1"], 1.53 + ["utf8", "UTF-8"], 1.54 + ["utf8_bom", "UTF-8", {1: "todo"}], 1.55 + ["utf8_bom2", "UTF-8", {1: "todo"}], 1.56 + ["utf8_nonbmp", "UTF-8", {1: "todo", 2: "todo", 3: "todo", 4: "todo"}], 1.57 + ["compoundflag", "iso-8859-1"], 1.58 + ["compoundrule", "iso-8859-1"], 1.59 + ["compoundrule2", "iso-8859-1"], 1.60 + ["compoundrule3", "iso-8859-1"], 1.61 + ["compoundrule4", "iso-8859-1"], 1.62 + ["compoundrule5", "UTF-8"], 1.63 + ["compoundrule6", "iso-8859-1"], 1.64 + ["compoundrule7", "iso-8859-1"], 1.65 + ["compoundrule8", "iso-8859-1"], 1.66 + ["compoundaffix", "iso-8859-1"], 1.67 + ["compoundaffix2", "iso-8859-1"], 1.68 + ["compoundaffix3", "iso-8859-1"], 1.69 + ["checkcompounddup", "iso-8859-1"], 1.70 + ["checkcompoundtriple", "iso-8859-1"], 1.71 + ["simplifiedtriple", "iso-8859-1"], 1.72 + ["checkcompoundrep", "iso-8859-1"], 1.73 + ["checkcompoundcase2", "iso-8859-1"], 1.74 + ["checkcompoundcaseutf", "UTF-8"], 1.75 + ["checkcompoundpattern", "iso-8859-1"], 1.76 + ["checkcompoundpattern2", "iso-8859-1"], 1.77 + ["checkcompoundpattern3", "iso-8859-1"], 1.78 + ["checkcompoundpattern4", "iso-8859-1"], 1.79 + ["utfcompound", "UTF-8"], 1.80 + ["checksharps", "iso-8859-1"], 1.81 + ["checksharpsutf", "UTF-8"], 1.82 + ["germancompounding", "iso-8859-1"], 1.83 + ["germancompoundingold", "iso-8859-1"], 1.84 + ["i35725", "iso-8859-1"], 1.85 + ["i53643", "iso-8859-1"], 1.86 + ["i54633", "iso-8859-1"], 1.87 + ["i54980", "iso-8859-1", {1: "todo", 3: "todo"}], 1.88 + ["maputf", "UTF-8"], 1.89 + ["reputf", "UTF-8"], 1.90 + ["ignore", "iso-8859-1"], 1.91 + ["ignoreutf", "UTF-8", 1.92 + {1: "todo", 2: "todo", 3: "todo", 4: "todo", 5: "todo", 6: "todo", 1.93 + 7: "todo", 8: "todo"}], 1.94 + ["1592880", "iso-8859-1"], 1.95 + ["1695964", "iso-8859-1"], 1.96 + ["1463589", "iso-8859-1"], 1.97 + ["1463589_utf", "UTF-8"], 1.98 + ["IJ", "iso-8859-1"], 1.99 + ["i68568", "iso-8859-1"], 1.100 + ["i68568utf", "UTF-8"], 1.101 + ["1706659", "iso-8859-1"], 1.102 + ["digits_in_words", "iso-8859-1"], 1.103 +// ["colons_in_words", "iso-8859-1"], Suggestion test only 1.104 + ["ngram_utf_fix", "UTF-8"], 1.105 + ["morph", "us-ascii", 1.106 + {11: "todo", 12: "todo", 13: "todo", 14: "todo", 15: "todo", 16: "todo", 1.107 + 17: "todo", 18: "todo", 19: "todo", 20: "todo", 21: "todo", 22: "todo", 1.108 + 23: "todo", 24: "todo", 25: "todo", 26: "todo", 27: "todo"}], 1.109 + ["1975530", "UTF-8"], 1.110 + ["fullstrip", "iso-8859-1"], 1.111 + ["iconv", "UTF-8"], 1.112 + ["oconv", "UTF-8"], 1.113 + ["encoding", "iso-8859-1", {1: "todo", 3: "todo"}], 1.114 + ["korean", "UTF-8"], 1.115 + ["opentaal_forbiddenword1", "UTF-8"], 1.116 + ["opentaal_forbiddenword2", "UTF-8"], 1.117 + ["opentaal_keepcase", "UTF-8"], 1.118 + ["arabic", "UTF-8"], 1.119 + ["2970240", "iso-8859-1"], 1.120 + ["2970242", "iso-8859-1"], 1.121 + ["breakoff", "iso-8859-1"], 1.122 + ["opentaal_cpdpat", "iso-8859-1"], 1.123 + ["opentaal_cpdpat2", "iso-8859-1"], 1.124 + ["2999225", "iso-8859-1"], 1.125 + ["onlyincompound2", "iso-8859-1"], 1.126 + ["forceucase", "iso-8859-1"], 1.127 + ["warn", "iso-8859-1"] 1.128 +]; 1.129 + 1.130 +function do_get_file_by_line(file, charset) { 1.131 + dump("getting file by line for file " + file.path + "\n"); 1.132 + dump("using charset " + charset +"\n"); 1.133 + let fis = Cc["@mozilla.org/network/file-input-stream;1"]. 1.134 + createInstance(Ci.nsIFileInputStream); 1.135 + fis.init(file, 0x1 /* READONLY */, 1.136 + 0444, Ci.nsIFileInputStream.CLOSE_ON_EOF); 1.137 + 1.138 + let lis = Cc["@mozilla.org/intl/converter-input-stream;1"]. 1.139 + createInstance(Ci.nsIConverterInputStream); 1.140 + lis.init(fis, charset, 1024, 0); 1.141 + lis.QueryInterface(Ci.nsIUnicharLineInputStream); 1.142 + 1.143 + var val = {}; 1.144 + while (lis.readLine(val)) { 1.145 + yield val.value; 1.146 + val = {}; 1.147 + } 1.148 +} 1.149 + 1.150 +function do_run_test(checker, name, charset, todo_good, todo_bad) { 1.151 + dump("\n\n\n\n"); 1.152 + dump("running test for " + name + "\n"); 1.153 + if (!checker) { 1.154 + do_throw("Need spell checker here!"); 1.155 + } 1.156 + 1.157 + let good = do_get_file("data/" + name + ".good", true); 1.158 + let bad = do_get_file("data/" + name + ".wrong", true); 1.159 + let sug = do_get_file("data/" + name + ".sug", true); 1.160 + 1.161 + dump("Need some expected output\n") 1.162 + do_check_true(good.exists() || bad.exists() || sug.exists()); 1.163 + 1.164 + dump("Setting dictionary to " + name + "\n"); 1.165 + checker.dictionary = name; 1.166 + 1.167 + if (good.exists()) { 1.168 + var good_counter = 0; 1.169 + for (val in do_get_file_by_line(good, charset)) { 1.170 + let todo = false; 1.171 + good_counter++; 1.172 + if (todo_good && todo_good[good_counter]) { 1.173 + todo = true; 1.174 + dump("TODO\n"); 1.175 + } 1.176 + 1.177 + dump("Expect word " + val + " is spelled correctly\n"); 1.178 + if (todo) { 1.179 + todo_check_true(checker.check(val)); 1.180 + } else { 1.181 + do_check_true(checker.check(val)); 1.182 + } 1.183 + } 1.184 + } 1.185 + 1.186 + if (bad.exists()) { 1.187 + var bad_counter = 0; 1.188 + for (val in do_get_file_by_line(bad, charset)) { 1.189 + let todo = false; 1.190 + bad_counter++; 1.191 + if (todo_bad && todo_bad[bad_counter]) { 1.192 + todo = true; 1.193 + dump("TODO\n"); 1.194 + } 1.195 + 1.196 + dump("Expect word " + val + " is spelled wrong\n"); 1.197 + if (todo) { 1.198 + todo_check_false(checker.check(val)); 1.199 + } else { 1.200 + do_check_false(checker.check(val)); 1.201 + } 1.202 + } 1.203 + } 1.204 + 1.205 + // XXXkhuey test suggestions 1.206 +} 1.207 + 1.208 +function run_test() { 1.209 + let spellChecker = Cc["@mozilla.org/spellchecker/engine;1"]. 1.210 + getService(Ci.mozISpellCheckingEngine); 1.211 + 1.212 + do_check_true(!!spellChecker, "Should have a spell checker"); 1.213 + spellChecker.QueryInterface(Ci.mozISpellCheckingEngine); 1.214 + let testdir = do_get_file("data/", false); 1.215 + spellChecker.loadDictionariesFromDir(testdir); 1.216 + 1.217 + function do_run_test_closure(test) { 1.218 + [name, charset, todo_good, todo_bad] = test; 1.219 + do_run_test(spellChecker, name, charset, todo_good, todo_bad); 1.220 + } 1.221 + 1.222 + tests.forEach(do_run_test_closure); 1.223 +}