Skip to content

Commit

Permalink
Add all webidl interfaces to the same test context
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebmaster committed Jun 1, 2019
1 parent b6ca097 commit 93e26f3
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions test/harness/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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}', {
Expand All @@ -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", () => {
Expand All @@ -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;
});
});

0 comments on commit 93e26f3

Please sign in to comment.