2020-08-22 17:33:08 +02:00
|
|
|
export default class T {
|
2021-05-14 02:25:30 +02:00
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
public readonly name: string;
|
2021-12-24 02:48:04 +01:00
|
|
|
private readonly _tests: [string, (() => (void | Promise<void>))][];
|
2021-05-14 02:25:30 +02:00
|
|
|
|
2022-01-14 13:58:15 +01:00
|
|
|
constructor(tests: [string, () => (Promise<void> | void)][]) {
|
|
|
|
this.name = this.constructor.name;
|
2021-09-22 05:02:09 +02:00
|
|
|
this._tests = tests;
|
|
|
|
}
|
|
|
|
|
2021-05-14 02:25:30 +02:00
|
|
|
static assertContains(needle: string, actual: string) {
|
|
|
|
if (actual.indexOf(needle) < 0) {
|
2021-02-20 01:45:51 +01:00
|
|
|
throw `The substring ${needle} was not found`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 13:59:49 +02:00
|
|
|
static isTrue(b: boolean, msg: string) {
|
2021-05-14 02:25:30 +02:00
|
|
|
if (!b) {
|
|
|
|
throw "Expected true, but got false: " + msg
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:54:22 +02:00
|
|
|
|
|
|
|
static equals(a, b, msg?) {
|
|
|
|
if (a !== b) {
|
|
|
|
throw "Not the same: " + (msg ?? "") + "\n" +
|
|
|
|
"Expcected: " + a + "\n" +
|
|
|
|
"Got : " + b
|
2021-10-06 17:48:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-05-14 02:25:30 +02:00
|
|
|
static isFalse(b: boolean, msg: string) {
|
|
|
|
if (b) {
|
|
|
|
throw "Expected false, but got true: " + msg
|
2021-04-09 13:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-16 02:54:22 +02:00
|
|
|
|
|
|
|
static listIdentical<T>(expected: T[], actual: T[]): void {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (expected === undefined) {
|
2021-10-16 02:54:22 +02:00
|
|
|
throw "ListIdentical failed: expected list is undefined"
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
if (actual === undefined) {
|
2021-10-16 02:54:22 +02:00
|
|
|
throw "ListIdentical failed: actual list is undefined"
|
|
|
|
}
|
|
|
|
if (expected.length !== actual.length) {
|
|
|
|
throw `ListIdentical failed: expected a list of length ${expected.length} but got a list of length ${actual.length}`
|
|
|
|
}
|
|
|
|
for (let i = 0; i < expected.length; i++) {
|
2022-01-06 15:33:25 +01:00
|
|
|
if(expected[i] !== undefined && expected[i]["length"] !== undefined ){
|
2022-01-06 14:39:42 +01:00
|
|
|
T.listIdentical(<any> expected[i], <any> actual[i])
|
|
|
|
}else if (expected[i] !== actual[i]) {
|
2021-11-07 16:34:51 +01:00
|
|
|
throw `ListIdentical failed at index ${i}: expected ${expected[i]} but got ${actual[i]}`
|
2021-10-16 02:54:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RUns the test, returns the error messages.
|
|
|
|
* Returns an empty list if successful
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-12-30 20:41:45 +01:00
|
|
|
public async Run(): Promise<{ testsuite: string, name: string, msg: string } []> {
|
2021-11-07 16:34:51 +01:00
|
|
|
const failures: { testsuite: string, name: string, msg: string } [] = []
|
|
|
|
for (const [name, test] of this._tests) {
|
|
|
|
try {
|
2021-12-24 02:48:04 +01:00
|
|
|
const r = test()
|
|
|
|
if (r instanceof Promise) {
|
2021-12-30 20:41:45 +01:00
|
|
|
try {
|
|
|
|
await r
|
|
|
|
} catch (e) {
|
2021-12-24 02:48:04 +01:00
|
|
|
console.log("ASYNC ERROR: ", e, e.stack)
|
|
|
|
failures.push({testsuite: this.name, name: name, msg: "" + e});
|
2021-12-30 20:41:45 +01:00
|
|
|
}
|
2021-12-24 02:48:04 +01:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log("ERROR: ", e, e.stack)
|
|
|
|
failures.push({testsuite: this.name, name: name, msg: "" + e});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failures.length == 0) {
|
|
|
|
return undefined
|
|
|
|
} else {
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|