Skip to content

Commit

Permalink
Merge pull request #93 from MehdiK/DehumanizeTo
Browse files Browse the repository at this point in the history
added non-generic DehumanizeTo - addresses #92
  • Loading branch information
MehdiK committed Feb 18, 2014
2 parents 69b4386 + bcef4ed commit 4ff641a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
14 changes: 8 additions & 6 deletions src/Humanizer.Tests/DehumanizeToEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ public class DehumanizeToEnumTests
public void ThrowsForNonEnums()
{
Assert.Throws<ArgumentException>(() => EnumTestsResources.CustomDescription.DehumanizeTo<DummyStructWithEnumInterfaces>());
Assert.Throws<ArgumentException>(() => EnumTestsResources.CustomDescription.DehumanizeTo(typeof(DummyStructWithEnumInterfaces)));
}

[Fact]
public void ThrowsForEnumNoMatch()
{
Assert.Throws<CannotMapToTargetException>(() => EnumTestsResources.CustomDescription.DehumanizeTo<DummyEnum>());
Assert.Throws<CannotMapToTargetException>(() => EnumTestsResources.CustomDescription.DehumanizeTo(typeof(DummyEnum)));
}

[Fact]
public void HonorsDescriptionAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithDescriptionAttribute, EnumTestsResources.CustomDescription.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithDescriptionAttribute, EnumTestsResources.CustomDescription.DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void DehumanizeMembersWithoutDescriptionAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithoutDescriptionAttribute, EnumTestsResources.MemberWithoutDescriptionAttributeSentence.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithoutDescriptionAttribute, EnumTestsResources.MemberWithoutDescriptionAttributeSentence.DehumanizeTo(typeof(EnumUnderTest)));
}

[Theory]
Expand All @@ -36,17 +40,15 @@ public void DehumanizeMembersWithoutDescriptionAttribute()
[InlineData(EnumTestsResources.MemberWithoutDescriptionAttributeSentence, EnumUnderTest.MemberWithoutDescriptionAttribute)]
public void IsCaseInsensitive(string input, EnumUnderTest expectedEnum)
{
Assert.Equal(
expectedEnum,
input.DehumanizeTo<EnumUnderTest>());
Assert.Equal(expectedEnum, input.DehumanizeTo<EnumUnderTest>());
Assert.Equal(expectedEnum, input.DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void AllCapitalMembersAreReturnedAsIs()
{
Assert.Equal(
EnumUnderTest.ALLCAPITALS,
EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo(typeof(EnumUnderTest)));
}

struct DummyStructWithEnumInterfaces : IComparable, IFormattable, IConvertible
Expand Down
22 changes: 22 additions & 0 deletions src/Humanizer/EnumDehumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input)

throw new CannotMapToTargetException("Couldn't find any enum member that matches the string " + input);
}

/// <summary>
/// Dehumanizes a string into the Enum it was originally Humanized from!
/// </summary>
/// <param name="input">The string to be converted</param>
/// <param name="targetEnum">The target enum</param>
/// <exception cref="ArgumentException">If targetEnum is not an enum</exception>
/// <exception cref="CannotMapToTargetException">If the provided string cannot be mapped to the target enum</exception>
/// <returns></returns>
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);
}
}

/// <summary>
Expand Down

0 comments on commit 4ff641a

Please sign in to comment.