String
Describes the possible expectations for strings.
Equality
You can verify that the string is equal to another one.
This expectation can be configured to ignore case, ignore newline style, ignoring leading or trailing white-space, or
use a custom IEqualityComparer<string>:
string subject = "Abbey Road";
await Expect.That(subject).IsEqualTo("Abbey Road")
.Because("it is equal");
await Expect.That(subject).IsEqualTo("ABBEY ROAD").IgnoringCase()
.Because("we ignored the casing");
await Expect.That("Abbey\r\nRoad").IsEqualTo("Abbey\nRoad").IgnoringNewlineStyle()
.Because("we ignored the newline style");
await Expect.That(subject).IsEqualTo(" Abbey Road").IgnoringLeadingWhiteSpace()
.Because("we ignored leading white-space");
await Expect.That(subject).IsEqualTo("Abbey Road \t").IgnoringTrailingWhiteSpace()
.Because("we ignored trailing white-space");
await Expect.That(subject).IsEqualTo("ABBEY ROAD").Using(StringComparer.OrdinalIgnoreCase)
.Because("the comparer ignored the casing");
Wildcards
You can also compare strings using wildcards:
string subject = "some text";
await Expect.That(subject).IsEqualTo("*me tex?").AsWildcard();
When using AsWildcard, the following wildcard specifiers are supported:
| Wildcard specifier | Matches |
|---|---|
| * (asterisk) | Zero or more characters |
| ? (question mark) | Exactly one character |
Regular expressions
You can also compare strings using regular expressions:
string subject = "some text";
await Expect.That(subject).IsEqualTo("(.*)xt").AsRegex();
The regex comparison uses the following
options:
Multiline(always)IgnoreCase(if theIgnoringCasemethod is also used)
Prefix / Suffix
You can also verify that the subject starts with or ends with a given string.
string subject = "Abbey Road";
await Expect.That(subject).IsEqualTo("Abbey").AsPrefix();
await Expect.That(subject).IsEqualTo("Road").AsSuffix();
One of
You can verify that the string is one of many alternatives.
This expectation can be configured to ignore case, ignore newline style, ignoring leading or trailing white-space, or
use a custom IEqualityComparer<string>:
string subject = "Abbey Road";
await Expect.That(subject).IsOneOf("Help!", "Abbey Road", "Revolver");
await Expect.That(subject).IsOneOf("HELP!", "ABBEY ROAD", "REVOLVER").IgnoringCase()
.Because("we ignored the casing");
await Expect.That(subject).IsOneOf("HELP!", "ABBEY ROAD", "REVOLVER").Using(StringComparer.OrdinalIgnoreCase)
.Because("the comparer ignored the casing");
Null, empty or white-space
You can verify that the string is null, empty or contains only whitespace:
string? subject = null;
await Expect.That(subject).IsNull();
await Expect.That("Abbey Road").IsNotNull();
await Expect.That("").IsEmpty();
await Expect.That("Abbey Road").IsNotEmpty()
.Because("the string is not empty");
await Expect.That(subject).IsNullOrEmpty();
await Expect.That("Abbey Road").IsNotNullOrEmpty();
await Expect.That(subject).IsNullOrWhiteSpace();
await Expect.That("Abbey Road").IsNotNullOrWhiteSpace();
Length
You can verify that the string has the expected length:
string subject = "Abbey Road";
await Expect.That(subject).HasLength().EqualTo(10);
await Expect.That(subject).HasLength().NotEqualTo(9);
await Expect.That(subject).HasLength().GreaterThan(8);
await Expect.That(subject).HasLength().GreaterThanOrEqualTo(9);
await Expect.That(subject).HasLength().LessThanOrEqualTo(11);
await Expect.That(subject).HasLength().LessThan(12);
String start / end
You can verify that the string starts or ends with a given string.
These expectations can be configured to ignore case, ignore newline style, ignoring leading or trailing white-space, or
use a custom IEqualityComparer<string>:
string subject = "Abbey Road";
await Expect.That(subject).StartsWith("Abbey");
await Expect.That(subject).StartsWith("ABBEY").IgnoringCase()
.Because("we ignored the casing");
await Expect.That(subject).StartsWith("ABBEY").Using(StringComparer.OrdinalIgnoreCase)
.Because("the comparer ignored the casing");
await Expect.That(subject).EndsWith("Road");
await Expect.That(subject).EndsWith("ROAD").IgnoringCase()
.Because("we ignored the casing");
await Expect.That(subject).EndsWith("ROAD").Using(StringComparer.OrdinalIgnoreCase)
.Because("the comparer ignored the casing");
Contains
You can verify that the string contains a given substring.
These expectations can be configured to ignore case, ignore newline style, ignoring leading or trailing white-space, or
use a custom IEqualityComparer<string>:
string subject = "Strawberry Fields Forever";
await Expect.That(subject).Contains("Fields");
await Expect.That(subject).Contains("FIELDS").IgnoringCase()
.Because("we ignored the casing");
await Expect.That(subject).Contains("FIELDS").Using(StringComparer.OrdinalIgnoreCase)
.Because("the comparer ignored the casing");
You can also specify, how often the substring should be found:
string subject = "get back, get back, get back to where you once belonged.";
// 'get' can be found 3 times
await Expect.That(subject).Contains("get").MoreThan(1)
.Because("count should be '> 1'");
await Expect.That(subject).Contains("get").AtLeast(2)
.Because("count should be '>= 2'");
await Expect.That(subject).Contains("get").Exactly(3)
.Because("count should be '== 3'");
await Expect.That(subject).Contains("get").AtMost(4)
.Because("count should be '<= 4'");
await Expect.That(subject).Contains("get").LessThan(5)
.Because("count should be '< 5'");
await Expect.That(subject).Contains("get").Between(1).And(6)
.Because("count should be '>= 1 AND <= 6'");
Character casing
You can verify that the characters in a string are all upper or lower cased:
await Expect.That("1ST PLACE").IsUpperCased()
.Because("it contains no lowercase characters");
await Expect.That("1st PLACE").IsNotUpperCased()
.Because("it contains at least one lowercase characters");
await Expect.That("1st place").IsLowerCased()
.Because("it contains no uppercase characters");
await Expect.That("1st PLACE").IsNotLowerCased()
.Because("it contains at least one uppercase characters");