Add tests for cycles

master
Paul Loyd 2017-11-30 17:19:44 +03:00
parent 04c2be0550
commit edc8f4eb6a
2 changed files with 32 additions and 3 deletions

View File

@ -6,6 +6,8 @@ declare module 'wu' {
declare export default class Wu<T> {
static <T>(Iterable<T>): Wu<T>;
static values<T: Object>(T): Wu<$Values<T>>;
tap(T => mixed): Wu<T>;
map<U>(T => U): Wu<U>;
@ -24,6 +26,8 @@ declare module 'wu' {
some(): boolean;
some(T => boolean): boolean;
forEach(T => mixed): void;
toArray(): T[];
}
}

View File

@ -2,11 +2,12 @@ import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as yaml from 'yaml-js';
import wu from 'wu';
import collect from '../src';
function run(title) {
let actual, expected;
let actual, expected: any;
// Run the collector only if the suite will be checked.
before(() => {
@ -14,9 +15,33 @@ function run(title) {
expected = yaml.load(fs.readFileSync(title + '.yaml', 'utf8'));
});
it('should provide expected types', () => {
assert.deepEqual((actual: any).types, (expected: any).types);
it('should not include cycles', () => {
assert.deepEqual(detectCycles(actual.types), new Set);
});
it('should provide expected types', () => {
assert.deepEqual(actual.types, expected.types);
});
}
function detectCycles(obj: mixed, cycles: Set<mixed> = new Set, objs: Set<mixed> = new Set) {
if (obj == null || typeof obj !== 'object') {
return cycles;
}
if (objs.has(obj)) {
cycles.add(obj);
}
objs.add(obj);
if (obj instanceof Array) {
wu(obj).forEach(item => detectCycles(item, cycles, objs));
} else {
wu.values(obj).forEach(item => detectCycles(item, cycles, objs));
}
return cycles;
}
function main() {