tapbundle/ts/tapbundle.classes.taptest.ts

81 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-05-04 21:15:08 +00:00
import * as plugins from './tapbundle.plugins'
2017-04-28 07:49:57 +00:00
import { tapCreator } from './tapbundle.tapcreator'
import { TapTools } from './tapbundle.classes.taptools'
// imported interfaces
import { HrtMeasurement } from 'early'
2017-05-04 21:15:08 +00:00
import { Deferred } from 'smartq'
2017-04-28 07:49:57 +00:00
// interfaces
export type TTestStatus = 'success' | 'error' | 'pending' | 'errorAfterSuccess' | 'timeout'
export interface ITestFunction {
(tapTools?: TapTools): Promise<any>
}
export class TapTest {
description: string
failureAllowed: boolean
hrtMeasurement: HrtMeasurement
parallel: boolean
status: TTestStatus
tapTools: TapTools
testFunction: ITestFunction
2017-07-11 13:10:05 +00:00
testKey: number // the testKey the position in the test qeue. Set upon calling .run()
2017-05-04 21:15:08 +00:00
testDeferred: Deferred<TapTest> = plugins.smartq.defer()
testPromise: Promise<TapTest> = this.testDeferred.promise
2017-04-28 07:49:57 +00:00
/**
* constructor
*/
constructor (optionsArg: {
description: string,
testFunction: ITestFunction,
parallel: boolean
}) {
this.description = optionsArg.description
this.hrtMeasurement = new HrtMeasurement()
this.parallel = optionsArg.parallel
this.status = 'pending'
this.tapTools = new TapTools(this)
this.testFunction = optionsArg.testFunction
}
/**
* run the test
*/
async run (testKeyArg: number) {
this.hrtMeasurement.start()
2017-07-11 13:10:05 +00:00
this.testKey = testKeyArg
let testNumber = testKeyArg + 1
2017-04-28 07:49:57 +00:00
try {
await this.testFunction(this.tapTools)
if (this.status === 'timeout') {
throw new Error ('Test succeeded, but timed out...')
}
this.hrtMeasurement.stop()
2017-07-11 13:10:05 +00:00
console.log(`ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`)
2017-04-28 07:49:57 +00:00
this.status = 'success'
2017-05-04 21:15:08 +00:00
this.testDeferred.resolve(this)
2017-04-28 07:49:57 +00:00
} catch (err) {
this.hrtMeasurement.stop()
2017-07-11 13:10:05 +00:00
console.log(`not ok ${testNumber} - ${this.description} # time=${this.hrtMeasurement.milliSeconds}ms`)
this.testDeferred.resolve(this)
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') {
this.status = 'errorAfterSuccess'
console.log('!!! ALERT !!!: weird behaviour, since test has been already successfull')
2017-07-11 13:10:05 +00:00
} else {
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) {
2017-04-28 07:49:57 +00:00
console.log(`please note: failure allowed!`)
}
console.log(err)
}
}
}