Skip to content

Commit b44b3a7

Browse files
Merge pull request #18 from IowaComputerGurus/feature/enums
Added Enum Helpers
2 parents e5bfe05 + b61dfd6 commit b44b3a7

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Xunit;
8+
using Xunit.Sdk;
9+
10+
namespace ICG.NetCore.Utilities.Tests
11+
{
12+
public class EnumExtensionsTests
13+
{
14+
[Fact]
15+
public void GetDisplayName_ShouldReturnDisplayName_WhenAttributeFound()
16+
{
17+
//Arrange
18+
var value = TestEnum.FormattedValue;
19+
var expectedLabel = "Testing";
20+
21+
//Act
22+
var result = value.GetDisplayName();
23+
24+
//Assert
25+
Assert.Equal(expectedLabel, result);
26+
}
27+
28+
[Fact]
29+
public void GetDisplayName_ShouldThrowNullReferenceException_WhenAttributeNotFound()
30+
{
31+
//Arrange
32+
var value = TestEnum.CleanValue;
33+
34+
//Act
35+
var recordData = Record.Exception(() => value.GetDisplayName());
36+
37+
//Assert
38+
Assert.NotNull(recordData);
39+
Assert.IsType<NullReferenceException>(recordData);
40+
}
41+
42+
[Theory]
43+
[InlineData(TestEnum.CleanValue, false)]
44+
[InlineData(TestEnum.FormattedValue, true)]
45+
public void HasDisplayName_ShouldReturnProperBoolValue(TestEnum value, bool expectedResult)
46+
{
47+
//Arrange
48+
49+
//Act
50+
var result = value.HasDisplayName();
51+
52+
//Assert
53+
Assert.Equal(expectedResult, result);
54+
}
55+
56+
[Theory]
57+
[InlineData(TestEnum.CleanValue, "CleanValue")]
58+
[InlineData(TestEnum.FormattedValue, "Testing")]
59+
public void GetDisplayNameOrStringValue_ShouldReturnDisplayName_OrEnumValue_WithoutException(TestEnum value,
60+
string expectedResult)
61+
{
62+
//Arrange
63+
64+
//Act
65+
var result = value.GetDisplayNameOrStringValue();
66+
67+
//Assert
68+
Assert.Equal(expectedResult, result);
69+
}
70+
}
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ICG.NetCore.Utilities.Tests;
4+
5+
public enum TestEnum
6+
{
7+
CleanValue = 0,
8+
9+
[Display(Name = "Testing")]
10+
FormattedValue = 1
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace ICG.NetCore.Utilities;
7+
8+
/// <summary>
9+
/// Helpful extension methods targeting Enums to help display formatted enum values
10+
/// </summary>
11+
public static class EnumExtensions
12+
{
13+
/// <summary>
14+
/// Returns the configured Display Name, or default string value for the given enum value
15+
/// </summary>
16+
/// <param name="enumValue"></param>
17+
/// <returns></returns>
18+
public static string GetDisplayNameOrStringValue(this Enum enumValue)
19+
{
20+
return enumValue?
21+
.GetType()?
22+
.GetMember(enumValue.ToString())?
23+
.First()?
24+
.GetCustomAttribute<DisplayAttribute>()?
25+
.GetName() ?? enumValue?.ToString();
26+
}
27+
28+
/// <summary>
29+
/// Gets the display name of an enum value, will return null if the [Display] attribute isn't found
30+
/// </summary>
31+
/// <exception cref="NullReferenceException">When the custom attribute is not found</exception>
32+
/// <param name="enumValue"></param>
33+
/// <returns></returns>
34+
public static string GetDisplayName(this Enum enumValue)
35+
{
36+
return enumValue.GetType()
37+
.GetMember(enumValue.ToString())
38+
.First()
39+
.GetCustomAttribute<DisplayAttribute>()
40+
.GetName();
41+
}
42+
43+
/// <summary>
44+
/// Checks to see if an element has a Display Attribute added
45+
/// </summary>
46+
/// <param name="enumValue"></param>
47+
/// <returns></returns>
48+
public static bool HasDisplayName(this Enum enumValue)
49+
{
50+
return enumValue.GetType()
51+
.GetMember(enumValue.ToString())
52+
.First()
53+
.GetCustomAttribute<DisplayAttribute>() != null;
54+
}
55+
}

0 commit comments

Comments
 (0)