Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix generator and renderer tests #803

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions test/TestUtils/TestGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { AbstractGenerator, CommonInputModel, CommonModel, IndentationTypes, RenderOutput } from '../../src';
import { AbstractGenerator, InputMetaModel, IndentationTypes, RenderOutput, ConstrainedMetaModel, MetaModel, ConstrainedAnyModel, Preset } from '../../src';

export const testOptions = {
indentation: {
type: IndentationTypes.SPACES,
size: 2,
}
};
export class TestGenerator extends AbstractGenerator {
constructor() {
super('TestGenerator', testOptions);
export class TestGenerator extends AbstractGenerator<any, any> {
constructor(options: any = testOptions) {
super('TestGenerator', options);
}

render(model: CommonModel, _inputModel: CommonInputModel): Promise<RenderOutput> {
return Promise.resolve(RenderOutput.toRenderOutput({result: model.$id || 'rendered content', renderedName: 'TestName'}));
public constrainToMetaModel(model: MetaModel): ConstrainedMetaModel {
return new ConstrainedAnyModel(model.name, undefined, '');
}

renderCompleteModel(_model: CommonModel, _inputModel: CommonInputModel, _options: any): Promise<RenderOutput> {
return Promise.resolve(RenderOutput.toRenderOutput({result: 'rendered complete content', renderedName: 'TestName'}));
public splitMetaModel(model: MetaModel): MetaModel[] {
return [model];
}
public render(model: MetaModel, inputModel: InputMetaModel): Promise<RenderOutput> {
return Promise.resolve(RenderOutput.toRenderOutput({result: model.name || 'rendered content', renderedName: 'TestName'}));
}
public renderCompleteModel(model: MetaModel, inputModel: InputMetaModel, options: any): Promise<RenderOutput> {
return Promise.resolve(RenderOutput.toRenderOutput({result: model.name || 'rendered complete content', renderedName: 'TestName'}));
}
public testGetPresets(string: string): Array<[Preset, unknown]> {
return this.getPresets(string);
}
}
16 changes: 9 additions & 7 deletions test/TestUtils/TestRenderers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { AbstractRenderer, CommonInputModel, CommonModel, RenderOutput } from '../../src';
import { AbstractRenderer, InputMetaModel, RenderOutput, ConstrainedAnyModel } from '../../src';
import { GoRenderer } from '../../src/generators/go/GoRenderer';
import { CSharpRenderer } from '../../src/generators/csharp/CSharpRenderer';
import { JavaRenderer } from '../../src/generators/java/JavaRenderer';
import { JavaScriptRenderer } from '../../src/generators/javascript/JavaScriptRenderer';
import { TypeScriptRenderer } from '../../src/generators/typescript/TypeScriptRenderer';
import {testOptions, TestGenerator} from './TestGenerator';
import { DartRenderer } from '../../src/generators/dart/DartRenderer';

export class TestRenderer extends AbstractRenderer {
constructor(presets = []) {
super(testOptions, new TestGenerator(), presets, new CommonModel(), new CommonInputModel());
super(testOptions, new TestGenerator(), presets, new ConstrainedAnyModel('', undefined, ''), new InputMetaModel());
}
render(): Promise<RenderOutput> {
return Promise.resolve(RenderOutput.toRenderOutput({result: '', renderedName: ''}));
}
}

export class MockJavaRenderer extends JavaRenderer {}
export class MockTypeScriptRenderer extends TypeScriptRenderer {}
export class MockGoRenderer extends GoRenderer {}
export class MockCSharpRenderer extends CSharpRenderer {}
export class MockJavaScriptRenderer extends JavaScriptRenderer {}
export class MockJavaRenderer extends JavaRenderer<any> {}
export class MockTypeScriptRenderer extends TypeScriptRenderer<any> {}
export class MockGoRenderer extends GoRenderer<any> {}
export class MockCSharpRenderer extends CSharpRenderer<any> {}
export class MockJavaScriptRenderer extends JavaScriptRenderer<any> {}
export class MockDartRenderer extends DartRenderer<any> {}
63 changes: 25 additions & 38 deletions test/generators/AbstractGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AbstractGenerator } from '../../src/generators';
import { CommonInputModel, CommonModel, RenderOutput } from '../../src/models';
import { InputMetaModel, CommonModel, AnyModel } from '../../src/models';
import {TestGenerator} from '../TestUtils/TestGenerator';
describe('AbstractGenerator', () => {
let generator: TestGenerator;
Expand All @@ -8,58 +7,61 @@ describe('AbstractGenerator', () => {
});

test('generate() should return OutputModels', async () => {
const doc: any = { $id: 'test' };
const outputModels = await generator.generate(doc);
const cim = new InputMetaModel();
const model = new AnyModel('test', undefined);
cim.models[model.name] = model;
const outputModels = await generator.generate(cim);

expect(outputModels[0].result).toEqual('test');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('generateCompleteModels() should return OutputModels', async () => {
const doc: any = { $id: 'test' };
const outputModels = await generator.generateCompleteModels(doc, {});
const cim = new InputMetaModel();
const model = new AnyModel('test', undefined);
cim.models[model.name] = model;
const outputModels = await generator.generateCompleteModels(cim, {});

expect(outputModels[0].result).toEqual('rendered complete content');
expect(outputModels[0].result).toEqual('test');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('generate() should process CommonInputModel instance', async () => {
const cim = new CommonInputModel();
const model = new CommonModel();
model.$id = 'test';
cim.models[model.$id] = model;
test('generate() should process InputMetaModel instance', async () => {
const cim = new InputMetaModel();
const model = new AnyModel('test', undefined);
cim.models[model.name] = model;
const outputModels = await generator.generate(cim);
expect(outputModels[0].result).toEqual('test');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('generateCompleteModels() should process CommonInputModel instance', async () => {
const cim = new CommonInputModel();
const model = new CommonModel();
model.$id = 'test';
cim.models[model.$id] = model;
test('generateCompleteModels() should process InputMetaModel instance', async () => {
const cim = new InputMetaModel();
const model = new AnyModel('test', undefined);
cim.models[model.name] = model;
const outputModels = await generator.generateCompleteModels(cim, {});

expect(outputModels[0].result).toEqual('rendered complete content');
expect(outputModels[0].result).toEqual('test');
expect(outputModels[0].modelName).toEqual('TestName');
});

test('should `process` function return CommonInputModel', async () => {
const doc: any = { $id: 'test' };
test('should `process` function return InputMetaModel', async () => {
const doc: any = { type: 'string', $id: 'test' };
const commonInputModel = await generator.process(doc);
const keys = Object.keys(commonInputModel.models);

expect(commonInputModel).toBeInstanceOf(CommonInputModel);
expect(commonInputModel).toBeInstanceOf(InputMetaModel);
expect(commonInputModel.models).toBeDefined();
expect(keys).toHaveLength(1);
expect(commonInputModel.models[keys[0]].originalInput).toEqual({
$id: 'test',
type: 'string',
'x-modelgen-inferred-name': 'root',
});
});

test('should `render` function return renderer model', async () => {
const doc: any = { $id: 'SomeModel' };
const doc: any = { type: 'string', $id: 'SomeModel' };
const commonInputModel = await generator.process(doc);
const keys = Object.keys(commonInputModel.models);
const renderedContent = await generator.render(commonInputModel.models[keys[0]], commonInputModel);
Expand All @@ -71,22 +73,7 @@ describe('AbstractGenerator', () => {

describe('getPresets()', () => {
test('getPresets()', () => {
class GeneratorWithPresets extends AbstractGenerator<any, any> {
constructor() {
super('TestGenerator', {presets: [{preset: {test: 'test2'}, options: {}}]});
}

render(model: CommonModel, inputModel: CommonInputModel): any { return; }

testGetPresets(string: string) {
return this.getPresets(string);
}

renderCompleteModel(model: CommonModel, inputModel: CommonInputModel, options: any): Promise<RenderOutput> {
throw new Error('Method not implemented.');
}
}
const newGenerator = new GeneratorWithPresets();
const newGenerator = new TestGenerator({presets: [{preset: {test: 'test2'}, options: {}}]});
expect(newGenerator.testGetPresets('test')).toEqual([['test2', {}]]);
});
});
Expand Down
10 changes: 2 additions & 8 deletions test/generators/AbstractRenderer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ describe('AbstractRenderer', () => {
const content = renderer.renderBlock(['Test1', 'Test2']);
expect(content).toEqual('Test1\nTest2');
});

test('can use generator inside renderer', async () => {
const generator = renderer.generator;
const doc: any = { $id: 'test' };
const outputModels = await generator.generate(doc);

expect(outputModels[0].result).toEqual('test');
});

describe('addDependency()', () => {
test('should add dependency', () => {
Expand Down Expand Up @@ -94,6 +86,7 @@ describe('AbstractRenderer', () => {
expect(presetCallback).not.toHaveBeenCalled();
});
});

describe('runPreset()', () => {
test('should use string', async () => {
const preset1Callback = jest.fn();
Expand Down Expand Up @@ -133,6 +126,7 @@ describe('AbstractRenderer', () => {
expect(preset1Callback).toHaveBeenCalled();
expect(preset2Callback).toHaveBeenCalled();
});

test('should not use previous preset if undefined returned', async () => {
const preset1Callback = jest.fn();
const preset2Callback = jest.fn();
Expand Down