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

Included TEnum type name to make not found exception messages more descriptive #6

Merged
merged 5 commits into from
Dec 11, 2017
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
3 changes: 3 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.cs]
indent_style = space
indent_size = 4
19 changes: 19 additions & 0 deletions src/SmartEnum.UnitTests/SmartEnumFromName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,24 @@ public void ThrowsGivenNonMatchingString()
{
Assert.Throws<SmartEnumNotFoundException>(() => TestEnum.FromName("Doesn't Exist"));
}

[Fact]
public void ThrowsWithExpectedMessageGivenNonMatchingString()
{
string name = "Doesn't Exist";
string expected = $"No TestEnum with Name \"{name}\" found.";
string actual = "";

try
{
var testEnum = TestEnum.FromName(name);
}
catch (SmartEnumNotFoundException ex)
{
actual = ex.Message;
}

Assert.Equal(expected, actual);
}
}
}
17 changes: 17 additions & 0 deletions src/SmartEnum.UnitTests/SmartEnumFromValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ public void ThrowsGivenNonMatchingValue()
Assert.Throws<SmartEnumNotFoundException>(() => TestEnum.FromValue(-1));
}

[Fact]
public void ThrowsWithExpectedMessageGivenNonMatchingValue()
{
string expected = $"No TestEnum with Value -1 found.";
string actual = "";

try
{
var testEnum = TestEnum.FromValue(-1);
}
catch (SmartEnumNotFoundException ex)
{
actual = ex.Message;
}

Assert.Equal(expected, actual);

[Fact]
public void ReturnsDefaultEnumGivenNonMatchingValue()
{
Expand Down
4 changes: 2 additions & 2 deletions src/SmartEnum/SmartEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static TEnum FromName(string name)
var result = List.FirstOrDefault(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase));
if (result == null)
{
throw new SmartEnumNotFoundException($"No option with Name \"{name}\" found.");
throw new SmartEnumNotFoundException($"No {typeof(TEnum).Name} with Name \"{name}\" found.");
}
return result;
}
Expand All @@ -57,7 +57,7 @@ public static TEnum FromValue(TValue value)
var result = List.FirstOrDefault(item => EqualityComparer<TValue>.Default.Equals(item.Value, value));
if (result == null)
{
throw new SmartEnumNotFoundException($"No option with Value {{value}} found.");
throw new SmartEnumNotFoundException($"No {typeof(TEnum).Name} with Value {value} found.");
}
return result;
}
Expand Down