CollectionSuperset Constraint
CollectionSupersetConstraint tests that one IEnumerable is a superset of another. Every item in the expected
collection must be present in the actual collection. An exception is thrown if the actual value does not implement
IEnumerable.
Usage
Is.SupersetOf(IEnumerable expected)
Modifiers
.Using(IComparer comparer)
.Using(IEqualityComparer comparer)
.Using<T>(IComparer<T> comparer)
.Using<T>(IEqualityComparer<T> comparer)
.Using<T>(Comparison<T> comparer)
.Using<T>(Func<T, T, bool> comparer)
.UsingPropertiesComparer() // NUnit 4.1+
Examples
[Test]
public void CollectionSupersetConstraint_Examples()
{
int[] actual = { 1, 2, 3, 4, 5 };
Assert.That(actual, Is.SupersetOf(new[] { 1, 3 })); // Passes
Assert.That(actual, Is.SupersetOf(new[] { 1, 2, 3, 4, 5 })); // Passes (equal sets)
Assert.That(actual, Is.SupersetOf(new int[] { })); // Passes (superset of empty)
Assert.That(actual, Is.Not.SupersetOf(new[] { 1, 6 })); // Passes (missing 6)
// Case-insensitive string comparison
string[] colors = { "Red", "Green", "Blue", "Yellow" };
Assert.That(colors, Is.SupersetOf(new[] { "red", "blue" }).Using((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase));
}
Notes
- A set is always a superset of itself.
- Every set is a superset of the empty set.
- Duplicate items are considered:
{1, 2}is not a superset of{1, 1}.
See Also
- CollectionSubset Constraint - Test that collection is a subset
- CollectionEquivalent Constraint - Test exact equivalence