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

Change the return type of DehumanizeTo<TTargetEnum> to TTargetEnum #69

Merged
merged 3 commits into from
Jan 27, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TTargetEnum>(this string input)
public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input)
```

And the usage is:
Expand Down
121 changes: 115 additions & 6 deletions src/Humanizer.Tests/DehumanizeToEnumTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using Xunit;

namespace Humanizer.Tests
{
public class DehumanizeToEnumTests
{
class DummyClass
{

}

[Fact]
public void HonorsDescriptionAttribute()
{
Expand All @@ -19,7 +15,13 @@ public void HonorsDescriptionAttribute()
[Fact]
public void ThrowsForNonEnums()
{
Assert.Throws<ArgumentException>(() => EnumTestsResources.CustomDescription.DehumanizeTo<DummyClass>());
Assert.Throws<ArgumentException>(() => EnumTestsResources.CustomDescription.DehumanizeTo<DummyStructWithEnumInterfaces>());
}

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

[Fact]
Expand Down Expand Up @@ -51,5 +53,112 @@ public void AllCapitalMembersAreReturnedAsIs()
EnumUnderTest.ALLCAPITALS,
EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo<EnumUnderTest>());
}

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
}
}



}
18 changes: 10 additions & 8 deletions src/Humanizer/EnumDehumanizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer
{
Expand All @@ -9,22 +11,22 @@ public static class EnumDehumanizeExtensions
/// </summary>
/// <typeparam name="TTargetEnum">The target enum</typeparam>
/// <param name="input">The string to be converted</param>
/// <exception cref="ArgumentException">If TTargetEnum is not an enum</exception>
/// <exception cref="KeyNotFoundException">If the provided string cannot be mapped to the target enum</exception>
/// <returns></returns>
public static Enum DehumanizeTo<TTargetEnum>(this string input)
public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input) where TTargetEnum : struct, IComparable, IFormattable, IConvertible
{
var values = (TTargetEnum[]) Enum.GetValues(typeof (TTargetEnum));
var values = Enum.GetValues(typeof(TTargetEnum)).Cast<TTargetEnum>();

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);
}
}
}