Skip to content

Commit 0eb0f5f

Browse files
Joshua-Lester3BenjaminMichaelis
authored andcommitted
Adjust for when key is possibly null due to ViewBag
1 parent e0b02ae commit 0eb0f5f

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

EssentialCSharp.Web.Tests/SiteMappingTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,16 @@ public void FindPercentComplete_KeyIsNull_ThrowsArgumentNullException()
8989
// Arrange
9090

9191
// Act
92+
string? percent = GetSiteMap().FindPercentComplete(null!);
9293

9394
// Assert
94-
Assert.Throws<ArgumentNullException>(() =>
95-
{
96-
GetSiteMap().FindPercentComplete(null!);
97-
});
95+
Assert.Null(percent);
9896
}
9997

10098
[Theory]
10199
[InlineData(" ")]
102100
[InlineData("")]
103-
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string key)
101+
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string? key)
104102
{
105103
// Arrange
106104

@@ -116,12 +114,12 @@ public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string k
116114
[Theory]
117115
[InlineData("hello-world", "50.00")]
118116
[InlineData("c-syntax-fundamentals", "100.00")]
119-
public void FindPercentComplete_ValidKey_Success(string key, string result)
117+
public void FindPercentComplete_ValidKey_Success(string? key, string result)
120118
{
121119
// Arrange
122120

123121
// Act
124-
string percent = GetSiteMap().FindPercentComplete(key);
122+
string? percent = GetSiteMap().FindPercentComplete(key);
125123

126124
// Assert
127125
Assert.Equal(result, percent);
@@ -134,7 +132,7 @@ public void FindPercentComplete_EmptySiteMappings_ReturnsZeroPercent()
134132
IList<SiteMapping> siteMappings = new List<SiteMapping>();
135133

136134
// Act
137-
string percent = siteMappings.FindPercentComplete("test");
135+
string? percent = siteMappings.FindPercentComplete("test");
138136

139137
// Assert
140138
Assert.Equal("0.00", percent);

EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ public static class SiteMappingListExtensions
3232
/// <param name="siteMappings">IList of SiteMappings</param>
3333
/// <param name="key">If null, uses the first key in the list</param>
3434
/// <returns>Returns a formatted double for use as the percent complete.</returns>
35-
public static string FindPercentComplete(this IList<SiteMapping> siteMappings, string key)
35+
public static string? FindPercentComplete(this IList<SiteMapping> siteMappings, string? key)
3636
{
37-
ArgumentNullException.ThrowIfNullOrWhiteSpace(key);
37+
if (key is null)
38+
{
39+
return null;
40+
}
41+
if (key.Trim().Length is 0)
42+
{
43+
throw new ArgumentException("Parameter key is whitespace or empty: ", nameof(key));
44+
}
3845
int currentMappingCount = 0;
3946
int overallMappingCount = 0;
4047
bool currentPageFound = false;

0 commit comments

Comments
 (0)