Skip to content

Commit

Permalink
Always remove enum undescore
Browse files Browse the repository at this point in the history
C# accepts undescore in any type of number.
  • Loading branch information
Rodrigo Cesar committed May 27, 2024
1 parent 4fb63de commit a1e636e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
5 changes: 1 addition & 4 deletions lib/csharp-models-to-json/EnumCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
23 changes: 15 additions & 8 deletions lib/csharp-models-to-json_test/ModelCollector_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}"
);

Expand All @@ -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"));
}

}
Expand Down
9 changes: 9 additions & 0 deletions test-files/TestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit a1e636e

Please sign in to comment.