| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { markup, reactive } from "@odoo/owl"; |
| 4 |
import { HootError, stringify } from "../hoot_utils"; |
| 5 |
import { Job } from "./job"; |
| 6 |
import { Tag } from "./tag"; |
| 7 |
|
| 8 |
/** |
| 9 |
* @template T |
| 10 |
* @typedef {T | PromiseLike<T>} MaybePromise |
| 11 |
*/ |
| 12 |
|
| 13 |
//----------------------------------------------------------------------------- |
| 14 |
// Global |
| 15 |
//----------------------------------------------------------------------------- |
| 16 |
|
| 17 |
const { |
| 18 |
Object: { freeze: $freeze }, |
| 19 |
} = globalThis; |
| 20 |
|
| 21 |
//----------------------------------------------------------------------------- |
| 22 |
// Internal |
| 23 |
//----------------------------------------------------------------------------- |
| 24 |
|
| 25 |
const SHARED_LOGS = $freeze({}); |
| 26 |
const SHARED_RESULTS = $freeze([]); |
| 27 |
|
| 28 |
//----------------------------------------------------------------------------- |
| 29 |
// Exports |
| 30 |
//----------------------------------------------------------------------------- |
| 31 |
|
| 32 |
/** |
| 33 |
* @param {Pick<Test, "name" | "parent">} test |
| 34 |
* @returns {HootError} |
| 35 |
*/ |
| 36 |
export function testError({ name, parent }, ...message) { |
| 37 |
const parentString = parent ? ` (in suite ${stringify(parent.name)})` : ""; |
| 38 |
return new HootError( |
| 39 |
`error while registering test ${stringify(name)}${parentString}: ${message.join("\n")}`, |
| 40 |
{ level: "critical" } |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
export class Test extends Job { |
| 45 |
static SKIPPED = 0; |
| 46 |
static PASSED = 1; |
| 47 |
static FAILED = 2; |
| 48 |
static ABORTED = 3; |
| 49 |
|
| 50 |
formatted = false; |
| 51 |
logs = reactive({ |
| 52 |
error: 0, |
| 53 |
warn: 0, |
| 54 |
}); |
| 55 |
/** @type {import("./expect").CaseResult[]} */ |
| 56 |
results = reactive([]); |
| 57 |
/** @type {() => MaybePromise<void> | null} */ |
| 58 |
run = null; |
| 59 |
runFnString = ""; |
| 60 |
status = Test.SKIPPED; |
| 61 |
|
| 62 |
get code() { |
| 63 |
if (!this.formatted) { |
| 64 |
this.formatted = true; |
| 65 |
this.runFnString = this.formatFunctionSource(this.runFnString); |
| 66 |
if (window.Prism) { |
| 67 |
const highlighted = window.Prism.highlight( |
| 68 |
this.runFnString, |
| 69 |
Prism.languages.javascript, |
| 70 |
"javascript" |
| 71 |
); |
| 72 |
this.runFnString = markup(highlighted); |
| 73 |
} |
| 74 |
} |
| 75 |
return this.runFnString; |
| 76 |
} |
| 77 |
|
| 78 |
get duration() { |
| 79 |
return this.results.reduce((acc, result) => acc + result.duration, 0); |
| 80 |
} |
| 81 |
|
| 82 |
/** @returns {import("./expect").CaseResult | null} */ |
| 83 |
get lastResults() { |
| 84 |
return this.results.at(-1); |
| 85 |
} |
| 86 |
|
| 87 |
cleanup() { |
| 88 |
this.run = null; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param {string} stringFn |
| 93 |
*/ |
| 94 |
formatFunctionSource(stringFn) { |
| 95 |
let modifiers = ""; |
| 96 |
let startingLine = 0; |
| 97 |
if (this.name) { |
| 98 |
for (const tag of this.tags) { |
| 99 |
if (this.parent.tags.includes(tag)) { |
| 100 |
continue; |
| 101 |
} |
| 102 |
switch (tag.key) { |
| 103 |
case Tag.TODO: |
| 104 |
case Tag.DEBUG: |
| 105 |
case Tag.SKIP: |
| 106 |
case Tag.ONLY: { |
| 107 |
modifiers += `.${tag.key}`; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
startingLine++; |
| 114 |
stringFn = `test${modifiers}(${stringify(this.name)}, ${stringFn});`; |
| 115 |
} |
| 116 |
|
| 117 |
const lines = stringFn.split("\n"); |
| 118 |
|
| 119 |
let toTrim = null; |
| 120 |
for (let i = startingLine; i < lines.length; i++) { |
| 121 |
if (!lines[i].trim()) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
const [, whiteSpaces] = lines[i].match(/^(\s*)/); |
| 125 |
if (toTrim === null || whiteSpaces.length < toTrim) { |
| 126 |
toTrim = whiteSpaces.length; |
| 127 |
} |
| 128 |
} |
| 129 |
if (toTrim) { |
| 130 |
for (let i = startingLine; i < lines.length; i++) { |
| 131 |
lines[i] = lines[i].slice(toTrim); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return lines.join("\n"); |
| 136 |
} |
| 137 |
|
| 138 |
minimize() { |
| 139 |
super.minimize(); |
| 140 |
|
| 141 |
this.setRunFn(null); |
| 142 |
this.runFnString = ""; |
| 143 |
this.logs = SHARED_LOGS; |
| 144 |
this.results = SHARED_RESULTS; |
| 145 |
} |
| 146 |
|
| 147 |
reset() { |
| 148 |
this.run = this.run.bind(this); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @param {() => MaybePromise<void>} fn |
| 153 |
*/ |
| 154 |
setRunFn(fn) { |
| 155 |
this.run = fn ? async () => fn() : null; |
| 156 |
if (fn) { |
| 157 |
this.formatted = false; |
| 158 |
this.runFnString = fn.toString(); |
| 159 |
} |
| 160 |
} |
| 161 |
} |