TestTraits.ts
60 lines
| 1.9 KiB
| video/mp2t
|
TypeScriptLexer
cin
|
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"; | ||||
cin
|
r1 | import { TapWriter } from "./Tap"; | ||
cin
|
r0 | |||
export class TapeWriter implements IDestroyable { | ||||
cin
|
r1 | |||
private readonly _t: TapWriter; | ||||
cin
|
r0 | |||
private readonly _subscriptions = new Array<IDestroyable>(); | ||||
cin
|
r2 | private _destroyed = false; | ||
cin
|
r0 | |||
cin
|
r1 | constructor(t: TapWriter) { | ||
argumentNotNull(t, "test"); | ||||
cin
|
r2 | this._t = t; | ||
cin
|
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) { | ||||
cin
|
r1 | this._t.comment(`DEBUG ${next.source.id} ${next}`); | ||
cin
|
r0 | } else if (next.level >= LogLevel) { | ||
cin
|
r1 | this._t.comment(`LOG ${next.source.id} ${next}`); | ||
cin
|
r0 | } else if (next.level >= WarnLevel) { | ||
cin
|
r1 | this._t.comment(`WARN ${next.source.id} ${next}`); | ||
cin
|
r0 | } else { | ||
cin
|
r1 | this._t.comment(`ERROR ${next.source.id} ${next}`); | ||
cin
|
r0 | } | ||
} | ||||
destroy() { | ||||
cin
|
r2 | if (this._destroyed) | ||
return; | ||||
this._destroyed = true; | ||||
cin
|
r0 | this._subscriptions.forEach(destroy); | ||
} | ||||
} | ||||
cin
|
r2 | type TestCallback = (ok: (msg: string) => void, fail: (msg: string) => void, trace: TraceSource) => void; | ||
type AsyncTestCallback = (trace: TraceSource) => PromiseLike<void>; | ||||
cin
|
r0 | |||
cin
|
r1 | export function test(name: string, cb: TestCallback | AsyncTestCallback) { | ||
cin
|
r2 | |||
cin
|
r1 | } | ||
cin
|
r0 | |||
cin
|
r1 | export function run() { | ||
cin
|
r0 | |||
} | ||||