tests: print selected branches on fail

This commit is contained in:
Peter Fajdiga
2024-09-20 21:27:26 +02:00
parent dac1d488b7
commit 279333dd1d
3 changed files with 25 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
{
runLog.length = 0;
const workspaceMock = initMocks();
const config = getDefaultConfig();
const world = new World(config);

View File

@@ -1,3 +1,5 @@
const runLog: string[] = [];
function assert(assertion: boolean, message?: string, skip: number = 0) {
if (assertion) {
return;
@@ -8,7 +10,14 @@ function assert(assertion: boolean, message?: string, skip: number = 0) {
} else {
console.assert(assertion);
}
console.log(getStackTrace(skip+1));
console.log("Random branches:")
for (const message of runLog) {
console.log(" " + message);
}
process.exit(1);
}

View File

@@ -1,11 +1,18 @@
function runOneOf(...fs: (() => void)[]) {
randomItem(fs)();
const index = randomInt(fs.length);
runLog.push(`${getStackFrame(1)} - Chose ${index}`);
fs[index]();
}
function runReorder(...fs: (() => void)[]) {
shuffle(fs);
for (const f of fs) {
f();
const fis = fs.map((f, index) => ({ f: f, index: index }));
shuffle(fis);
const indexes = fis.map((fi) => fi.index);
runLog.push(`${getStackFrame(1)} - Order ${indexes}`);
for (const fi of fis) {
fi.f();
}
}
@@ -13,10 +20,6 @@ function randomInt(n: number) {
return Math.floor(Math.random() * n);
}
function randomItem<T>(items: T[]): T {
return items[randomInt(items.length)];
}
function shuffle(items: any[]) {
for (let n = items.length; n > 1; n--) {
const i = n-1;
@@ -24,3 +27,7 @@ function shuffle(items: any[]) {
[items[i], items[j]] = [items[j], items[i]];
}
}
function getStackFrame(index: number) {
return new Error().stack!.split("\n")[index+2].substring(7);
}