From fb0fd3e3d4a425241a259367c6c614bce415c2a3 Mon Sep 17 00:00:00 2001 From: muhammed-abdulkadir Date: Fri, 1 Nov 2024 12:04:24 +0000 Subject: [PATCH] feat(visitor): add test and refactor build Signed-off-by: muhammed-abdulkadir --- lib/common/basevisitor.js | 4 +- lib/common/diagramvisitor.js | 3 +- .../fromcto/plantuml/plantumlvisitor.js | 13 ++ types/lib/common/basevisitor.d.ts | 131 ++++++++++++++++++ types/lib/common/diagramvisitor.d.ts | 124 +---------------- types/lib/common/graph.d.ts | 4 +- 6 files changed, 150 insertions(+), 129 deletions(-) create mode 100644 types/lib/common/basevisitor.d.ts diff --git a/lib/common/basevisitor.js b/lib/common/basevisitor.js index 3e05e809..f733cd1a 100644 --- a/lib/common/basevisitor.js +++ b/lib/common/basevisitor.js @@ -23,9 +23,7 @@ if (global === undefined) { /* eslint-enable no-unused-vars */ /** - * Convert the contents of a ModelManager a diagram format (such as PlantUML or Mermaid) - * Set a fileWriter property (instance of FileWriter) on the parameters - * object to control where the generated code is written to disk. + * Visitor class that traverses various model elements * * @protected * @class diff --git a/lib/common/diagramvisitor.js b/lib/common/diagramvisitor.js index ea79875c..be1303dc 100644 --- a/lib/common/diagramvisitor.js +++ b/lib/common/diagramvisitor.js @@ -21,7 +21,7 @@ const BaseVisitor = require('./basevisitor'); /* eslint-disable no-unused-vars */ /* istanbul ignore next */ if (global === undefined) { - const { ModelManager, ModelFile, ClassDeclaration, ScalarDeclaration, Field, EnumValueDeclaration, RelationshipDeclaration, Decorator} = require('@accordproject/concerto-core'); + const { ClassDeclaration, Field, EnumValueDeclaration} = require('@accordproject/concerto-core'); } /* eslint-enable no-unused-vars */ @@ -35,7 +35,6 @@ if (global === undefined) { */ class DiagramVisitor extends BaseVisitor { - /** * Visitor design pattern * @param {Field} field - the object being visited diff --git a/test/codegen/fromcto/plantuml/plantumlvisitor.js b/test/codegen/fromcto/plantuml/plantumlvisitor.js index eb210409..138a7830 100644 --- a/test/codegen/fromcto/plantuml/plantumlvisitor.js +++ b/test/codegen/fromcto/plantuml/plantumlvisitor.js @@ -590,6 +590,19 @@ describe('PlantUMLVisitor', function () { param.fileWriter.writeLine.withArgs(1, '+ Bob').calledOnce.should.be.ok; }); + + it('should not write a line for enum value if writer is undefined', () => { + let param = {}; + + + let mockEnumValueDecl = sinon.createStubInstance(EnumValueDeclaration); + mockEnumValueDecl.isEnumValue.returns(true); + mockEnumValueDecl.getName.returns('Bob'); + mockEnumValueDecl.getDecorators.returns([]); + + plantUMLvisitor.visitField(mockEnumValueDecl, param); + mockEnumValueDecl.getName().never; + }); }); describe('visitDecorator', () => { diff --git a/types/lib/common/basevisitor.d.ts b/types/lib/common/basevisitor.d.ts new file mode 100644 index 00000000..ed4c31a2 --- /dev/null +++ b/types/lib/common/basevisitor.d.ts @@ -0,0 +1,131 @@ +export = BaseVisitor; +/** + * Visitor class that traverses various model elements + * + * @protected + * @class + */ +declare class BaseVisitor { + /** + * Visitor design pattern + * @param {Object} thing - the object being visited + * @param {Object} parameters - the parameter + * @return {Object} the result of visiting or null + * @protected + */ + protected visit(thing: any, parameters: any): any; + /** + * Visitor design pattern + * @param {ModelManager} modelManager - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitModelManager(modelManager: ModelManager, parameters: any): void; + /** + * Visitor design pattern + * @param {ModelFile} modelFile - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitModelFile(modelFile: ModelFile, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitAssetDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitEnumDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitEventDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitParticipantDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitTransactionDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {ClassDeclaration} classDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @param {string} type - the type of the declaration + * @protected + */ + protected visitClassDeclaration(classDeclaration: ClassDeclaration, parameters: any, type?: string): void; + /** + * Visitor design pattern + * @param {ScalarDeclaration} scalarDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitScalarDeclaration(scalarDeclaration: ScalarDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {MapDeclaration} mapDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitMapDeclaration(mapDeclaration: MapDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {Field} field - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitScalarField(field: Field, parameters: any): void; + /** + * Visitor design pattern + * @param {RelationshipDeclaration} relationship - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitRelationship(relationship: RelationshipDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {Field} field - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitField(field: Field, parameters: any): void; + /** + * Visitor design pattern + * @param {EnumValueDeclaration} enumValueDeclaration - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitEnumValueDeclaration(enumValueDeclaration: EnumValueDeclaration, parameters: any): void; + /** + * Visitor design pattern + * @param {Decorator} decorator - the object being visited + * @param {Object} parameters - the parameter + * @protected + */ + protected visitDecorator(decorator: Decorator, parameters: any): void; +} +import { ModelManager } from "@accordproject/concerto-core"; +import { ModelFile } from "@accordproject/concerto-core"; +import { ClassDeclaration } from "@accordproject/concerto-core"; +import { ScalarDeclaration } from "@accordproject/concerto-core"; +import { Field } from "@accordproject/concerto-core"; +import { RelationshipDeclaration } from "@accordproject/concerto-core"; +import { EnumValueDeclaration } from "@accordproject/concerto-core"; +import { Decorator } from "@accordproject/concerto-core"; diff --git a/types/lib/common/diagramvisitor.d.ts b/types/lib/common/diagramvisitor.d.ts index 7af9558a..cc876946 100644 --- a/types/lib/common/diagramvisitor.d.ts +++ b/types/lib/common/diagramvisitor.d.ts @@ -7,121 +7,7 @@ export = DiagramVisitor; * @protected * @class */ -declare class DiagramVisitor { - /** - * Visitor design pattern - * @param {Object} thing - the object being visited - * @param {Object} parameters - the parameter - * @return {Object} the result of visiting or null - * @protected - */ - protected visit(thing: any, parameters: any): any; - /** - * Visitor design pattern - * @param {ModelManager} modelManager - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitModelManager(modelManager: ModelManager, parameters: any): void; - /** - * Visitor design pattern - * @param {ModelFile} modelFile - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitModelFile(modelFile: ModelFile, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitAssetDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitEnumDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitEventDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitParticipantDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitTransactionDeclaration(classDeclaration: ClassDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {ClassDeclaration} classDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @param {string} type - the type of the declaration - * @protected - */ - protected visitClassDeclaration(classDeclaration: ClassDeclaration, parameters: any, type?: string): void; - /** - * Visitor design pattern - * @param {ScalarDeclaration} scalarDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitScalarDeclaration(scalarDeclaration: ScalarDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {MapDeclaration} mapDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitMapDeclaration(mapDeclaration: MapDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {Field} field - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitScalarField(field: Field, parameters: any): void; - /** - * Visitor design pattern - * @param {RelationshipDeclaration} relationship - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitRelationship(relationship: RelationshipDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {Field} field - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitField(field: Field, parameters: any): void; - /** - * Visitor design pattern - * @param {EnumValueDeclaration} enumValueDeclaration - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitEnumValueDeclaration(enumValueDeclaration: EnumValueDeclaration, parameters: any): void; - /** - * Visitor design pattern - * @param {Decorator} decorator - the object being visited - * @param {Object} parameters - the parameter - * @protected - */ - protected visitDecorator(decorator: Decorator, parameters: any): void; +declare class DiagramVisitor extends BaseVisitor { /** * Visitor design pattern * @param {ClassDeclaration} classDeclaration - the object being visited @@ -135,11 +21,5 @@ declare namespace DiagramVisitor { let AGGREGATION: string; let INHERITANCE: string; } -import { ModelManager } from "@accordproject/concerto-core"; -import { ModelFile } from "@accordproject/concerto-core"; +import BaseVisitor = require("./basevisitor"); import { ClassDeclaration } from "@accordproject/concerto-core"; -import { ScalarDeclaration } from "@accordproject/concerto-core"; -import { Field } from "@accordproject/concerto-core"; -import { RelationshipDeclaration } from "@accordproject/concerto-core"; -import { EnumValueDeclaration } from "@accordproject/concerto-core"; -import { Decorator } from "@accordproject/concerto-core"; diff --git a/types/lib/common/graph.d.ts b/types/lib/common/graph.d.ts index 6b7eddab..22f4ccb2 100644 --- a/types/lib/common/graph.d.ts +++ b/types/lib/common/graph.d.ts @@ -6,7 +6,7 @@ * @class * @memberof module:concerto-util */ -export class ConcertoGraphVisitor extends DiagramVisitor { +export class ConcertoGraphVisitor extends BaseVisitor { /** * Visitor design pattern * @param {ClassDeclaration} classDeclaration - the object being visited @@ -85,7 +85,7 @@ export class DirectedGraph { */ print(writer: Writer): void; } -import DiagramVisitor = require("./diagramvisitor"); +import BaseVisitor = require("./basevisitor"); import { ClassDeclaration } from "@accordproject/concerto-core"; import { MapDeclaration } from "@accordproject/concerto-core"; import { Writer } from "@accordproject/concerto-util";