summaryrefslogtreecommitdiff
path: root/tst/test_utils.ts
blob: 5c6237cbd16e96b9657cb080c4501e96ccd9f551 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { type ITrace, type ITraceWith, TraceableImpl } from '../lib/index';

export class CollectingTrace<TraceWith> implements ITrace<TraceWith> {
    constructor(
        private readonly collector: Array<Array<ITraceWith<TraceWith>>> = [],
        private readonly scopes: Array<ITraceWith<TraceWith>> = [],
    ) {}

    public get events() {
        return this.collector;
    }

    public traceScope(trace: ITraceWith<TraceWith>): ITrace<TraceWith> {
        return new CollectingTrace(this.collector, this.scopes.concat([trace]));
    }

    public trace(trace: ITraceWith<TraceWith>) {
        this.collector.push(this.scopes.concat([trace]));
    }
}

export class TestTraceable<T, TraceWith> extends TraceableImpl<T, TraceWith> {
    constructor(item: T, trace: ITrace<TraceWith>) {
        super(item, trace);
    }

    static of<T, TraceWith>(item: T, trace: ITrace<TraceWith>) {
        return new TestTraceable<T, TraceWith>(item, trace);
    }
}