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