flow2schema/tests/run.js

56 lines
1.3 KiB
JavaScript

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;
// Run the collector only if the suite will be checked.
before(() => {
actual = collect(title + '/source.js');
expected = yaml.load(fs.readFileSync(title + '/types.yaml', 'utf8'));
});
it('should not include cycles', () => {
assert.deepEqual(detectCycles(actual.types), new Set);
});
it('should provide expected types', () => {
assert.deepEqual(actual.types, expected);
});
}
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() {
process.chdir(path.join(__dirname, 'samples'));
for (const title of fs.readdirSync('.')) {
describe(title, () => run(title));
}
}
main();