Commit 82d240c7 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[test] Introduce assertThrowsAsync

We often use raw assertPromiseResult with {success ==
assertUnreachable} for that. Having a separate helper increases
readability and allows us to generate consistent (and better) error
messages.

R=titzer@chromium.org

Bug: chromium:926311
Change-Id: I507941eacaafe6c576098d7829a76b27384a4fb6
Reviewed-on: https://chromium-review.googlesource.com/c/1456039Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59417}
parent f5e5d495
...@@ -107,14 +107,21 @@ var assertNotNull; ...@@ -107,14 +107,21 @@ var assertNotNull;
// Assert that the passed function or eval code throws an exception. // Assert that the passed function or eval code throws an exception.
// The optional second argument is an exception constructor that the // The optional second argument is an exception constructor that the
// thrown exception is checked against with "instanceof". // thrown exception is checked against with "instanceof".
// The optional third argument is a message type string that is compared // The optional third argument is a message type string or RegExp object that is
// to the type property on the thrown exception. // compared to the message of the thrown exception.
var assertThrows; var assertThrows;
// Assert that the passed function throws an exception. // Assert that the passed function throws an exception.
// The exception is checked against the second argument using assertEquals. // The exception is checked against the second argument using assertEquals.
var assertThrowsEquals; var assertThrowsEquals;
// Assert that the passed promise does not resolve, but eventually throws an
// exception. The optional second argument is an exception constructor that the
// thrown exception is checked against with "instanceof".
// The optional third argument is a message type string or RegExp object that is
// compared to the message of the thrown exception.
var assertThrowsAsync;
// Assert that the passed function or eval code does not throw an exception. // Assert that the passed function or eval code does not throw an exception.
var assertDoesNotThrow; var assertDoesNotThrow;
...@@ -488,27 +495,33 @@ var prettyPrinted; ...@@ -488,27 +495,33 @@ var prettyPrinted;
': <' + prettyPrinted(code) + '>'); ': <' + prettyPrinted(code) + '>');
} }
function checkException(e, type_opt, cause_opt) {
if (type_opt !== undefined) {
assertEquals('function', typeof type_opt);
assertInstanceof(e, type_opt);
}
if (RegExp !== undefined && cause_opt instanceof RegExp) {
assertMatches(cause_opt, e.message, 'Error message');
} else if (cause_opt !== undefined) {
assertEquals(cause_opt, e.message, 'Error message');
}
}
assertThrows = function assertThrows(code, type_opt, cause_opt) { assertThrows = function assertThrows(code, type_opt, cause_opt) {
if (type_opt !== undefined && typeof type_opt !== 'function') {
failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
}
try { try {
executeCode(code); executeCode(code);
} catch (e) { } catch (e) {
if (typeof type_opt === 'function') { checkException(e, type_opt, cause_opt);
assertInstanceof(e, type_opt);
} else if (type_opt !== void 0) {
failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
}
if (arguments.length >= 3) {
if (cause_opt instanceof RegExp) {
assertMatches(cause_opt, e.message, "Error message");
} else {
assertEquals(cause_opt, e.message, "Error message");
}
}
// Success.
return; return;
} }
failWithMessage("Did not throw exception"); let msg = 'Did not throw exception';
if (type_opt !== undefined && type_opt.name !== undefined)
msg += ', expected ' + type_opt.name;
failWithMessage(msg);
}; };
assertThrowsEquals = function assertThrowsEquals(fun, val) { assertThrowsEquals = function assertThrowsEquals(fun, val) {
...@@ -518,9 +531,24 @@ var prettyPrinted; ...@@ -518,9 +531,24 @@ var prettyPrinted;
assertSame(val, e); assertSame(val, e);
return; return;
} }
failWithMessage("Did not throw exception"); failWithMessage('Did not throw exception, expected ' + prettyPrinted(val));
}; };
assertThrowsAsync = function assertThrowsAsync(promise, type_opt, cause_opt) {
if (type_opt !== undefined && typeof type_opt !== 'function') {
failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
}
let msg = 'Promise did not throw exception';
if (type_opt !== undefined && type_opt.name !== undefined)
msg += ', expected ' + type_opt.name;
return assertPromiseResult(
promise,
// Use setTimeout to throw the error again to get out of the promise
// chain.
res => setTimeout(_ => fail('<throw>', res, msg), 0),
e => checkException(e, type_opt, cause_opt));
};
assertInstanceof = function assertInstanceof(obj, type) { assertInstanceof = function assertInstanceof(obj, type) {
if (!(obj instanceof type)) { if (!(obj instanceof type)) {
...@@ -584,6 +612,7 @@ var prettyPrinted; ...@@ -584,6 +612,7 @@ var prettyPrinted;
assertPromiseResult = function(promise, success, fail) { assertPromiseResult = function(promise, success, fail) {
if (success !== undefined) assertEquals('function', typeof success); if (success !== undefined) assertEquals('function', typeof success);
if (fail !== undefined) assertEquals('function', typeof fail); if (fail !== undefined) assertEquals('function', typeof fail);
assertInstanceof(promise, Promise);
const stack = (new Error()).stack; const stack = (new Error()).stack;
var test_promise = promise.then( var test_promise = promise.then(
......
...@@ -11,13 +11,9 @@ async function assertCompiles(buffer) { ...@@ -11,13 +11,9 @@ async function assertCompiles(buffer) {
function assertCompileError(buffer, msg) { function assertCompileError(buffer, msg) {
assertEquals('string', typeof msg); assertEquals('string', typeof msg);
msg = 'WebAssembly.compile(): ' + msg; return assertThrowsAsync(
function checkException(e) { WebAssembly.compile(buffer), WebAssembly.CompileError,
if (!(e instanceof WebAssembly.CompileError)) throw e; 'WebAssembly.compile(): ' + msg);
assertEquals(msg, e.message, 'Error message');
}
return assertPromiseResult(
WebAssembly.compile(buffer), assertUnreachable, checkException);
} }
assertPromiseResult(async function basicCompile() { assertPromiseResult(async function basicCompile() {
......
...@@ -35,10 +35,7 @@ function checkFailingInstantiation(builder, ffi, error, message) { ...@@ -35,10 +35,7 @@ function checkFailingInstantiation(builder, ffi, error, message) {
assertThrows(_ => builder.instantiate(ffi), error, message); assertThrows(_ => builder.instantiate(ffi), error, message);
// Test asynchronous instantiation. // Test asynchronous instantiation.
assertPromiseResult(builder.asyncInstantiate(ffi), assertUnreachable, e => { assertThrowsAsync(builder.asyncInstantiate(ffi), error, message);
assertInstanceof(e, error);
assertEquals(message, e.message);
});
} }
(function testValidFFI() { (function testValidFFI() {
......
...@@ -96,11 +96,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -96,11 +96,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
print('InvalidBinaryAsyncCompilation...'); print('InvalidBinaryAsyncCompilation...');
let builder = new WasmModuleBuilder(); let builder = new WasmModuleBuilder();
builder.addFunction('f', kSig_i_i).addBody([kExprCallFunction, 0]); builder.addFunction('f', kSig_i_i).addBody([kExprCallFunction, 0]);
let promise = WebAssembly.compile(builder.toBuffer()); assertThrowsAsync(
assertPromiseResult( WebAssembly.compile(builder.toBuffer()), WebAssembly.CompileError);
promise, compiled => assertUnreachable(
'should not be able to compile invalid blob.'),
e => assertInstanceof(e, WebAssembly.CompileError));
})(); })();
// Multiple instances tests. // Multiple instances tests.
......
...@@ -773,11 +773,7 @@ assertEq(compile, compileDesc.value); ...@@ -773,11 +773,7 @@ assertEq(compile, compileDesc.value);
assertEq(compile.length, 1); assertEq(compile.length, 1);
assertEq(compile.name, 'compile'); assertEq(compile.name, 'compile');
function assertCompileError(args, err, msg) { function assertCompileError(args, err, msg) {
var error = null; assertThrowsAsync(compile(...args), err /* TODO , msg */);
assertPromiseResult(compile(...args), unexpectedSuccess, error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
} }
assertCompileError([], TypeError, /requires more than 0 arguments/); assertCompileError([], TypeError, /requires more than 0 arguments/);
assertCompileError( assertCompileError(
...@@ -819,11 +815,7 @@ assertEq(instantiate, instantiateDesc.value); ...@@ -819,11 +815,7 @@ assertEq(instantiate, instantiateDesc.value);
assertEq(instantiate.length, 1); assertEq(instantiate.length, 1);
assertEq(instantiate.name, 'instantiate'); assertEq(instantiate.name, 'instantiate');
function assertInstantiateError(args, err, msg) { function assertInstantiateError(args, err, msg) {
var error = null; assertThrowsAsync(instantiate(...args), err /* TODO , msg */);
assertPromiseResult(instantiate(...args), unexpectedSuccess, error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
} }
var scratch_memory = new WebAssembly.Memory({ initial: 0 }); var scratch_memory = new WebAssembly.Memory({ initial: 0 });
assertInstantiateError([], TypeError, /requires more than 0 arguments/); assertInstantiateError([], TypeError, /requires more than 0 arguments/);
......
...@@ -151,9 +151,7 @@ assertThrows(() => {instantiate(kSig_i_v, [kExprI32Const, 0]);}); ...@@ -151,9 +151,7 @@ assertThrows(() => {instantiate(kSig_i_v, [kExprI32Const, 0]);});
assertThrows( assertThrows(
() => builder.instantiate(), WebAssembly.RuntimeError, /unreachable/); () => builder.instantiate(), WebAssembly.RuntimeError, /unreachable/);
assertPromiseResult(builder.asyncInstantiate(), assertUnreachable, assertThrowsAsync(builder.asyncInstantiate(), WebAssembly.RuntimeError);
e => assertInstanceof(e, WebAssembly.RuntimeError)); assertThrowsAsync(
assertPromiseResult(WebAssembly.instantiate(builder.toModule()), WebAssembly.instantiate(builder.toModule()), WebAssembly.RuntimeError);
assertUnreachable,
e => assertInstanceof(e, WebAssembly.RuntimeError));
})(); })();
...@@ -32,14 +32,9 @@ function toBuffer(binary) { ...@@ -32,14 +32,9 @@ function toBuffer(binary) {
} }
function testErrorPosition(bytes, pos, test_name) { function testErrorPosition(bytes, pos, test_name) {
assertPromiseResult( assertThrowsAsync(
WebAssembly.compile(toBuffer(bytes)), assertUnreachable, e => { WebAssembly.compile(toBuffer(bytes)), WebAssembly.CompileError,
print(test_name); new RegExp('@\\+' + pos));
assertInstanceof(e, WebAssembly.CompileError);
let regex = new RegExp('@\\+' + pos);
print(e.message);
assertMatches(regex, e.message, 'Error Position');
});
} }
(function testInvalidMagic() { (function testInvalidMagic() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment