[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/1456039 Reviewed-by: Ben Titzer <titzer@chromium.org> Commit-Queue: Clemens Hammacher <clemensh@chromium.org> Cr-Commit-Position: refs/heads/master@{#59417}
This commit is contained in:
parent
f5e5d49533
commit
82d240c736
@ -107,14 +107,21 @@ var assertNotNull;
|
||||
// Assert that the passed function or eval code 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 that is compared
|
||||
// to the type property on the thrown exception.
|
||||
// The optional third argument is a message type string or RegExp object that is
|
||||
// compared to the message of the thrown exception.
|
||||
var assertThrows;
|
||||
|
||||
// Assert that the passed function throws an exception.
|
||||
// The exception is checked against the second argument using assertEquals.
|
||||
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.
|
||||
var assertDoesNotThrow;
|
||||
|
||||
@ -488,27 +495,33 @@ var prettyPrinted;
|
||||
': <' + 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) {
|
||||
if (type_opt !== undefined && typeof type_opt !== 'function') {
|
||||
failWithMessage(
|
||||
'invalid use of assertThrows, maybe you want assertThrowsEquals');
|
||||
}
|
||||
try {
|
||||
executeCode(code);
|
||||
} catch (e) {
|
||||
if (typeof type_opt === 'function') {
|
||||
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.
|
||||
checkException(e, type_opt, cause_opt);
|
||||
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) {
|
||||
@ -518,9 +531,24 @@ var prettyPrinted;
|
||||
assertSame(val, e);
|
||||
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) {
|
||||
if (!(obj instanceof type)) {
|
||||
@ -584,6 +612,7 @@ var prettyPrinted;
|
||||
assertPromiseResult = function(promise, success, fail) {
|
||||
if (success !== undefined) assertEquals('function', typeof success);
|
||||
if (fail !== undefined) assertEquals('function', typeof fail);
|
||||
assertInstanceof(promise, Promise);
|
||||
const stack = (new Error()).stack;
|
||||
|
||||
var test_promise = promise.then(
|
||||
|
@ -11,13 +11,9 @@ async function assertCompiles(buffer) {
|
||||
|
||||
function assertCompileError(buffer, msg) {
|
||||
assertEquals('string', typeof msg);
|
||||
msg = 'WebAssembly.compile(): ' + msg;
|
||||
function checkException(e) {
|
||||
if (!(e instanceof WebAssembly.CompileError)) throw e;
|
||||
assertEquals(msg, e.message, 'Error message');
|
||||
}
|
||||
return assertPromiseResult(
|
||||
WebAssembly.compile(buffer), assertUnreachable, checkException);
|
||||
return assertThrowsAsync(
|
||||
WebAssembly.compile(buffer), WebAssembly.CompileError,
|
||||
'WebAssembly.compile(): ' + msg);
|
||||
}
|
||||
|
||||
assertPromiseResult(async function basicCompile() {
|
||||
|
@ -35,10 +35,7 @@ function checkFailingInstantiation(builder, ffi, error, message) {
|
||||
assertThrows(_ => builder.instantiate(ffi), error, message);
|
||||
|
||||
// Test asynchronous instantiation.
|
||||
assertPromiseResult(builder.asyncInstantiate(ffi), assertUnreachable, e => {
|
||||
assertInstanceof(e, error);
|
||||
assertEquals(message, e.message);
|
||||
});
|
||||
assertThrowsAsync(builder.asyncInstantiate(ffi), error, message);
|
||||
}
|
||||
|
||||
(function testValidFFI() {
|
||||
|
@ -96,11 +96,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
|
||||
print('InvalidBinaryAsyncCompilation...');
|
||||
let builder = new WasmModuleBuilder();
|
||||
builder.addFunction('f', kSig_i_i).addBody([kExprCallFunction, 0]);
|
||||
let promise = WebAssembly.compile(builder.toBuffer());
|
||||
assertPromiseResult(
|
||||
promise, compiled => assertUnreachable(
|
||||
'should not be able to compile invalid blob.'),
|
||||
e => assertInstanceof(e, WebAssembly.CompileError));
|
||||
assertThrowsAsync(
|
||||
WebAssembly.compile(builder.toBuffer()), WebAssembly.CompileError);
|
||||
})();
|
||||
|
||||
// Multiple instances tests.
|
||||
|
@ -773,11 +773,7 @@ assertEq(compile, compileDesc.value);
|
||||
assertEq(compile.length, 1);
|
||||
assertEq(compile.name, 'compile');
|
||||
function assertCompileError(args, err, msg) {
|
||||
var error = null;
|
||||
assertPromiseResult(compile(...args), unexpectedSuccess, error => {
|
||||
assertTrue(error instanceof err);
|
||||
// TODO assertTrue(Boolean(error.message.match(msg)));
|
||||
});
|
||||
assertThrowsAsync(compile(...args), err /* TODO , msg */);
|
||||
}
|
||||
assertCompileError([], TypeError, /requires more than 0 arguments/);
|
||||
assertCompileError(
|
||||
@ -819,11 +815,7 @@ assertEq(instantiate, instantiateDesc.value);
|
||||
assertEq(instantiate.length, 1);
|
||||
assertEq(instantiate.name, 'instantiate');
|
||||
function assertInstantiateError(args, err, msg) {
|
||||
var error = null;
|
||||
assertPromiseResult(instantiate(...args), unexpectedSuccess, error => {
|
||||
assertTrue(error instanceof err);
|
||||
// TODO assertTrue(Boolean(error.message.match(msg)));
|
||||
});
|
||||
assertThrowsAsync(instantiate(...args), err /* TODO , msg */);
|
||||
}
|
||||
var scratch_memory = new WebAssembly.Memory({ initial: 0 });
|
||||
assertInstantiateError([], TypeError, /requires more than 0 arguments/);
|
||||
|
@ -151,9 +151,7 @@ assertThrows(() => {instantiate(kSig_i_v, [kExprI32Const, 0]);});
|
||||
|
||||
assertThrows(
|
||||
() => builder.instantiate(), WebAssembly.RuntimeError, /unreachable/);
|
||||
assertPromiseResult(builder.asyncInstantiate(), assertUnreachable,
|
||||
e => assertInstanceof(e, WebAssembly.RuntimeError));
|
||||
assertPromiseResult(WebAssembly.instantiate(builder.toModule()),
|
||||
assertUnreachable,
|
||||
e => assertInstanceof(e, WebAssembly.RuntimeError));
|
||||
assertThrowsAsync(builder.asyncInstantiate(), WebAssembly.RuntimeError);
|
||||
assertThrowsAsync(
|
||||
WebAssembly.instantiate(builder.toModule()), WebAssembly.RuntimeError);
|
||||
})();
|
||||
|
@ -32,14 +32,9 @@ function toBuffer(binary) {
|
||||
}
|
||||
|
||||
function testErrorPosition(bytes, pos, test_name) {
|
||||
assertPromiseResult(
|
||||
WebAssembly.compile(toBuffer(bytes)), assertUnreachable, e => {
|
||||
print(test_name);
|
||||
assertInstanceof(e, WebAssembly.CompileError);
|
||||
let regex = new RegExp('@\\+' + pos);
|
||||
print(e.message);
|
||||
assertMatches(regex, e.message, 'Error Position');
|
||||
});
|
||||
assertThrowsAsync(
|
||||
WebAssembly.compile(toBuffer(bytes)), WebAssembly.CompileError,
|
||||
new RegExp('@\\+' + pos));
|
||||
}
|
||||
|
||||
(function testInvalidMagic() {
|
||||
|
Loading…
Reference in New Issue
Block a user