Skip to content

Commit

Permalink
test: fix tests for concerto-core
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman committed Jan 5, 2024
1 parent a027e85 commit a1548b9
Show file tree
Hide file tree
Showing 41 changed files with 472 additions and 1,248 deletions.
6 changes: 3 additions & 3 deletions packages/concerto-core/src/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,15 +599,15 @@ class ClassDeclaration extends Declaration {
}

/**
* Returns the string representation of this class
* @return {String} the string representation of the class
* Returns the string representation of this class declaration
* @return {String} the string representation of the class declaration
*/
toString() {
let superType = '';
if (this.superType) {
superType = ' super=' + this.superType;
}
return 'ClassDeclaration {id=' + this.getFullyQualifiedName() + superType + ' declarationKind=' + this.declarationKind() + ' abstract=' + this.isAbstract() + ' id=' + this.idField + '}';
return 'ClassDeclaration {id=' + this.getFullyQualifiedName() + superType + ' declarationKind=' + this.declarationKind() + ' abstract=' + this.isAbstract() + ' idField=' + this.idField + '}';
}
}

Expand Down
26 changes: 22 additions & 4 deletions packages/concerto-core/src/introspect/declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const IllegalModelException = require('./illegalmodelexception');
const Decorated = require('./decorated');

// Types needed for TypeScript generation.
Expand Down Expand Up @@ -48,13 +49,30 @@ class Declaration extends Decorated {
}

