From e852726b66521e28cde813183b1c37691128eb7c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 31 Mar 2023 18:53:08 +0100 Subject: [PATCH] test: 100% coverage on Enumerable and String methods in Text namespace --- X10D.Tests/src/Text/EnumerableTests.cs | 12 ++++ X10D.Tests/src/Text/StringTests.cs | 92 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/X10D.Tests/src/Text/EnumerableTests.cs b/X10D.Tests/src/Text/EnumerableTests.cs index f498f5cdb..879368285 100644 --- a/X10D.Tests/src/Text/EnumerableTests.cs +++ b/X10D.Tests/src/Text/EnumerableTests.cs @@ -19,6 +19,18 @@ public void Grep_ShouldFilterCorrectly_GivenPattern() CollectionAssert.AreEqual(expectedResult, actualResult); } + [TestMethod] + public void Grep_ShouldYieldNoResults_GivenEmptySource() + { + string[] source = Array.Empty(); + string[] expectedResult = Array.Empty(); + + const string pattern = /*lang=regex*/@"[0-9]+"; + string[] actualResult = source.Grep(pattern).ToArray(); + + CollectionAssert.AreEqual(expectedResult, actualResult); + } + [TestMethod] public void Grep_ShouldMatchUpperCase_GivenIgnoreCaseTrue() { diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index be148c680..e3e61d8ed 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -195,6 +195,22 @@ public void EnsureEndsWith_ShouldReturnString_GivenEndsWithReturnTrue() Assert.AreEqual(substring, substring.EnsureEndsWith(substring)); } + [TestMethod] + public void EnsureEndsWith_ShouldReturnString_GivenEmptySourceString() + { + const string substring = "World"; + + Assert.AreEqual(substring, string.Empty.EnsureEndsWith(substring)); + } + + [TestMethod] + public void EnsureEndsWith_ShouldReturnString_GivenEmptySubString() + { + const string substring = "World"; + + Assert.AreEqual(substring, substring.EnsureEndsWith(string.Empty)); + } + [TestMethod] public void EnsureStartsWith_ShouldAppendSubstring_GivenEndsWithReturnFalse() { @@ -212,6 +228,22 @@ public void EnsureStartsWith_ShouldReturnString_GivenEndsWithReturnTrue() Assert.AreEqual(substring, substring.EnsureStartsWith(substring)); } + [TestMethod] + public void EnsureStartsWith_ShouldReturnString_GivenEmptySourceString() + { + const string substring = "World"; + + Assert.AreEqual(substring, string.Empty.EnsureStartsWith(substring)); + } + + [TestMethod] + public void EnsureStartsWith_ShouldReturnString_GivenEmptySubString() + { + const string substring = "World"; + + Assert.AreEqual(substring, substring.EnsureStartsWith(string.Empty)); + } + [TestMethod] public void EnumParse_ShouldReturnCorrectValue_GivenString() { @@ -665,6 +697,66 @@ public void Split_ShouldThrow_GivenNullString() Assert.ThrowsException(() => value!.Split(0).ToArray()); } + [TestMethod] + public void StartsWithAny_ShouldReturnFalse_GivenNullInput() + { + string? value = null; + Assert.IsFalse(value.StartsWithAny()); + Assert.IsFalse(value.StartsWithAny(StringComparison.Ordinal)); + } + + [TestMethod] + public void StartsWithAny_ShouldReturnFalse_GivenEmptyInput() + { + Assert.IsFalse(string.Empty.StartsWithAny()); + Assert.IsFalse(string.Empty.StartsWithAny(StringComparison.Ordinal)); + } + + [TestMethod] + public void StartsWithAny_ShouldReturnFalse_GivenValuesThatDontMatch() + { + const string value = "Hello World"; + Assert.IsFalse(value.StartsWithAny("World", "Goodbye")); + Assert.IsFalse(value.StartsWithAny(StringComparison.Ordinal, "World", "Goodbye")); + } + + [TestMethod] + public void StartsWithAny_ShouldReturnTrue_GivenValuesThatMatch() + { + const string value = "Hello World"; + Assert.IsTrue(value.StartsWithAny("World", "Hello")); + Assert.IsTrue(value.StartsWithAny(StringComparison.Ordinal, "World", "Hello")); + } + + [TestMethod] + public void StartsWithAny_ShouldReturnTrue_GivenValuesThatMatch_WithIgnoreCaseComparison() + { + const string value = "Hello World"; + Assert.IsTrue(value.StartsWithAny(StringComparison.OrdinalIgnoreCase, "WORLD", "HELLO")); + } + + [TestMethod] + public void StartsWithAny_ShouldReturnFalse_GivenEmptyValues() + { + const string input = "Hello World"; + Assert.IsFalse(input.StartsWithAny()); + } + + [TestMethod] + public void StartsWithAny_ShouldThrowArgumentNullException_GivenNullValues() + { + Assert.ThrowsException(() => string.Empty.StartsWithAny(null!)); + Assert.ThrowsException(() => string.Empty.StartsWithAny(StringComparison.Ordinal, null!)); + } + + [TestMethod] + public void StartsWithAny_ShouldThrowArgumentNullException_GivenANullValue() + { + var values = new[] {"Hello", null!, "World"}; + Assert.ThrowsException(() => "Foobar".StartsWithAny(values)); + Assert.ThrowsException(() => "Foobar".StartsWithAny(StringComparison.Ordinal, values)); + } + [TestMethod] public void WithEmptyAlternative_ShouldBeCorrect() {