From 93e26f3f4901a442ce0a5d71f72aac34ff5bbbec Mon Sep 17 00:00:00 2001 From: Sebastian Mayr Date: Sat, 1 Jun 2019 18:58:08 +0200 Subject: [PATCH] Add all webidl interfaces to the same test context --- test/harness/index.test.js | 119 +++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 52 deletions(-) diff --git a/test/harness/index.test.js b/test/harness/index.test.js index db70de0a..9942b4b9 100644 --- a/test/harness/index.test.js +++ b/test/harness/index.test.js @@ -19,6 +19,9 @@ function createSandbox(setupScripts) { // eslint-disable-next-line global-require return require(p); } + if (p.startsWith("../implementations/")) { + return class Stub {}; + } const resolved = path.resolve(__dirname, "../output", p); const src = readFileSync(resolved, { encoding: "utf8" }); @@ -34,15 +37,7 @@ function createSandbox(setupScripts) { return sandbox; } -async function testInterface(name, setupScripts) { - const idlToTest = await fs.readFile( - path.join(__dirname, `../cases/${name}.webidl`), - { - encoding: "utf8" - } - ); - - const sandbox = createSandbox(setupScripts); +async function prepareInterface({ name, content }, idlArray, sandbox) { vm.runInContext( ` Object.defineProperty(self, '${name}', { @@ -54,47 +49,7 @@ async function testInterface(name, setupScripts) { sandbox ); - const prom = new Promise((resolve, reject) => { - const errors = []; - - sandbox.add_result_callback(test => { - if (test.status === 1) { - errors.push( - `Failed in "${test.name}": \n${test.message}\n\n${test.stack}` - ); - } else if (test.status === 2) { - errors.push( - `Timeout in "${test.name}": \n${test.message}\n\n${test.stack}` - ); - } else if (test.status === 3) { - errors.push( - `Uncompleted test "${test.name}": \n${test.message}\n\n${test.stack}` - ); - } - }); - - sandbox.add_completion_callback((tests, harnessStatus) => { - if (harnessStatus.status === 2) { - errors.push(new Error(`test harness should not timeout`)); - } - - if (errors.length === 1) { - reject(new Error(errors[0])); - } else if (errors.length) { - reject( - new Error(`${errors.length} errors in test:\n\n${errors.join("\n")}`) - ); - } else { - resolve(); - } - }); - }); - - const idlArray = new sandbox.IdlArray(); - idlArray.add_idls(idlToTest); - - idlArray.test(); - await prom; + idlArray.add_idls(content); } describe("IDL Harness", () => { @@ -114,7 +69,67 @@ describe("IDL Harness", () => { ); }); - test("URL.js", async () => { - await testInterface("URL", setupScripts); + const files = ["URL", "Unscopable"]; + test(`IDLHarness.js`, async () => { + const idlSources = await Promise.all( + files.map(async f => ({ + name: f.replace(/\.webidl$/, ""), + content: await fs.readFile( + path.join(__dirname, `../cases/${f}.webidl`), + { + encoding: "utf8" + } + ) + })) + ); + const sandbox = createSandbox(setupScripts); + + const idlArray = new sandbox.IdlArray(); + idlSources.forEach(source => { + prepareInterface(source, idlArray, sandbox); + }); + + const prom = new Promise((resolve, reject) => { + const errors = []; + + sandbox.add_result_callback(test => { + if (test.status === 1) { + errors.push( + `Failed in "${test.name}": \n${test.message}\n\n${test.stack}` + ); + } else if (test.status === 2) { + errors.push( + `Timeout in "${test.name}": \n${test.message}\n\n${test.stack}` + ); + } else if (test.status === 3) { + errors.push( + `Uncompleted test "${test.name}": \n${test.message}\n\n${ + test.stack + }` + ); + } + }); + + sandbox.add_completion_callback((tests, harnessStatus) => { + if (harnessStatus.status === 2) { + errors.push(new Error(`test harness should not timeout`)); + } + + if (errors.length === 1) { + reject(new Error(errors[0])); + } else if (errors.length) { + reject( + new Error( + `${errors.length} errors in test:\n\n${errors.join("\n")}` + ) + ); + } else { + resolve(); + } + }); + }); + + idlArray.test(); + await prom; }); });