Range Constraint
RangeConstraint tests that a value falls within an inclusive range. Both the lower and upper bounds are included in
the valid range.
Usage
Is.InRange(IComparable from, IComparable to)
Modifiers
.Using(IComparer comparer)
.Using<T>(IComparer<T> comparer)
.Using<T>(Comparison<T> comparer)
Examples
[Test]
public void RangeConstraint_Examples()
{
// Numeric ranges (inclusive)
Assert.That(5, Is.InRange(1, 10));
Assert.That(1, Is.InRange(1, 10)); // Lower bound is included
Assert.That(10, Is.InRange(1, 10)); // Upper bound is included
Assert.That(0, Is.Not.InRange(1, 10));
// DateTime ranges
var start = new DateTime(2024, 1, 1);
var end = new DateTime(2024, 12, 31);
Assert.That(new DateTime(2024, 6, 15), Is.InRange(start, end));
// String ranges (alphabetical comparison)
Assert.That("banana", Is.InRange("apple", "cherry"));
}
Notes
The range is inclusive on both ends:
Is.InRange(1, 10)passes for values 1, 10, and everything in between.For exclusive bounds, combine
Is.GreaterThanandIs.LessThanwithAnd:Assert.That(5, Is.GreaterThan(1).And.LessThan(10)); // Exclusive bounds