Skip to content

Commit

Permalink
Merge pull request #36 from VahidFarahmandian/expr2sql
Browse files Browse the repository at this point in the history
GetDisplayName added to EnumExtensions
  • Loading branch information
VahidFarahmandian authored Dec 29, 2021
2 parents 101007c + 12acdac commit 9ab9da7
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 15 deletions.
108 changes: 94 additions & 14 deletions Jinget.Core.Tests/ExtensionMethods/Enums/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,101 @@
using System;
using Jinget.Core.ExtensionMethods.Enums;
using Jinget.Core.Tests._BaseData;
using System.Collections.Generic;
using System.Linq;
using static Jinget.Core.Tests._BaseData.SampleEnum;

namespace Jinget.Core.Tests.ExtensionMethods.Enums
{
[TestClass()]
public class EnumExtensionsTests
{
enum ProgrammingLanguage
#region DisplayName

[TestMethod()]
public void should_return_default_display_name()
{
CSharp,
ProgrammingLanguage langauge = default;
string expected = "";
string result = langauge.GetDisplayName();

[System.ComponentModel.Description("F#.Net")]
FSharp,
VB
Assert.AreEqual(expected, result);
}

[TestMethod()]
public void should_return_enum_field_value()
public void should_return_display_name_for_member_with_display_attribute()
{
ProgrammingLanguage langauge = ProgrammingLanguage.CSharp;
string expected = "CSharp";
var result = langauge.GetDescription();
ProgrammingLanguage langauge = ProgrammingLanguage.FSharp;
string expected = "F#";
string result = langauge.GetDisplayName();

Assert.AreEqual(expected, result);
}

[TestMethod()]
public void should_return_display_name_for_member_without_display_attribute()
{
ProgrammingLanguage langauge = ProgrammingLanguage.Golang;
string expected = "Golang";
string result = langauge.GetDisplayName();

Assert.AreEqual(expected, result);
}

[TestMethod()]
public void should_return_enum_value_where_display_name_provided()
{
string enumDisplayName = "C#";
List<ProgrammingLanguage> expected = new() { ProgrammingLanguage.CSharp, ProgrammingLanguage.VB };

List<ProgrammingLanguage> result = EnumExtensions.GetValueFromDisplayName<ProgrammingLanguage>(enumDisplayName);

Assert.IsTrue(expected.SequenceEqual(result));
}

[TestMethod()]
public void should_return_enum_value_where_display_name_not_provided()
{
string enumDisplayName = "Golang";
List<ProgrammingLanguage> expected = new() { ProgrammingLanguage.Golang };

List<ProgrammingLanguage> result = EnumExtensions.GetValueFromDisplayName<ProgrammingLanguage>(enumDisplayName);

Assert.IsTrue(expected.SequenceEqual(result));
}

[TestMethod()]
[ExpectedException(typeof(System.ComponentModel.InvalidEnumArgumentException))]
public void should_throw_exception_where_display_name_not_found()
{
string enumDescription = "Java";
EnumExtensions.GetValueFromDisplayName<ProgrammingLanguage>(enumDescription);
}

[TestMethod()]
[ExpectedException(typeof(InvalidOperationException))]
public void should_throw_exception_where_enum_type_is_invalid_in_display_name()
{
string enumDescription = "Java";
EnumExtensions.GetValueFromDisplayName<InvalidStruct>(enumDescription);
}

#endregion

#region Description

[TestMethod()]
public void should_return_default_description()
{
ProgrammingLanguage langauge = default;
string expected = "";
string result = langauge.GetDescription();

Assert.AreEqual(expected, result);
}

[TestMethod()]
public void should_return_enum_field_description_value()
public void should_return_description_for_member_with_description_attribute()
{
ProgrammingLanguage langauge = ProgrammingLanguage.FSharp;
string expected = "F#.Net";
Expand All @@ -38,7 +106,17 @@ public void should_return_enum_field_description_value()
}

[TestMethod()]
public void should_return_enum_field_using_description()
public void should_return_description_for_member_without_description_attribute()
{
ProgrammingLanguage langauge = ProgrammingLanguage.CSharp;
string expected = "CSharp";
var result = langauge.GetDescription();

Assert.AreEqual(expected, result);
}

[TestMethod()]
public void should_return_enum_value_where_description_provided()
{
string enumDescription = "F#.Net";
ProgrammingLanguage expected = ProgrammingLanguage.FSharp;
Expand All @@ -49,7 +127,7 @@ public void should_return_enum_field_using_description()
}

[TestMethod()]
public void should_return_enum_field_using_field_name()
public void should_return_enum_value_where_description_not_provided()
{
string enumDescription = "VB";
ProgrammingLanguage expected = ProgrammingLanguage.VB;
Expand All @@ -61,18 +139,20 @@ public void should_return_enum_field_using_field_name()

[TestMethod()]
[ExpectedException(typeof(System.ComponentModel.InvalidEnumArgumentException))]
public void should_generate_InvalidEnumArgumentException()
public void should_throw_exception_where_description_not_found()
{
string enumDescription = "Java";
EnumExtensions.GetValueFromDescription<ProgrammingLanguage>(enumDescription);
}

[TestMethod()]
[ExpectedException(typeof(InvalidOperationException))]
public void should_generate_InvalidOperationException()
public void should_throw_exception_where_enum_type_is_invalid_in_description()
{
string enumDescription = "Java";
EnumExtensions.GetValueFromDescription<InvalidStruct>(enumDescription);
}

#endregion region
}
}
22 changes: 22 additions & 0 deletions Jinget.Core.Tests/_BaseData/SampleEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace Jinget.Core.Tests._BaseData
{
internal class SampleEnum
{
internal enum ProgrammingLanguage
{
[Display(Name = "C#")]
CSharp = 1,

[System.ComponentModel.Description("F#.Net")]
[Display(Name = "F#")]
FSharp = 2,

[Display(Name = "C#")]
VB = 3,

Golang = 4
}
}
}
51 changes: 50 additions & 1 deletion Jinget.Core/ExtensionMethods/Enums/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Jinget.Core.ExtensionMethods.Enums
{
public static class EnumExtensions
{
/// <summary>
/// Get Name property of Display attribute for a specific enum value
/// If no Name is set on a field, then the stringfied value will be returned
/// </summary>
public static string GetDisplayName(this Enum value)
{
var fi = value.GetType().GetField(value.ToString());
if (fi == null)
return string.Empty;
var attributes = (DisplayAttribute[])fi.GetCustomAttributes(typeof(DisplayAttribute), false);
return attributes.Length > 0 ? attributes[0].Name : value.ToString();
}

/// <summary>
/// Get Description of a specific enum value
/// If no Description is set on a field, then the stringfied value will be returned
/// </summary>
public static string GetDescription(this Enum value)
{
var fi = value.GetType().GetField(value.ToString());

if (fi == null)
return string.Empty;
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
Expand Down Expand Up @@ -44,5 +61,37 @@ public static TEnum GetValueFromDescription<TEnum>(string description) where TEn

throw new InvalidEnumArgumentException($"Enum member with description/name '{description}', not found!");
}

/// <summary>
/// Return enum value based on the given Name property of Display attribute.
/// If no Name is set on a field, then field name will be compared aginst the given displayName
/// </summary>
/// <typeparam name="TEnum">typeof enum</typeparam>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="InvalidEnumArgumentException"></exception>
public static List<TEnum> GetValueFromDisplayName<TEnum>(string displayName) where TEnum : struct, IConvertible
{
var type = typeof(TEnum);
if (!type.IsEnum)
throw new InvalidOperationException();

List<TEnum> results = new List<TEnum>();

foreach (var field in type.GetFields())
{
//Is there any Description attribute set for the field?
if (
string.Equals(
Attribute.GetCustomAttribute(field, typeof(DisplayAttribute)) is DisplayAttribute attribute ? attribute.Name : field.Name,
displayName,
StringComparison.CurrentCultureIgnoreCase)
)
results.Add((TEnum)field.GetValue(null));
}
if (results.Any())
return results;

throw new InvalidEnumArgumentException($"Enum member with displayName/name '{displayName}', not found!");
}
}
}

0 comments on commit 9ab9da7

Please sign in to comment.