Thanks!
Here is the code itself:
using System;
namespace UtilityLibraries
{
public static class StringLibrary
{
public static bool StartsWithUpper(this string s)
{
if (String.IsNullOrWhiteSpace(s))
return false;
return Char.IsUpper(s[0]);
}
public static bool StartsWithLower(this string s)
{
if (String.IsNullOrWhiteSpace(s))
return false;
return Char.IsLower(s[0]);
}
public static bool HasEmbeddedSpaces(this string s)
{
foreach (var ch in s.Trim())
{
if (ch == ' ')
return true;
}
return false;
}
}
}
And the testing code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UtilityLibraries;
namespace StringLibraryTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestStartsWithUpper()
{
// Tests that we expect to return true.
string[] words = { "Alphabet", "Zebra", "ABC", "Αθήνα", "Москва" };
foreach (var word in words)
{
bool result = word.StartsWithUpper();
Assert.IsTrue(result,
$"Expected for '{word}': true; Actual: {result}");
}
}
[TestMethod]
public void TestDoesNotStartWithUpper()
{
// Tests that we expect to return false.
string[] words = { "alphabet", "zebra", "abc", "αυτοκινητοβιομηχανία",
"государство",
"1234", ".", ";", " " };
foreach (var word in words)
{
bool result = word.StartsWithUpper();
Assert.IsFalse(result,
$"Expected for '{word}': false; Actual: {result}");
}
}
[TestMethod]
public void DirectCallWithNullOrEmpty()
{
// Tests that we expect to return false.
string[] words = { String.Empty, null };
foreach (var word in words)
{
bool result = StringLibrary.StartsWithUpper(word);
Assert.IsFalse(result,
$"Expected for '{(word == null ? "<null>" : word)}': " +
$"false; Actual: {result}");
}
}
}
}