using System; using System.Linq; using System.Text.RegularExpressions; using NUnit.Framework; using Sarand.Net.Standard.Base.Utils; namespace Sarand.UnitTests.Core.Base.Utils { [TestFixture] public class StringUtil_Tester { // --- Data Members --- private Random random = new Random(); // --- Empty and Blank Values --- [Test] public void StringUtil_IsEmpty() { // Leading spaces. Assert.That(StringUtil.IsEmpty(null), Is.EqualTo(true)); Assert.That(StringUtil.IsEmpty(""), Is.EqualTo(true)); Assert.That(StringUtil.IsEmpty(" "), Is.EqualTo(false)); Assert.That(StringUtil.IsEmpty("A"), Is.EqualTo(false)); Assert.That(StringUtil.IsEmpty("Abc"), Is.EqualTo(false)); Assert.That("".IsEmpty(), Is.EqualTo(true)); Assert.That("Abc".IsEmpty(), Is.EqualTo(false)); } [Test] public void StringUtil_IsBlank() { // Leading spaces. Assert.That(StringUtil.IsBlank(null), Is.EqualTo(true)); Assert.That(StringUtil.IsBlank(""), Is.EqualTo(true)); Assert.That(StringUtil.IsBlank(" "), Is.EqualTo(true)); Assert.That(StringUtil.IsBlank(" "), Is.EqualTo(true)); Assert.That(StringUtil.IsBlank(" \n\t "), Is.EqualTo(true)); Assert.That(StringUtil.IsBlank("A"), Is.EqualTo(false)); Assert.That(StringUtil.IsBlank("Abc"), Is.EqualTo(false)); Assert.That("".IsBlank(), Is.EqualTo(true)); Assert.That(" ".IsBlank(), Is.EqualTo(true)); Assert.That("Abc".IsBlank(), Is.EqualTo(false)); } // --- Regular Expression Matching --- [Test] public void StringUtil_IsMatch() { // IsMatch. Assert.That("ABCDEF".IsMatch("ABCDEF"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch("^ABCDEF$"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatchCI("AbCdEf"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatchCI("^AbCdEf$"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch("^ABC"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatchCI("AbC"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch("CDE"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatchCI("cDe"), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch("CDE", System.Text.RegularExpressions.RegexOptions.None), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch(new Regex("CDE", System.Text.RegularExpressions.RegexOptions.None)), Is.EqualTo(true)); Assert.That("ABCDEF".IsMatch(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.IgnoreCase)), Is.EqualTo(true)); // IsMatch with escaped content. Assert.That("ABC\\sDEF".IsMatch("\\\\s"), Is.EqualTo(true)); Assert.That("AB(CD)EF".IsMatch("(..)"), Is.EqualTo(true)); Assert.That("AB(CD)EF".IsMatch("\\(CD\\)"), Is.EqualTo(true)); // Not match. Assert.That("ABCDEF".IsMatch("cDe"), Is.EqualTo(false)); Assert.That("ABCDEF".IsMatchCI("eFg"), Is.EqualTo(false)); Assert.That("ABCDEF".IsMatch("cDe", System.Text.RegularExpressions.RegexOptions.None), Is.EqualTo(false)); Assert.That("ABCDEF".IsMatch(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.None)), Is.EqualTo(false)); } [Test] public void StringUtil_Match() { // Match. Assert.That("ABCDEF".GetMatch("CDE"), Is.EqualTo("CDE")); Assert.That("ABCDEF".GetMatchCI("cDe"), Is.EqualTo("CDE")); Assert.That("ABCDEF".Match("CDE", System.Text.RegularExpressions.RegexOptions.None).Value, Is.EqualTo("CDE")); Assert.That("ABCDEF".Match(new Regex("CDE", System.Text.RegularExpressions.RegexOptions.None)).Value, Is.EqualTo("CDE")); Assert.That("ABCDEF".Match(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.IgnoreCase)).Value, Is.EqualTo("CDE")); // Match with escaped content. Assert.That("ABC DEF".GetMatch("\\s"), Is.EqualTo(" ")); Assert.That("ABC\\sDEF".GetMatch("\\\\s"), Is.EqualTo("\\s")); Assert.That("AB(CD)EF".GetMatch("(..)"), Is.EqualTo("AB")); Assert.That("AB(CD)EF".GetMatch("\\(..\\)"), Is.EqualTo("(CD)")); Assert.That("AB(CD)EF".GetMatch("\\(CD\\)"), Is.EqualTo("(CD)")); // Not match. Assert.That("ABCDEF".GetMatch("cDe"), Is.EqualTo(null)); Assert.That("ABCDEF".GetMatch("eFg"), Is.EqualTo(null)); Assert.That("ABCDEF".Match("cDe", System.Text.RegularExpressions.RegexOptions.None).Value, Is.Empty); Assert.That("ABCDEF".Match(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.None)).Value, Is.Empty); } [Test] public void StringUtil_Matches() { // Match. Assert.That("ABCDEF".GetMatches("CDE").Count, Is.EqualTo(1)); Assert.That("ABCDEF".GetMatchesCI("cDe").Count, Is.EqualTo(1)); Assert.That("ABCDEF".Matches("CDE", System.Text.RegularExpressions.RegexOptions.None).Count, Is.EqualTo(1)); Assert.That("ABCDEF".Matches(new Regex("CDE", System.Text.RegularExpressions.RegexOptions.None)).Count, Is.EqualTo(1)); Assert.That("ABCDEF".Matches(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.IgnoreCase)).Count, Is.EqualTo(1)); // Match with escaped content. Assert.That("ABC DEF".GetMatchesCI("\\s").Count, Is.EqualTo(1)); Assert.That("ABC\\sDEF".GetMatchesCI("\\\\s").Count, Is.EqualTo(1)); Assert.That("AB(CD)EF".GetMatchesCI("(..)").Count, Is.EqualTo(4)); Assert.That("AB(CD)EF".GetMatchesCI("(..)")[2], Is.EqualTo("D)")); Assert.That("AB(CD)EF".GetMatchesCI("\\(..\\)").Count, Is.EqualTo(1)); Assert.That("AB(CD)EF".GetMatchesCI("\\(CD\\)").Count, Is.EqualTo(1)); // Not match. Assert.That("ABCDEF".GetMatches("cDe").Count, Is.EqualTo(0)); Assert.That("ABCDEF".GetMatches("eFg").Count, Is.EqualTo(0)); Assert.That("ABCDEF".Matches("cDe", System.Text.RegularExpressions.RegexOptions.None).Count, Is.EqualTo(0)); Assert.That("ABCDEF".Matches(new Regex("cDe", System.Text.RegularExpressions.RegexOptions.None)).Count, Is.EqualTo(0)); } [Test] public void StringUtil_MatchCount() { // Match. Assert.That("ABCDEF".GetMatchCount("CDE"), Is.EqualTo(1)); Assert.That("ABCDEF".MatchesCount(new Regex("CDE")), Is.EqualTo(1)); Assert.That("ABCDEFGHICDEJKL".GetMatchCount("CDE"), Is.EqualTo(2)); Assert.That("ABCDEFGHICDEJKL".MatchesCount(new Regex("CDE")), Is.EqualTo(2)); } // --- Padding --- [Test] public void StringUtil_PadLeft() { // Leading spaces. Assert.That("".PadLeft(3, ' '), Is.EqualTo(" ")); Assert.That("A".PadLeft(3, ' '), Is.EqualTo(" A")); Assert.That("AB".PadLeft(3, ' '), Is.EqualTo(" AB")); Assert.That("ABC".PadLeft(3, ' '), Is.EqualTo("ABC")); Assert.That("ABCD".PadLeft(3, ' '), Is.EqualTo("ABCD")); // Leading zeroes. Assert.That("".PadLeft(2, '0'), Is.EqualTo("00")); Assert.That("1".PadLeft(2, '0'), Is.EqualTo("01")); Assert.That("123".PadLeft(3, '0'), Is.EqualTo("123")); Assert.That("123".PadLeft(4, '0'), Is.EqualTo("0123")); Assert.That("123".PadLeft(5, '0'), Is.EqualTo("00123")); } [Test] public void StringUtil_PadRight() { // Leading spaces. Assert.That("".PadRight(3, ' '), Is.EqualTo(" ")); Assert.That("A".PadRight(3, ' '), Is.EqualTo("A ")); Assert.That("AB".PadRight(3, ' '), Is.EqualTo("AB ")); Assert.That("ABC".PadRight(3, ' '), Is.EqualTo("ABC")); Assert.That("ABCD".PadRight(3, ' '), Is.EqualTo("ABCD")); // Trailing zeroes. Assert.That("".PadRight(2, '0'), Is.EqualTo("00")); Assert.That("1".PadRight(2, '0'), Is.EqualTo("10")); Assert.That("12".PadRight(2, '0'), Is.EqualTo("12")); Assert.That("123".PadRight(3, '0'), Is.EqualTo("123")); Assert.That("123".PadRight(4, '0'), Is.EqualTo("1230")); Assert.That("123".PadRight(5, '0'), Is.EqualTo("12300")); } // --- Safe Substrings --- [Test] public void StringUtil_SubstringSafe() { Assert.That(StringUtil.SubstringSafe(null, 0), Is.Empty); Assert.That(StringUtil.SubstringSafe("", 0), Is.Empty); Assert.That("".SubstringSafe(0), Is.Empty); // Right portion from start position. Assert.That("abc".SubstringSafe(-3), Is.EqualTo("abc")); Assert.That("abc".SubstringSafe(-2), Is.EqualTo("abc")); Assert.That("abc".SubstringSafe(-1), Is.EqualTo("abc")); Assert.That("abc".SubstringSafe(0), Is.EqualTo("abc")); Assert.That("abc".SubstringSafe(1), Is.EqualTo("bc")); Assert.That("abc".SubstringSafe(2), Is.EqualTo("c")); Assert.That("abc".SubstringSafe(3), Is.Empty); // Zero length. Assert.That(StringUtil.SubstringSafe(null, 0, 0), Is.Empty); Assert.That(StringUtil.SubstringSafe("", 0, 0), Is.Empty); Assert.That("".SubstringSafe(0, 0), Is.Empty); Assert.That("abc".SubstringSafe(-1, 0), Is.Empty); Assert.That("abc".SubstringSafe(0, 0), Is.Empty); Assert.That("abc".SubstringSafe(1, 0), Is.Empty); Assert.That("abc".SubstringSafe(2, 0), Is.Empty); Assert.That("abc".SubstringSafe(3, 0), Is.Empty); Assert.That("abc".SubstringSafe(4, 0), Is.Empty); // Mid portion. Assert.That("abcde".SubstringSafe(0, 3), Is.EqualTo("abc")); Assert.That("abcde".SubstringSafe(1, 3), Is.EqualTo("bcd")); Assert.That("abcde".SubstringSafe(2, 3), Is.EqualTo("cde")); // Excessive length. Assert.That("abcde".SubstringSafe(-1, 12), Is.EqualTo("abcde")); Assert.That("abcde".SubstringSafe(0, 12), Is.EqualTo("abcde")); Assert.That("abcde".SubstringSafe(1, 12), Is.EqualTo("bcde")); Assert.That("abcde".SubstringSafe(2, 12), Is.EqualTo("cde")); Assert.That("abcde".SubstringSafe(3, 12), Is.EqualTo("de")); Assert.That("abcde".SubstringSafe(4, 12), Is.EqualTo("e")); Assert.That("abcde".SubstringSafe(5, 12), Is.Empty); } // --- The End --- } }