TestTraits.ts
62 lines
| 2.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r0 | import { IObservable, ICancellation, IDestroyable } from "@implab/core-amd/interfaces"; | ||
| import { Cancellation } from "@implab/core-amd/Cancellation"; | ||||
| import { TraceEvent, LogLevel, WarnLevel, DebugLevel, TraceSource } from "@implab/core-amd/log/TraceSource"; | ||||
| import { argumentNotNull, destroy } from "@implab/core-amd/safe"; | ||||
|
|
r1 | import { TapWriter } from "./Tap"; | ||
|
|
r0 | |||
| export class TapeWriter implements IDestroyable { | ||||
|
|
r1 | |||
| private readonly _t: TapWriter; | ||||
|
|
r0 | |||
| private readonly _subscriptions = new Array<IDestroyable>(); | ||||
|
|
r2 | private _destroyed = false; | ||
|
|
r0 | |||
|
|
r1 | constructor(t: TapWriter) { | ||
| argumentNotNull(t, "test"); | ||||
|
|
r2 | this._t = t; | ||
|
|
r0 | } | ||
| writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | ||||
| if (!this._destroyed) { | ||||
| const subscription = source.on(this.writeEvent.bind(this)); | ||||
| if (ct.isSupported()) { | ||||
| ct.register(subscription.destroy.bind(subscription)); | ||||
| } | ||||
| this._subscriptions.push(subscription); | ||||
| } | ||||
| } | ||||
| writeEvent(next: TraceEvent) { | ||||
| if (next.level >= DebugLevel) { | ||||
|
|
r1 | this._t.comment(`DEBUG ${next.source.id} ${next}`); | ||
|
|
r0 | } else if (next.level >= LogLevel) { | ||
|
|
r1 | this._t.comment(`LOG ${next.source.id} ${next}`); | ||
|
|
r0 | } else if (next.level >= WarnLevel) { | ||
|
|
r1 | this._t.comment(`WARN ${next.source.id} ${next}`); | ||
|
|
r0 | } else { | ||
|
|
r1 | this._t.comment(`ERROR ${next.source.id} ${next}`); | ||
|
|
r0 | } | ||
| } | ||||
| destroy() { | ||||
|
|
r2 | if (this._destroyed) | ||
| return; | ||||
| this._destroyed = true; | ||||
|
|
r0 | this._subscriptions.forEach(destroy); | ||
| } | ||||
| } | ||||
|
|
r2 | type TestCallback = (ok: (msg: string) => void, fail: (msg: string) => void, trace: TraceSource) => void; | ||
| type AsyncTestCallback = (trace: TraceSource) => PromiseLike<void>; | ||||
|
|
r0 | |||
|
|
r2 | export function test(name: string, cb: TestCallback ): void; | ||
| export function test(name: string, cb: AsyncTestCallback): void; | ||||
|
|
r1 | export function test(name: string, cb: TestCallback | AsyncTestCallback) { | ||
|
|
r2 | |||
|
|
r1 | } | ||
|
|
r0 | |||
|
|
r1 | export function run() { | ||
|
|
r0 | |||
| } | ||||
