diff --git a/lib/csharp-models-to-json/EnumCollector.cs b/lib/csharp-models-to-json/EnumCollector.cs index a446433..412fdea 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,11 @@ 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; + + 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 359fecc..c73551d 100644 --- a/lib/csharp-models-to-json_test/ModelCollector_test.cs +++ b/lib/csharp-models-to-json_test/ModelCollector_test.cs @@ -172,5 +172,38 @@ 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 = 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 + }" + ); + + var root = (CompilationUnitSyntax)tree.GetRoot(); + + var enumCollector = new EnumCollector(); + enumCollector.VisitEnumDeclaration(root.DescendantNodes().OfType().First()); + + var model = enumCollector.Enums.First(); + + Assert.That(model, Is.Not.Null); + Assert.That(model.Values, Is.Not.Null); + + 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")); + } + } } \ No newline at end of file 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 + } }