##// END OF EJS Templates
Added support for commonjs module format, all tests are pass.
Added support for commonjs module format, all tests are pass.

File last commit:

r59:ba3ff79c2832 default
r60:b23fe4a1683a default
Show More
TestTraits.ts
88 lines | 2.5 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 {
readonly _tape: tape.Test;
_subscriptions = new Array<IDestroyable>();
constructor(t: tape.Test) {
argumentNotNull(t, "tape");
this._tape = t;
}
writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) {
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) {
this._tape.comment(`DEBUG ${next.source.id} ${next.arg}`);
} else if (next.level >= LogLevel) {
this._tape.comment(`LOG ${next.source.id} ${next.arg}`);
} else if (next.level >= WarnLevel) {
this._tape.comment(`WARN ${next.source.id} ${next.arg}`);
} else {
this._tape.comment(`ERROR ${next.source.id} ${next.arg}`);
}
}
destroy() {
cin
working on support commonjs modules format
r59 this._subscriptions.forEach(destroy);
cin
tests moved under src/ folder...
r50 }
}
export async function delay(timeout: number, ct: ICancellation = Cancellation.none) {
let un: IDestroyable;
try {
await new Promise((resolve, reject) => {
if (ct.isRequested()) {
un = ct.register(reject);
} else {
const ht = setTimeout(() => {
resolve();
}, timeout);
un = ct.register(e => {
clearTimeout(ht);
reject(e);
});
}
});
} finally {
cin
working on support commonjs modules format
r59 destroy(un);
cin
tests moved under src/ folder...
r50 }
}
export function test(name: string, cb: (t: tape.Test) => any) {
tape(name, async t => {
const writer = new TapeWriter(t);
TraceSource.on(ts => {
ts.level = DebugLevel;
writer.writeEvents(ts.events);
});
try {
await cb(t);
} 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
tests moved under src/ folder...
r50 }
});
}