Skip to content

Commit

Permalink
refactor(declarations): move declaration uniqueness check to model fi…
Browse files Browse the repository at this point in the history
…le (accordproject#794)

* refactor(declarations): Move unique name check to model file.

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor(test): move validation checks for duplicate class to model file

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* test: empty commit to trigger test

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

---------

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>
  • Loading branch information
ekarademir authored and mttrbrts committed Mar 10, 2024
1 parent 65d6f7c commit e3c433c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
15 changes: 0 additions & 15 deletions packages/concerto-core/src/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,6 @@ class ClassDeclaration extends Declaration {
validate() {
super.validate();

const declarations = this.getModelFile().getAllDeclarations();
const declarationNames = declarations.map(
d => d.getFullyQualifiedName()
);
const uniqueNames = new Set(declarationNames);

if (uniqueNames.size !== declarations.length) {
const duplicateElements = declarationNames.filter(
(item, index) => declarationNames.indexOf(item) !== index
);
throw new IllegalModelException(
`Duplicate class name ${duplicateElements[0]}`
);
}

// if we have a super type make sure it exists
if (this.superType !== null) {
// and make sure that the class isn't extending itself
Expand Down
16 changes: 16 additions & 0 deletions packages/concerto-core/src/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ class ModelFile {
});

// Validate all of the types in this model file.
// Check if names of the declarations are unique.
const declarationNames = this.declarations.map(
d => d.getFullyQualifiedName()
);
const uniqueNames = new Set(declarationNames);

if (uniqueNames.size !== this.declarations.length) {
const duplicateElements = declarationNames.filter(
(item, index) => declarationNames.indexOf(item) !== index
);
throw new IllegalModelException(
`Duplicate class name ${duplicateElements[0]}`
);
}

// Run validations on class declarations
for(let n=0; n < this.declarations.length; n++) {
let classDeclaration = this.declarations[n];
classDeclaration.validate();
Expand Down
37 changes: 37 additions & 0 deletions packages/concerto-core/test/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ describe('ModelFile', () => {
const carLeaseModel = fs.readFileSync(path.resolve(__dirname, '../data/model/carlease.cto'), 'utf8');
let modelManager;
let sandbox;
let introspectUtils;

beforeEach(() => {
modelManager = new ModelManager();
Util.addComposerModel(modelManager);
introspectUtils = new IntrospectUtils(modelManager);
sandbox = sinon.createSandbox();
});

Expand Down Expand Up @@ -204,6 +206,41 @@ describe('ModelFile', () => {

describe('#validate', () => {

it('should throw when asset name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupeassetname.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw when transaction name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupetransactionname.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw when participant name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupeparticipantname.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw when concept name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupeconceptname.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw when enum name is duplicted in a modelfile', () => {
let asset = introspectUtils.loadModelFile('test/data/parser/classdeclaration.dupeenumname.cto');
(() => {
asset.validate();
}).should.throw(/Duplicate class/);
});

it('should throw if an import exists for an invalid namespace', () => {
const model = `
namespace org.acme@1.0.0
Expand Down

0 comments on commit e3c433c

Please sign in to comment.