Search Results for

    Show / Hide Table of Contents

    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

    1. Whitespace characters are defined by the Unicode standard as interpreted by Char.IsWhiteSpace().
    2. Unlike Is.Empty, Is.WhiteSpace also returns true for null values.
    3. Common whitespace characters include: space ( ), tab (\t), newline (\n), carriage return (\r).

    See Also

    • EmptyString Constraint
    • Empty Constraint
    • Null Constraint
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0