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 file generator tests #782

Merged
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
3 changes: 2 additions & 1 deletion src/generators/csharp/CSharpFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class CSharpFileGenerator extends CSharpGenerator implements AbstractFile
*/
public async generateToFiles(input: Record<string, unknown> | InputMetaModel, outputDirectory: string, options: CSharpRenderCompleteModelOptions): Promise<OutputModel[]> {
let generatedModels = await this.generateCompleteModels(input, options);
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== undefined; });
//Filter anything out that have not been successfully generated
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== ''; });
for (const outputModel of generatedModels) {
const filePath = path.resolve(outputDirectory, `${outputModel.modelName}.cs`);
await FileHelpers.writerToFileSystem(outputModel.result, filePath);
Expand Down
3 changes: 2 additions & 1 deletion src/generators/dart/DartFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class DartFileGenerator extends DartGenerator implements AbstractFileGene
*/
public async generateToFiles(input: Record<string, unknown> | InputMetaModel, outputDirectory: string, options: DartRenderCompleteModelOptions): Promise<OutputModel[]> {
let generatedModels = await this.generateCompleteModels(input, options);
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== undefined; });
//Filter anything out that have not been successfully generated
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== ''; });
for (const outputModel of generatedModels) {
const filePath = path.resolve(outputDirectory, `${outputModel.modelName}.dart`);
await FileHelpers.writerToFileSystem(outputModel.result, filePath);
Expand Down
3 changes: 2 additions & 1 deletion src/generators/go/GoFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class GoFileGenerator extends GoGenerator implements AbstractFileGenerato
*/
public async generateToFiles(input: Record<string, unknown> | InputMetaModel, outputDirectory: string, options: GoRenderCompleteModelOptions): Promise<OutputModel[]> {
let generatedModels = await this.generateCompleteModels(input, options);
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== undefined; });
//Filter anything out that have not been successfully generated
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== ''; });
for (const outputModel of generatedModels) {
const filePath = path.resolve(outputDirectory, `${outputModel.modelName}.go`);
await FileHelpers.writerToFileSystem(outputModel.result, filePath);
Expand Down
3 changes: 2 additions & 1 deletion src/generators/java/JavaFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class JavaFileGenerator extends JavaGenerator implements AbstractFileGene
*/
public async generateToFiles(input: Record<string, unknown> | InputMetaModel, outputDirectory: string, options: JavaRenderCompleteModelOptions): Promise<OutputModel[]> {
let generatedModels = await this.generateCompleteModels(input, options);
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== undefined; });
//Filter anything out that have not been successfully generated
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== ''; });
for (const outputModel of generatedModels) {
const filePath = path.resolve(outputDirectory, `${outputModel.modelName}.java`);
await FileHelpers.writerToFileSystem(outputModel.result, filePath);
Expand Down
92 changes: 92 additions & 0 deletions test/generators/FileGenerators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { FileHelpers, DartFileGenerator, OutputModel, ConstrainedAnyModel, InputMetaModel, GoFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, CSharpFileGenerator } from '../../src';
import * as path from 'path';

const generatorsToTest = [
{
generator: new GoFileGenerator(),
generatorOptions: {packageName: 'some_package'},
fileExtension: 'go'
},
{
generator: new DartFileGenerator(),
generatorOptions: {packageName: 'SomePackage'},
fileExtension: 'dart'
},
{
generator: new JavaFileGenerator(),
generatorOptions: {packageName: 'SomePackage'},
fileExtension: 'java'
},
{
generator: new JavaScriptFileGenerator(),
generatorOptions: {},
fileExtension: 'js'
},
{
generator: new TypeScriptFileGenerator(),
generatorOptions: {},
fileExtension: 'ts'
},
{
generator: new CSharpFileGenerator(),
generatorOptions: {namespace: 'SomeNamespace'},
fileExtension: 'cs'
}
];

describe.each(generatorsToTest)('generateToFiles', ({generator, generatorOptions, fileExtension}) => {
afterEach(() => {
jest.restoreAllMocks();
});

const doc = {
$id: 'CustomClass',
type: 'object',
additionalProperties: true,
properties: {
someProp: { type: 'string' },
someEnum: {
$id: 'CustomEnum',
type: 'string',
enum: ['Texas', 'Alabama', 'California'],
}
}
};

test('should throw accurate error if file cannot be written', async () => {
const expectedError = new Error('write error');
jest.spyOn(FileHelpers, 'writerToFileSystem').mockRejectedValue(expectedError);
jest.spyOn(generator, 'generateCompleteModels').mockResolvedValue([new OutputModel('content', new ConstrainedAnyModel('', undefined, ''), 'test', new InputMetaModel(), [])]);

await expect(generator.generateToFiles(doc, '/test/', generatorOptions as any)).rejects.toEqual(expectedError);
expect(generator.generateCompleteModels).toHaveBeenCalledTimes(1);
expect(FileHelpers.writerToFileSystem).toHaveBeenCalledTimes(1);
});
test('should try and generate models to files', async () => {
const outputDir = './test';
const expectedOutputDirPath = path.resolve(outputDir);
const expectedOutputFilePath = path.resolve(`${outputDir}/test.${fileExtension}`);
const expectedWriteToFileParameters = [
'content',
expectedOutputFilePath,
];
jest.spyOn(FileHelpers, 'writerToFileSystem').mockResolvedValue(undefined);
jest.spyOn(generator, 'generateCompleteModels').mockResolvedValue([new OutputModel('content', new ConstrainedAnyModel('', undefined, ''), 'test', new InputMetaModel(), [])]);

await generator.generateToFiles(doc, expectedOutputDirPath, generatorOptions as any);
expect(generator.generateCompleteModels).toHaveBeenCalledTimes(1);
expect(FileHelpers.writerToFileSystem).toHaveBeenCalledTimes(1);
expect((FileHelpers.writerToFileSystem as jest.Mock).mock.calls[0]).toEqual(expectedWriteToFileParameters);
});
test('should ignore models that have not been rendered', async () => {
const outputDir = './test';
const expectedOutputDirPath = path.resolve(outputDir);
jest.spyOn(FileHelpers, 'writerToFileSystem').mockResolvedValue(undefined);
jest.spyOn(generator, 'generateCompleteModels').mockResolvedValue([new OutputModel('', new ConstrainedAnyModel('', undefined, ''), '', new InputMetaModel(), [])]);

const models = await generator.generateToFiles(doc, expectedOutputDirPath, generatorOptions as any);
expect(generator.generateCompleteModels).toHaveBeenCalledTimes(1);
expect(models).toHaveLength(0);
expect(FileHelpers.writerToFileSystem).toHaveBeenCalledTimes(0);
});
});
51 changes: 0 additions & 51 deletions test/generators/csharp/CSharpFileGenerator.spec.ts

This file was deleted.

51 changes: 0 additions & 51 deletions test/generators/dart/DartFileGenerator.spec.ts

This file was deleted.

51 changes: 0 additions & 51 deletions test/generators/go/GoFileGenerator.spec.ts

This file was deleted.

51 changes: 0 additions & 51 deletions test/generators/java/JavaFileGenerator.spec.ts

This file was deleted.

Loading