Skip to content

Order dependence of And and Or constraints should be documented #660

Closed
@chphe

Description

@chphe

While playing around with the Constraint Assertion feature of NUnit, my team mates made the following remark.
When combining Null and GreaterThan (or similar constraint) the order of writing is critical when used with Or condition.

Take following examples:

[TestCase(10)]
[TestCase(null)]
public void ConstraintNUnitNull(int? i)
{
    Assert.That(i, Is.Null.Or.GreaterThan(9));
}
[TestCase(10)]
[TestCase(null, ExpectedException = typeof(ArgumentException))]
public void ConstraintNUnitNullFail(int? i)
{
    Assert.That(i, Is.GreaterThan(9).Or.Null);
}

From the perspective of fluent reading, both should work and behave the same. But in the second case, it throws an exception.
Technically I can understand why the exception is thrown, and I'm not sure this should be logged as a bug or that lazy evaluation should be documented.

I'm using NUnit Version 2.6.3.13283 and I also quickly tested the case when using Compound Constraints, it's the same...

[TestCase(10)]
[TestCase(null)]
public void ConstraintOperatorNUnitNull2(int? i)
{
    Assert.That(i, Is.Null | Is.GreaterThan(9));
}

[TestCase(10)]
[TestCase(null, ExpectedException = typeof(ArgumentException))]
public void ConstraintOperatorNUnitNull1(int? i)
{
    Assert.That(i, Is.GreaterThan(9) | Is.Null);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions