diff --git a/src/NetCore.Utilities.Tests/EnumExtensionsTests.cs b/src/NetCore.Utilities.Tests/EnumExtensionsTests.cs new file mode 100644 index 0000000..2e94f7e --- /dev/null +++ b/src/NetCore.Utilities.Tests/EnumExtensionsTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Xunit; +using Xunit.Sdk; + +namespace ICG.NetCore.Utilities.Tests +{ + public class EnumExtensionsTests + { + [Fact] + public void GetDisplayName_ShouldReturnDisplayName_WhenAttributeFound() + { + //Arrange + var value = TestEnum.FormattedValue; + var expectedLabel = "Testing"; + + //Act + var result = value.GetDisplayName(); + + //Assert + Assert.Equal(expectedLabel, result); + } + + [Fact] + public void GetDisplayName_ShouldThrowNullReferenceException_WhenAttributeNotFound() + { + //Arrange + var value = TestEnum.CleanValue; + + //Act + var recordData = Record.Exception(() => value.GetDisplayName()); + + //Assert + Assert.NotNull(recordData); + Assert.IsType(recordData); + } + + [Theory] + [InlineData(TestEnum.CleanValue, false)] + [InlineData(TestEnum.FormattedValue, true)] + public void HasDisplayName_ShouldReturnProperBoolValue(TestEnum value, bool expectedResult) + { + //Arrange + + //Act + var result = value.HasDisplayName(); + + //Assert + Assert.Equal(expectedResult, result); + } + + [Theory] + [InlineData(TestEnum.CleanValue, "CleanValue")] + [InlineData(TestEnum.FormattedValue, "Testing")] + public void GetDisplayNameOrStringValue_ShouldReturnDisplayName_OrEnumValue_WithoutException(TestEnum value, + string expectedResult) + { + //Arrange + + //Act + var result = value.GetDisplayNameOrStringValue(); + + //Assert + Assert.Equal(expectedResult, result); + } + } +} diff --git a/src/NetCore.Utilities.Tests/TestEnum.cs b/src/NetCore.Utilities.Tests/TestEnum.cs new file mode 100644 index 0000000..5c65b6b --- /dev/null +++ b/src/NetCore.Utilities.Tests/TestEnum.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace ICG.NetCore.Utilities.Tests; + +public enum TestEnum +{ + CleanValue = 0, + + [Display(Name = "Testing")] + FormattedValue = 1 +} \ No newline at end of file diff --git a/src/NetCore.Utilities/EnumExtensions.cs b/src/NetCore.Utilities/EnumExtensions.cs new file mode 100644 index 0000000..2ab2bb8 --- /dev/null +++ b/src/NetCore.Utilities/EnumExtensions.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection; + +namespace ICG.NetCore.Utilities; + +/// +/// Helpful extension methods targeting Enums to help display formatted enum values +/// +public static class EnumExtensions +{ + /// + /// Returns the configured Display Name, or default string value for the given enum value + /// + /// + /// + public static string GetDisplayNameOrStringValue(this Enum enumValue) + { + return enumValue? + .GetType()? + .GetMember(enumValue.ToString())? + .First()? + .GetCustomAttribute()? + .GetName() ?? enumValue?.ToString(); + } + + /// + /// Gets the display name of an enum value, will return null if the [Display] attribute isn't found + /// + /// When the custom attribute is not found + /// + /// + public static string GetDisplayName(this Enum enumValue) + { + return enumValue.GetType() + .GetMember(enumValue.ToString()) + .First() + .GetCustomAttribute() + .GetName(); + } + + /// + /// Checks to see if an element has a Display Attribute added + /// + /// + /// + public static bool HasDisplayName(this Enum enumValue) + { + return enumValue.GetType() + .GetMember(enumValue.ToString()) + .First() + .GetCustomAttribute() != null; + } +} \ No newline at end of file