From 279333dd1dfef50361a323d6e18156a8576caa35 Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 20 Sep 2024 21:27:26 +0200 Subject: [PATCH] tests: print selected branches on fail --- src/tests/flows/maximization.ts | 1 + src/tests/utils/assert.ts | 9 +++++++++ src/tests/utils/random.ts | 23 +++++++++++++++-------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/tests/flows/maximization.ts b/src/tests/flows/maximization.ts index b799640..33a4fc3 100644 --- a/src/tests/flows/maximization.ts +++ b/src/tests/flows/maximization.ts @@ -1,4 +1,5 @@ { + runLog.length = 0; const workspaceMock = initMocks(); const config = getDefaultConfig(); const world = new World(config); diff --git a/src/tests/utils/assert.ts b/src/tests/utils/assert.ts index b57621e..c5e03d4 100644 --- a/src/tests/utils/assert.ts +++ b/src/tests/utils/assert.ts @@ -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); } diff --git a/src/tests/utils/random.ts b/src/tests/utils/random.ts index 377dd4f..a70544b 100644 --- a/src/tests/utils/random.ts +++ b/src/tests/utils/random.ts @@ -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(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); +}