Support samples w/o types.yaml and schema.json

master
Paul Loyd 2018-11-21 15:58:56 +03:00
parent 06a15e2d67
commit a51d4bc5b3
1 changed files with 18 additions and 2 deletions

View File

@ -15,8 +15,8 @@ function run(title) {
// Run the collector only if the suite will be checked.
before(() => {
actual = collect(title + '/source.js');
expectedTypes = yaml.load(fs.readFileSync(title + '/types.yaml', 'utf8'));
expectedSchema = JSON.parse(fs.readFileSync(title + '/schema.json', 'utf8'));
expectedTypes = readFileAndPrepare(title + '/types.yaml', yaml.load);
expectedSchema = readFileAndPrepare(title + '/schema.json', JSON.parse);
});
it('should not include cycles', () => {
@ -40,6 +40,22 @@ function run(title) {
});
}
function readFileAndPrepare<R>(path: string, prepare: string => R): R | void {
let data;
try {
data = fs.readFileSync(path, 'utf8');
} catch (ex) {
if (ex.code === 'ENOENT') {
return undefined;
}
throw ex;
}
return prepare(data);
}
function detectCycles(obj: mixed, cycles: Set<mixed> = new Set, objs: Set<mixed> = new Set) {
if (obj == null || typeof obj !== 'object') {
return cycles;