Skip to content

Commit 402ee6a

Browse files
Guarantees zero percent on missing sitemap key
Ensures that the FindPercentComplete method returns "0.00" when a key is not found in the sitemap, preventing potential incorrect calculations or errors.
1 parent 24d602a commit 402ee6a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

EssentialCSharp.Web.Tests/SiteMappingTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,16 @@ public void FindPercentComplete_EmptySiteMappings_ReturnsZeroPercent()
137137
// Assert
138138
Assert.Equal("0.00", percent);
139139
}
140+
141+
[Fact]
142+
public void FindPercentComplete_KeyNotFound_ReturnsZeroPercent()
143+
{
144+
// Arrange
145+
146+
// Act
147+
string? percent = GetSiteMap().FindPercentComplete("non-existent-key");
148+
149+
// Assert
150+
Assert.Equal("0.00", percent);
151+
}
140152
}

EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ public static class SiteMappingListExtensions
6464
}
6565
}
6666
}
67-
if (overallMappingCount is 0)
67+
if (overallMappingCount is 0 || !currentPageFound)
6868
{
6969
return "0.00";
7070
}
71+
7172
double result = (double)currentMappingCount / overallMappingCount * 100;
7273
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
7374
}

0 commit comments

Comments
 (0)