Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | * |
michael@0 | 5 | * This is a shim for the W3C testharness.js, mapping those of |
michael@0 | 6 | * its functions that we need to the testing/xpcshell/head.js API. |
michael@0 | 7 | * See <http://www.w3.org/2008/webapps/wiki/Harness> for documentation. |
michael@0 | 8 | * This shim does some tests a little differently than the W3C test |
michael@0 | 9 | * harness; equality comparisons, especially, are less precise. |
michael@0 | 10 | * The difference does not presently affect any test results. |
michael@0 | 11 | * |
michael@0 | 12 | * We use the lower-level do_report_result throughout this file, |
michael@0 | 13 | * rather than the high-level xpcshell/head.js API that has near |
michael@0 | 14 | * equivalents for the W3C assert_* functions, because only |
michael@0 | 15 | * do_report_result allows us to provide Components.stack.caller. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | function assert_equals(a, b, msg) { |
michael@0 | 19 | let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + |
michael@0 | 20 | " == " + _wrap_with_quotes_if_necessary(b); |
michael@0 | 21 | do_report_result(a == b, text, Components.stack.caller, false); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | function assert_not_equals(a, b, msg) { |
michael@0 | 25 | let text = msg + ": " + _wrap_with_quotes_if_necessary(a) + |
michael@0 | 26 | " != " + _wrap_with_quotes_if_necessary(b); |
michael@0 | 27 | do_report_result(a != b, text, Components.stack.caller, false); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | function assert_array_equals(a, b, msg) { |
michael@0 | 31 | do_report_result(a.length == b.length, |
michael@0 | 32 | msg + ": (length) " + a.length + " == " + b.length, |
michael@0 | 33 | Components.stack.caller, false); |
michael@0 | 34 | for (let i = 0; i < a.length; ++i) { |
michael@0 | 35 | if (a[i] !== b[i]) { |
michael@0 | 36 | do_report_result(false, |
michael@0 | 37 | msg + ": [" + i + "] " + |
michael@0 | 38 | _wrap_with_quotes_if_necessary(a[i]) + |
michael@0 | 39 | " === " + |
michael@0 | 40 | _wrap_with_quotes_if_necessary(b[i]), |
michael@0 | 41 | Components.stack.caller, false); |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | // If we get here, all array elements are equal. |
michael@0 | 45 | do_report_result(true, msg + ": all array elements equal", |
michael@0 | 46 | Components.stack.caller, false); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | function assert_true(cond, msg) { |
michael@0 | 50 | do_report_result(!!cond, msg + ": " + uneval(cond), |
michael@0 | 51 | Components.stack.caller, false); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | function assert_throws(ex, func) { |
michael@0 | 55 | if (!('name' in ex)) |
michael@0 | 56 | do_throw("first argument to assert_throws must be of the form " + |
michael@0 | 57 | "{'name': something}"); |
michael@0 | 58 | |
michael@0 | 59 | let msg = "expected to catch an exception named " + ex.name; |
michael@0 | 60 | |
michael@0 | 61 | try { |
michael@0 | 62 | func(); |
michael@0 | 63 | } catch (e) { |
michael@0 | 64 | if ('name' in e) |
michael@0 | 65 | do_report_result(e.name == ex.name, |
michael@0 | 66 | msg + ", got " + e.name, |
michael@0 | 67 | Components.stack.caller, false); |
michael@0 | 68 | else |
michael@0 | 69 | do_report_result(false, |
michael@0 | 70 | msg + ", got " + legible_exception(ex), |
michael@0 | 71 | Components.stack.caller, false); |
michael@0 | 72 | |
michael@0 | 73 | return; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // Call this here, not in the 'try' clause, so do_report_result's own |
michael@0 | 77 | // throw doesn't get caught by our 'catch' clause. |
michael@0 | 78 | do_report_result(false, msg + ", but returned normally", |
michael@0 | 79 | Components.stack.caller, false); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | let tests = []; |
michael@0 | 83 | |
michael@0 | 84 | function test(func, msg) { |
michael@0 | 85 | tests.push({msg: msg, func: func, |
michael@0 | 86 | filename: Components.stack.caller.filename }); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | function run_test() { |
michael@0 | 90 | tests.forEach(function(t) { |
michael@0 | 91 | _log("test_info", {source_file: t.filename, |
michael@0 | 92 | diagnostic: "test group: " + t.msg}); |
michael@0 | 93 | t.func(); |
michael@0 | 94 | }); |
michael@0 | 95 | }; |