WhiteSpace Constraint
WhiteSpaceConstraint tests that a string is null, empty, or contains only whitespace characters. It is equivalent to
string.IsNullOrWhiteSpace().
Note
This constraint was added in NUnit 4.2.
Usage
Is.WhiteSpace
Is.Not.WhiteSpace
Examples
[Test]
public void WhiteSpaceConstraint_Examples()
{
Assert.That("", Is.WhiteSpace); // Empty string
Assert.That(" ", Is.WhiteSpace); // Space
Assert.That(" \t\n", Is.WhiteSpace); // Mixed whitespace
Assert.That(null, Is.WhiteSpace); // Null
Assert.That("Hello", Is.Not.WhiteSpace);
Assert.That(" Hello ", Is.Not.WhiteSpace); // Has non-whitespace content
}
Notes
- Whitespace characters are defined by the Unicode standard as interpreted by
Char.IsWhiteSpace(). - Unlike
Is.Empty,Is.WhiteSpacealso returns true fornullvalues. - Common whitespace characters include: space (
), tab (\t), newline (\n), carriage return (\r).