Add tests for cycles
parent
04c2be0550
commit
edc8f4eb6a
|
@ -6,6 +6,8 @@ declare module 'wu' {
|
||||||
declare export default class Wu<T> {
|
declare export default class Wu<T> {
|
||||||
static <T>(Iterable<T>): Wu<T>;
|
static <T>(Iterable<T>): Wu<T>;
|
||||||
|
|
||||||
|
static values<T: Object>(T): Wu<$Values<T>>;
|
||||||
|
|
||||||
tap(T => mixed): Wu<T>;
|
tap(T => mixed): Wu<T>;
|
||||||
|
|
||||||
map<U>(T => U): Wu<U>;
|
map<U>(T => U): Wu<U>;
|
||||||
|
@ -24,6 +26,8 @@ declare module 'wu' {
|
||||||
some(): boolean;
|
some(): boolean;
|
||||||
some(T => boolean): boolean;
|
some(T => boolean): boolean;
|
||||||
|
|
||||||
|
forEach(T => mixed): void;
|
||||||
|
|
||||||
toArray(): T[];
|
toArray(): T[];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
31
tests/run.js
31
tests/run.js
|
@ -2,11 +2,12 @@ import * as assert from 'assert';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as yaml from 'yaml-js';
|
import * as yaml from 'yaml-js';
|
||||||
|
import wu from 'wu';
|
||||||
|
|
||||||
import collect from '../src';
|
import collect from '../src';
|
||||||
|
|
||||||
function run(title) {
|
function run(title) {
|
||||||
let actual, expected;
|
let actual, expected: any;
|
||||||
|
|
||||||
// Run the collector only if the suite will be checked.
|
// Run the collector only if the suite will be checked.
|
||||||
before(() => {
|
before(() => {
|
||||||
|
@ -14,9 +15,33 @@ function run(title) {
|
||||||
expected = yaml.load(fs.readFileSync(title + '.yaml', 'utf8'));
|
expected = yaml.load(fs.readFileSync(title + '.yaml', 'utf8'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should provide expected types', () => {
|
it('should not include cycles', () => {
|
||||||
assert.deepEqual((actual: any).types, (expected: any).types);
|
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() {
|
function main() {
|
||||||
|
|
Loading…
Reference in New Issue