test: 100% coverage on Enumerable and String methods in Text namespace

This commit is contained in:
Oliver Booth 2023-03-31 18:53:08 +01:00
parent 369882c1e8
commit e852726b66
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 104 additions and 0 deletions

View File

@ -19,6 +19,18 @@ public class EnumerableTests
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()
{

View File

@ -195,6 +195,22 @@ public class StringTests
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 class StringTests
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 class StringTests
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()
{