diff --git a/src/SmartEnum/SmartEnum.cs b/src/SmartEnum/SmartEnum.cs index f1a5f012..9ce44548 100644 --- a/src/SmartEnum/SmartEnum.cs +++ b/src/SmartEnum/SmartEnum.cs @@ -60,8 +60,7 @@ public abstract class SmartEnum : var dictionary = new Dictionary(GetValueComparer()); foreach (var item in _enumOptions.Value) { - if (item._value != null && !dictionary.ContainsKey(item._value)) - dictionary.Add(item._value, item); + dictionary.TryAdd(item._value, item); } return dictionary; }); @@ -106,6 +105,9 @@ protected SmartEnum(string name, TValue value) if (String.IsNullOrEmpty(name)) ThrowHelper.ThrowArgumentNullOrEmptyException(nameof(name)); + if (value is null) + ThrowHelper.ThrowArgumentNullException(nameof(value)); + _name = name; _value = value; } @@ -216,22 +218,12 @@ public static bool TryFromName(string name, bool ignoreCase, out TEnum result) /// public static TEnum FromValue(TValue value) { - TEnum result; + if (value is null) + ThrowHelper.ThrowArgumentNullException(nameof(value)); - if (value != null) - { - if (!_fromValue.Value.TryGetValue(value, out result)) - { - ThrowHelper.ThrowValueNotFoundException(value); - } - } - else + if (!_fromValue.Value.TryGetValue(value, out var result)) { - result = _enumOptions.Value.FirstOrDefault(x => x.Value == null); - if (result == null) - { - ThrowHelper.ThrowValueNotFoundException(value); - } + ThrowHelper.ThrowValueNotFoundException(value); } return result; } diff --git a/test/SmartEnum.UnitTests/SmartEnumStringFromValue.cs b/test/SmartEnum.UnitTests/SmartEnumStringFromValue.cs index e82c8b59..7ce45063 100644 --- a/test/SmartEnum.UnitTests/SmartEnumStringFromValue.cs +++ b/test/SmartEnum.UnitTests/SmartEnumStringFromValue.cs @@ -23,18 +23,10 @@ public void ReturnsEnumGivenMatchingValue(string value, TestStringEnum expected) } [Fact] - public void ReturnsEnumGivenMatchingNullValue() + public void ThrowsGivenNonMatchingValue() { - var result = TestNullableStringEnum.FromValue(null); + var value = string.Empty; - result.Should().BeSameAs(TestNullableStringEnum.None); - } - - [Theory] - [InlineData("invalid")] - [InlineData(null)] - public void ThrowsGivenNonMatchingValue(string value) - { Action action = () => TestStringEnum.FromValue(value); action.Should() diff --git a/test/SmartEnum.UnitTests/TestEnum.cs b/test/SmartEnum.UnitTests/TestEnum.cs index 95a02b5c..1735f15f 100644 --- a/test/SmartEnum.UnitTests/TestEnum.cs +++ b/test/SmartEnum.UnitTests/TestEnum.cs @@ -46,22 +46,12 @@ public class TestStringEnum : SmartEnum public static readonly TestStringEnum One = new TestStringEnum(nameof(One), nameof(One)); public static readonly TestStringEnum Two = new TestStringEnum(nameof(Two), nameof(Two)); public static readonly TestStringEnum Three = new TestStringEnum(nameof(Three), nameof(Three)); - public static readonly TestStringEnum Empty = new TestStringEnum(nameof(Empty), string.Empty); protected TestStringEnum(string name, string value) : base(name, value) { } } - public class TestNullableStringEnum : SmartEnum - { - public static readonly TestNullableStringEnum None = new TestNullableStringEnum(nameof(None), null); - - protected TestNullableStringEnum(string name, string value) : base(name, value) - { - } - } - public class TestBaseEnumWithDerivedValues : SmartEnum { protected TestBaseEnumWithDerivedValues(string name, int value) : base(name, value)