Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Add flag enum support
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
Michael Hancock committed Apr 7, 2020
1 parent 33f8ccd commit eb0f05e
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 11 deletions.
80 changes: 69 additions & 11 deletions src/EnumDisplayName/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

#if NETSTANDARD1_3

using System.Reflection;

#endif

namespace Enable.EnumDisplayName
Expand Down Expand Up @@ -35,26 +39,80 @@ public static string GetDisplayName(this Enum enumValue)

if (string.Compare(fieldName, field.Name, StringComparison.Ordinal) == 0)
{
var customAttributes = field.GetCustomAttributes(false);
var enumDisplayNameAttribute = customAttributes.OfType<EnumDisplayNameAttribute>().FirstOrDefault();
var fieldAttributes = field.GetCustomAttributes(false).Cast<Attribute>();
return GetNameFromEnumField(enumValue, fieldAttributes);
}
}

if (enumDisplayNameAttribute != null)
{
return enumDisplayNameAttribute.GetDisplayName();
}
return string.Empty;
}

/// <summary>
/// Returns the display names of a flag enum as a comma seperated string as specified by the
/// EnumDisplayNameAttribute declared on the enum field. If no EnumDisplayNameAttribute is
/// declared on the enum field then the name of the enum field is returned.
/// </summary>
public static string GetFlagsDisplayName(this Enum enumValue)
{
if (enumValue == null)
{
return string.Empty;
}

var displayNameAttribute = customAttributes.OfType<DisplayAttribute>().FirstOrDefault();
var enumType = enumValue.GetType();
var fieldNames = Enum.GetNames(enumType);
var fields = enumType.GetFields()
.Where(o => fieldNames.Any(p => p == o.Name))
.ToArray();

if (displayNameAttribute != null)
var fieldPosition = 0;
var result = string.Empty;
foreach (Enum currentValue in Enum.GetValues(enumType))
{
if (enumValue.HasFlag(currentValue))
{
var currentField = fields[fieldPosition];
if (currentField.IsSpecialName)
{
return displayNameAttribute.GetName();
fieldPosition += 1;
continue;
}

return fieldName;
var fieldAttributes = currentField
.GetCustomAttributes(false)
.Cast<Attribute>();

var fieldName = GetNameFromEnumField(enumValue, fieldAttributes);

result = result == string.Empty ? fieldName
: result + "," + fieldName;
}

fieldPosition += 1;
}

return string.Empty;
return result;
}

private static string GetNameFromEnumField(Enum enumValue, IEnumerable<Attribute> fieldAttributes)
{
var enumType = enumValue.GetType();
var fieldName = Enum.GetName(enumType, enumValue);
var enumDisplayNameAttribute = fieldAttributes.OfType<EnumDisplayNameAttribute>().FirstOrDefault();

if (enumDisplayNameAttribute != null)
{
return enumDisplayNameAttribute.GetDisplayName();
}

var displayNameAttribute = fieldAttributes.OfType<DisplayAttribute>().FirstOrDefault();

if (displayNameAttribute != null)
{
return displayNameAttribute.GetName();
}

return fieldName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using Xunit;

namespace Enable.EnumDisplayName
{
public class EnumExtensionsGetFlagsDisplayNameTests
{
[Fact]
public void GetFlagsDisplayName_ReturnsEmptyStringWhenSourceIsNull()
{
// Arrange
Enum source = null;

// Act
var result = source.GetFlagsDisplayName();

// Assert
Assert.Equal(string.Empty, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEnumDisplayNameAttributeDisplayName()
{
// Act
var result = MockFlagEnum.EnumDisplayNameAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.EnumDisplayNameAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEnumDisplayNameAttributeWithResourceDisplayName()
{
// Act
var result = MockFlagEnum.EnumDisplayNameAttributeWithResource.GetFlagsDisplayName();

// Assert
Assert.Equal(Resources.MockEnum.EnumDisplayNameAttributeWithResource, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsDisplayAttributeDisplayName()
{
// Act
var result = MockFlagEnum.DisplayAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.DisplayAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsFieldName()
{
// Act
var result = MockFlagEnum.NoAttribute.GetFlagsDisplayName();

// Assert
Assert.Equal(MockEnumDisplayNames.NoAttribute, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsEmptyStringWithSpecialNameAttribute()
{
// Act
var result = MockFlagEnum.SpecialName.GetFlagsDisplayName();

// Assert
Assert.Equal(string.Empty, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsCommaSeperatedStringForFlagValues()
{
// Arrange
var sut = MockFlagEnum.DisplayAttribute | MockFlagEnum.EnumDisplayNameAttribute;

// Act
var result = sut.GetFlagsDisplayName();

// Assert
var expected = $"{MockEnumDisplayNames.EnumDisplayNameAttribute},{MockEnumDisplayNames.DisplayAttribute}";
Assert.Equal(expected, result);
}

[Fact]
public void GetFlagsDisplayName_ReturnsCommaSeperatedStringForFlagValues_ExceptSpecialNameAttributes()
{
// Arrange
var sut = MockFlagEnum.DisplayAttribute | MockFlagEnum.SpecialName;

// Act
var result = sut.GetFlagsDisplayName();

// Assert
var expected = $"{MockEnumDisplayNames.DisplayAttribute}";
Assert.Equal(expected, result);
}
}
}
25 changes: 25 additions & 0 deletions test/EnumDisplayName.Test/Enums/MockFlagEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace Enable.EnumDisplayName
{
[Flags]
public enum MockFlagEnum
{
[EnumDisplayName(MockEnumDisplayNames.EnumDisplayNameAttribute)]
EnumDisplayNameAttribute = 1,

[EnumDisplayName(MockEnumDisplayNames.EnumDisplayNameAttributeWithResource, typeof(Resources.MockEnum))]
EnumDisplayNameAttributeWithResource = 2,

[Display(Name = MockEnumDisplayNames.DisplayAttribute)]
DisplayAttribute = 4,

NoAttribute = 8,

[SpecialName]
[EnumDisplayName(MockEnumDisplayNames.SpecialName)]
SpecialName = 16
}
}

0 comments on commit eb0f05e

Please sign in to comment.