1 // Subset of testharness.js API for use in Service Worker tests.
2 // FIXME: Support async tests, possibly by just pulling in testharness.js
6 // assert_true(n > 1, 'n should be greater than 1');
7 // assert_false(n < 1, 'n should not be less than 1');
8 // assert_equals(n, 1, 'n should be equal to 1');
9 // assert_array_equals(a, [1,2,3], 'arrays should match');
10 // assert_throws({name: 'TypeError'}, function() { "123".call(); }, 'Should throw');
12 // assert_unreached('This should not happen');
14 // }, 'Test description here');
18 function assert(expected_true, error, description) {
20 var message = description ? error + ': ' + description : error;
25 function assert_true(actual, m) {
26 assert(actual === true, 'expected true, got ' + actual, m);
29 function assert_false(actual, m) {
30 assert(actual === false, 'expected false, got ' + actual, m);
34 // Same as Object.is() from ES6
37 return (a !== 0) || (1 / a === 1 / b); // -0
38 return a !== a && b !== b; // NaN
40 assert(is(NaN, NaN), 'NaN should be equal to itself');
41 assert(!is(0, -0), '0 and -0 should not be equal');
43 function assert_equals(actual, expected, m) {
44 assert(is(actual, expected),
45 'expected (' + typeof expected + ') ' + expected +
46 ' but got (' + typeof actual + ') ' + actual, m);
49 function assert_array_equals(actual, expected, m) {
50 assert(actual.length === expected.length,
51 'lengths differ, expected ' + expected.length + ' got ' + actual.length, m);
52 for (var i = 0; i < expected.length; ++i) {
53 var ai = actual[i], ei = expected[i];
55 'index ' + i + ', expected (' + typeof ei + ') ' + ei +
56 ' but got (' + typeof ai + ') ' + ai, m);
60 function assert_throws(example, func, m) {
65 assert(ex.name === example.name,
66 String(func) + ' threw ' + ex.name + ' expected ' + example.name, m);
69 assert(false, String(func) + ' did not throw', m);
72 function assert_unreached(m) {
73 assert(false, 'Reached unreachable code', m);
76 function test(func, name) {
77 tests.push({func:func, name:name});
80 // This assumes the test is started via 'service_worker_test' in test-helpers.js
82 self.addEventListener('message', function(e) {
84 var port = e.ports[0];
86 tests.forEach(function(test) { test.func(); });
87 port.postMessage('pass');
95 global.assert_true = assert_true;
96 global.assert_false = assert_false;
97 global.assert_equals = assert_equals;
98 global.assert_array_equals = assert_array_equals;
99 global.assert_throws = assert_throws;
100 global.assert_unreached = assert_unreached;