import { Consumer } from "andross"; import { expect } from "chai"; import { ICollectors, IStream, IStreamFactory, ITry, ITryFactory } from "../main"; /** @internal */ export function Expect(stream: IStream): Chai.Assertion { return expect(Array.from(stream)); } /** @internal */ export function expectTry(ATry: ITry, success: boolean, handler?: Consumer): void { expect(ATry.success).to.equal(success); if (handler !== undefined) { ATry.ifPresent(t => handler(expect(t)), e => handler(expect(e))); } } /** @internal */ export function toObj(map: Map): {[s: string]: any} { const obj: {[s: string]: any} = {}; for (const entry of map.entries()) { obj[entry[0]] = entry[1]; } return obj; } /** @internal */ export function setOf(...items: T[]): Set { return new Set(items); } /** @internal */ export class StreamTest { protected factory: IStreamFactory; constructor(factory: IStreamFactory) { this.factory = factory; } protected stream(iterable: Iterable, preparer: (stream: IStream) => IStream): Chai.Assertion { const stream = this.factory.stream(iterable); return Expect(preparer(stream)); } protected terminal(iterable: Iterable, preparer: (stream: IStream) => S): Chai.Assertion { const stream = this.factory.stream(iterable); return expect(preparer(stream)); } protected async promiseMe(iterable: Iterable, preparer: (stream: IStream) => Promise): Promise { const stream = this.factory.stream(iterable); const result = await preparer(stream); return result; } protected action(iterable: Iterable, preparer: (stream: IStream) => void): Chai.Assertion { const stream = this.factory.stream(iterable); return expect(() => preparer(stream)); } protected inf(): IStream { return this.factory.step(Infinity); } } /** @internal */ export interface IMain { Collectors: ICollectors; InplaceStreamFactory: IStreamFactory; TryFactory: ITryFactory; TypesafeStreamFactory: IStreamFactory; consumeFirst: (iterable: Iterable, sink: T[] | Consumer) => Iterable; monkeyPatch: (inplace?: boolean, force?: boolean) => void; } export function load(action: Consumer): void { const files = ["../main", "../../web/main", "../../elbe", "../../elbe.min"]; for (const file of files) { const lib = require(file); const main = { Collectors: lib.Collectors, InplaceStreamFactory: lib.InplaceStreamFactory, TryFactory: lib.TryFactory, TypesafeStreamFactory: lib.TypesafeStreamFactory, consumeFirst: lib.Methods.consumeFirst, monkeyPatch: lib.monkeyPatch, }; for (const key in main) { if (!Object.hasOwnProperty.call(main, key)) { continue; } const prop = Object.getOwnPropertyDescriptor(main, key); if (prop === undefined || prop.value === undefined) { throw new Error("Lib was not loaded correctly: " + key); } } (global as any).describe(`With compiled library at ${file}`, () => action(main)); } }