2022-03-14 10:22:17 +00:00
|
|
|
import * as plugins from './tapbundle.plugins.js';
|
|
|
|
import { tapCreator } from './tapbundle.tapcreator.js';
|
|
|
|
import { TapTools } from './tapbundle.classes.taptools.js';
|
2017-04-28 07:49:57 +00:00
|
|
|
|
|
|
|
// imported interfaces
|
2023-07-13 00:09:53 +00:00
|
|
|
import { Deferred } from '@push.rocks/smartpromise';
|
|
|
|
import { HrtMeasurement } from '@push.rocks/smarttime';
|
2017-04-28 07:49:57 +00:00
|
|
|
|
|
|
|
// interfaces
|
2018-06-28 21:34:41 +00:00
|
|
|
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout';
|
2017-04-28 07:49:57 +00:00
|
|
|
|
2023-08-12 09:40:29 +00:00
|
|
|
export interface ITestFunction<T> {
|
|
|
|
(tapTools?: TapTools): Promise<T>;
|
|
|
|
}
|
2017-04-28 07:49:57 +00:00
|
|
|
|
2023-08-12 09:40:29 +00:00
|
|
|
export class TapTest<T = unknown> {
|
2019-11-16 22:40:36 +00:00
|
|
|
public description: string;
|
|
|
|
public failureAllowed: boolean;
|
|
|
|
public hrtMeasurement: HrtMeasurement;
|
|
|
|
public parallel: boolean;
|
|
|
|
public status: TTestStatus;
|
|
|
|
public tapTools: TapTools;
|
2021-01-26 03:15:50 +00:00
|
|
|
public testFunction: ITestFunction<T>;
|
2019-11-16 22:40:36 +00:00
|
|
|
public testKey: number; // the testKey the position in the test qeue. Set upon calling .run()
|
2021-01-26 03:15:50 +00:00
|
|
|
private testDeferred: Deferred<TapTest<T>> = plugins.smartpromise.defer();
|
|
|
|
public testPromise: Promise<TapTest<T>> = this.testDeferred.promise;
|
|
|
|
private testResultDeferred: Deferred<T> = plugins.smartpromise.defer();
|
|
|
|
public testResultPromise: Promise<T> = this.testResultDeferred.promise;
|
2017-04-28 07:49:57 +00:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*/
|
2023-08-12 09:40:29 +00:00
|
|
|
constructor(optionsArg: {
|
|
|
|
description: string;
|
|
|
|
testFunction: ITestFunction<T>;
|
|
|
|
parallel: boolean;
|
|
|
|
}) {
|
2018-06-28 21:34:41 +00:00
|
|
|
this.description = optionsArg.description;
|
|
|
|
this.hrtMeasurement = new HrtMeasurement();
|
|
|
|
this.parallel = optionsArg.parallel;
|
|
|
|
this.status = 'pending';
|
|
|
|
this.tapTools = new TapTools(this);
|
|
|
|
this.testFunction = optionsArg.testFunction;
|
2017-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the test
|
|
|
|
*/
|
2019-11-16 22:40:36 +00:00
|
|
|
public async run(testKeyArg: number) {
|
2018-06-28 21:34:41 +00:00
|
|
|
this.hrtMeasurement.start();
|
|
|
|
this.testKey = testKeyArg;
|
2019-11-16 22:40:36 +00:00
|
|
|
const testNumber = testKeyArg + 1;
|
2017-04-28 07:49:57 +00:00
|
|
|
try {
|
2021-01-26 02:35:02 +00:00
|
|
|
const testReturnValue = await this.testFunction(this.tapTools);
|
2017-04-28 07:49:57 +00:00
|
|
|
if (this.status === 'timeout') {
|
2018-06-28 21:34:41 +00:00
|
|
|
throw new Error('Test succeeded, but timed out...');
|
2017-04-28 07:49:57 +00:00
|
|
|
}
|
2018-06-28 21:34:41 +00:00
|
|
|
this.hrtMeasurement.stop();
|
|
|
|
console.log(
|
|
|
|
`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
|
|
|
|
);
|
|
|
|
this.status = 'success';
|
|
|
|
this.testDeferred.resolve(this);
|
2021-01-26 02:35:02 +00:00
|
|
|
this.testResultDeferred.resolve(testReturnValue);
|
2021-12-10 16:34:06 +00:00
|
|
|
} catch (err: any) {
|
2018-06-28 21:34:41 +00:00
|
|
|
this.hrtMeasurement.stop();
|
|
|
|
console.log(
|
|
|
|
`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`
|
|
|
|
);
|
|
|
|
this.testDeferred.resolve(this);
|
2021-01-26 02:35:02 +00:00
|
|
|
this.testResultDeferred.resolve(err);
|
2017-05-04 21:15:08 +00:00
|
|
|
|
|
|
|
// if the test has already succeeded before
|
2017-04-28 07:49:57 +00:00
|
|
|
if (this.status === 'success') {
|
2018-06-28 21:34:41 +00:00
|
|
|
this.status = 'errorAfterSuccess';
|
|
|
|
console.log('!!! ALERT !!!: weird behaviour, since test has been already successfull');
|
2017-07-11 13:10:05 +00:00
|
|
|
} else {
|
2018-06-28 21:34:41 +00:00
|
|
|
this.status = 'error';
|
2017-04-28 07:49:57 +00:00
|
|
|
}
|
2017-05-04 21:15:08 +00:00
|
|
|
|
|
|
|
// if the test is allowed to fail
|
|
|
|
if (this.failureAllowed) {
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(`please note: failure allowed!`);
|
2017-04-28 07:49:57 +00:00
|
|
|
}
|
2018-06-28 21:34:41 +00:00
|
|
|
console.log(err);
|
2017-04-28 07:49:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|