Skip to content

Commit

Permalink
adapt tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 30, 2024
1 parent a22a35a commit aad20bc
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ public partial class Root
{
return JsonSerializer.Serialize(this, options);
}
public static Root Deserialize(string json)
public static Root Deserialize(string json, JsonSerializerOptions options = null)
{
var deserializeOptions = new JsonSerializerOptions();
deserializeOptions.Converters.Add(new RootConverter());
return JsonSerializer.Deserialize<Root>(json, deserializeOptions);
return JsonSerializer.Deserialize<Root>(json, options);
}
}
internal class RootConverter : JsonConverter<Root>
{
public override bool CanConvert(System.Type objectType)
{
// this converter can be applied to any type
return true;
}
public override Root Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
Expand All @@ -60,10 +53,12 @@ internal class RootConverter : JsonConverter<Root>
}
string propertyName = reader.GetString();
// Advance to the value token
reader.Read();
if (propertyName == \\"email\\")
{
var value = JsonSerializer.Deserialize<string?>(ref reader, options);
instance.Email = value;
instance.Email = JsonSerializer.Deserialize<string?>(ref reader);
continue;
}
}
Expand All @@ -72,26 +67,19 @@ internal class RootConverter : JsonConverter<Root>
}
public override void Write(Utf8JsonWriter writer, Root value, JsonSerializerOptions options)
{
if (value == null)
{
JsonSerializer.Serialize(writer, null, options);
return;
}
var properties = value.GetType().GetProperties();
var properties = value?.GetType().GetProperties();
writer.WriteStartObject();
if(value.Email != null) {
if(value?.Email != null) {
// write property name and let the serializer serialize the value itself
writer.WritePropertyName(\\"email\\");
JsonSerializer.Serialize(writer, value.Email, options);
JsonSerializer.Serialize(writer, value?.Email, options);
}
writer.WriteEndObject();
}
}
",
}",
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,32 @@ Array [
Delivered,
Cancelled
}
public static class OrderStatusExtensions
{
public static int? GetValue(this OrderStatus enumValue)
public static class OrderStatusExtensions
{
switch (enumValue)
public static int? GetValue(this OrderStatus enumValue)
{
case OrderStatus.Ordered: return 30;
case OrderStatus.UnderDelivery: return 40;
case OrderStatus.Delivered: return 50;
case OrderStatus.Cancelled: return 99;
switch (enumValue)
{
case OrderStatus.Ordered: return 30;
case OrderStatus.UnderDelivery: return 40;
case OrderStatus.Delivered: return 50;
case OrderStatus.Cancelled: return 99;
}
return null;
}
return null;
}
public static OrderStatus? ToOrderStatus(dynamic? value)
{
switch (value)
public static OrderStatus? ToOrderStatus(dynamic? value)
{
case 30: return OrderStatus.Ordered;
case 40: return OrderStatus.UnderDelivery;
case 50: return OrderStatus.Delivered;
case 99: return OrderStatus.Cancelled;
switch (value)
{
case 30: return OrderStatus.Ordered;
case 40: return OrderStatus.UnderDelivery;
case 50: return OrderStatus.Delivered;
case 99: return OrderStatus.Cancelled;
}
return null;
}
return null;
}
}
",
]
`;
94 changes: 49 additions & 45 deletions test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,37 +135,42 @@ exports[`CSharpGenerator should render \`enum\` type 1`] = `
NUMBER_1,
RESERVED_NUMBER_1,
RESERVED_FALSE,
CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT
CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT,
SQUARELEFT_QUOTATION_TEST_QUOTATION_COMMA_1_SQUARERIGHT,
RESERVED_NULL
}
public static class ThingsExtensions
{
public static dynamic? GetValue(this Things enumValue)
public static class ThingsExtensions
{
switch (enumValue)
public static dynamic? GetValue(this Things enumValue)
{
case Things.TEXAS: return \\"Texas\\";
case Things.NUMBER_1: return \\"1\\";
case Things.RESERVED_NUMBER_1: return 1;
case Things.RESERVED_FALSE: return false;
case Things.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT: return \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\";
switch (enumValue)
{
case Things.TEXAS: return \\"Texas\\";
case Things.NUMBER_1: return \\"1\\";
case Things.RESERVED_NUMBER_1: return 1;
case Things.RESERVED_FALSE: return false;
case Things.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT: return \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\";
case Things.SQUARELEFT_QUOTATION_TEST_QUOTATION_COMMA_1_SQUARERIGHT: return \\"[\\\\\\"test\\\\\\",1]\\";
case Things.RESERVED_NULL: return null;
}
return null;
}
return null;
}
public static Things? ToThings(dynamic? value)
{
switch (value)
public static Things? ToThings(dynamic? value)
{
case \\"Texas\\": return Things.TEXAS;
case \\"1\\": return Things.NUMBER_1;
case 1: return Things.RESERVED_NUMBER_1;
case false: return Things.RESERVED_FALSE;
case \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\": return Things.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT;
switch (value)
{
case \\"Texas\\": return Things.TEXAS;
case \\"1\\": return Things.NUMBER_1;
case 1: return Things.RESERVED_NUMBER_1;
case false: return Things.RESERVED_FALSE;
case \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\": return Things.CURLYLEFT_QUOTATION_TEST_QUOTATION_COLON_QUOTATION_TEST_QUOTATION_CURLYRIGHT;
case \\"[\\\\\\"test\\\\\\",1]\\": return Things.SQUARELEFT_QUOTATION_TEST_QUOTATION_COMMA_1_SQUARERIGHT;
case null: return Things.RESERVED_NULL;
}
return null;
}
return null;
}
}
"
`;
Expand Down Expand Up @@ -193,35 +198,34 @@ exports[`CSharpGenerator should render enums with translated special characters
TEST_QUESTION_EXCLAMATION,
ASTERISK_TEST
}
public static class StatesExtensions
{
public static string? GetValue(this States enumValue)
public static class StatesExtensions
{
switch (enumValue)
public static string? GetValue(this States enumValue)
{
case States.TEST_PLUS: return \\"test+\\";
case States.TEST: return \\"test\\";
case States.TEST_MINUS: return \\"test-\\";
case States.TEST_QUESTION_EXCLAMATION: return \\"test?!\\";
case States.ASTERISK_TEST: return \\"*test\\";
switch (enumValue)
{
case States.TEST_PLUS: return \\"test+\\";
case States.TEST: return \\"test\\";
case States.TEST_MINUS: return \\"test-\\";
case States.TEST_QUESTION_EXCLAMATION: return \\"test?!\\";
case States.ASTERISK_TEST: return \\"*test\\";
}
return null;
}
return null;
}
public static States? ToStates(dynamic? value)
{
switch (value)
public static States? ToStates(dynamic? value)
{
case \\"test+\\": return States.TEST_PLUS;
case \\"test\\": return States.TEST;
case \\"test-\\": return States.TEST_MINUS;
case \\"test?!\\": return States.TEST_QUESTION_EXCLAMATION;
case \\"*test\\": return States.ASTERISK_TEST;
switch (value)
{
case \\"test+\\": return States.TEST_PLUS;
case \\"test\\": return States.TEST;
case \\"test-\\": return States.TEST_MINUS;
case \\"test?!\\": return States.TEST_QUESTION_EXCLAMATION;
case \\"*test\\": return States.ASTERISK_TEST;
}
return null;
}
return null;
}
}
"
`;
Expand Down
Loading

0 comments on commit aad20bc

Please sign in to comment.