diff --git a/readme.md b/readme.md index b74130cdc..82786d9c4 100644 --- a/readme.md +++ b/readme.md @@ -95,7 +95,7 @@ Hopefully this will help avoid littering enums with unnecessary attributes! Dehumanizes a string into the Enum it was originally Humanized from! The API looks like: ```C# -public static Enum DehumanizeTo(this string input) +public static TTargetEnum DehumanizeTo(this string input) ``` And the usage is: diff --git a/src/Humanizer.Tests/DehumanizeToEnumTests.cs b/src/Humanizer.Tests/DehumanizeToEnumTests.cs index 72e69c46f..a398ec992 100644 --- a/src/Humanizer.Tests/DehumanizeToEnumTests.cs +++ b/src/Humanizer.Tests/DehumanizeToEnumTests.cs @@ -1,15 +1,11 @@ using System; +using System.Collections.Generic; using Xunit; namespace Humanizer.Tests { public class DehumanizeToEnumTests { - class DummyClass - { - - } - [Fact] public void HonorsDescriptionAttribute() { @@ -19,7 +15,13 @@ public void HonorsDescriptionAttribute() [Fact] public void ThrowsForNonEnums() { - Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo()); + Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo()); + } + + [Fact] + public void ThrowsForEnumNoMatch() + { + Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo()); } [Fact] @@ -51,5 +53,112 @@ public void AllCapitalMembersAreReturnedAsIs() EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo()); } + + struct DummyStructWithEnumInterfaces : IComparable, IFormattable, IConvertible + { + public int CompareTo(object obj) + { + throw new NotImplementedException(); + } + + public string ToString(string format, IFormatProvider formatProvider) + { + throw new NotImplementedException(); + } + + public TypeCode GetTypeCode() + { + throw new NotImplementedException(); + } + + public bool ToBoolean(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public char ToChar(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public sbyte ToSByte(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public byte ToByte(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public short ToInt16(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public ushort ToUInt16(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public int ToInt32(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public uint ToUInt32(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public long ToInt64(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public ulong ToUInt64(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public float ToSingle(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public double ToDouble(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public decimal ToDecimal(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public DateTime ToDateTime(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public string ToString(IFormatProvider provider) + { + throw new NotImplementedException(); + } + + public object ToType(Type conversionType, IFormatProvider provider) + { + throw new NotImplementedException(); + } + } + + enum DummyEnum + { + First, + Second + } } + + + } diff --git a/src/Humanizer/EnumDehumanizeExtensions.cs b/src/Humanizer/EnumDehumanizeExtensions.cs index b2802586c..646a7069f 100644 --- a/src/Humanizer/EnumDehumanizeExtensions.cs +++ b/src/Humanizer/EnumDehumanizeExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace Humanizer { @@ -9,22 +11,22 @@ public static class EnumDehumanizeExtensions /// /// The target enum /// The string to be converted + /// If TTargetEnum is not an enum + /// If the provided string cannot be mapped to the target enum /// - public static Enum DehumanizeTo(this string input) + public static TTargetEnum DehumanizeTo(this string input) where TTargetEnum : struct, IComparable, IFormattable, IConvertible { - var values = (TTargetEnum[]) Enum.GetValues(typeof (TTargetEnum)); + var values = Enum.GetValues(typeof(TTargetEnum)).Cast(); foreach (var value in values) { - var enumValue = value as Enum; - if (enumValue == null) - return null; + var @enum = value as Enum; - if (string.Equals(enumValue.Humanize(), input, StringComparison.OrdinalIgnoreCase)) - return enumValue; + if (string.Equals(@enum.Humanize(), input, StringComparison.OrdinalIgnoreCase)) + return value; } - return null; + throw new KeyNotFoundException("Couldn't find a dehumanized enum value that matches the string : " + input); } } } \ No newline at end of file