Search Results for

    Show / Hide Table of Contents

    CollectionSubset Constraint

    CollectionSubsetConstraint tests that one IEnumerable is a subset of another. Every item in the actual collection must be present in the expected collection. An exception is thrown if the actual value does not implement IEnumerable.

    Usage

    Is.SubsetOf(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 CollectionSubsetConstraint_Examples()
    {
        int[] superset = { 1, 2, 3, 4, 5 };
    
        Assert.That(new[] { 1, 3 }, Is.SubsetOf(superset));       // Passes
        Assert.That(new[] { 1, 2, 3, 4, 5 }, Is.SubsetOf(superset)); // Passes (equal sets are subsets)
        Assert.That(new int[] { }, Is.SubsetOf(superset));        // Passes (empty set is subset of any set)
        Assert.That(new[] { 1, 6 }, Is.Not.SubsetOf(superset));   // Passes (6 not in superset)
    
        // Case-insensitive string comparison
        string[] colors = { "Red", "Green", "Blue" };
        Assert.That(new[] { "red", "blue" }, Is.SubsetOf(colors).Using((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase));
    }
    

    Notes

    1. A set is always a subset of itself.
    2. The empty set is a subset of every set.
    3. Duplicate items are considered: {1, 1} is a subset of {1, 1, 2} but not of {1, 2}.

    See Also

    • CollectionSuperset Constraint - Test that collection is a superset
    • CollectionEquivalent Constraint - Test exact equivalence
    • 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