Skip to content

Commit

Permalink
test(cto): improve code coverage (#505)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Roberts <[email protected]>
  • Loading branch information
mttrbrts authored Sep 6, 2022
1 parent b59141d commit 4999e75
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/concerto-cto/lib/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function resolveExternal(models, options, fileDownloader) {
if (path.extname(name) === '.cto') {
return Parser.parse(data);
}
return data;
throw new Error('External model file references are expected to have a .cto extension');
};
fileDownloader = new FileDownloader(new DefaultFileLoader(processFile), MetaModelUtil.getExternalImports);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/concerto-cto/test/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ describe('#resolveExternal', () => {
getModelFiles().forEach(({ file, model, resolved }) => {
it(`Should resolve external models in ${file}`, async () => {
const result = await resolveExternal(model, {}, null);
// console.log(model);
result.should.deep.equal(resolved);
});
});

it('Should reject external model with non-cto extension', async () => {
const model = JSON.parse(fs.readFileSync('./test/external/importJson', 'utf8'));
await resolveExternal(model, {}, null).should.be.rejectedWith(/External model file references are expected to have a .cto extension/);
});
});
18 changes: 18 additions & 0 deletions packages/concerto-cto/test/external/importJson
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$class": "[email protected]",
"models": [
{
"$class": "[email protected]",
"decorators": [],
"namespace": "test.person",
"imports": [
{
"$class": "[email protected]",
"namespace": "org.accordproject.time",
"uri": "github://accordproject/concerto/master/packages/concerto-cto/test/external/person.json"
}
],
"declarations": []
}
]
}
10 changes: 10 additions & 0 deletions packages/concerto-cto/test/parserMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ describe('parser', () => {
mm.should.deep.equal(JSON.parse(ast));
});
});

it('Should parse multiple files', () => {
const mm = Parser.parseModels(getCTOFiles().map(({ content }) => content), { skipLocationNodes: true });
mm.should.deep.equal(
{
$class: '[email protected]',
models: getCTOFiles().map(({ ast }) => JSON.parse(ast)),
}
);
});
});

describe('parser-exception', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/concerto-cto/test/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ describe('parser', () => {
cto.should.equal(content);
});
});

it('Should throw error for invalid import', () => {
(() => Printer.toCTO({
$class: '[email protected]',
namespace: '[email protected]',
imports: [{
$class: 'foo'
}],
declarations: [],
})).should.throw(Error, 'Unrecognized import');
});
});

0 comments on commit 4999e75

Please sign in to comment.