feat: add the uml generator

This commit is contained in:
Michel Jonathan Schmitz
2019-05-27 13:10:41 +02:00
parent a9c0fddb23
commit 0f21da4a92
24 changed files with 2310 additions and 96 deletions

View File

@@ -29,4 +29,5 @@ export class CommonSpec {
async getTsconfigPath() {
expect(getTsconfigPath(__dirname)).to.be.equal(cwd());
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {resolve} from 'path';
import {existsSync, unlinkSync} from 'fs';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {getProjectReflection} from '../src/common';
import {createDiagram, createDiagramFromString} from '../src/uml/createDiagram';
import {UMLConfig} from './../lib/uml/umlConfig.d';
import {readDefinitions} from '../src/uml/readDefinitions';
import {LightweightDefinition} from '../src/uml/model/LightweightDefinition';
import nock = require('nock');
@suite(timeout(15000), slow(5000))
export class CreateDiagramSpec {
plantUmlConfig: UMLConfig;
definitions: LightweightDefinition[];
constructor() {
this.plantUmlConfig = {
definitions: [],
showAssociations: true,
showEnumValues: true,
showInheritance: true,
showInheritedProperties: true,
showOptionalProperties: true,
showProperties: true,
};
const projectReflection = getProjectReflection('./test/model', true);
this.definitions = readDefinitions(projectReflection);
}
@test
async shouldRefuseRequest() {
const testPlantUmlCode: string = 'class Test{\n}';
try {
await createDiagramFromString(testPlantUmlCode, "http://plantuml:8080");
} catch (e) {
expect(e.message).to.equal(new Error('getaddrinfo ENOTFOUND plantuml plantuml:8080').message);
}
}
/**
* This test will only test the functionality of the method
* - Converting the definitions to plantuml code
* - Sending the code to a server
* - Writing the response to a file
* This test will not check the file content
*/
@test
async shouldCreateDiagrams() {
nock('http://plantuml:8080')
.persist()
.get(() => true)
.reply(200, 'This will be the file content')
let fileName = await createDiagram(this.definitions, this.plantUmlConfig, "http://plantuml:8080");
let filePath = resolve(__dirname, '..', fileName);
expect(await existsSync(filePath)).to.equal(true);
await unlinkSync(fileName);
this.plantUmlConfig.showAssociations = false;
this.plantUmlConfig.showInheritance = false;
fileName = await createDiagram(this.definitions, this.plantUmlConfig, "http://plantuml:8080");
filePath = resolve(__dirname, '..', fileName);
expect(await existsSync(filePath)).to.equal(true);
await unlinkSync(fileName);
nock.cleanAll();
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {getProjectReflection} from '../src/common';
import {readDefinitions} from '../src/uml/readDefinitions';
import {generatedModel} from './model/generatedModel';
@suite(timeout(10000), slow(5000))
export class ReadDefinitionsSpec {
@test
async testReadDefinitions() {
const projectReflection = getProjectReflection('./test/model', true);
const definitions = readDefinitions(projectReflection);
expect(definitions).to.be.deep.equal(generatedModel);
}
}

34
test/model/TestClass.ts Normal file
View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {TestFirstUnion} from './TestUnion';
export class TestClass<T> {
test2: T;
test4: TestFirstUnion;
constructor(type: T) {
this.test2 = type;
this.test4 = 'test1';
}
/**
* Should not be processed at all
*/
testClassFunction(): boolean {
return true;
}
}
export class TestSecondClass extends TestClass<string> {}

25
test/model/TestEnum.ts Normal file
View File

@@ -0,0 +1,25 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
export enum TestFirstEnum {
TEST1,
TEST2,
TEST3,
}
export enum TestSecondEnum {
TEST1 = 'one',
TEST2 = 'two',
TEST3 = 'three',
}

View File

@@ -0,0 +1,17 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
export function testFunction(): boolean {
return true;
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {TestClass, TestSecondClass} from './TestClass';
import {TestFirstEnum} from './TestEnum';
import {TestThirdUnion} from './TestUnion';
export interface TestInterface {
articleBody: string[];
categorySpecificValues?: { [s: string]: string };
inputType: 'multipleChoice';
maintainer: TestThirdUnion | TestFirstEnum;
remainingAttendeeCapacity?: number;
test1: Array<TestThirdUnion | typeof TestFirstEnum>;
test2: TestClass<string>;
test3: 'test1' | 'test2';
test4: TestSecondClass;
universityRole: keyof TestFirstEnum;
}
export interface TestSecondInterface {
[k: string]: string;
}

24
test/model/TestUnion.ts Normal file
View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2018-2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
export type TestFirstUnion = 'test1' | 'test2';
export type TestSecondUnion = 'test3';
export type TestThirdUnion = TestFirstUnion | TestSecondUnion;
export type TestFourthUnion<T extends TestFirstUnion> = T extends TestFirstUnion
? TestFirstUnion
: never;

View File

@@ -0,0 +1,552 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {LightweightClassDefinition} from '../../src/uml/model/LightweightClassDefinition';
import {LightweightDefinition} from '../../src/uml/model/LightweightDefinition';
import {LightweightEnumDefinition} from '../../src/uml/model/LightweightEnumDefinition';
export const generatedModel: Array<
LightweightDefinition | LightweightClassDefinition | LightweightEnumDefinition
> = [
{
name: 'TestClass',
type: 'class',
properties: [
{
name: 'test2',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: true,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'T',
},
},
{
name: 'test4',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestFirstUnion',
},
},
],
extendedDefinitions: [],
implementedDefinitions: [],
typeParameters: ['T'],
},
{
name: 'TestSecondClass',
type: 'class',
properties: [
{
name: 'test2',
optional: false,
inherited: true,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'string',
},
},
{
name: 'test4',
optional: false,
inherited: true,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestFirstUnion',
},
},
],
extendedDefinitions: ['TestClass'],
implementedDefinitions: [],
typeParameters: [],
},
{
name: 'TestFirstEnum',
values: ['TEST1', 'TEST2', 'TEST3'],
},
{
name: 'TestSecondEnum',
values: ['TEST1 = "one"', 'TEST2 = "two"', 'TEST3 = "three"'],
},
{
name: 'TestInterface',
type: 'interface',
properties: [
{
name: 'articleBody',
optional: false,
inherited: false,
type: {
hasTypeInformation: false,
isArray: true,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'string',
},
],
genericsTypes: [],
name: 'string',
},
},
{
name: 'categorySpecificValues',
optional: true,
inherited: false,
type: {
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: true,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'undefined',
},
{
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: true,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'object',
},
],
genericsTypes: [],
name: '',
},
},
{
name: 'inputType',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: true,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'multipleChoice',
},
},
{
name: 'maintainer',
optional: false,
inherited: false,
type: {
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: true,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestThirdUnion',
},
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestFirstEnum',
},
],
genericsTypes: [],
name: '',
},
},
{
name: 'remainingAttendeeCapacity',
optional: true,
inherited: false,
type: {
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: true,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'undefined',
},
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'number',
},
],
genericsTypes: [],
name: '',
},
},
{
name: 'test1',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: true,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [
{
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: true,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestThirdUnion',
},
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestFirstEnum',
},
],
genericsTypes: [],
name: '',
},
],
name: 'Array',
},
},
{
name: 'test2',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: true,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'string',
},
],
name: 'TestClass',
},
},
{
name: 'test3',
optional: false,
inherited: false,
type: {
hasTypeInformation: false,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: true,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: true,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'test1',
},
{
hasTypeInformation: true,
isArray: false,
isLiteral: true,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'test2',
},
],
genericsTypes: [],
name: '',
},
},
{
name: 'test4',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: true,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestSecondClass',
},
},
{
name: 'universityRole',
optional: false,
inherited: false,
type: {
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: true,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [
{
hasTypeInformation: true,
isArray: false,
isLiteral: false,
isPrimitive: false,
isReference: false,
isReflection: false,
isTyped: false,
isTypeParameter: false,
isUnion: false,
specificationTypes: [],
genericsTypes: [],
name: 'TestFirstEnum',
},
],
genericsTypes: [],
name: 'keyof TestFirstEnum',
},
},
],
extendedDefinitions: [],
implementedDefinitions: [],
typeParameters: [],
},
{
name: 'TestSecondInterface',
type: 'interface',
properties: [],
extendedDefinitions: [],
implementedDefinitions: [],
typeParameters: [],
},
{
name: 'TestFirstUnion',
values: ['test1', 'test2'],
},
{
name: 'TestFourthUnion',
values: [],
},
{
name: 'TestSecondUnion',
values: ['test3'],
},
{
name: 'TestThirdUnion',
values: ['TestFirstUnion', 'TestSecondUnion'],
},
];