Skip to content

Commit

Permalink
fix: special cases in enum values for C# generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-debajyoti authored Jan 4, 2022
1 parent 120181d commit ef4c0b7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/generators/csharp/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CSharpRenderer } from '../CSharpRenderer';
import { EnumPreset } from '../../../models';
import { pascalCase } from 'change-case';
import { FormatHelpers } from '../../../helpers';

/**
* Renderer for C#'s `enum` type
Expand Down Expand Up @@ -107,7 +108,7 @@ export const CSHARP_DEFAULT_ENUM_PRESET: EnumPreset<EnumRenderer> = {
return renderer.defaultSelf();
},
item({ item }) {
let itemName = String(item);
let itemName = FormatHelpers.replaceSpecialCharacters(String(item), { exclude: [' '], separator: '_' });
if (typeof item === 'number' || typeof item === 'bigint') {
itemName = `Number_${itemName}`;
} else if (typeof item === 'object') {
Expand Down
29 changes: 25 additions & 4 deletions test/generators/csharp/CSharpGenerator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ describe('CSharpGenerator', () => {
enum: ['Texas', 'Alabama', 'California'],
};

const inputModel = await generator.process(doc);
const model = inputModel.models['CustomEnum'];

let enumModel = await generator.render(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);

enumModel = await generator.renderEnum(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);
});

test('should render enums with translated special characters', async () => {
const doc = {
$id: 'States',
type: 'string',
enum: ['test+', 'test', 'test-', 'test?!', '*test']
};

generator = new CSharpGenerator({ presets: [
{
enum: {
Expand All @@ -137,19 +156,20 @@ describe('CSharpGenerator', () => {
},
}
}
] });
]});

const inputModel = await generator.process(doc);
const model = inputModel.models['CustomEnum'];
const model = inputModel.models['States'];

let enumModel = await generator.render(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);

enumModel = await generator.renderEnum(model, inputModel);
expect(enumModel.result).toMatchSnapshot();
expect(enumModel.dependencies).toEqual([]);
});

test('should render models and their dependencies', async () => {
const doc = {
$id: 'Address',
Expand Down Expand Up @@ -177,6 +197,7 @@ describe('CSharpGenerator', () => {
expect(models[0].result).toMatchSnapshot();
expect(models[1].result).toMatchSnapshot();
});

test('should throw error when reserved keyword is used for package name', async () => {
const doc = {
$id: 'Address',
Expand Down
70 changes: 70 additions & 0 deletions test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,76 @@ public static class StatesExtensions {
"
`;
exports[`CSharpGenerator should render enums with translated special characters 1`] = `
"public enum States {
TestPlus, Test, TestMinus, TestQuestionExclamation, AsteriskTest
}
public static class StatesExtensions {
public static dynamic GetValue(this States enumValue)
{
switch (enumValue)
{
case States.TestPlus: return \\"test+\\";
case States.Test: return \\"test\\";
case States.TestMinus: return \\"test-\\";
case States.TestQuestionExclamation: return \\"test?!\\";
case States.AsteriskTest: return \\"*test\\";
}
return null;
}
public static States? ToStates(dynamic value)
{
switch (value)
{
case \\"test+\\": return States.TestPlus;
case \\"test\\": return States.Test;
case \\"test-\\": return States.TestMinus;
case \\"test?!\\": return States.TestQuestionExclamation;
case \\"*test\\": return States.AsteriskTest;
}
return null;
}
}
"
`;
exports[`CSharpGenerator should render enums with translated special characters 2`] = `
"public enum States {
TestPlus, Test, TestMinus, TestQuestionExclamation, AsteriskTest
}
public static class StatesExtensions {
public static dynamic GetValue(this States enumValue)
{
switch (enumValue)
{
case States.TestPlus: return \\"test+\\";
case States.Test: return \\"test\\";
case States.TestMinus: return \\"test-\\";
case States.TestQuestionExclamation: return \\"test?!\\";
case States.AsteriskTest: return \\"*test\\";
}
return null;
}
public static States? ToStates(dynamic value)
{
switch (value)
{
case \\"test+\\": return States.TestPlus;
case \\"test\\": return States.Test;
case \\"test-\\": return States.TestMinus;
case \\"test?!\\": return States.TestQuestionExclamation;
case \\"*test\\": return States.AsteriskTest;
}
return null;
}
}
"
`;
exports[`CSharpGenerator should render models and their dependencies 1`] = `
"namespace Test.Package
{
Expand Down

0 comments on commit ef4c0b7

Please sign in to comment.