Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

605: Fix enum no display attrib #608

Merged
merged 3 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Humanizer.Tests.Shared/EnumHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ public void HonorsDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttribute, EnumUnderTest.MemberWithDisplayAttribute.Humanize());
}
[Fact]
public void HandlesDisplayAttributeWithNoDescription()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttributeWithoutDescription, EnumUnderTest.MemberWithDisplayAttributeWithoutDescription.Humanize());
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithLocalizedDisplayAttribute, EnumUnderTest.MemberWithLocalizedDisplayAttribute.Humanize());
}

}
}
5 changes: 4 additions & 1 deletion src/Humanizer.Tests.Shared/EnumUnderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public enum EnumUnderTest
[Display(Description = EnumTestsResources.MemberWithDisplayAttribute)]
MemberWithDisplayAttribute,
[Display(Description = "MemberWithLocalizedDisplayAttribute", ResourceType = typeof(EnumTestsResources))]
MemberWithLocalizedDisplayAttribute
MemberWithLocalizedDisplayAttribute,
[Display(Name = EnumTestsResources.MemberWithDisplayAttributeWithoutDescription)]
MemberWithDisplayAttributeWithoutDescription
}

public class EnumTestsResources
Expand All @@ -37,6 +39,7 @@ public class EnumTestsResources
public const string MemberWithoutDescriptionAttributeTitle = "Member Without Description Attribute";
public const string MemberWithoutDescriptionAttributeLowerCase = "member without description attribute";
public const string MemberWithDisplayAttribute = "Description from Display attribute";
public const string MemberWithDisplayAttributeWithoutDescription = "Displayattribute without description";
public static string MemberWithLocalizedDisplayAttribute { get { return "Localized description from Display attribute"; } }
}

Expand Down
18 changes: 15 additions & 3 deletions src/Humanizer/EnumHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static class EnumHumanizeExtensions
{
private const string DisplayAttributeTypeName = "System.ComponentModel.DataAnnotations.DisplayAttribute";
private const string DisplayAttributeGetDescriptionMethodName = "GetDescription";
private const string DisplayAttributeGetNameMethodName = "GetName";

private static readonly Func<PropertyInfo, bool> StringTypedProperty = p => p.PropertyType == typeof(string);

Expand Down Expand Up @@ -69,10 +70,21 @@ private static string GetCustomDescription(MemberInfo memberInfo)
var attrType = attr.GetType();
if (attrType.FullName == DisplayAttributeTypeName)
{
var method = attrType.GetRuntimeMethod(DisplayAttributeGetDescriptionMethodName, new Type[0]);
if (method != null)
return method.Invoke(attr, new object[0]).ToString();
var methodGetDescription = attrType.GetRuntimeMethod(DisplayAttributeGetDescriptionMethodName, new Type[0]);
if (methodGetDescription != null)
{
var executedMethod = methodGetDescription.Invoke(attr, new object[0]);
if (executedMethod != null) return executedMethod.ToString();
}
var methodGetName = attrType.GetRuntimeMethod(DisplayAttributeGetNameMethodName, new Type[0]);
if(methodGetName != null)
Copy link
Contributor

@aloisdg aloisdg Jan 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space here if ( ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)

{
var executedMethod = methodGetName.Invoke(attr, new object[0]);
if (executedMethod != null) return executedMethod.ToString();
}
return null;
}

var descriptionProperty =
attrType.GetRuntimeProperties()
.Where(StringTypedProperty)
Expand Down