From 4fb63de099e72927e54d72b4068ff55f552669bb Mon Sep 17 00:00:00 2001 From: Rodrigo Cesar Date: Tue, 20 Feb 2024 09:33:19 -0300 Subject: [PATCH 1/2] Validate enum binary value Javascript do not have underline separating values. --- lib/csharp-models-to-json/EnumCollector.cs | 11 +++++--- .../ModelCollector_test.cs | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/csharp-models-to-json/EnumCollector.cs b/lib/csharp-models-to-json/EnumCollector.cs index a446433..10bd646 100644 --- a/lib/csharp-models-to-json/EnumCollector.cs +++ b/lib/csharp-models-to-json/EnumCollector.cs @@ -5,13 +5,13 @@ namespace CSharpModelsToJson { - class Enum + public class Enum { public string Identifier { get; set; } public Dictionary Values { get; set; } } - class EnumCollector: CSharpSyntaxWalker + public class EnumCollector: CSharpSyntaxWalker { public readonly List Enums = new List(); @@ -20,9 +20,14 @@ public override void VisitEnumDeclaration(EnumDeclarationSyntax node) var values = new Dictionary(); foreach (var member in node.Members) { - values[member.Identifier.ToString()] = member.EqualsValue != null + var value = member.EqualsValue != null ? member.EqualsValue.Value.ToString() : null; + + if (value?.StartsWith("0b") == true) + value = value.Replace("_", ""); + + values[member.Identifier.ToString()] = value; } this.Enums.Add(new Enum() { diff --git a/lib/csharp-models-to-json_test/ModelCollector_test.cs b/lib/csharp-models-to-json_test/ModelCollector_test.cs index 359fecc..e59a769 100644 --- a/lib/csharp-models-to-json_test/ModelCollector_test.cs +++ b/lib/csharp-models-to-json_test/ModelCollector_test.cs @@ -172,5 +172,31 @@ public void DictionaryInheritance_ReturnsIndexAccessor() Assert.That(modelCollector.Models.First().BaseClasses, Is.Not.Null); Assert.That(modelCollector.Models.First().BaseClasses, Is.EqualTo(new[] { "Dictionary" })); } + + [Test] + public void EnumBinaryValue() + { + var tree = CSharpSyntaxTree.ParseText(@" + public enum A + { + A = 0b_0000_0001, + B = 0b00000010, + }" + ); + + var root = (CompilationUnitSyntax)tree.GetRoot(); + + var enumCollector = new EnumCollector(); + enumCollector.VisitEnumDeclaration(root.DescendantNodes().OfType().First()); + + var model = enumCollector.Enums.First(); + + Assert.IsNotNull(model); + Assert.IsNotNull(model.Values); + + Assert.AreEqual("0b00000001", model.Values["A"]); + Assert.AreEqual("0b00000010", model.Values["B"]); + } + } } \ No newline at end of file From a1e636e3ca40f1e032f6c522369fd41044d1d33f Mon Sep 17 00:00:00 2001 From: Rodrigo Cesar Date: Mon, 27 May 2024 08:50:32 -0300 Subject: [PATCH 2/2] Always remove enum undescore C# accepts undescore in any type of number. --- lib/csharp-models-to-json/EnumCollector.cs | 5 +--- .../ModelCollector_test.cs | 23 ++++++++++++------- test-files/TestClass.cs | 9 ++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/csharp-models-to-json/EnumCollector.cs b/lib/csharp-models-to-json/EnumCollector.cs index 10bd646..412fdea 100644 --- a/lib/csharp-models-to-json/EnumCollector.cs +++ b/lib/csharp-models-to-json/EnumCollector.cs @@ -24,10 +24,7 @@ public override void VisitEnumDeclaration(EnumDeclarationSyntax node) ? member.EqualsValue.Value.ToString() : null; - if (value?.StartsWith("0b") == true) - value = value.Replace("_", ""); - - values[member.Identifier.ToString()] = value; + values[member.Identifier.ToString()] = value?.Replace("_", ""); } this.Enums.Add(new Enum() { diff --git a/lib/csharp-models-to-json_test/ModelCollector_test.cs b/lib/csharp-models-to-json_test/ModelCollector_test.cs index e59a769..c73551d 100644 --- a/lib/csharp-models-to-json_test/ModelCollector_test.cs +++ b/lib/csharp-models-to-json_test/ModelCollector_test.cs @@ -177,10 +177,13 @@ public void DictionaryInheritance_ReturnsIndexAccessor() public void EnumBinaryValue() { var tree = CSharpSyntaxTree.ParseText(@" - public enum A - { - A = 0b_0000_0001, - B = 0b00000010, + public enum A { + A = 1, // decimal: 1 + B = 1_002, // decimal: 1002 + C = 0b011, // binary: 3 in decimal + D = 0b_0000_0100, // binary: 4 in decimal + E = 0x005, // hexadecimal: 5 in decimal + F = 0x000_01a, // hexadecimal: 26 in decimal }" ); @@ -191,11 +194,15 @@ public enum A var model = enumCollector.Enums.First(); - Assert.IsNotNull(model); - Assert.IsNotNull(model.Values); + Assert.That(model, Is.Not.Null); + Assert.That(model.Values, Is.Not.Null); - Assert.AreEqual("0b00000001", model.Values["A"]); - Assert.AreEqual("0b00000010", model.Values["B"]); + Assert.That(model.Values["A"], Is.EqualTo("1")); + Assert.That(model.Values["B"], Is.EqualTo("1002")); + Assert.That(model.Values["C"], Is.EqualTo("0b011")); + Assert.That(model.Values["D"], Is.EqualTo("0b00000100")); + Assert.That(model.Values["E"], Is.EqualTo("0x005")); + Assert.That(model.Values["F"], Is.EqualTo("0x00001a")); } } diff --git a/test-files/TestClass.cs b/test-files/TestClass.cs index b760865..7bee6e5 100644 --- a/test-files/TestClass.cs +++ b/test-files/TestClass.cs @@ -21,4 +21,13 @@ public class TestClass public bool BooleanProperty { get; set; } } + + public enum TestEnum { + A = 1, // decimal: 1 + B = 1_002, // decimal: 1002 + C = 0b011, // binary: 3 in decimal + D = 0b_0000_0100, // binary: 4 in decimal + E = 0x005, // hexadecimal: 5 in decimal + F = 0x000_01a, // hexadecimal: 26 in decimal + } }