AllItems Constraint
AllItemsConstraint applies a constraint to each item in an IEnumerable, succeeding only if all items satisfy the
constraint. An exception is thrown if the actual value does not implement IEnumerable.
Usage
Is.All.<constraint>
Has.All.<constraint>
Examples
[Test]
public void AllItemsConstraint_Examples()
{
int[] numbers = { 1, 2, 3, 4, 5 };
string[] names = { "Alice", "Bob", "Carol" };
// All items must satisfy the constraint
Assert.That(numbers, Is.All.GreaterThan(0)); // All positive
Assert.That(numbers, Is.All.LessThan(10)); // All less than 10
Assert.That(names, Is.All.Not.Null); // No nulls
Assert.That(names, Is.All.Not.Empty); // No empty strings
Assert.That(names, Is.All.InstanceOf<string>()); // All are strings
// Has.All is equivalent to Is.All
Assert.That(numbers, Has.All.GreaterThan(0));
// Combining with property constraints
Assert.That(names, Is.All.Length.GreaterThan(2)); // All names > 2 chars
// Complex constraints
Assert.That(numbers, Is.All.InRange(1, 10));
Assert.That(names, Is.All.Matches<string>(n => n.Length <= 5));
}
Notes
Is.AllandHas.Allare interchangeable.- The constraint fails on the first item that doesn't match.
- An empty collection satisfies
Is.Allfor any constraint (vacuous truth).
See Also
- SomeItems Constraint - At least one item matches
- NoItem Constraint - No items match
- ExactCount Constraint - Specific number of items match