Skip to content

Commit

Permalink
test: 100% coverage on Enumerable and String methods in Text namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Mar 31, 2023
1 parent 369882c commit e852726
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
12 changes: 12 additions & 0 deletions X10D.Tests/src/Text/EnumerableTests.cs
Expand Up @@ -19,6 +19,18 @@ public void Grep_ShouldFilterCorrectly_GivenPattern()
CollectionAssert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
public void Grep_ShouldYieldNoResults_GivenEmptySource()
{
string[] source = Array.Empty<string>();
string[] expectedResult = Array.Empty<string>();

const string pattern = /*lang=regex*/@"[0-9]+";
string[] actualResult = source.Grep(pattern).ToArray();

CollectionAssert.AreEqual(expectedResult, actualResult);
}

[TestMethod]
public void Grep_ShouldMatchUpperCase_GivenIgnoreCaseTrue()
{
Expand Down
92 changes: 92 additions & 0 deletions X10D.Tests/src/Text/StringTests.cs
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -665,6 +697,66 @@ public void Split_ShouldThrow_GivenNullString()
Assert.ThrowsException<ArgumentNullException>(() => 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<ArgumentNullException>(() => string.Empty.StartsWithAny(null!));
Assert.ThrowsException<ArgumentNullException>(() => string.Empty.StartsWithAny(StringComparison.Ordinal, null!));
}

[TestMethod]
public void StartsWithAny_ShouldThrowArgumentNullException_GivenANullValue()
{
var values = new[] {"Hello", null!, "World"};
Assert.ThrowsException<ArgumentNullException>(() => "Foobar".StartsWithAny(values));
Assert.ThrowsException<ArgumentNullException>(() => "Foobar".StartsWithAny(StringComparison.Ordinal, values));
}

[TestMethod]
public void WithEmptyAlternative_ShouldBeCorrect()
{
Expand Down

0 comments on commit e852726

Please sign in to comment.