String and Char Assertions

These assertion extension methods can be applied to only string object types (including char for the purposes of the documentation).

ShouldEqualIgnoringCase

Asserts that this string/char value is equal to the specified string/char (ignoring case).

Usage

Where T is: string or char
public static T ShouldEqualIgnoringCase(this T source, T comparer)
public static T ShouldEqualIgnoringCase(this T source, T comparer, string message)
public static T ShouldEqualIgnoringCase(this T source, T comparer, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foo";
   foo.ShouldEqualIgnoringCase("FOO");
}
Supported Releases: All

ShouldNotEqualIgnoringCase

Asserts that this string/char value is not equal to the specified string/char (ignoring case).

Usage

Where T is: string or char
public static T ShouldNotEqualIgnoringCase(this T source, T comparer)
public static T ShouldNotEqualIgnoringCase(this T source, T comparer, string message)
public static T ShouldNotEqualIgnoringCase(this T source, T comparer, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foo";
   foo.ShouldNotEqualIgnoringCase("BAR");

   foo.ShouldNotEqualIgnoringCase("FOO");  // This assertion would fail
}
Supported Releases: All

ShouldBeEmpty

Asserts that this string value is empty.

Usage

public static string ShouldBeEmpty(this string source)
public static string ShouldBeEmpty(this string source, string message)
public static string ShouldBeEmpty(this string source, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "";
   foo.ShouldBeEmpty();
}
Supported Releases: All

ShouldNotBeEmpty

Asserts that this string value is not empty.

Usage

public static string ShouldNotBeEmpty(this string source)
public static string ShouldNotBeEmpty(this string source, string message)
public static string ShouldNotBeEmpty(this string source, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foo";
   foo.ShouldNotBeEmpty();
}
Supported Releases: All


ShouldBePrintable

Asserts that this char value is in the printable range.

Usage

public static char ShouldBePrintable(this char source)
public static char ShouldBePrintable(this char source, string message)
public static char ShouldBePrintable(this char source, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   char foo = "f";
   foo.ShouldBePrintable();
}
Supported Releases: All

ShouldNotBePrintable

Asserts that this char value is not in the printable range.

Usage

public static char ShouldNotBePrintable(this char source)
public static char ShouldNotBePrintable(this char source, string message)
public static char ShouldNotBePrintable(this char source, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   char foo = (char)3;
   foo.ShouldNotBePrintable();
}
Supported Releases: All

ShouldContain

Asserts that this string value contains the specified substring.

Usage

public static string ShouldContain(this string source, string substring)
public static string ShouldContain(this string source, string substring, string message)
public static string ShouldContain(this string source, string substring, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldContain("foo");
}
Supported Releases: 1.1 and later

ShouldContainIgnoringCase

Asserts that this string value contains the specified substring, ignoring case.

Usage

public static string ShouldContainIgnoringCase(this string source, string substring)
public static string ShouldContainIgnoringCase(this string source, string substring, string message)
public static string ShouldContainIgnoringCase(this string source, string substring, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldContainIgnoringCase("FOO");
}
Supported Releases: 1.1 and later

ShouldNotContain

Asserts that this string value does not contain the specified substring.

Usage

public static string ShouldNotContain(this string source, string substring)
public static string ShouldNotContain(this string source, string substring, string message)
public static string ShouldNotContain(this string source, string substring, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldNotContain("bar");
}
Supported Releases: 1.1 and later

ShouldNotContainIgnoringCase

Asserts that this string value does not contain the specified substring, ignoring case.

Usage

public static string ShouldNotContainIgnoringCase(this string source, string substring)
public static string ShouldNotContainIgnoringCase(this string source, string substring, string message)
public static string ShouldNotContainIgnoringCase(this string source, string substring, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldNotContainIgnoringCase("TEST");
}
Supported Releases: 1.1 and later

ShouldHaveLengthOf

Asserts that this string value has a specified length.

Usage

public static string ShouldHaveLengthOf(this string source, int length)
public static string ShouldHaveLengthOf(this string source, int length, string message)
public static string ShouldHaveLengthOf(this string source, int length, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldHaveLengthOf(6);
}
Supported Releases: 1.2 and later

ShouldNotHaveLengthOf

Asserts that this string value does not have a specified length.

Usage

public static string ShouldNotHaveLengthOf(this string source, int length)
public static string ShouldNotHaveLengthOf(this string source, int length, string message)
public static string ShouldNotHaveLengthOf(this string source, int length, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldNotHaveLengthOf(5);
}
Supported Releases: 1.2 and later

ShouldHaveLengthOfAtLeast

Asserts that this string value has a minimum specified length.

Usage

public static string ShouldHaveLengthOfAtLeast(this string source, int length)
public static string ShouldHaveLengthOfAtLeast(this string source, int length, string message)
public static string ShouldHaveLengthOfAtLeast(this string source, int length, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldHaveLengthOfAtLeast(5);
}
Supported Releases: 1.2 and later

ShouldHaveLengthOfAtMost

Asserts that this string value has a maximum specified length.

Usage

public static string ShouldHaveLengthOfAtMost(this string source, int length)
public static string ShouldHaveLengthOfAtMost(this string source, int length, string message)
public static string ShouldHaveLengthOfAtMost(this string source, int length, string message, params object[] parameters)

Example

[TestMethod]
public void Test()
{
   string foo = "foobar";
   foo.ShouldHaveLengthOfAtMost(7);
}
Supported Releases: 1.2 and later

Last edited Dec 9, 2010 at 2:51 PM by jeremiahclark, version 5

Comments

No comments yet.