1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/test/unit/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + * 1.8 + * This is a shim for the W3C testharness.js, mapping those of 1.9 + * its functions that we need to the testing/xpcshell/head.js API. 1.10 + * See <http://www.w3.org/2008/webapps/wiki/Harness> for documentation. 1.11 + * This shim does some tests a little differently than the W3C test 1.12 + * harness; equality comparisons, especially, are less precise. 1.13 + * The difference does not presently affect any test results. 1.14 + * 1.15 + * We use the lower-level do_report_result throughout this file, 1.16 + * rather than the high-level xpcshell/head.js API that has near 1.17 + * equivalents for the W3C assert_* functions, because only 1.18 + * do_report_result allows us to provide Components.stack.caller. 1.19 + */ 1.20 + 1.21 +function assert_equals(a, b, msg) { 1.22 + let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + 1.23 + " == " + _wrap_with_quotes_if_necessary(b); 1.24 + do_report_result(a == b, text, Components.stack.caller, false); 1.25 +} 1.26 + 1.27 +function assert_not_equals(a, b, msg) { 1.28 + let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + 1.29 + " != " + _wrap_with_quotes_if_necessary(b); 1.30 + do_report_result(a != b, text, Components.stack.caller, false); 1.31 +} 1.32 + 1.33 +function assert_array_equals(a, b, msg) { 1.34 + do_report_result(a.length == b.length, 1.35 + msg + ": (length) " + a.length + " == " + b.length, 1.36 + Components.stack.caller, false); 1.37 + for (let i = 0; i < a.length; ++i) { 1.38 + if (a[i] !== b[i]) { 1.39 + do_report_result(false, 1.40 + msg + ": [" + i + "] " + 1.41 + _wrap_with_quotes_if_necessary(a[i]) + 1.42 + " === " + 1.43 + _wrap_with_quotes_if_necessary(b[i]), 1.44 + Components.stack.caller, false); 1.45 + } 1.46 + } 1.47 + // If we get here, all array elements are equal. 1.48 + do_report_result(true, msg + ": all array elements equal", 1.49 + Components.stack.caller, false); 1.50 +} 1.51 + 1.52 +function assert_true(cond, msg) { 1.53 + do_report_result(!!cond, msg + ": " + uneval(cond), 1.54 + Components.stack.caller, false); 1.55 +} 1.56 + 1.57 +function assert_throws(ex, func) { 1.58 + if (!('name' in ex)) 1.59 + do_throw("first argument to assert_throws must be of the form " + 1.60 + "{'name': something}"); 1.61 + 1.62 + let msg = "expected to catch an exception named " + ex.name; 1.63 + 1.64 + try { 1.65 + func(); 1.66 + } catch (e) { 1.67 + if ('name' in e) 1.68 + do_report_result(e.name == ex.name, 1.69 + msg + ", got " + e.name, 1.70 + Components.stack.caller, false); 1.71 + else 1.72 + do_report_result(false, 1.73 + msg + ", got " + legible_exception(ex), 1.74 + Components.stack.caller, false); 1.75 + 1.76 + return; 1.77 + } 1.78 + 1.79 + // Call this here, not in the 'try' clause, so do_report_result's own 1.80 + // throw doesn't get caught by our 'catch' clause. 1.81 + do_report_result(false, msg + ", but returned normally", 1.82 + Components.stack.caller, false); 1.83 +} 1.84 + 1.85 +let tests = []; 1.86 + 1.87 +function test(func, msg) { 1.88 + tests.push({msg: msg, func: func, 1.89 + filename: Components.stack.caller.filename }); 1.90 +} 1.91 + 1.92 +function run_test() { 1.93 + tests.forEach(function(t) { 1.94 + _log("test_info", {source_file: t.filename, 1.95 + diagnostic: "test group: " + t.msg}); 1.96 + t.func(); 1.97 + }); 1.98 +};