Search Results for

    Show / Hide Table of Contents

    Or Constraint

    OrConstraint combines two constraints and succeeds if either one succeeds.

    Usage

    <constraint>.Or.<constraint>
    

    Examples

    [Test]
    public void OrConstraint_Examples()
    {
        int value = 42;
    
        // Either condition can be true
        Assert.That(value, Is.LessThan(10).Or.GreaterThan(40));
        Assert.That(value, Is.EqualTo(0).Or.Positive);
    
        // String constraints
        Assert.That("test", Does.StartWith("te").Or.EndWith("ing"));
    
        // Multiple alternatives
        Assert.That(value, Is.EqualTo(1).Or.EqualTo(42).Or.EqualTo(100));
    }
    

    Evaluation Order and Precedence

    Constraints are evaluated left to right and short-circuit when one succeeds:

    [Test]
    public void OrConstraint_NullPattern_Examples()
    {
        int? nullable = null;
    
        // Null-or-value pattern (check null first!)
        Assert.That(nullable, Is.Null.Or.GreaterThan(0));  // Passes for null
    
        // Collection: empty or contains specific item
        var items = new List<int>();
        Assert.That(items, Is.Empty.Or.Contains(1));
    }
    
    Note

    Or has higher precedence than And. The expression A.And.B.Or.C is evaluated as A.And.(B.Or.C).

    See Also

    • And Constraint - Both conditions must be true
    • Not Constraint - Negate a constraint
    • AnyOf Constraint - Value equals any of several options
    • 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