From 6eb9f93c97910ad26947cc8851afb8a65200a62e Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 17 Feb 2014 17:17:26 +0330 Subject: [PATCH 1/2] added non-generic DehumanizeTo - addresses #92 --- readme.md | 12 +++++++++++ src/Humanizer.Tests/DehumanizeToEnumTests.cs | 14 +++++++------ src/Humanizer/EnumDehumanizeExtensions.cs | 22 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index f2be0a96e..d218d1175 100644 --- a/readme.md +++ b/readme.md @@ -106,6 +106,18 @@ And the usage is: And just like the Humanize API it honors the `Description` attribute. You don't have to provide the casing you provided during humanization: it figures it out. +There is also a non-generic counterpart for when the original Enum is not known at compile time: + +```C# +public static Enum DehumanizeTo(this string input, Type targetEnum) +``` + +which can be used like: + +```C# +"Member without description attribute".DehumanizeTo(typeof(EnumUnderTest)) => EnumUnderTest.MemberWithoutDescriptionAttribute +``` + ###Humanize DateTime You can `Humanize` an instance of `DateTime` and get back a string telling how far back or forward in time that is: diff --git a/src/Humanizer.Tests/DehumanizeToEnumTests.cs b/src/Humanizer.Tests/DehumanizeToEnumTests.cs index 075cbaaf0..4839340d7 100644 --- a/src/Humanizer.Tests/DehumanizeToEnumTests.cs +++ b/src/Humanizer.Tests/DehumanizeToEnumTests.cs @@ -10,24 +10,28 @@ public class DehumanizeToEnumTests public void ThrowsForNonEnums() { Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo()); + Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo(typeof(DummyStructWithEnumInterfaces))); } [Fact] public void ThrowsForEnumNoMatch() { Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo()); + Assert.Throws(() => EnumTestsResources.CustomDescription.DehumanizeTo(typeof(DummyEnum))); } [Fact] public void HonorsDescriptionAttribute() { Assert.Equal(EnumUnderTest.MemberWithDescriptionAttribute, EnumTestsResources.CustomDescription.DehumanizeTo()); + Assert.Equal(EnumUnderTest.MemberWithDescriptionAttribute, EnumTestsResources.CustomDescription.DehumanizeTo(typeof(EnumUnderTest))); } [Fact] public void DehumanizeMembersWithoutDescriptionAttribute() { Assert.Equal(EnumUnderTest.MemberWithoutDescriptionAttribute, EnumTestsResources.MemberWithoutDescriptionAttributeSentence.DehumanizeTo()); + Assert.Equal(EnumUnderTest.MemberWithoutDescriptionAttribute, EnumTestsResources.MemberWithoutDescriptionAttributeSentence.DehumanizeTo(typeof(EnumUnderTest))); } [Theory] @@ -36,17 +40,15 @@ public void DehumanizeMembersWithoutDescriptionAttribute() [InlineData(EnumTestsResources.MemberWithoutDescriptionAttributeSentence, EnumUnderTest.MemberWithoutDescriptionAttribute)] public void IsCaseInsensitive(string input, EnumUnderTest expectedEnum) { - Assert.Equal( - expectedEnum, - input.DehumanizeTo()); + Assert.Equal(expectedEnum, input.DehumanizeTo()); + Assert.Equal(expectedEnum, input.DehumanizeTo(typeof(EnumUnderTest))); } [Fact] public void AllCapitalMembersAreReturnedAsIs() { - Assert.Equal( - EnumUnderTest.ALLCAPITALS, - EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo()); + Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo()); + Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo(typeof(EnumUnderTest))); } struct DummyStructWithEnumInterfaces : IComparable, IFormattable, IConvertible diff --git a/src/Humanizer/EnumDehumanizeExtensions.cs b/src/Humanizer/EnumDehumanizeExtensions.cs index cd7c380e2..5bb081087 100644 --- a/src/Humanizer/EnumDehumanizeExtensions.cs +++ b/src/Humanizer/EnumDehumanizeExtensions.cs @@ -27,6 +27,28 @@ public static TTargetEnum DehumanizeTo(this string input) throw new CannotMapToTargetException("Couldn't find any enum member that matches the string " + input); } + + /// + /// Dehumanizes a string into the Enum it was originally Humanized from! + /// + /// The string to be converted + /// The target enum + /// If targetEnum is not an enum + /// If the provided string cannot be mapped to the target enum + /// + public static Enum DehumanizeTo(this string input, Type targetEnum) + { + var values = Enum.GetValues(targetEnum); + + foreach (var value in values) + { + var @enum = value as Enum; + if (string.Equals(@enum.Humanize(), input, StringComparison.OrdinalIgnoreCase)) + return @enum; + } + + throw new CannotMapToTargetException("Couldn't find any enum member that matches the string " + input); + } } /// From bcef4edb82fc26368f9484f1ad6db1fe57ce7178 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Mon, 17 Feb 2014 17:19:42 +0330 Subject: [PATCH 2/2] updated the release notes --- release_notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release_notes.md b/release_notes.md index e32dda2d8..93ae3f754 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,5 +1,5 @@ ###In Development - - + - [#93](https://github.com/MehdiK/Humanizer/pull/93): added non-generic DehumanizeTo for Enums unknown at compile time [Commits](https://github.com/MehdiK/Humanizer/compare/v1.10.1...master)