Skip to content

Commit fc348af

Browse files
strandloperardalis
authored andcommitted
Included TEnum type name to make not found exception messages more descriptive (#6)
* Added .editorconfig for indent consistency * Removed brace escaping from exception message * Added TEnum name to not found exception message
1 parent bb73e2d commit fc348af

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[*.cs]
2+
indent_style = space
3+
indent_size = 4

src/SmartEnum.UnitTests/SmartEnumFromName.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,24 @@ public void ThrowsGivenNonMatchingString()
4343
{
4444
Assert.Throws<SmartEnumNotFoundException>(() => TestEnum.FromName("Doesn't Exist"));
4545
}
46+
47+
[Fact]
48+
public void ThrowsWithExpectedMessageGivenNonMatchingString()
49+
{
50+
string name = "Doesn't Exist";
51+
string expected = $"No TestEnum with Name \"{name}\" found.";
52+
string actual = "";
53+
54+
try
55+
{
56+
var testEnum = TestEnum.FromName(name);
57+
}
58+
catch (SmartEnumNotFoundException ex)
59+
{
60+
actual = ex.Message;
61+
}
62+
63+
Assert.Equal(expected, actual);
64+
}
4665
}
4766
}

src/SmartEnum.UnitTests/SmartEnumFromValue.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ public void ThrowsGivenNonMatchingValue()
1717
Assert.Throws<SmartEnumNotFoundException>(() => TestEnum.FromValue(-1));
1818
}
1919

20+
[Fact]
21+
public void ThrowsWithExpectedMessageGivenNonMatchingValue()
22+
{
23+
string expected = $"No TestEnum with Value -1 found.";
24+
string actual = "";
25+
26+
try
27+
{
28+
var testEnum = TestEnum.FromValue(-1);
29+
}
30+
catch (SmartEnumNotFoundException ex)
31+
{
32+
actual = ex.Message;
33+
}
34+
35+
Assert.Equal(expected, actual);
36+
2037
[Fact]
2138
public void ReturnsDefaultEnumGivenNonMatchingValue()
2239
{

src/SmartEnum/SmartEnum.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static TEnum FromName(string name)
4545
var result = List.FirstOrDefault(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase));
4646
if (result == null)
4747
{
48-
throw new SmartEnumNotFoundException($"No option with Name \"{name}\" found.");
48+
throw new SmartEnumNotFoundException($"No {typeof(TEnum).Name} with Name \"{name}\" found.");
4949
}
5050
return result;
5151
}
@@ -57,7 +57,7 @@ public static TEnum FromValue(TValue value)
5757
var result = List.FirstOrDefault(item => EqualityComparer<TValue>.Default.Equals(item.Value, value));
5858
if (result == null)
5959
{
60-
throw new SmartEnumNotFoundException($"No option with Value {{value}} found.");
60+
throw new SmartEnumNotFoundException($"No {typeof(TEnum).Name} with Value {value} found.");
6161
}
6262
return result;
6363
}

0 commit comments

Comments
 (0)