##// END OF EJS Templates
StringBuilder, TextWriter, ConsoleWriter tests
StringBuilder, TextWriter, ConsoleWriter tests

File last commit:

r82:025f02eff3b2 v1.3.0 default
r82:025f02eff3b2 v1.3.0 default
Show More
TestTraits.ts
74 lines | 2.3 KiB | video/mp2t | TypeScriptLexer
cin
tests moved under src/ folder...
r50 import { IObservable, ICancellation, IDestroyable } from "@implab/core/interfaces";
import { Cancellation } from "@implab/core/Cancellation";
import { TraceEvent, LogLevel, WarnLevel, DebugLevel, TraceSource } from "@implab/core/log/TraceSource";
import * as tape from "tape";
cin
working on support commonjs modules format
r59 import { argumentNotNull, destroy } from "@implab/core/safe";
cin
tests moved under src/ folder...
r50
export class TapeWriter implements IDestroyable {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 private readonly _tape: tape.Test;
cin
tests moved under src/ folder...
r50
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 private readonly _subscriptions = new Array<IDestroyable>();
private _destroyed;
cin
tests moved under src/ folder...
r50
constructor(t: tape.Test) {
argumentNotNull(t, "tape");
this._tape = t;
}
writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 if (!this._destroyed) {
const subscription = source.on(this.writeEvent.bind(this));
if (ct.isSupported()) {
ct.register(subscription.destroy.bind(subscription));
}
this._subscriptions.push(subscription);
cin
tests moved under src/ folder...
r50 }
}
writeEvent(next: TraceEvent) {
if (next.level >= DebugLevel) {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 this._tape.comment(`DEBUG ${next.source.id} ${next}`);
cin
tests moved under src/ folder...
r50 } else if (next.level >= LogLevel) {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 this._tape.comment(`LOG ${next.source.id} ${next}`);
cin
tests moved under src/ folder...
r50 } else if (next.level >= WarnLevel) {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 this._tape.comment(`WARN ${next.source.id} ${next}`);
cin
tests moved under src/ folder...
r50 } else {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 this._tape.comment(`ERROR ${next.source.id} ${next}`);
cin
tests moved under src/ folder...
r50 }
}
destroy() {
cin
working on support commonjs modules format
r59 this._subscriptions.forEach(destroy);
cin
tests moved under src/ folder...
r50 }
}
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 export function test(name: string, cb: (t: tape.Test, trace: TraceSource) => any) {
cin
tests moved under src/ folder...
r50 tape(name, async t => {
const writer = new TapeWriter(t);
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 // this trace is not announced through the TraceSource global registry
const trace = new TraceSource(name);
trace.level = DebugLevel;
writer.writeEvents(trace.events);
const h = TraceSource.on(ts => {
cin
tests moved under src/ folder...
r50 ts.level = DebugLevel;
writer.writeEvents(ts.events);
});
try {
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 await cb(t, trace);
cin
tests moved under src/ folder...
r50 } catch (e) {
// verbose error information
// tslint:disable-next-line
console.error(e);
t.fail(e);
} finally {
t.end();
cin
working on support commonjs modules format
r59 destroy(writer);
cin
StringBuilder, TextWriter, ConsoleWriter tests
r82 destroy(h);
cin
tests moved under src/ folder...
r50 }
});
}