michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: * michael@0: * This is a shim for the W3C testharness.js, mapping those of michael@0: * its functions that we need to the testing/xpcshell/head.js API. michael@0: * See for documentation. michael@0: * This shim does some tests a little differently than the W3C test michael@0: * harness; equality comparisons, especially, are less precise. michael@0: * The difference does not presently affect any test results. michael@0: * michael@0: * We use the lower-level do_report_result throughout this file, michael@0: * rather than the high-level xpcshell/head.js API that has near michael@0: * equivalents for the W3C assert_* functions, because only michael@0: * do_report_result allows us to provide Components.stack.caller. michael@0: */ michael@0: michael@0: function assert_equals(a, b, msg) { michael@0: let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + michael@0: " == " + _wrap_with_quotes_if_necessary(b); michael@0: do_report_result(a == b, text, Components.stack.caller, false); michael@0: } michael@0: michael@0: function assert_not_equals(a, b, msg) { michael@0: let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + michael@0: " != " + _wrap_with_quotes_if_necessary(b); michael@0: do_report_result(a != b, text, Components.stack.caller, false); michael@0: } michael@0: michael@0: function assert_array_equals(a, b, msg) { michael@0: do_report_result(a.length == b.length, michael@0: msg + ": (length) " + a.length + " == " + b.length, michael@0: Components.stack.caller, false); michael@0: for (let i = 0; i < a.length; ++i) { michael@0: if (a[i] !== b[i]) { michael@0: do_report_result(false, michael@0: msg + ": [" + i + "] " + michael@0: _wrap_with_quotes_if_necessary(a[i]) + michael@0: " === " + michael@0: _wrap_with_quotes_if_necessary(b[i]), michael@0: Components.stack.caller, false); michael@0: } michael@0: } michael@0: // If we get here, all array elements are equal. michael@0: do_report_result(true, msg + ": all array elements equal", michael@0: Components.stack.caller, false); michael@0: } michael@0: michael@0: function assert_true(cond, msg) { michael@0: do_report_result(!!cond, msg + ": " + uneval(cond), michael@0: Components.stack.caller, false); michael@0: } michael@0: michael@0: function assert_throws(ex, func) { michael@0: if (!('name' in ex)) michael@0: do_throw("first argument to assert_throws must be of the form " + michael@0: "{'name': something}"); michael@0: michael@0: let msg = "expected to catch an exception named " + ex.name; michael@0: michael@0: try { michael@0: func(); michael@0: } catch (e) { michael@0: if ('name' in e) michael@0: do_report_result(e.name == ex.name, michael@0: msg + ", got " + e.name, michael@0: Components.stack.caller, false); michael@0: else michael@0: do_report_result(false, michael@0: msg + ", got " + legible_exception(ex), michael@0: Components.stack.caller, false); michael@0: michael@0: return; michael@0: } michael@0: michael@0: // Call this here, not in the 'try' clause, so do_report_result's own michael@0: // throw doesn't get caught by our 'catch' clause. michael@0: do_report_result(false, msg + ", but returned normally", michael@0: Components.stack.caller, false); michael@0: } michael@0: michael@0: let tests = []; michael@0: michael@0: function test(func, msg) { michael@0: tests.push({msg: msg, func: func, michael@0: filename: Components.stack.caller.filename }); michael@0: } michael@0: michael@0: function run_test() { michael@0: tests.forEach(function(t) { michael@0: _log("test_info", {source_file: t.filename, michael@0: diagnostic: "test group: " + t.msg}); michael@0: t.func(); michael@0: }); michael@0: };