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-09-22 05:02:09 +02:00
|
|
|
private readonly _tests: [string, (() => void)][];
|
2021-05-14 02:25:30 +02:00
|
|
|
|
|
|
|
constructor(testsuite: string, tests: [string, () => void][]) {
|
2021-06-08 16:52:31 +02:00
|
|
|
this.name = testsuite
|
2021-09-22 05:02:09 +02:00
|
|
|
this._tests = tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RUns the test, returns the error messages.
|
|
|
|
* Returns an empty list if successful
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-10-16 02:54:22 +02:00
|
|
|
public Run(): ({ testsuite: string, name: string, msg: string } []) {
|
|
|
|
const failures: { testsuite: string, name: string, msg: string } [] = []
|
2021-09-22 05:02:09 +02:00
|
|
|
for (const [name, test] of this._tests) {
|
2020-08-22 17:33:08 +02:00
|
|
|
try {
|
|
|
|
test();
|
|
|
|
} catch (e) {
|
2021-10-16 02:54:22 +02:00
|
|
|
failures.push({testsuite: this.name, name: name, msg: "" + e});
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-22 05:02:09 +02:00
|
|
|
if (failures.length == 0) {
|
|
|
|
return undefined
|
2020-08-22 17:33:08 +02:00
|
|
|
} else {
|
2021-10-16 02:54:22 +02:00
|
|
|
return failures
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if(expected === undefined){
|
|
|
|
throw "ListIdentical failed: expected list is undefined"
|
|
|
|
}
|
|
|
|
if(actual === undefined){
|
|
|
|
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++) {
|
|
|
|
if (expected[i] !== actual[i]) {
|
|
|
|
throw `ListIdentical failed at index ${i}: expected ${expected[i]} but got ${actual[i]}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 17:33:08 +02:00
|
|
|
}
|