/**
* Process the AST and build the model
* Semantic validation of the declaration. Subclasses should
* override this method to impose additional semantic constraints on the
* contents/relations of declarations.
*
* @throws {IllegalModelException}
* @private
* @protected
*/
process() {
super.process();
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 declaration name ${duplicateElements[0]}`
);
}
}

/**
Expand Down
42 changes: 15 additions & 27 deletions packages/concerto-core/src/introspect/decorated.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ if (global === undefined) {
* Decorated defines a model element that may have decorators attached.
*
* @private
* @abstract
* @class
* @memberof module:concerto-core
*/
Expand All @@ -44,57 +43,46 @@ class Decorated extends ModelElement {
*/
constructor(modelFile, ast) {
super(modelFile, ast);
this.decorators = [];
}

/**
* Extracts the decorators applied to an AST.
* @param {*} ast - the AST created by the parser
* @param {ModelManager} modelManager - the ModelManager that supplies decorator factories
* @returns {Decorator[]} the decorators
* Process the AST and build the model
*
* @throws {IllegalModelException}
* @private
*/
static processDecorators(ast, modelManager) {
const decorators = [];
if(ast.decorators) {
for(let n=0; n < ast.decorators.length; n++ ) {
let thing = ast.decorators[n];
process() {
if(this.ast.decorators) {
this.decorators = [];
const modelManager = this.modelFile.getModelManager();
for(let n=0; n < this.ast.decorators.length; n++ ) {
let thing = this.ast.decorators[n];
let factories = modelManager.getDecoratorFactories();
let decorator;
for (let factory of factories) {
decorator = factory.newDecorator(this, thing);
decorator = factory.newDecorator(this.modelFile, this, thing);
if (decorator) {
break;
}
}
if (!decorator) {
decorator = new Decorator(this, thing);
decorator = new Decorator(this.modelFile, this, thing);
}
decorators.push(decorator);
this.decorators.push(decorator);
}
}

return decorators;
}

/**
* Process the AST and build the model
*
* @throws {IllegalModelException}
* @private
*/
process() {
this.decorators = Decorated.processDecorators(this.ast, this.getModelFile().getModelManager());
}

/**
* Semantic validation of the structure of this decorated. Subclasses should
* override this method to impose additional semantic constraints on the
* contents/relations of fields.
*
* @param {...*} args the validation arguments
* @throws {IllegalModelException}
* @protected
*/
validate(...args) {
validate() {
if (this.decorators && this.decorators.length > 0) {
for(let n=0; n < this.decorators.length; n++) {
this.decorators[n].validate();
Expand Down
20 changes: 7 additions & 13 deletions packages/concerto-core/src/introspect/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const ModelElement = require('./modelelement');
const { MetaModelNamespace } = require('@accordproject/concerto-metamodel');

// Types needed for TypeScript generation.
Expand All @@ -25,19 +26,20 @@ if (global === undefined) {
/* eslint-enable no-unused-vars */

/**
* Decorator encapsulates a decorator (annotation) on a class or property.
* Decorator encapsulates a decorator (annotation) on a declaration or property.
* @class
* @memberof module:concerto-core
*/
class Decorator {
class Decorator extends ModelElement {
/**
* Create a Decorator.
* @param {ModelElement} parent - the owner of this property
* @param {ModelFile} modelFile - the model file for this decorator
* @param {ModelElement} [parent] - the owner of this property
* @param {Object} ast - The AST created by the parser
* @throws {IllegalModelException}
*/
constructor(parent, ast) {
this.ast = ast;
constructor(modelFile, parent, ast) {
super(modelFile, ast);
this.parent = parent;
this.arguments = null;
this.process();
Expand Down Expand Up @@ -97,14 +99,6 @@ class Decorator {
*/
validate() { }

/**
* Returns the name of a decorator
* @return {string} the name of this decorator
*/
getName() {
return this.name;
}

/**
* Returns the arguments for this decorator
* @return {object[]} the arguments for this decorator
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/src/introspect/enumvalue.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ if (global === undefined) {
*/
class EnumValue extends Property {
/**
* Create a EnumValueDeclaration.
* Create a EnumValue.
* @param {ClassDeclaration} parent - The owner of this property
* @param {Object} ast - The AST created by the parser
* @throws {IllegalModelException}
Expand Down
47 changes: 0 additions & 47 deletions packages/concerto-core/src/introspect/enumvaluedeclaration.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/concerto-core/src/introspect/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Field extends Property {

/**
* Unboxes a field that references a scalar type to an
* underlying Field definition.
* underlying primitive Field definition.
* @throws {Error} throws an error if this field is not a scalar type.
* @returns {Field} the primitive field for this scalar
*/
Expand Down
53 changes: 2 additions & 51 deletions packages/concerto-core/src/introspect/mapdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const Declaration = require('./declaration');
const IllegalModelException = require('./illegalmodelexception');
const MapValueType = require('./mapvaluetype');
const MapKeyType = require('./mapkeytype');
const ModelUtil = require('../modelutil');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
Expand All @@ -31,8 +30,8 @@ if (global === undefined) {
* MapDeclaration defines a Map data structure, which allows storage of a collection
* of values, where each value is associated and indexed with a unique key.
*
* @extends Decorated
* @see See {@link Decorated}
* @extends Declaration
* @see See {@link Declaration}
* @class
* @memberof module:concerto-core
*/
Expand Down Expand Up @@ -68,18 +67,8 @@ class MapDeclaration extends Declaration {
throw new IllegalModelException(`MapDeclaration must contain Key & Value properties ${this.ast.name}`, this.modelFile, this.ast.location);
}

if (!ModelUtil.isValidMapKey(this.ast.key)) {
throw new IllegalModelException(`MapDeclaration must contain valid MapKeyType ${this.ast.name}`, this.modelFile, this.ast.location);
}

if (!ModelUtil.isValidMapValue(this.ast.value)) {
throw new IllegalModelException(`MapDeclaration must contain valid MapValueType, for MapDeclaration ${this.ast.name}` , this.modelFile, this.ast.location);
}

this.name = this.ast.name;
this.key = new MapKeyType(this, this.ast.key);
this.value = new MapValueType(this, this.ast.value);
this.fqn = ModelUtil.getFullyQualifiedName(this.modelFile.getNamespace(), this.ast.name);
}

/**
Expand All @@ -94,36 +83,6 @@ class MapDeclaration extends Declaration {
this.value.validate();
}

/**
* Returns the fully qualified name of this class.
* The name will include the namespace if present.
*
* @return {string} the fully-qualified name of this class
*/
getFullyQualifiedName() {
return this.fqn;
}

/**
* Returns the ModelFile that defines this class.
*
* @public
* @return {ModelFile} the owning ModelFile
*/
getModelFile() {
return this.modelFile;
}

/**
* Returns the short name of a class. This name does not include the
* namespace from the owning ModelFile.
*
* @return {string} the short name of this class
*/
getName() {
return this.name;
}

/**
* Returns the type of the Map key property.
*
Expand All @@ -141,14 +100,6 @@ class MapDeclaration extends Declaration {
getValue() {
return this.value;
}

/**
* Returns the string representation of this class
* @return {String} the string representation of the class
*/
toString() {
return 'MapDeclaration {id=' + this.getFullyQualifiedName() + '}';
}
}

module.exports = MapDeclaration;
Loading

0 comments on commit a1548b9

Please sign in to comment.