Skip to content

Allow to use collection expressions with collection constraints #4839

@Dreamescaper

Description

@Dreamescaper

Currently the following code is not possible:

var actual = new[] { 3, 2, 1 };
Assert.That(actual, Is.EquivalentTo([1, 2, 3]));
// CS9174	Cannot initialize type 'IEnumerable' with a collection expression because the type is not constructible.

I suggest to add IEnumerable<T> overloads for all constraint methods, which currently accept non-generic IEnumerable:

        public static CollectionEquivalentConstraint EquivalentTo<T>(IEnumerable<T> expected)
        {
            return new CollectionEquivalentConstraint(expected);
        }

        public static CollectionSubsetConstraint SubsetOf<T>(IEnumerable<T> expected)
        {
            return new CollectionSubsetConstraint(expected);
        }

        // etc

Note: it is partially related to #53, but tackles a very specific issue of using collection expressions.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions