Skip to content

Commit c23fe65

Browse files
Adjust for when key is possibly null due to ViewBag
1 parent 875c1c5 commit c23fe65

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
@@ -85,18 +85,16 @@ public void FindPercentComplete_KeyIsNull_ThrowsArgumentNullException()
8585
// Arrange
8686

8787
// Act
88+
string? percent = GetSiteMap().FindPercentComplete(null!);
8889

8990
// Assert
90-
Assert.Throws<ArgumentNullException>(() =>
91-
{
92-
GetSiteMap().FindPercentComplete(null!);
93-
});
91+
Assert.Null(percent);
9492
}
9593

9694
[Theory]
9795
[InlineData(" ")]
9896
[InlineData("")]
99-
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string key)
97+
public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string? key)
10098
{
10199
// Arrange
102100

@@ -112,12 +110,12 @@ public void FindPercentComplete_KeyIsWhiteSpace_ThrowsArgumentException(string k
112110
[Theory]
113111
[InlineData("hello-world", "50.00")]
114112
[InlineData("c-syntax-fundamentals", "100.00")]
115-
public void FindPercentComplete_ValidKey_Success(string key, string result)
113+
public void FindPercentComplete_ValidKey_Success(string? key, string result)
116114
{
117115
// Arrange
118116

119117
// Act
120-
string percent = GetSiteMap().FindPercentComplete(key);
118+
string? percent = GetSiteMap().FindPercentComplete(key);
121119

122120
// Assert
123121
Assert.Equal(result, percent);
@@ -130,7 +128,7 @@ public void FindPercentComplete_EmptySiteMappings_ReturnsZeroPercent()
130128
IList<SiteMapping> siteMappings = new List<SiteMapping>();
131129

132130
// Act
133-
string percent = siteMappings.FindPercentComplete("test");
131+
string? percent = siteMappings.FindPercentComplete("test");
134132

135133
// Assert
136134
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)