From c69e13cb3bef81c9ea9a0f1da961cf2f571aed1c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 29 Sep 2026 05:57:58 +0000 Subject: [PATCH] Improve site mapping argument validation Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- EssentialCSharp.Web.Tests/SiteMappingTests.cs | 14 ++++++++++++++ .../Extensions/SiteMappingListExtensions.cs | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/EssentialCSharp.Web.Tests/SiteMappingTests.cs b/EssentialCSharp.Web.Tests/SiteMappingTests.cs index 91b4e7d9..457b2e9d 100644 --- a/EssentialCSharp.Web.Tests/SiteMappingTests.cs +++ b/EssentialCSharp.Web.Tests/SiteMappingTests.cs @@ -59,6 +59,13 @@ public async Task FindHelloWorldWithAnchorSlugReturnsCorrectSiteMap() await Assert.That(foundSiteMap).IsEquivalentTo(HelloWorldSiteMapping); } + [Test] + public async Task Find_NullSiteMappings_ThrowsArgumentNullException() + { + await Assert.That(() => ((IList)null!).Find("hello-world")) + .Throws(); + } + [Test] public async Task FindCSyntaxFundamentalsWithSpacesReturnsCorrectSiteMap() { @@ -95,6 +102,13 @@ public async Task FindPercentComplete_KeyIsNull_ReturnsNull() await Assert.That(percent).IsNull(); } + [Test] + public async Task FindPercentComplete_NullSiteMappings_ThrowsArgumentNullException() + { + await Assert.That(() => ((IList)null!).FindPercentComplete("hello-world")) + .Throws(); + } + [Test] [Arguments(" ")] [Arguments("")] diff --git a/EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs b/EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs index 3e50d4e5..90ba2e37 100644 --- a/EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs +++ b/EssentialCSharp.Web/Extensions/SiteMappingListExtensions.cs @@ -12,6 +12,8 @@ public static class SiteMappingListExtensions /// If found, the site mapping that matches the key, otherwise null. public static SiteMapping? Find(this IList siteMappings, string? key) { + ArgumentNullException.ThrowIfNull(siteMappings); + if (string.IsNullOrWhiteSpace(key)) { return siteMappings.FirstOrDefault(); @@ -28,6 +30,8 @@ public static class SiteMappingListExtensions /// Returns a formatted double for use as the percent complete. public static string? FindPercentComplete(this IList siteMappings, string? key) { + ArgumentNullException.ThrowIfNull(siteMappings); + if (key is null) { return null;