//===============================================================================
//
// StringsTests
//
// PURPOSE:
// Tests the functionality of Strings.
//
// NOTES:
//
//
//===============================================================================
//
// Copyright (C) 2003 Wallis Software Solutions
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//
//===============================================================================
using NUnit.Framework;
namespace Agile.Common.Tests
{
/// <summary>
/// Tests the functionality of Strings.
/// </summary>
[TestFixture]
public class StringsTests
{
/// <summary>
/// Verifies the functionality of the Contains fucntion
/// - Check, search for 'gi' in Agile returns true
/// - Check, search for 'o' in Agile returns false
/// - Check, search for 'DeF' in 'abc def ghi' returns true
/// </summary>
[Test]
public void ContainsTests()
{
// Check, search for 'gi' in Agile returns true
Assert.IsTrue(Strings.Contains("Agile", "gi"), "Check results contains 'gi'");
// Check, search for 'o' in Agile returns false
Assert.IsFalse(Strings.Contains("Agile", "o"), "Check results contains 'o'");
// Check, search for 'DeF' in 'abc def ghi' returns true
Assert.IsTrue(Strings.Contains("abc def ghi", "DeF"), "Check results contains 'DeF'");
//// Check, search for 'namespace' in string with a new line returns true
Assert.IsTrue(Strings.Contains(@"//========
namespace blah", "namespace"),
"Check results contains 'namespace'");
}
/// <summary>
/// Verifies that the function apply the correct ordinal for the given number
/// - Check 1 returns 1st
/// - Check 2 returns 2nd
/// - Check 3 returns 3rd
/// - Check 4 returns 4th
/// - Check 11 returns 11th
/// - Check 12 returns 12th
/// - Check 13 returns 13th
/// - Check 14 returns 14th
/// - Check 20 returns 20th
/// - Check 21st returns 21st
/// - Check 22 returns 22nd
/// - Check 23 returns 23rd
/// - Check 24 returns 24th
/// </summary>
[Test]
public void ConvertWithOrdinalTests()
{
// Check 1 returns 1st
Assert.AreEqual("1st", Strings.ConvertWithOrdinal(1), "Check 1 returns 1st");
// Check 2 returns 2nd
Assert.AreEqual("2nd", Strings.ConvertWithOrdinal(2), "Check 2 returns 2nd");
// Check 3 returns 3rd
Assert.AreEqual("3rd", Strings.ConvertWithOrdinal(3), "Check 3 returns 3rd");
// Check 4 returns 4th
Assert.AreEqual("4th", Strings.ConvertWithOrdinal(4), "Check 4 returns 4th");
// Check 11 returns 11th
Assert.AreEqual("11th", Strings.ConvertWithOrdinal(11), "Check 11 returns 11th");
// Check 12 returns 12th
Assert.AreEqual("12th", Strings.ConvertWithOrdinal(12), "Check 12 returns 12th");
// Check 13 returns 13th
Assert.AreEqual("13th", Strings.ConvertWithOrdinal(13), "Check 13 returns 13th");
// Check 14 returns 14th
Assert.AreEqual("14th", Strings.ConvertWithOrdinal(14), "Check 14 returns 14th");
// Check 20 returns 20th
Assert.AreEqual("20th", Strings.ConvertWithOrdinal(20), "Check 20 returns 20th");
// Check 21st returns 21st
Assert.AreEqual("21st", Strings.ConvertWithOrdinal(21), "Check 21 returns 21st");
// Check 22 returns 22nd
Assert.AreEqual("22nd", Strings.ConvertWithOrdinal(22), "Check 22 returns 22nd");
// Check 23 returns 23rd
Assert.AreEqual("23rd", Strings.ConvertWithOrdinal(23), "Check 23 returns 23rd");
// Check 24 returns 24th
Assert.AreEqual("24th", Strings.ConvertWithOrdinal(24), "Check 24 returns 24th");
}
/// <summary>
/// Verifies we can get the string that is between two other strings, INCLUDING the start and finish strings
/// - Create a string
/// - Check GetStringBetween returns the expected result
/// </summary>
[Test]
public void GetStringBetweenInclusiveSplitterTests()
{
// Create a string
string searchIn = @"The quick
brown fox jumped over
the
lazy dog";
// Check GetStringBetween returns the expected result
Assert.AreEqual("foxabcjumped"
, Strings.GetStringBetweenInclusive(searchIn, "fox", "jumped", "abc")
, "Check GetStringBetween returns the expected result");
Assert.AreEqual("fox jumped over"
, Strings.GetStringBetweenInclusive(searchIn, "fox ", " over", "abc")
, "Check GetStringBetween returns the expected result, ie not including the splitter.");
}
/// <summary>
/// Verifies we can get the string that is between two other strings, INCLUDING the start and finish strings
/// - Create a string
/// - Check GetStringBetween returns the expected result
/// </summary>
[Test]
public void GetStringBetweenInclusiveTests()
{
// Create a string
string searchIn = @"The quick
brown fox jumped over
the
lazy dog";
// Check GetStringBetween returns the expected result
Assert.AreEqual("brown fox jumped over"
, Strings.GetStringBetweenInclusive(searchIn, "brown", "over")
, "Check GetStringBetween returns the expected result");
Assert.AreEqual(@"over
the
lazy dog"
, Strings.GetStringBetweenInclusive(searchIn, "over", "dog")
, "Check GetStringBetween returns the expected result");
}
/// <summary>
/// Verifies we can get the string that is between two other strings
/// - Create a string
/// - Check GetStringBetween returns the expected result
/// </summary>
[Test]
public void GetStringBetweenTests()
{
// Create a string
string searchIn = @"abc
def!>@#GKG#$#K^ 56&%$&L$% 4 #
PS$)#%K
sdf
-65E_^$%_&5";
// Check GetStringBetween returns the expected result
Assert.AreEqual("GKG", Strings.GetStringBetween(searchIn, "!>@#", "#$#K")
, "Check GetStringBetween returns the expected result");
Assert.AreEqual(" ", Strings.GetStringBetween(searchIn, "K^", "56&")
, "Check GetStringBetween returns the expected result");
}
/// <summary>
/// Verifies that the function still works when there are no instances of the string to remove
/// - Create a string that doesnt contain commas
/// - Set string to remove to be a comma
/// - Check RemoveFirstInstanceOf returns the original string
/// </summary>
[Test]
public void RemoveFirstInstanceOfWithNoInstancesTests()
{
// Create a string that doesnt contain commas
string someString = "Some string that doesn't contain commas<>@#$%";
// Set string to remove to be a comma
string toRemove = ",";
// Check RemoveFirstInstanceOf returns the original string
Assert.AreEqual(someString, Strings.RemoveFirstInstanceOf(toRemove, someString),
"Check RemoveFirstInstanceOf returns the original string");
}
/// <summary>
/// Checks functionality of RemoveNonAlphaNumericCharacters.
/// - Check a string with all valid characters - uppercase
/// - Check a string with all valid characters - lowercase
/// - Check string with all invalid characters returns an empty string.
/// - check a string with both valid and invalid characters returns the correct SAFE string
/// - check spaces are removed.
/// </summary>
[Test]
public void RemoveNonAlphanumericCharactersTests()
{
string tester = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Check a string with all valid characters - uppercase
Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", Strings.RemoveNonAlphanumericCharacters(tester),
"Check a string with all valid characters");
tester = "abcdefghijklmnopqrstuvwxyz0123456789";
// Check a string with all valid characters - lowercase
Assert.AreEqual("abcdefghijklmnopqrstuvwxyz0123456789", Strings.RemoveNonAlphanumericCharacters(tester),
"Check a string with all valid characters");
// Check string with all invalid characters returns an empty string.
Assert.AreEqual("_", Strings.RemoveNonAlphanumericCharacters("!�$%^&*()_-+=[]{}#~;':@,.<>?|"),
"Check string with all invalid characters returns an empty string.");
// Check a string with both valid and invalid characters returns the correct SAFE string
Assert.AreEqual("asdfghjkl0_987654321qwertyuiopm",
Strings.RemoveNonAlphanumericCharacters(
"a!s�d$f%g^h&j*k(l)0_9-8+7=6[5]4{3}2#1~q;w'e:r@t,y.u<i>o?p|m"),
"check a string with both valid and invalid characters returns the correct SAFE string");
// check spaces are removed.
Assert.AreEqual("stringwithspaces", Strings.RemoveNonAlphanumericCharacters(" string with spaces "),
"check spaces are removed.");
}
/// <summary>
/// Checks functionality of RemoveSpacesFromString
/// - check string with no spaces
/// - check string with one space
/// - check string with lots of spaces
/// - check string with space at start
/// - check string with space at end
/// - check multiple spaces together
/// </summary>
[Test]
public void RemoveSpacesFromStringTests()
{
// check string with no spaces
Assert.AreEqual("nospaces", Strings.RemoveSpacesFromString("nospaces"), "check string with no spaces");
// check string with one space
Assert.AreEqual("onespace", Strings.RemoveSpacesFromString("one space"), "check string with one space");
// check string with lots of spaces
Assert.AreEqual("lotsofspaces", Strings.RemoveSpacesFromString("l o t s o f s p a c e s"),
"check string with lots of spaces");
// check string with space at start
Assert.AreEqual("spaceatstart", Strings.RemoveSpacesFromString(" spaceatstart"),
"check string with space at start");
// check string with space at end
Assert.AreEqual("spaceatend", Strings.RemoveSpacesFromString("spaceatend "),
"check string with space at end");
// check multiple spaces together
Assert.AreEqual("multiplespaces", Strings.RemoveSpacesFromString("mult iple spa c es"),
"check multiple spaces together");
}
/// <summary>
/// Tests the IsValidNumber function
/// </summary>
[Test]
public void TestisValidNumber()
{
Assert.AreEqual(true, Strings.IsValidNumber("123"));
Assert.AreEqual(true, Strings.IsValidNumber("123.45"));
Assert.AreEqual(true, Strings.IsValidNumber("12314987129487214682146871247825.12486128496875970123"));
Assert.AreEqual(false, Strings.IsValidNumber(""));
Assert.AreEqual(false, Strings.IsValidNumber(null));
Assert.AreEqual(false, Strings.IsValidNumber("hi"));
Assert.AreEqual(false, Strings.IsValidNumber("5x"));
Assert.AreEqual(false, Strings.IsValidNumber("5 "));
Assert.AreEqual(false, Strings.IsValidNumber("5."));
Assert.AreEqual(true, Strings.IsValidNumber("5.7"));
Assert.AreEqual(true, Strings.IsValidNumber("0.7"));
Assert.AreEqual(true, Strings.IsValidNumber("0000.7"));
Assert.AreEqual(true, Strings.IsValidNumber(".77"));
Assert.AreEqual(true, Strings.IsValidNumber("-.77"));
Assert.AreEqual(false, Strings.IsValidNumber("-44."));
Assert.AreEqual(true, Strings.IsValidNumber("-44.38"));
Assert.AreEqual(true, Strings.IsValidNumber("-0000.7888"));
}
/// <summary>
/// get the leftmost values
/// - test left("", 5) = ""
/// - test left("hello", 4 ) = "hell"
/// - test left("hello", 0 ) = ""
/// - test left("hello", 6 ) = "hello"
/// </summary>
[Test]
public void TestLeft()
{
// - test left("", 5) = ""
Assert.AreEqual(string.Empty, Strings.Left(string.Empty, 5),
"left of empty string");
// - test left("hello", 4 ) = "hell"
Assert.AreEqual("hell", Strings.Left("hello", 4),
"get a proper substring");
// - test left("hello", 0 ) = ""
Assert.AreEqual(string.Empty, Strings.Left("hello", 0),
"left zero characters");
// - test left("hello", 6 ) = "hello"
Assert.AreEqual("hello", Strings.Left("hello", 6),
"not long enough string");
}
/// <summary>
/// Test splitting the split function
/// - split string.empty
/// - check we got one, empty value
/// - split string 'hello', 'a'
/// - check we got one value, 'hello'
/// - check string 'hello', 'll'
/// - check we got two values 'he', 'o'
/// - check string good morning
/// - check we got 4 values
/// - check we got 'g', '', 'd m', 'nrning'
/// - check string firstGOsecondgo, go
/// - check we got "first", second"
///
/// </summary>
[Test]
public void TestSplit()
{
// - split string.empty
// - check we got one, empty value
Assert.AreEqual(
new[] {string.Empty},
Strings.Split(string.Empty, "1"),
"split string.empty");
// - split string 'hello', 'a'
// - check we got one value, 'hello'
Assert.AreEqual(
new[] {"hello"},
Strings.Split("hello", "a"),
"Split string 'hello', 'a'");
// - check string 'hello', 'll'
// - check we got two values 'he', 'o'
Assert.AreEqual(
new[] {"he", "o"},
Strings.Split("hello", "ll"),
"check string 'hello', 'll'");
// - check string good morning
// - check we got 4 values
// - check we got 'g', '', 'd m', 'rning'
Assert.AreEqual(
new[] {"g", "", "d m", "rning"},
Strings.Split("good morning", "o"),
"check string good morning");
// - check string firstGOsecondgo, go
// - check we got "first", second", ""
Assert.AreEqual(
new[] {"first", "second", ""},
Strings.Split("firstGOsecondgo", "go"),
"check string firstGOsecondgo");
}
/// <summary>
/// Verifies ToCamelCase converts strings to camel case as expected
/// - Check SomeString returns someString
/// - Check someString returns someString
/// </summary>
[Test]
public void ToCamelCaseTests()
{
// Check SomeString returns someString
Assert.AreEqual("someString", Strings.ToCamelCase("SomeString")
, "Check SomeString returns someString");
// Check someString returns someString
Assert.AreEqual("someString", Strings.ToCamelCase("someString")
, "Check someString returns someString");
}
}
}