Skip to content

Commit

Permalink
Enums of any size
Browse files Browse the repository at this point in the history
  • Loading branch information
loxsmoke committed Jul 19, 2021
1 parent ff153cc commit f899b82
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
26 changes: 23 additions & 3 deletions src/DocXml/DocXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Numerics;

// Disable warning for missing XML comments
#pragma warning disable CS1591
Expand Down Expand Up @@ -229,17 +230,36 @@ public EnumComments GetEnumComments(Type enumType, bool fillValues = false)
GetCommonComments(comments, typeNode);
};

bool valueCommentsExist = false;
var underlyingType = Enum.GetUnderlyingType(enumType);
var valueCommentsExist = false;
foreach (var enumName in enumType.GetEnumNames())
{
var valueNode = GetXmlMemberNode(enumType.EnumValueId(enumName), enumType);
valueCommentsExist |= (valueNode != null);
var parsedValue = Enum.Parse(enumType, enumName);
var valueComment = new EnumValueComment()
{
Name = enumName,
ValueObject = Convert.ChangeType(Enum.Parse(enumType, enumName), Enum.GetUnderlyingType(enumType))
IsBigValue = true
};
valueComment.Value = valueComment.ValueObject as int? ?? 0;
if (underlyingType == typeof(uint))
{
valueComment.BigValue = Convert.ToUInt32(parsedValue);
}
else if (underlyingType == typeof(long))
{
valueComment.BigValue = Convert.ToInt64(parsedValue);
}
else if (underlyingType == typeof(ulong))
{
valueComment.BigValue = Convert.ToUInt64(parsedValue);
}
else
{
valueComment.IsBigValue = false;
valueComment.Value = Convert.ToInt32(parsedValue);
valueComment.BigValue = valueComment.Value;
}
comments.ValueComments.Add(valueComment);
GetCommonComments(valueComment, valueNode);
}
Expand Down
17 changes: 12 additions & 5 deletions src/DocXml/EnumValueComment.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;
using LoxSmoke.DocXml;

Expand All @@ -16,25 +17,31 @@ public class EnumValueComment : CommonComments
public string Name { get; set; }

/// <summary>
/// Integer value of the enum, if the enum is the default (int32) base type; otherwise, returns 0.
/// Integer value of the enum if enum value fits in signed 32-bit integer.
/// If value is too big (uint, long or ulong) then returned value is 0.
/// </summary>
/// <remarks>
/// Use <see cref="ValueObject"/> to get the base integer value regardless of integer type.
/// Use <see cref="BigValue"/> to get the value regardless of integer type.
/// </remarks>
public int Value { get; set; }

/// <summary>
/// Integer value of the enum, whether signed or unsigned, or 8, 16, 32, or 64 bits in length.
/// True if enum value is too big to fit in int Value property. Use BigValue property instead.
/// </summary>
public object ValueObject { get; set; }
public bool IsBigValue { get; set; }

/// <summary>
/// The value of the enum. This field can handle any enum size.
/// </summary>
public BigInteger BigValue { get; set; }

/// <summary>
/// Debugging-friendly text.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"{(Name??"")}={ValueObject??Value}" + (Summary != null ? $" {Summary}" : "");
return $"{(Name??"")}={(IsBigValue ? BigValue.ToString() : Value.ToString())}" + (Summary != null ? $" {Summary}" : "");
}
}
}
30 changes: 18 additions & 12 deletions test/DocXmlUnitTests/EnumCommentsUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LoxSmoke.DocXml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Numerics;

#pragma warning disable CS1591

Expand Down Expand Up @@ -34,6 +35,17 @@ public class EnumCommentsUnitTests : BaseTestClass
void AssertEnumComment(int expectedValue, string expectedName, string expectedSummary, EnumValueComment comment)
{
Assert.AreEqual(expectedValue, comment.Value);
Assert.AreEqual(expectedValue, comment.BigValue);
Assert.AreEqual(expectedName, comment.Name);
Assert.AreEqual(expectedSummary, comment.Summary);
}

void AssertBigEnumComment(BigInteger expectedValue, string expectedName, string expectedSummary, EnumValueComment comment)
{
Assert.IsTrue(comment.IsBigValue);
Assert.AreEqual(expectedValue, comment.BigValue);
Assert.AreEqual(0, comment.Value);
Assert.AreEqual(expectedValue, comment.BigValue);
Assert.AreEqual(expectedName, comment.Name);
Assert.AreEqual(expectedSummary, comment.Summary);
}
Expand Down Expand Up @@ -113,32 +125,26 @@ public void EnumType_WithValues_UInt8()
{
var mm = Reader.GetEnumComments(TestEnumUInt8_Type);
Assert.AreEqual(2, mm.ValueComments.Count);
AssertEnumComment(0, "Value1", "Enum value one", mm.ValueComments[0]);
AssertEnumComment(0, "Value2", "Enum value two", mm.ValueComments[1]);
Assert.AreEqual((byte)10, mm.ValueComments[0].ValueObject);
Assert.AreEqual((byte)20, mm.ValueComments[1].ValueObject);
AssertEnumComment(10, "Value1", "Enum value one", mm.ValueComments[0]);
AssertEnumComment(20, "Value2", "Enum value two", mm.ValueComments[1]);
}

[TestMethod]
public void EnumType_WithValues_UInt64()
{
var mm = Reader.GetEnumComments(TestEnumUInt64_Type);
Assert.AreEqual(2, mm.ValueComments.Count);
AssertEnumComment(0, "Value1", "Enum value one", mm.ValueComments[0]);
AssertEnumComment(0, "Value2", "Enum value two", mm.ValueComments[1]);
Assert.AreEqual(10UL, mm.ValueComments[0].ValueObject);
Assert.AreEqual(20UL, mm.ValueComments[1].ValueObject);
AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", mm.ValueComments[0]);
AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", mm.ValueComments[1]);
}

[TestMethod]
public void EnumType_WithValues_Int64()
{
var mm = Reader.GetEnumComments(TestEnumInt64_Type);
Assert.AreEqual(2, mm.ValueComments.Count);
AssertEnumComment(0, "Value1", "Enum value one", mm.ValueComments[0]);
AssertEnumComment(0, "Value2", "Enum value two", mm.ValueComments[1]);
Assert.AreEqual(10L, mm.ValueComments[0].ValueObject);
Assert.AreEqual(20L, mm.ValueComments[1].ValueObject);
AssertBigEnumComment(new BigInteger(10L), "Value1", "Enum value one", mm.ValueComments[0]);
AssertBigEnumComment(new BigInteger(20L), "Value2", "Enum value two", mm.ValueComments[1]);
}

[TestMethod]
Expand Down

0 comments on commit f899b82

Please sign in to comment.