-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from IowaComputerGurus/feature/enums
Added Enum Helpers
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |