From 427878763e91e05fe09d5d27bd4f119a74e78d99 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sat, 16 Nov 2019 23:40:36 +0100 Subject: [PATCH] feat(pretask): introduce new pretask functionality for setting up testing environment --- ts/tapbundle.classes.pretask.ts | 19 +++++++++++++++++++ ts/tapbundle.classes.tap.ts | 12 ++++++++++++ ts/tapbundle.classes.taptest.ts | 28 +++++++++++++--------------- ts/tapbundle.classes.tapwrap.ts | 1 + 4 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 ts/tapbundle.classes.pretask.ts diff --git a/ts/tapbundle.classes.pretask.ts b/ts/tapbundle.classes.pretask.ts new file mode 100644 index 0000000..9291b1f --- /dev/null +++ b/ts/tapbundle.classes.pretask.ts @@ -0,0 +1,19 @@ +import * as plugins from './tapbundle.plugins'; +import { TapTools } from './tapbundle.classes.taptools'; + + +export interface IPreTaskFunction { + (tapTools?: TapTools): Promise; +} + +export class PreTask { + public preTaskFunction: IPreTaskFunction; + + constructor(preTaskFunctionArg: IPreTaskFunction) { + this.preTaskFunction = preTaskFunctionArg; + } + + public async run () { + await this.preTaskFunction(new TapTools(null)); + } +} \ No newline at end of file diff --git a/ts/tapbundle.classes.tap.ts b/ts/tapbundle.classes.tap.ts index 79c59a7..ece242e 100644 --- a/ts/tapbundle.classes.tap.ts +++ b/ts/tapbundle.classes.tap.ts @@ -1,5 +1,6 @@ import * as plugins from './tapbundle.plugins'; +import { IPreTaskFunction, PreTask } from './tapbundle.classes.pretask'; import { TapTest, ITestFunction } from './tapbundle.classes.taptest'; import { TapWrap, ITapWrapFunction } from './tapbundle.classes.tapwrap'; export class Tap { @@ -25,6 +26,7 @@ export class Tap { } }; + private _tapPreTasks: PreTask[] = []; private _tapTests: TapTest[] = []; private _tapTestsOnly: TapTest[] = []; @@ -51,6 +53,10 @@ export class Tap { return localTest; } + public preTask (functionArg: IPreTaskFunction) { + this._tapPreTasks.push(new PreTask(functionArg)); + } + /** * wraps function */ @@ -82,6 +88,7 @@ export class Tap { // safeguard against empty test array if (this._tapTests.length === 0) { console.log('no tests specified. Ending here!'); + // TODO: throw proper error return; } @@ -93,6 +100,11 @@ export class Tap { concerningTests = this._tapTests; } + // lets run the pretasks + for (const preTask of this._tapPreTasks) { + await preTask.run(); + } + console.log(`1..${concerningTests.length}`); for (let testKey = 0; testKey < concerningTests.length; testKey++) { const currentTest = concerningTests[testKey]; diff --git a/ts/tapbundle.classes.taptest.ts b/ts/tapbundle.classes.taptest.ts index b308d6e..51b4411 100644 --- a/ts/tapbundle.classes.taptest.ts +++ b/ts/tapbundle.classes.taptest.ts @@ -9,21 +9,19 @@ import { Deferred } from '@pushrocks/smartpromise'; // interfaces export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout'; -export interface ITestFunction { - (tapTools?: TapTools): Promise; -} +export type ITestFunction = (tapTools?: TapTools) => Promise; export class TapTest { - description: string; - failureAllowed: boolean; - hrtMeasurement: HrtMeasurement; - parallel: boolean; - status: TTestStatus; - tapTools: TapTools; - testFunction: ITestFunction; - testKey: number; // the testKey the position in the test qeue. Set upon calling .run() - testDeferred: Deferred = plugins.smartpromise.defer(); - testPromise: Promise = this.testDeferred.promise; + public description: string; + public failureAllowed: boolean; + public hrtMeasurement: HrtMeasurement; + public parallel: boolean; + public status: TTestStatus; + public tapTools: TapTools; + public testFunction: ITestFunction; + public testKey: number; // the testKey the position in the test qeue. Set upon calling .run() + public testDeferred: Deferred = plugins.smartpromise.defer(); + public testPromise: Promise = this.testDeferred.promise; /** * constructor */ @@ -39,10 +37,10 @@ export class TapTest { /** * run the test */ - async run(testKeyArg: number) { + public async run(testKeyArg: number) { this.hrtMeasurement.start(); this.testKey = testKeyArg; - let testNumber = testKeyArg + 1; + const testNumber = testKeyArg + 1; try { await this.testFunction(this.tapTools); if (this.status === 'timeout') { diff --git a/ts/tapbundle.classes.tapwrap.ts b/ts/tapbundle.classes.tapwrap.ts index ef96a4c..4f5438b 100644 --- a/ts/tapbundle.classes.tapwrap.ts +++ b/ts/tapbundle.classes.tapwrap.ts @@ -19,6 +19,7 @@ export class TapWrap { * run the wrapFunction */ async run() { + // TODO: make sure it makes sense what we do here. await this.wrapFunction(); } }