2021-01-05 06:07:27 -05:00
|
|
|
const noColor = globalThis.Deno?.noColor ?? true;
|
|
|
|
const enabled = !noColor;
|
|
|
|
|
|
|
|
function code(open, close) {
|
|
|
|
return {
|
|
|
|
open: `\x1b[${open.join(";")}m`,
|
|
|
|
close: `\x1b[${close}m`,
|
|
|
|
regexp: new RegExp(`\\x1b\\[${close}m`, "g"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function run(str, code) {
|
|
|
|
return enabled
|
|
|
|
? `${code.open}${str.replace(code.regexp, code.open)}${code.close}`
|
|
|
|
: str;
|
|
|
|
}
|
|
|
|
|
|
|
|
function red(str) {
|
|
|
|
return run(str, code([31], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function green(str) {
|
|
|
|
return run(str, code([32], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function yellow(str) {
|
|
|
|
return run(str, code([33], 39));
|
|
|
|
}
|
|
|
|
|
|
|
|
const testResults = [];
|
|
|
|
const testsExpectFail = JSON.parse(Deno.args[0]);
|
2021-01-05 13:50:40 -05:00
|
|
|
function shouldExpectFail(name) {
|
|
|
|
if (testsExpectFail.includes(name)) return true;
|
|
|
|
for (const expectFail of testsExpectFail) {
|
|
|
|
if (name.startsWith(expectFail)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-05 06:07:27 -05:00
|
|
|
|
|
|
|
window.add_result_callback(({ message, name, stack, status }) => {
|
2021-01-05 13:50:40 -05:00
|
|
|
const expectFail = shouldExpectFail(name);
|
2021-01-12 20:14:17 -05:00
|
|
|
testResults.push({
|
|
|
|
name,
|
|
|
|
passed: status === 0,
|
|
|
|
expectFail,
|
|
|
|
message,
|
|
|
|
stack,
|
|
|
|
});
|
2021-01-05 06:07:27 -05:00
|
|
|
let simpleMessage = `test ${name} ... `;
|
|
|
|
switch (status) {
|
|
|
|
case 0:
|
|
|
|
if (expectFail) {
|
|
|
|
simpleMessage += red("ok (expected fail)");
|
|
|
|
} else {
|
|
|
|
simpleMessage += green("ok");
|
2021-01-12 20:14:17 -05:00
|
|
|
if (Deno.args[1] == "--quiet") {
|
|
|
|
// don't print `ok` tests if --quiet is enabled
|
|
|
|
return;
|
|
|
|
}
|
2021-01-05 06:07:27 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (expectFail) {
|
|
|
|
simpleMessage += yellow("failed (expected)");
|
|
|
|
} else {
|
|
|
|
simpleMessage += red("failed");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (expectFail) {
|
|
|
|
simpleMessage += yellow("failed (expected)");
|
|
|
|
} else {
|
|
|
|
simpleMessage += red("failed (timeout)");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (expectFail) {
|
|
|
|
simpleMessage += yellow("failed (expected)");
|
|
|
|
} else {
|
|
|
|
simpleMessage += red("failed (incomplete)");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(simpleMessage);
|
|
|
|
});
|
|
|
|
|
|
|
|
window.add_completion_callback((tests, harnessStatus) => {
|
|
|
|
const failed = testResults.filter((t) => !t.expectFail && !t.passed);
|
|
|
|
const expectedFailedButPassed = testResults.filter((t) =>
|
|
|
|
t.expectFail && t.passed
|
|
|
|
);
|
|
|
|
const expectedFailedButPassedCount = expectedFailedButPassed.length;
|
|
|
|
const failedCount = failed.length + expectedFailedButPassedCount;
|
|
|
|
const expectedFailedAndFailedCount = testResults.filter((t) =>
|
|
|
|
t.expectFail && !t.passed
|
|
|
|
).length;
|
|
|
|
const totalCount = testResults.length;
|
|
|
|
const passedCount = totalCount - failedCount - expectedFailedAndFailedCount;
|
|
|
|
|
|
|
|
if (failed.length > 0) {
|
|
|
|
console.log(`\nfailures:`);
|
|
|
|
}
|
|
|
|
for (const result of failed) {
|
|
|
|
console.log(
|
|
|
|
`\n${result.name}\n${result.message}\n${result.stack}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed.length > 0) {
|
|
|
|
console.log(`\nfailures:\n`);
|
|
|
|
}
|
|
|
|
for (const result of failed) {
|
2021-01-24 16:29:36 -05:00
|
|
|
console.log(` ${JSON.stringify(result.name)}`);
|
2021-01-05 06:07:27 -05:00
|
|
|
}
|
|
|
|
if (expectedFailedButPassedCount > 0) {
|
|
|
|
console.log(`\nexpected failures that passed:\n`);
|
|
|
|
}
|
|
|
|
for (const result of expectedFailedButPassed) {
|
2021-01-24 16:29:36 -05:00
|
|
|
console.log(` ${JSON.stringify(result.name)}`);
|
2021-01-05 06:07:27 -05:00
|
|
|
}
|
|
|
|
console.log(
|
|
|
|
`\ntest result: ${
|
|
|
|
failedCount > 0 ? red("failed") : green("ok")
|
|
|
|
}. ${passedCount} passed; ${failedCount} failed; ${expectedFailedAndFailedCount} expected failure; total ${totalCount}\n`,
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.exit(failedCount > 0 ? 1 : 0);
|
|
|
|
});
|