2021-07-19 11:48:58 +00:00
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-04-19 15:16:28 +00:00
|
|
|
|
2021-07-19 11:48:58 +00:00
|
|
|
namespace X10D.Tests.Core
|
|
|
|
{
|
2020-04-19 15:16:28 +00:00
|
|
|
/// <summary>
|
2020-07-15 13:32:59 +00:00
|
|
|
/// Tests for <see cref="StringExtensions" />.
|
2020-04-19 15:16:28 +00:00
|
|
|
/// </summary>
|
|
|
|
[TestClass]
|
|
|
|
public class StringTests
|
|
|
|
{
|
2020-10-20 14:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tests for <see cref="StringExtensions.AsNullIfEmpty" />.
|
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void AsNullIfEmpty()
|
|
|
|
{
|
|
|
|
Assert.AreEqual(null, string.Empty.AsNullIfEmpty());
|
|
|
|
Assert.AreEqual(null, ((string)null).AsNullIfEmpty());
|
|
|
|
Assert.AreEqual(" ", " ".AsNullIfEmpty());
|
|
|
|
Assert.AreEqual("foo", "foo".AsNullIfEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Tests for <see cref="StringExtensions.AsNullIfWhiteSpace" />.
|
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void AsNullIfWhiteSpace()
|
|
|
|
{
|
|
|
|
Assert.AreEqual(null, string.Empty.AsNullIfWhiteSpace());
|
|
|
|
Assert.AreEqual(null, ((string)null).AsNullIfWhiteSpace());
|
|
|
|
Assert.AreEqual(null, " ".AsNullIfWhiteSpace());
|
|
|
|
Assert.AreEqual("foo", "foo".AsNullIfWhiteSpace());
|
|
|
|
}
|
|
|
|
|
2020-04-19 15:16:28 +00:00
|
|
|
/// <summary>
|
2020-07-15 13:32:59 +00:00
|
|
|
/// Tests for <see cref="StringExtensions.Repeat" />.
|
2020-04-19 15:16:28 +00:00
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void Repeat()
|
|
|
|
{
|
|
|
|
Assert.AreEqual("foofoofoofoofoo", "foo".Repeat(5));
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:08:20 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tests for <see cref="StringExtensions.Reverse" />.
|
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void Reverse()
|
|
|
|
{
|
|
|
|
Assert.AreEqual("dlroW olleH", StringExtensions.Reverse("Hello World"));
|
|
|
|
Assert.AreEqual("Foobar", StringExtensions.Reverse("rabooF"));
|
|
|
|
}
|
|
|
|
|
2020-04-19 15:16:28 +00:00
|
|
|
/// <summary>
|
2020-07-15 13:32:59 +00:00
|
|
|
/// Tests for <see cref="StringExtensions.Split" />.
|
2020-04-19 15:16:28 +00:00
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void Split()
|
|
|
|
{
|
|
|
|
const string str = "Hello World";
|
|
|
|
|
|
|
|
// ReSharper disable once SuggestVarOrType_Elsewhere
|
|
|
|
var arr = str.Split(2).ToArray();
|
|
|
|
CollectionAssert.AreEqual(new[] { "He", "ll", "o ", "Wo", "rl", "d" }, arr);
|
|
|
|
}
|
2020-10-20 14:08:20 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-03-06 17:36:58 +00:00
|
|
|
/// Tests <see cref="StringExtensions.WithEmptyAlternative" /> and
|
|
|
|
/// <see cref="StringExtensions.WithWhiteSpaceAlternative"/>.
|
2020-10-20 14:08:20 +00:00
|
|
|
/// </summary>
|
|
|
|
[TestMethod]
|
|
|
|
public void WithAlternative()
|
|
|
|
{
|
2022-03-06 17:36:58 +00:00
|
|
|
const string inputA = "Hello World";
|
|
|
|
const string inputB = " ";
|
|
|
|
const string inputC = "";
|
|
|
|
const string? inputD = null;
|
|
|
|
const string alternative = "ALTERNATIVE";
|
|
|
|
|
|
|
|
string resultA = inputA.WithEmptyAlternative(alternative);
|
|
|
|
string resultB = inputB.WithEmptyAlternative(alternative);
|
|
|
|
string resultBWithWhitespace = inputB.WithWhiteSpaceAlternative(alternative);
|
|
|
|
string resultC = inputC.WithEmptyAlternative(alternative);
|
|
|
|
string resultD = inputD.WithEmptyAlternative(alternative);
|
|
|
|
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null).WithEmptyAlternative(null!));
|
|
|
|
|
|
|
|
Assert.AreEqual(resultA, inputA);
|
|
|
|
Assert.AreEqual(resultB, inputB);
|
|
|
|
Assert.AreEqual(resultBWithWhitespace, alternative);
|
|
|
|
Assert.AreEqual(resultC, alternative);
|
|
|
|
Assert.AreEqual(resultD, alternative);
|
2020-10-20 14:08:20 +00:00
|
|
|
}
|
2020-04-19 15:16:28 +00:00
|
|
|
}
|
|
|
|
}
|