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

Reverted PR 412 #518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 8 additions & 16 deletions src/SmartEnum/SmartEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public abstract class SmartEnum<TEnum, TValue> :
var dictionary = new Dictionary<TValue, TEnum>(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;
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -216,22 +218,12 @@ public static bool TryFromName(string name, bool ignoreCase, out TEnum result)
/// <seealso cref="SmartEnum{TEnum, TValue}.TryFromValue(TValue, out TEnum)"/>
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<TEnum, TValue>(value);
}
}
else
if (!_fromValue.Value.TryGetValue(value, out var result))
{
result = _enumOptions.Value.FirstOrDefault(x => x.Value == null);
if (result == null)
{
ThrowHelper.ThrowValueNotFoundException<TEnum, TValue>(value);
}
ThrowHelper.ThrowValueNotFoundException<TEnum, TValue>(value);
}
return result;
}
Expand Down
12 changes: 2 additions & 10 deletions test/SmartEnum.UnitTests/SmartEnumStringFromValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 0 additions & 10 deletions test/SmartEnum.UnitTests/TestEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,12 @@ public class TestStringEnum : SmartEnum<TestStringEnum, string>
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<TestNullableStringEnum, string>
{
public static readonly TestNullableStringEnum None = new TestNullableStringEnum(nameof(None), null);

protected TestNullableStringEnum(string name, string value) : base(name, value)
{
}
}

public class TestBaseEnumWithDerivedValues : SmartEnum<TestBaseEnumWithDerivedValues>
{
protected TestBaseEnumWithDerivedValues(string name, int value) : base(name, value)
Expand Down