Last active
October 31, 2019 09:08
-
-
Save chivandikwa/dc3774144f513f668496c997d5b1945a to your computer and use it in GitHub Desktop.
Enum to enum / string to enum mapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class EnumMapper | |
{ | |
public static TEnum MapToEnum<TEnum>(this Enum originalEnumValue, bool ignoreCase = true) where TEnum : struct, IConvertible | |
{ | |
if (!typeof(TEnum).IsEnum) | |
{ | |
throw new ArgumentException("TEnum must be an enumerated type"); | |
} | |
var parsed = Enum.TryParse(originalEnumValue.ToString(), ignoreCase, out TEnum result); | |
if (parsed) | |
return result; | |
throw new ArgumentOutOfRangeException(nameof(originalEnumValue), $"Value {originalEnumValue} of enum type {originalEnumValue.GetType().Name} could not be mapped to {typeof(TEnum).Name} enum."); | |
} | |
public static TEnum MapToEnum<TEnum>(this string originalEnumValue, bool ignoreCase = true) where TEnum : struct, IConvertible | |
{ | |
if (!typeof(TEnum).IsEnum) | |
{ | |
throw new ArgumentException("TEnum must be an enumerated type"); | |
} | |
var parsed = Enum.TryParse(originalEnumValue, ignoreCase, out TEnum result); | |
if (parsed) | |
return result; | |
throw new ArgumentOutOfRangeException(nameof(originalEnumValue), $"Value {originalEnumValue} of enum type {originalEnumValue.GetType().Name} could not be mapped to {typeof(TEnum).Name} enum."); | |
} | |
} | |
using System; | |
using FluentAssertions; | |
using Xunit; | |
public class EnumMapperTests | |
{ | |
private enum DayOfWeekCustom | |
{ | |
Friday, | |
Funday, | |
SUNDAY | |
} | |
[Fact] | |
public void MapToEnum_WhenMatchExists_ShouldReturnMatchingValue() | |
{ | |
var original = DayOfWeekCustom.Friday; | |
var result = original.MapToEnum<DayOfWeek>(); | |
result.Should().Be(DayOfWeek.Friday); | |
} | |
[Fact] | |
public void MapToEnum_FromString_WhenMatchExists_ShouldReturnMatchingValue() | |
{ | |
var original = "Friday"; | |
var result = original.MapToEnum<DayOfWeek>(); | |
result.Should().Be(DayOfWeek.Friday); | |
} | |
[Fact] | |
public void MapToEnum_Default_ShouldBeCaseInsensitive() | |
{ | |
var original = DayOfWeekCustom.SUNDAY; | |
var result = original.MapToEnum<DayOfWeek>(); | |
result.Should().Be(DayOfWeek.Sunday); | |
} | |
[Fact] | |
public void MapToEnum_FromString_Default_ShouldBeCaseInsensitive() | |
{ | |
var original = "SUNDAY"; | |
var result = original.MapToEnum<DayOfWeek>(); | |
result.Should().Be(DayOfWeek.Sunday); | |
} | |
[Fact] | |
public void MapToEnum_WhenMatchDoesNotExist_ThrowsArgumentOutOfRangeException() | |
{ | |
var original = DayOfWeekCustom.Funday; | |
Action mapAction = () => original.MapToEnum<DayOfWeek>(); | |
mapAction.Should().Throw<ArgumentOutOfRangeException>(); | |
} | |
[Fact] | |
public void MapToEnum_FromString_WhenMatchDoesNotExist_ThrowsArgumentOutOfRangeException() | |
{ | |
var original = "Funday"; | |
Action mapAction = () => original.MapToEnum<DayOfWeek>(); | |
mapAction.Should().Throw<ArgumentOutOfRangeException>(); | |
} | |
[Fact] | |
public void MapToEnum_WhenMadeCaseSensitive_ThrowsArgumentOutOfRangeExceptionOnFailedMatch() | |
{ | |
var original = DayOfWeekCustom.SUNDAY; | |
Action mapAction = () => original.MapToEnum<DayOfWeek>(ignoreCase: false); | |
mapAction.Should().Throw<ArgumentOutOfRangeException>(); | |
} | |
[Fact] | |
public void MapToEnum_FromString_WhenMadeCaseSensitive_ThrowsArgumentOutOfRangeExceptionOnFailedMatch() | |
{ | |
var original = "SUNDAY"; | |
Action mapAction = () => original.MapToEnum<DayOfWeek>(ignoreCase: false); | |
mapAction.Should().Throw<ArgumentOutOfRangeException>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment