From a179cf23bebb09a19deb93a3386f6b047ae65ae3 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Mon, 29 Sep 2025 14:53:42 +0200 Subject: [PATCH] Add assert_exception to the core test harnesses --- test/harness/async_index.js | 22 +++++++++++++++++++++- test/harness/sync_index.js | 10 ++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/test/harness/async_index.js b/test/harness/async_index.js index 7b4fcd1ae4..94de545dd5 100644 --- a/test/harness/async_index.js +++ b/test/harness/async_index.js @@ -281,6 +281,26 @@ function assert_trap(action, source) { .catch(_ => {}); } +function assert_exception(action, source) { + const test = `Test that a WebAssembly code throws an exception (${source})`; + const loc = new Error().stack.toString().replace("Error", ""); + chain = Promise.all([chain, action()]) + .then( + result => { + uniqueTest(_ => { + assert_true(false, loc); + }, test); + }, + error => { + uniqueTest(_ => { + // Pass + }, test); + } + ) + // Clear all exceptions, so that subsequent tests get executed. + .catch(_ => {}); +} + function assert_return(action, source, ...expected) { const test = `Test that a WebAssembly code returns a specific result (${source})`; const loc = new Error().stack.toString().replace("Error", ""); @@ -355,7 +375,7 @@ try { } function assert_exhaustion(action) { - const test = "Test that a WebAssembly code exhauts the stack space"; + const test = "Test that a WebAssembly code exhausts the stack space"; const loc = new Error().stack.toString().replace("Error", ""); chain = Promise.all([action(), chain]) .then( diff --git a/test/harness/sync_index.js b/test/harness/sync_index.js index 77f601f590..a984be4964 100644 --- a/test/harness/sync_index.js +++ b/test/harness/sync_index.js @@ -339,6 +339,16 @@ function assert_trap(action, source) { }, `A wast module that must trap at runtime. (${source})`); } +function assert_exception(action, source) { + let result = action(); + + _assert(result instanceof Result); + + uniqueTest(() => { + assert_true(result.isError(), 'expected error result'); + }, `A wast module that must throw an exception at runtime. (${source})`); +} + let StackOverflow; try { (function f() { 1 + f() })() } catch (e) { StackOverflow = e.constructor }