-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added helper functions to handle null reference warnings
- Loading branch information
Marthijn van den Heuvel
committed
Jul 22, 2024
1 parent
4fe3404
commit f9f786a
Showing
8 changed files
with
214 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Sidio.Sitemap.AspNetCore.Tests/SitemapExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Sidio.Sitemap.AspNetCore.Tests; | ||
|
||
public sealed class SitemapExtensionsTests | ||
{ | ||
[Fact] | ||
public void TryAdd_WhenUrlIsValid_SitemapNodeAdded() | ||
{ | ||
// arrange | ||
var sitemap = new Core.Sitemap(); | ||
const string Url = "/index.html"; | ||
|
||
// act | ||
var result = sitemap.TryAdd(Url); | ||
|
||
// assert | ||
result.Should().BeTrue(); | ||
sitemap.Nodes.Count.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void TryAdd_WhenUrlIsEmpty_SitemapNodeNotAdded() | ||
{ | ||
// arrange | ||
var sitemap = new Core.Sitemap(); | ||
|
||
// act | ||
var result = sitemap.TryAdd(string.Empty); | ||
|
||
// assert | ||
result.Should().BeFalse(); | ||
sitemap.Nodes.Should().BeEmpty(); | ||
} | ||
|
||
[Fact] | ||
public void TryAdd_WhenUrlsAreValid_SitemapNodesAdded() | ||
{ | ||
// arrange | ||
var sitemap = new Core.Sitemap(); | ||
const string Url = "/index.html"; | ||
|
||
// act | ||
var result = sitemap.TryAdd(Url, Url, Url); | ||
|
||
// assert | ||
result.Should().Be(3); | ||
sitemap.Nodes.Count.Should().Be(3); | ||
} | ||
|
||
[Fact] | ||
public void TryAdd_WhenSomeUrlsAreEmpty_SitemapNodeAdded() | ||
{ | ||
// arrange | ||
var sitemap = new Core.Sitemap(); | ||
const string Url = "/index.html"; | ||
|
||
// act | ||
var result = sitemap.TryAdd(Url, string.Empty, Url, " "); | ||
|
||
// assert | ||
result.Should().Be(2); | ||
sitemap.Nodes.Count.Should().Be(2); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Sidio.Sitemap.AspNetCore.Tests/SitemapIndexExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Sidio.Sitemap.Core; | ||
|
||
namespace Sidio.Sitemap.AspNetCore.Tests; | ||
|
||
public sealed class SitemapIndexExtensionsTests | ||
{ | ||
[Fact] | ||
public void TryAdd_WhenUrlIsValid_SitemapIndexNodeAdded() | ||
{ | ||
// arrange | ||
var sitemapIndex = new SitemapIndex(); | ||
const string Url = "/index.html"; | ||
|
||
// act | ||
var result = sitemapIndex.TryAdd(Url); | ||
|
||
// assert | ||
result.Should().BeTrue(); | ||
sitemapIndex.Nodes.Count.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public void TryAdd_WhenUrlIsEmpty_SitemapIndexNodeNotAdded() | ||
{ | ||
// arrange | ||
var sitemapIndex = new SitemapIndex(); | ||
|
||
// act | ||
var result = sitemapIndex.TryAdd(string.Empty); | ||
|
||
// assert | ||
result.Should().BeFalse(); | ||
sitemapIndex.Nodes.Should().BeEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Diagnostics; | ||
using Sidio.Sitemap.Core; | ||
|
||
namespace Sidio.Sitemap.AspNetCore; | ||
|
||
/// <summary> | ||
/// The Sitemap extensions. | ||
/// </summary> | ||
public static class SitemapExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a nullable URL to the sitemap. | ||
/// </summary> | ||
/// <param name="sitemap">The sitemap.</param> | ||
/// <param name="url">The URL.</param> | ||
/// <returns>Returns <c>true</c> when the url was added.</returns> | ||
public static bool TryAdd(this Core.Sitemap sitemap, string? url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
{ | ||
return false; | ||
} | ||
|
||
return sitemap.Add(new SitemapNode(url)) == 1; | ||
} | ||
|
||
/// <summary> | ||
/// Adds a range of nullable URLs to the sitemap. | ||
/// </summary> | ||
/// <param name="sitemap">The sitemap.</param> | ||
/// <param name="urls">The urls.</param> | ||
/// <returns>Returns actual the number of nodes that were added.</returns> | ||
public static int TryAdd(this Core.Sitemap sitemap, params string?[] urls) | ||
{ | ||
var nonNullableUrls = urls.Where(x => !string.IsNullOrWhiteSpace(x)) | ||
.Select(x => new SitemapNode(x ?? throw new UnreachableException())).Cast<ISitemapNode>().ToArray(); | ||
|
||
return nonNullableUrls.Length > 0 ? sitemap.Add(nonNullableUrls) : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Sidio.Sitemap.Core; | ||
|
||
namespace Sidio.Sitemap.AspNetCore; | ||
|
||
/// <summary> | ||
/// The Sitemap-index extensions. | ||
/// </summary> | ||
public static class SitemapIndexExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a nullable URL to the sitemap index. | ||
/// </summary> | ||
/// <param name="sitemapIndex">The sitemap index.</param> | ||
/// <param name="url">The URL.</param> | ||
/// <returns>Returns <c>true</c> when the url was added.</returns> | ||
public static bool TryAdd(this SitemapIndex sitemapIndex, string? url) | ||
{ | ||
if (string.IsNullOrWhiteSpace(url)) | ||
{ | ||
return false; | ||
} | ||
|
||
return sitemapIndex.Add(new SitemapIndexNode(url)) == 1; | ||
} | ||
} |