Skip to content

Commit 4999e75

Browse files
authored
test(cto): improve code coverage (#505)
Signed-off-by: Matt Roberts <[email protected]>
1 parent b59141d commit 4999e75

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

packages/concerto-cto/lib/external.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async function resolveExternal(models, options, fileDownloader) {
7272
if (path.extname(name) === '.cto') {
7373
return Parser.parse(data);
7474
}
75-
return data;
75+
throw new Error('External model file references are expected to have a .cto extension');
7676
};
7777
fileDownloader = new FileDownloader(new DefaultFileLoader(processFile), MetaModelUtil.getExternalImports);
7878
}

packages/concerto-cto/test/external.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ describe('#resolveExternal', () => {
4747
getModelFiles().forEach(({ file, model, resolved }) => {
4848
it(`Should resolve external models in ${file}`, async () => {
4949
const result = await resolveExternal(model, {}, null);
50-
// console.log(model);
5150
result.should.deep.equal(resolved);
5251
});
5352
});
53+
54+
it('Should reject external model with non-cto extension', async () => {
55+
const model = JSON.parse(fs.readFileSync('./test/external/importJson', 'utf8'));
56+
await resolveExternal(model, {}, null).should.be.rejectedWith(/External model file references are expected to have a .cto extension/);
57+
});
5458
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$class": "[email protected]",
3+
"models": [
4+
{
5+
"$class": "[email protected]",
6+
"decorators": [],
7+
"namespace": "test.person",
8+
"imports": [
9+
{
10+
"$class": "[email protected]",
11+
"namespace": "org.accordproject.time",
12+
"uri": "github://accordproject/concerto/master/packages/concerto-cto/test/external/person.json"
13+
}
14+
],
15+
"declarations": []
16+
}
17+
]
18+
}

packages/concerto-cto/test/parserMain.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ describe('parser', () => {
5454
mm.should.deep.equal(JSON.parse(ast));
5555
});
5656
});
57+
58+
it('Should parse multiple files', () => {
59+
const mm = Parser.parseModels(getCTOFiles().map(({ content }) => content), { skipLocationNodes: true });
60+
mm.should.deep.equal(
61+
{
62+
$class: '[email protected]',
63+
models: getCTOFiles().map(({ ast }) => JSON.parse(ast)),
64+
}
65+
);
66+
});
5767
});
5868

5969
describe('parser-exception', () => {

packages/concerto-cto/test/printer.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ describe('parser', () => {
5151
cto.should.equal(content);
5252
});
5353
});
54+
55+
it('Should throw error for invalid import', () => {
56+
(() => Printer.toCTO({
57+
$class: '[email protected]',
58+
namespace: '[email protected]',
59+
imports: [{
60+
$class: 'foo'
61+
}],
62+
declarations: [],
63+
})).should.throw(Error, 'Unrecognized import');
64+
});
5465
});

0 commit comments

Comments
 (0)