Skip to content

Commit

Permalink
Merge pull request #18 from IowaComputerGurus/feature/enums
Browse files Browse the repository at this point in the history
Added Enum  Helpers
  • Loading branch information
mitchelsellers authored Jun 30, 2022
2 parents e5bfe05 + b61dfd6 commit b44b3a7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/NetCore.Utilities.Tests/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<NullReferenceException>(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);
}
}
}
11 changes: 11 additions & 0 deletions src/NetCore.Utilities.Tests/TestEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace ICG.NetCore.Utilities.Tests;

public enum TestEnum
{
CleanValue = 0,

[Display(Name = "Testing")]
FormattedValue = 1
}
55 changes: 55 additions & 0 deletions src/NetCore.Utilities/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;

namespace ICG.NetCore.Utilities;

/// <summary>
/// Helpful extension methods targeting Enums to help display formatted enum values
/// </summary>
public static class EnumExtensions
{
/// <summary>
/// Returns the configured Display Name, or default string value for the given enum value
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetDisplayNameOrStringValue(this Enum enumValue)
{
return enumValue?
.GetType()?
.GetMember(enumValue.ToString())?
.First()?
.GetCustomAttribute<DisplayAttribute>()?
.GetName() ?? enumValue?.ToString();
}

/// <summary>
/// Gets the display name of an enum value, will return null if the [Display] attribute isn't found
/// </summary>
/// <exception cref="NullReferenceException">When the custom attribute is not found</exception>
/// <param name="enumValue"></param>
/// <returns></returns>
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}

/// <summary>
/// Checks to see if an element has a Display Attribute added
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static bool HasDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>() != null;
}
}

0 comments on commit b44b3a7

Please sign in to comment.