diff --git a/quickjs-libc.c b/quickjs-libc.c index e0d6594..10d3935 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -3965,8 +3965,6 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv) JS_SetPropertyStr(ctx, global_obj, "print", JS_NewCFunction(ctx, js_print, "print", 1)); - JS_SetPropertyStr(ctx, global_obj, "__loadScript", - JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1)); JS_FreeValue(ctx, global_obj); } diff --git a/tests/assert.js b/tests/assert.js new file mode 100644 index 0000000..c8240c8 --- /dev/null +++ b/tests/assert.js @@ -0,0 +1,49 @@ +export function assert(actual, expected, message) { + if (arguments.length === 1) + expected = true; + + if (typeof actual === typeof expected) { + if (actual === expected) { + if (actual !== 0 || (1 / actual) === (1 / expected)) + return; + } + if (typeof actual === 'number') { + if (isNaN(actual) && isNaN(expected)) + return; + } + if (typeof actual === 'object') { + if (actual !== null && expected !== null + && actual.constructor === expected.constructor + && actual.toString() === expected.toString()) + return; + } + } + throw Error("assertion failed: got |" + actual + "|" + + ", expected |" + expected + "|" + + (message ? " (" + message + ")" : "")); +} + +export function assertThrows(err, func) +{ + var ex; + ex = false; + try { + func(); + } catch(e) { + ex = true; + assert(e instanceof err); + } + assert(ex, true, "exception expected"); +} + +export function assertArrayEquals(a, b) +{ + if (!Array.isArray(a) || !Array.isArray(b)) + return assert(false); + + assert(a.length, b.length); + + a.forEach((value, idx) => { + assert(b[idx], value); + }); +} diff --git a/tests/test_bigint.js b/tests/test_bigint.js index 0e67c8b..5fd1f91 100644 --- a/tests/test_bigint.js +++ b/tests/test_bigint.js @@ -1,39 +1,5 @@ -"use strict"; +import { assert, assertThrows } from "./assert.js"; -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (actual === expected) - return; - - if (actual !== null && expected !== null - && typeof actual == 'object' && typeof expected == 'object' - && actual.toString() === expected.toString()) - return; - - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} - -function assertThrows(err, func) -{ - var ex; - ex = false; - try { - func(); - } catch(e) { - ex = true; - assert(e instanceof err); - } - assert(ex, true, "exception expected"); -} - -// load more elaborate version of assert if available -try { __loadScript("test_assert.js"); } catch(e) {} - -/*----------------*/ function bigint_pow(a, n) { @@ -89,8 +55,8 @@ function test_bigint1() a = bigint_pow(3n, 100n); assert((a - 1n) != a); - assert(a == 515377520732011331036461129765621272702107522001n); - assert(a == 0x5a4653ca673768565b41f775d6947d55cf3813d1n); + assert(a, 515377520732011331036461129765621272702107522001n); + assert(a, 0x5a4653ca673768565b41f775d6947d55cf3813d1n); r = 1n << 31n; assert(r, 2147483648n, "1 << 31n === 2147483648n"); diff --git a/tests/test_bjson.js b/tests/test_bjson.js index 34c9b39..0a92162 100644 --- a/tests/test_bjson.js +++ b/tests/test_bjson.js @@ -1,21 +1,5 @@ import * as bjson from "bjson"; - -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (actual === expected) - return; - - if (actual !== null && expected !== null - && typeof actual == 'object' && typeof expected == 'object' - && actual.toString() === expected.toString()) - return; - - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} +import { assert } from "./assert.js"; function toHex(a) { @@ -196,12 +180,12 @@ function bjson_test_symbol() o = Symbol.for('foo'); buf = bjson.write(o); r = bjson.read(buf, 0, buf.byteLength); - assert(o === r); + assert(o, r); o = Symbol.toStringTag; buf = bjson.write(o); r = bjson.read(buf, 0, buf.byteLength); - assert(o === r); + assert(o, r); } function bjson_test_all() diff --git a/tests/test_builtin.js b/tests/test_builtin.js index e02a0f7..0dbe243 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -1,4 +1,5 @@ import * as os from "os"; +import { assert, assertThrows } from "./assert.js"; // Keep this at the top; it tests source positions. function test_exception_source_pos() @@ -6,21 +7,21 @@ function test_exception_source_pos() var e; try { - throw new Error(""); // line 9, column 19 + throw new Error(""); // line 10, column 19 } catch(_e) { e = _e; } - assert(e.stack.includes("test_builtin.js:9:19")); + assert(e.stack.includes("test_builtin.js:10:19")); } // Keep this at the top; it tests source positions. -function test_function_source_pos() // line 18, column 1 +function test_function_source_pos() // line 19, column 1 { - function inner() {} // line 20, column 5 + function inner() {} // line 21, column 5 var f = eval("function f() {} f"); - assert(`${test_function_source_pos.lineNumber}:${test_function_source_pos.columnNumber}`, "18:1"); - assert(`${inner.lineNumber}:${inner.columnNumber}`, "20:5"); + assert(`${test_function_source_pos.lineNumber}:${test_function_source_pos.columnNumber}`, "19:1"); + assert(`${inner.lineNumber}:${inner.columnNumber}`, "21:5"); assert(`${f.lineNumber}:${f.columnNumber}`, "1:1"); } @@ -35,19 +36,19 @@ function test_exception_prepare_stack() }; try { - throw new Error(""); // line 38, column 19 + throw new Error(""); // line 39, column 19 } catch(_e) { e = _e; } Error.prepareStackTrace = undefined; - assert(e.stack.length === 2); + assert(e.stack.length, 2); const f = e.stack[0]; - assert(f.getFunctionName() === 'test_exception_prepare_stack'); + assert(f.getFunctionName(), 'test_exception_prepare_stack'); assert(f.getFileName().endsWith('test_builtin.js')); - assert(f.getLineNumber() === 38); - assert(f.getColumnNumber() === 19); + assert(f.getLineNumber(), 39); + assert(f.getColumnNumber(), 19); assert(!f.isNative()); } @@ -63,7 +64,7 @@ function test_exception_stack_size_limit() }; try { - throw new Error(""); // line 66, column 19 + throw new Error(""); // line 67, column 19 } catch(_e) { e = _e; } @@ -71,61 +72,15 @@ function test_exception_stack_size_limit() Error.stackTraceLimit = 10; Error.prepareStackTrace = undefined; - assert(e.stack.length === 1); + assert(e.stack.length, 1); const f = e.stack[0]; - assert(f.getFunctionName() === 'test_exception_stack_size_limit'); + assert(f.getFunctionName(), 'test_exception_stack_size_limit'); assert(f.getFileName().endsWith('test_builtin.js')); - assert(f.getLineNumber() === 66); - assert(f.getColumnNumber() === 19); + assert(f.getLineNumber(), 67); + assert(f.getColumnNumber(), 19); assert(!f.isNative()); } -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (typeof actual === typeof expected) { - if (actual === expected) { - if (actual !== 0 || (1 / actual) === (1 / expected)) - return; - } - if (typeof actual === 'number') { - if (isNaN(actual) && isNaN(expected)) - return true; - } - if (typeof actual === 'object') { - if (actual !== null && expected !== null - && actual.constructor === expected.constructor - && actual.toString() === expected.toString()) - return; - } - } - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} - -function assert_throws(expected_error, func) -{ - var err = false; - try { - func(); - } catch(e) { - err = true; - if (!(e instanceof expected_error)) { - throw Error("unexpected exception type"); - } - } - if (!err) { - throw Error("expected exception"); - } -} - -// load more elaborate version of assert if available -try { __loadScript("test_assert.js"); } catch(e) {} - -/*----------------*/ - function my_func(a, b) { return a + b; @@ -155,7 +110,7 @@ function test_function() r = (function () { return 1; }).apply(null, undefined); assert(r, 1); - assert_throws(TypeError, (function() { + assertThrows(TypeError, (function() { Reflect.apply((function () { return 1; }), null, undefined); })); @@ -221,7 +176,7 @@ function test() assert(typeof a.y, "undefined", "extensible"); assert(err, true, "extensible"); - assert_throws(TypeError, () => Object.setPrototypeOf(Object.prototype, {})); + assertThrows(TypeError, () => Object.setPrototypeOf(Object.prototype, {})); } function test_enum() @@ -1031,10 +986,10 @@ function test_cur_pc() get: function() { throw Error("a[1]_get"); }, set: function(x) { throw Error("a[1]_set"); } }); - assert_throws(Error, function() { return a[1]; }); - assert_throws(Error, function() { a[1] = 1; }); - assert_throws(Error, function() { return [...a]; }); - assert_throws(Error, function() { return ({...b} = a); }); + assertThrows(Error, function() { return a[1]; }); + assertThrows(Error, function() { a[1] = 1; }); + assertThrows(Error, function() { return [...a]; }); + assertThrows(Error, function() { return ({...b} = a); }); var o = {}; Object.defineProperty(o, 'x', { @@ -1042,34 +997,34 @@ function test_cur_pc() set: function(x) { throw Error("o.x_set"); } }); o.valueOf = function() { throw Error("o.valueOf"); }; - assert_throws(Error, function() { return +o; }); - assert_throws(Error, function() { return -o; }); - assert_throws(Error, function() { return o+1; }); - assert_throws(Error, function() { return o-1; }); - assert_throws(Error, function() { return o*1; }); - assert_throws(Error, function() { return o/1; }); - assert_throws(Error, function() { return o%1; }); - assert_throws(Error, function() { return o**1; }); - assert_throws(Error, function() { return o<<1; }); - assert_throws(Error, function() { return o>>1; }); - assert_throws(Error, function() { return o>>>1; }); - assert_throws(Error, function() { return o&1; }); - assert_throws(Error, function() { return o|1; }); - assert_throws(Error, function() { return o^1; }); - assert_throws(Error, function() { return o<1; }); - assert_throws(Error, function() { return o==1; }); - assert_throws(Error, function() { return o++; }); - assert_throws(Error, function() { return o--; }); - assert_throws(Error, function() { return ++o; }); - assert_throws(Error, function() { return --o; }); - assert_throws(Error, function() { return ~o; }); + assertThrows(Error, function() { return +o; }); + assertThrows(Error, function() { return -o; }); + assertThrows(Error, function() { return o+1; }); + assertThrows(Error, function() { return o-1; }); + assertThrows(Error, function() { return o*1; }); + assertThrows(Error, function() { return o/1; }); + assertThrows(Error, function() { return o%1; }); + assertThrows(Error, function() { return o**1; }); + assertThrows(Error, function() { return o<<1; }); + assertThrows(Error, function() { return o>>1; }); + assertThrows(Error, function() { return o>>>1; }); + assertThrows(Error, function() { return o&1; }); + assertThrows(Error, function() { return o|1; }); + assertThrows(Error, function() { return o^1; }); + assertThrows(Error, function() { return o<1; }); + assertThrows(Error, function() { return o==1; }); + assertThrows(Error, function() { return o++; }); + assertThrows(Error, function() { return o--; }); + assertThrows(Error, function() { return ++o; }); + assertThrows(Error, function() { return --o; }); + assertThrows(Error, function() { return ~o; }); Object.defineProperty(globalThis, 'xxx', { get: function() { throw Error("xxx_get"); }, set: function(x) { throw Error("xxx_set"); } }); - assert_throws(Error, function() { return xxx; }); - assert_throws(Error, function() { xxx = 1; }); + assertThrows(Error, function() { return xxx; }); + assertThrows(Error, function() { xxx = 1; }); } test(); diff --git a/tests/test_closure.js b/tests/test_closure.js index f5d4160..4d63879 100644 --- a/tests/test_closure.js +++ b/tests/test_closure.js @@ -1,3 +1,5 @@ +// This test cannot use imports because it needs to run in non-strict mode. + function assert(actual, expected, message) { if (arguments.length == 1) expected = true; @@ -15,9 +17,6 @@ function assert(actual, expected, message) { (message ? " (" + message + ")" : "")); } -// load more elaborate version of assert if available -try { __loadScript("test_assert.js"); } catch(e) {} - /*----------------*/ var log_str = ""; @@ -135,9 +134,9 @@ function test_arrow_function() assert(a.length, 2); assert(a[0] === 1 && a[1] === 2); - assert(f2.call("this_val") === "this_val"); - assert(f3.call("this_val") === "this_val"); - assert(new f4() === f4); + assert(f2.call("this_val"), "this_val"); + assert(f3.call("this_val"), "this_val"); + assert(new f4(), f4); var o1 = { f() { return this; } }; var o2 = { f() { @@ -145,7 +144,7 @@ function test_arrow_function() } }; o2.__proto__ = o1; - assert(o2.f() === o2); + assert(o2.f(), o2); } function test_with() @@ -153,20 +152,20 @@ function test_with() var o1 = { x: "o1", y: "o1" }; var x = "local"; eval('var z="var_obj";'); - assert(z === "var_obj"); + assert(z, "var_obj"); with (o1) { - assert(x === "o1"); - assert(eval("x") === "o1"); + assert(x, "o1"); + assert(eval("x"), "o1"); var f = function () { o2 = { x: "o2" }; with (o2) { - assert(x === "o2"); - assert(y === "o1"); - assert(z === "var_obj"); - assert(eval("x") === "o2"); - assert(eval("y") === "o1"); - assert(eval("z") === "var_obj"); - assert(eval('eval("x")') === "o2"); + assert(x, "o2"); + assert(y, "o1"); + assert(z, "var_obj"); + assert(eval("x"), "o2"); + assert(eval("y"), "o1"); + assert(eval("z"), "var_obj"); + assert(eval('eval("x")'), "o2"); } }; f(); @@ -182,7 +181,7 @@ function test_eval_closure() eval("tab.push(function g1() { return i; })"); } for(let i = 0; i < 3; i++) { - assert(tab[i]() === i); + assert(tab[i](), i); } tab = []; @@ -193,7 +192,7 @@ function test_eval_closure() f(); } for(let i = 0; i < 3; i++) { - assert(tab[i]() === i); + assert(tab[i](), i); } } diff --git a/tests/test_language.js b/tests/test_language.js index ad2dfe8..dbb4947 100644 --- a/tests/test_language.js +++ b/tests/test_language.js @@ -1,3 +1,5 @@ +// This test cannot use imports because it needs to run in non-strict mode. + function assert(actual, expected, message) { if (arguments.length == 1) expected = true; @@ -43,9 +45,6 @@ function assert_throws(expected_error, func, message) } } -// load more elaborate version of assert if available -try { __loadScript("test_assert.js"); } catch(e) {} - /*----------------*/ function test_op1() @@ -119,20 +118,20 @@ function test_op1() function test_cvt() { - assert((NaN | 0) === 0); - assert((Infinity | 0) === 0); - assert(((-Infinity) | 0) === 0); - assert(("12345" | 0) === 12345); - assert(("0x12345" | 0) === 0x12345); - assert(((4294967296 * 3 - 4) | 0) === -4); + assert((NaN | 0), 0); + assert((Infinity | 0), 0); + assert(((-Infinity) | 0), 0); + assert(("12345" | 0), 12345); + assert(("0x12345" | 0), 0x12345); + assert(((4294967296 * 3 - 4) | 0), -4); - assert(("12345" >>> 0) === 12345); - assert(("0x12345" >>> 0) === 0x12345); - assert((NaN >>> 0) === 0); - assert((Infinity >>> 0) === 0); - assert(((-Infinity) >>> 0) === 0); - assert(((4294967296 * 3 - 4) >>> 0) === (4294967296 - 4)); - assert((19686109595169230000).toString() === "19686109595169230000"); + assert(("12345" >>> 0), 12345); + assert(("0x12345" >>> 0), 0x12345); + assert((NaN >>> 0), 0); + assert((Infinity >>> 0), 0); + assert(((-Infinity) >>> 0), 0); + assert(((4294967296 * 3 - 4) >>> 0), (4294967296 - 4)); + assert((19686109595169230000).toString(), "19686109595169230000"); } function test_eq() @@ -317,36 +316,36 @@ function test_class() } } - assert(C.F() === -1); + assert(C.F(), -1); assert(Object.getOwnPropertyDescriptor(C.prototype, "y").get.name === "get y"); o = new C(); - assert(o.f() === 1); - assert(o.x === 10); + assert(o.f(), 1); + assert(o.x, 10); - assert(D.F() === -1); - assert(D.G() === -2); - assert(D.H() === -1); + assert(D.F(), -1); + assert(D.G(), -2); + assert(D.H(), -1); o = new D(); - assert(o.f() === 1); - assert(o.g() === 2); - assert(o.x === 10); - assert(o.z === 20); - assert(o.h() === 1); + assert(o.f(), 1); + assert(o.g(), 2); + assert(o.x, 10); + assert(o.z, 20); + assert(o.h(), 1); /* test class name scope */ var E1 = class E { static F() { return E; } }; - assert(E1 === E1.F()); + assert(E1, E1.F()); class S { static x = 42; static y = S.x; static z = this.x; } - assert(S.x === 42); - assert(S.y === 42); - assert(S.z === 42); + assert(S.x, 42); + assert(S.y, 42); + assert(S.z, 42); class P { get; @@ -357,10 +356,10 @@ function test_class() async = () => "789"; static() { return 42; } } - assert(new P().get() === "123"); - assert(new P().set() === "456"); - assert(new P().async() === "789"); - assert(new P().static() === 42); + assert(new P().get(), "123"); + assert(new P().set(), "456"); + assert(new P().async(), "789"); + assert(new P().static(), 42); }; function test_template() @@ -390,7 +389,7 @@ function test_object_literal() var x = 0, get = 1, set = 2; async = 3; a = { get: 2, set: 3, async: 4, get a(){ return this.get} }; assert(JSON.stringify(a), '{"get":2,"set":3,"async":4,"a":2}'); - assert(a.a === 2); + assert(a.a, 2); a = { x, get, set, async }; assert(JSON.stringify(a), '{"x":0,"get":1,"set":2,"async":3}'); @@ -400,10 +399,10 @@ function test_regexp_skip() { var a, b; [a, b = /abc\(/] = [1]; - assert(a === 1); + assert(a, 1); [a, b =/abc\(/] = [2]; - assert(a === 2); + assert(a, 2); } function test_labels() diff --git a/tests/test_loop.js b/tests/test_loop.js index 62b3e41..97fc2f6 100644 --- a/tests/test_loop.js +++ b/tests/test_loop.js @@ -1,3 +1,5 @@ +// This test cannot use imports because it needs to run in non-strict mode. + function assert(actual, expected, message) { if (arguments.length == 1) expected = true; @@ -15,9 +17,6 @@ function assert(actual, expected, message) { (message ? " (" + message + ")" : "")); } -// load more elaborate version of assert if available -try { __loadScript("test_assert.js"); } catch(e) {} - /*----------------*/ function test_while() @@ -29,7 +28,7 @@ function test_while() c++; i++; } - assert(c === 3); + assert(c, 3); } function test_while_break() diff --git a/tests/test_queue_microtask.js b/tests/test_queue_microtask.js index 106dde4..f06dc8b 100644 --- a/tests/test_queue_microtask.js +++ b/tests/test_queue_microtask.js @@ -1,57 +1,12 @@ -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (actual === expected) - return; - - if (actual !== null && expected !== null - && typeof actual == 'object' && typeof expected == 'object' - && actual.toString() === expected.toString()) - return; - - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} - -function assert_throws(expected_error, func) -{ - var err = false; - try { - func(); - } catch(e) { - err = true; - if (!(e instanceof expected_error)) { - throw Error("unexpected exception type"); - } - } - if (!err) { - throw Error("expected exception"); - } -} - -function assert_array_equals(a, b) { - if (!Array.isArray(a) || !Array.isArray(b)) - return assert(false); - - assert(a.length === b.length); - - a.forEach((value, idx) => { - assert(b[idx] === value); - }); -} - -// load more elaborate version of assert if available -try { std.loadScript("test_assert.js"); } catch(e) {} +import { assert, assertArrayEquals, assertThrows } from "./assert.js"; function test_types() { - assert_throws(TypeError, () => queueMicrotask(), "no argument"); - assert_throws(TypeError, () => queueMicrotask(undefined), "undefined"); - assert_throws(TypeError, () => queueMicrotask(null), "null"); - assert_throws(TypeError, () => queueMicrotask(0), "0"); - assert_throws(TypeError, () => queueMicrotask({ handleEvent() { } }), "an event handler object"); - assert_throws(TypeError, () => queueMicrotask("window.x = 5;"), "a string"); + assertThrows(TypeError, () => queueMicrotask(), "no argument"); + assertThrows(TypeError, () => queueMicrotask(undefined), "undefined"); + assertThrows(TypeError, () => queueMicrotask(null), "null"); + assertThrows(TypeError, () => queueMicrotask(0), "0"); + assertThrows(TypeError, () => queueMicrotask({ handleEvent() { } }), "an event handler object"); + assertThrows(TypeError, () => queueMicrotask("window.x = 5;"), "a string"); } function test_async() { @@ -74,7 +29,7 @@ function test_async_order() { queueMicrotask(() => happenings.push("b")); Promise.reject().catch(() => happenings.push("c")); queueMicrotask(() => { - assert_array_equals(happenings, ["a", "b", "c"]); + assertArrayEquals(happenings, ["a", "b", "c"]); }); } diff --git a/tests/test_std.js b/tests/test_std.js index 3826308..7e9e2a4 100644 --- a/tests/test_std.js +++ b/tests/test_std.js @@ -1,30 +1,10 @@ import * as std from "std"; import * as os from "os"; +import { assert } from "./assert.js"; const isWin = os.platform === 'win32'; const isCygwin = os.platform === 'cygwin'; -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (actual === expected) - return; - - if (actual !== null && expected !== null - && typeof actual == 'object' && typeof expected == 'object' - && actual.toString() === expected.toString()) - return; - - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} - -// load more elaborate version of assert if available -try { std.loadScript("test_assert.js"); } catch(e) {} - -/*----------------*/ function test_printf() { @@ -47,19 +27,19 @@ function test_file1() f.seek(0, std.SEEK_SET); str1 = f.readAsString(); - assert(str1 === str); + assert(str1, str); f.seek(0, std.SEEK_END); size = f.tell(); - assert(size === str.length); + assert(size, str.length); f.seek(0, std.SEEK_SET); buf = new Uint8Array(size); ret = f.read(buf.buffer, 0, size); - assert(ret === size); + assert(ret, size); for(i = 0; i < size; i++) - assert(buf[i] === str.charCodeAt(i)); + assert(buf[i], str.charCodeAt(i)); f.close(); } @@ -74,9 +54,9 @@ function test_file2() f.putByte(str.charCodeAt(i)); f.seek(0, std.SEEK_SET); for(i = 0; i < size; i++) { - assert(str.charCodeAt(i) === f.getByte()); + assert(str.charCodeAt(i), f.getByte()); } - assert(f.getByte() === -1); + assert(f.getByte(), -1); f.close(); } @@ -97,11 +77,11 @@ function test_getline() line = f.getline(); if (line === null) break; - assert(line == lines[line_count]); + assert(line, lines[line_count]); line_count++; } assert(f.eof()); - assert(line_count === lines.length); + assert(line_count, lines.length); f.close(); } @@ -145,7 +125,7 @@ function test_os() os.remove(fdir); err = os.mkdir(fdir, 0o755); - assert(err === 0); + assert(err, 0); fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC); assert(fd >= 0); @@ -153,22 +133,22 @@ function test_os() buf = new Uint8Array(10); for(i = 0; i < buf.length; i++) buf[i] = i; - assert(os.write(fd, buf.buffer, 0, buf.length) === buf.length); + assert(os.write(fd, buf.buffer, 0, buf.length), buf.length); - assert(os.seek(fd, 0, std.SEEK_SET) === 0); + assert(os.seek(fd, 0, std.SEEK_SET), 0); buf2 = new Uint8Array(buf.length); - assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length); + assert(os.read(fd, buf2.buffer, 0, buf2.length), buf2.length); for(i = 0; i < buf.length; i++) assert(buf[i] == buf2[i]); if (typeof BigInt !== "undefined") { assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6)); - assert(os.read(fd, buf2.buffer, 0, 1) === 1); + assert(os.read(fd, buf2.buffer, 0, 1), 1); assert(buf[6] == buf2[0]); } - assert(os.close(fd) === 0); + assert(os.close(fd), 0); [files, err] = os.readdir(fdir); assert(err, 0); @@ -186,7 +166,7 @@ function test_os() if (!isWin) { err = os.symlink(fname, link_path); - assert(err === 0); + assert(err, 0); [st, err] = os.lstat(link_path); assert(err, 0); @@ -286,7 +266,7 @@ function test_timeout_order() function a() { s += "a"; os.setTimeout(c, 300); } function b() { s += "b"; } function c() { s += "c"; } - function d() { assert(s === "abc"); } // not "acb" + function d() { assert(s, "abc"); } // not "acb" } function test_stdio_close() diff --git a/tests/test_worker.js b/tests/test_worker.js index 818739e..bd0d0b9 100644 --- a/tests/test_worker.js +++ b/tests/test_worker.js @@ -1,23 +1,5 @@ -/* os.Worker API test */ -import * as std from "std"; import * as os from "os"; - -function assert(actual, expected, message) { - if (arguments.length == 1) - expected = true; - - if (actual === expected) - return; - - if (actual !== null && expected !== null - && typeof actual == 'object' && typeof expected == 'object' - && actual.toString() === expected.toString()) - return; - - throw Error("assertion failed: got |" + actual + "|" + - ", expected |" + expected + "|" + - (message ? " (" + message + ")" : "")); -} +import { assert } from "./assert.js"; var worker;