Skip to content

Commit

Permalink
refactor: update with master (asyncapi#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored and Ferror committed Mar 7, 2023
1 parent 81a6e14 commit c1f7fad
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class RootConverter : JsonConverter<Root>
{
if (value == null)
{
JsonSerializer.Serialize(writer, null);
JsonSerializer.Serialize(writer, null, options);
return;
}
var properties = value.GetType().GetProperties();
Expand All @@ -72,7 +72,7 @@ internal class RootConverter : JsonConverter<Root>
if(value.Email != null) {
// write property name and let the serializer serialize the value itself
writer.WritePropertyName(\\"email\\");
JsonSerializer.Serialize(writer, value.Email);
JsonSerializer.Serialize(writer, value.Email, options);
}
Expand Down
6 changes: 5 additions & 1 deletion src/generators/csharp/CSharpRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractRenderer } from '../AbstractRenderer';
import { CSharpGenerator, CSharpOptions } from './CSharpGenerator';
import { Preset, ConstrainedMetaModel, InputMetaModel } from '../../models';
import { Preset, ConstrainedMetaModel, InputMetaModel, ConstrainedObjectPropertyModel } from '../../models';
import { FormatHelpers } from '../../helpers/FormatHelpers';
import { CSharpDependencyManager } from './CSharpDependencyManager';

Expand All @@ -27,4 +27,8 @@ export abstract class CSharpRenderer<
lines = FormatHelpers.breakLines(lines);
return lines.map((line) => `// ${line}`).join('\n');
}

optionalMark(propertyModel: ConstrainedObjectPropertyModel): string {
return propertyModel.required ? '' : '?';
}
}
51 changes: 51 additions & 0 deletions test/generators/dart/DartFileGenerator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { CommonInputModel, CommonModel, FileHelpers, DartFileGenerator, OutputModel } from '../../../src';
import * as path from 'path';

describe('DartFileGenerator', () => {
afterEach(() => {
jest.restoreAllMocks();
});

describe('generateToFile()', () => {
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 generator = new DartFileGenerator();
const expectedError = new Error('write error');
jest.spyOn(FileHelpers, 'writerToFileSystem').mockRejectedValue(expectedError);
jest.spyOn(generator, 'generateCompleteModels').mockResolvedValue([new OutputModel('content', new CommonModel(), '', new CommonInputModel(), [])]);

await expect(generator.generateToFiles(doc, '/test/', {packageName: 'SomePackage'})).rejects.toEqual(expectedError);
expect(generator.generateCompleteModels).toHaveBeenCalledTimes(1);
expect(FileHelpers.writerToFileSystem).toHaveBeenCalledTimes(1);
});
test('should try and generate models to files', async () => {
const generator = new DartFileGenerator();
const outputDir = './test';
const expectedOutputDirPath = path.resolve(outputDir);
const expectedOutputFilePath = path.resolve(`${outputDir}/test.dart`);
const expectedWriteToFileParameters = [
'content',
expectedOutputFilePath,
];
jest.spyOn(FileHelpers, 'writerToFileSystem').mockResolvedValue(undefined);
jest.spyOn(generator, 'generateCompleteModels').mockResolvedValue([new OutputModel('content', new CommonModel(), 'test', new CommonInputModel(), [])]);

await generator.generateToFiles(doc, expectedOutputDirPath, {packageName: 'SomePackage'});
expect(generator.generateCompleteModels).toHaveBeenCalledTimes(1);
expect(FileHelpers.writerToFileSystem).toHaveBeenCalledTimes(1);
expect((FileHelpers.writerToFileSystem as jest.Mock).mock.calls[0]).toEqual(expectedWriteToFileParameters);
});
});
});
21 changes: 21 additions & 0 deletions test/generators/java/renderers/EnumRenderer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JavaGenerator } from '../../../../src/generators';
import { EnumRenderer } from '../../../../src/generators/java/renderers/EnumRenderer';
import { CommonInputModel, CommonModel } from '../../../../src/models';

describe('EnumRenderer', () => {
let renderer: EnumRenderer;
beforeEach(() => {
renderer = new EnumRenderer(JavaGenerator.defaultOptions, new JavaGenerator(), [], new CommonModel(), new CommonInputModel());
});

describe('normalizeKey()', () => {
test('should correctly format " " to correct key', () => {
const key = renderer.normalizeKey('something something');
expect(key).toEqual('SOMETHING_SOMETHING');
});
test('should correctly format "_" to correct key', () => {
const key = renderer.normalizeKey('something_something');
expect(key).toEqual('SOMETHING_SOMETHING');
});
});
});

0 comments on commit c1f7fad

Please sign in to comment.