|
| 1 | +function assert(actual, expected, message) { |
| 2 | + if (arguments.length == 1) |
| 3 | + expected = true; |
| 4 | + |
| 5 | + if (actual === expected) |
| 6 | + return; |
| 7 | + |
| 8 | + if (actual !== null && expected !== null |
| 9 | + && typeof actual == 'object' && typeof expected == 'object' |
| 10 | + && actual.toString() === expected.toString()) |
| 11 | + return; |
| 12 | + |
| 13 | + throw Error("assertion failed: got |" + actual + "|" + |
| 14 | + ", expected |" + expected + "|" + |
| 15 | + (message ? " (" + message + ")" : "")); |
| 16 | +} |
| 17 | + |
| 18 | +function assert_throws(expected_error, func) |
| 19 | +{ |
| 20 | + var err = false; |
| 21 | + try { |
| 22 | + func(); |
| 23 | + } catch(e) { |
| 24 | + err = true; |
| 25 | + if (!(e instanceof expected_error)) { |
| 26 | + throw Error("unexpected exception type"); |
| 27 | + } |
| 28 | + } |
| 29 | + if (!err) { |
| 30 | + throw Error("expected exception"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function assert_array_equals(a, b) { |
| 35 | + if (!Array.isArray(a) || !Array.isArray(b)) |
| 36 | + return assert(false); |
| 37 | + |
| 38 | + assert(a.length === b.length); |
| 39 | + |
| 40 | + a.forEach((value, idx) => { |
| 41 | + assert(b[idx] === value); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +// load more elaborate version of assert if available |
| 46 | +try { std.loadScript("test_assert.js"); } catch(e) {} |
| 47 | + |
| 48 | +function test_types() { |
| 49 | + assert_throws(TypeError, () => queueMicrotask(), "no argument"); |
| 50 | + assert_throws(TypeError, () => queueMicrotask(undefined), "undefined"); |
| 51 | + assert_throws(TypeError, () => queueMicrotask(null), "null"); |
| 52 | + assert_throws(TypeError, () => queueMicrotask(0), "0"); |
| 53 | + assert_throws(TypeError, () => queueMicrotask({ handleEvent() { } }), "an event handler object"); |
| 54 | + assert_throws(TypeError, () => queueMicrotask("window.x = 5;"), "a string"); |
| 55 | +} |
| 56 | + |
| 57 | +function test_async() { |
| 58 | + let called = false; |
| 59 | + queueMicrotask(() => { |
| 60 | + called = true; |
| 61 | + }); |
| 62 | + assert(!called); |
| 63 | +} |
| 64 | + |
| 65 | +function test_arguments() { |
| 66 | + queueMicrotask(function () { // note: intentionally not an arrow function |
| 67 | + assert(arguments.length === 0); |
| 68 | + }, "x", "y"); |
| 69 | +}; |
| 70 | + |
| 71 | +function test_async_order() { |
| 72 | + const happenings = []; |
| 73 | + Promise.resolve().then(() => happenings.push("a")); |
| 74 | + queueMicrotask(() => happenings.push("b")); |
| 75 | + Promise.reject().catch(() => happenings.push("c")); |
| 76 | + queueMicrotask(() => { |
| 77 | + assert_array_equals(happenings, ["a", "b", "c"]); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +test_types(); |
| 82 | +test_async(); |
| 83 | +test_arguments(); |
| 84 | +test_async_order(); |
0 commit comments