Skip to content

Generic overloads for CollectionAssert #989

Closed
Closed

Description

Motivation

I have been using CollectionAssert for quite some time but when I want to check whether the contents of a ReadOnlyCollection<T> match my expectation a cannot use CollectionAssert because many generic collection interfaces do not inherit from ICollection.

Proposal

Add new generic overloads of all methods to take IReadOnlyCollection<T> or IEnumerable<T> instead of ICollection.

AB#2112229

Activity

  1. Haplois commented on Oct 1, 2021

    @Haplois
    Contributor

    Hi, thanks for the proposal.

    If you're up for it, we are open for community PR's.

  2. daniel-lerch commented on Nov 18, 2021

    @daniel-lerch
    Author

    Because of some difficulties described in #990, I will not continue to work on this issue and migrate my unit tests to Xunit instead.

  3. added
    area/assertion
    and removed on Nov 30, 2022
  4. 10 remaining items

  5. RioMcBoo commented on Apr 30, 2026

    @RioMcBoo

    Hi, how can i see the code and do changes? I created fork, but it is only headers inside =(

    What the problem with Indentation? I wanted to help

  6. nohwnd commented on May 4, 2026

    @nohwnd
    Member

    RioMcBoo which file are you looking at where there are only headers?

  7. RioMcBoo commented on May 5, 2026

    @RioMcBoo

    @RioMcBoo which file are you looking at where there are only headers?

    https://github.com/microsoft/testfx/blob/main/src/TestFramework/TestFramework/Assertions/CollectionAssert.Equality.cs - this one

    The methods nested in head methods highlighted by white color and any attempts to open them to look inside follows with "you can not move caret to it" or something like this

  8. nohwnd commented on May 5, 2026

    @nohwnd
    Member

    In those files I see regions.

    Image

    Once expanded the code is there, and in the code I can navigate to other parts of the class with ctrl+click.

    Image

    I see no issue on my side.

    Please record a gif / video (e.g. via snipping tool that is built-in in Windows) if this did not help, because I don't know what else to look for :)

    Those regions look useless now that the classes are split, do you still find them useful Amaury Levé (@Evangelink)? (I have VS set to always expand regions so I don't care either way).

  9. RioMcBoo commented on May 5, 2026

    @RioMcBoo

    ok, this is something wrong with the project (i've never seen that architecture). Depends on which project me opening i get or errors or unreachable code.

    When i did my dll on C# i knew what i should open and run to test my code. I had my own project and couple others with tests. But i cannot find even "the" project =(

    It looks like i am newbie in it

    Image Image
  10. nohwnd commented on May 5, 2026

    @nohwnd
    Member

    try closing the solution, and delete all bin and obj folders. Or open cmdline in the folder where you have solution, and do dotnet clean, followed by dotnet build.

    That should show all errors that you have there, if any. This looks like broken cache to me. VS should recover.

  11. RioMcBoo commented on May 5, 2026

    @RioMcBoo

    I guess I just don't know how to use projects like this. I don't know what to open. There are dozens of projects here. And they're all unhappy with something.

  12. RioMcBoo commented on May 5, 2026

    @RioMcBoo

    I'm currently working on my own collection and wanted to write tests for it. I was extremely surprised that this tool doesn't implement modern interfaces.

    However, I also encountered an issue with element identification. So, I was interested in the outcome of the issue mentioned in the pull request.

    The gist of it was that in binary lists, sort-equality doesn't always equal equal-equality. This requires a strict implementation of the Iquatable and IComparable interfaces so that they determine element equality in the same way. Otherwise, this leads to collisions and requires O(MxN) checks within the collision.

    Maybe this will help with the issue. I no longer have any desire to delve into the project.

  13. Evangelink commented on May 20, 2026

    @Evangelink
    Member

    Closing as superseded — the modern Assert.* family added in 4.x already accepts IEnumerable<T> (which is strictly broader than IReadOnlyCollection<T> and ICollection<T>), so no extra CollectionAssert overloads are needed. The recommended replacements are all on Assert itself:

    CollectionAssert.* Modern Assert.* replacement (takes IEnumerable<T>)
    AreEqual (ordered) Assert.AreSequenceEqual<T>(expected, actual) (#8334)
    AreNotEqual (ordered) Assert.AreNotSequenceEqual<T>(notExpected, actual) (#8334)
    AreEqual (any order) Assert.AreSequenceEqual<T>(expected, actual, SequenceOrder.InAnyOrder) (#8334)
    AreEquivalent / structural Assert.AreEquivalent<T> / AreNotEquivalent<T> (#8266)
    Contains Assert.Contains<T>(item, collection) (#8265)
    DoesNotContain Assert.DoesNotContain<T>(item, collection) (#8265)
    AllItemsAreNotNull Assert.AreAllNotNull<T>(collection) (#8237)
    AllItemsAreUnique Assert.AreAllDistinct<T>(collection) (#8237)
    AllItemsAreInstancesOfType Assert.AreAllOfType<T>(collection) (#8237)
    IsSubsetOf Assert.ContainsAll<T>(expected, collection) (#8234)
    IsNotSubsetOf Assert.DoesNotContainAll<T>(expected, collection) (#8234)
    count check Assert.HasCount<T>(n, collection) / IsEmpty / IsNotEmpty (#8262)

    All overloads use RFC 012 structured failure messages and apply to any IReadOnlyCollection<T>, custom enumerable, or LINQ query without needing .ToList() / casts.

    For the umbrella analyzer guidance pushing users toward these APIs (extension of MSTEST0037), see #8338.

  14. cremor commented on May 21, 2026

    @cremor

    the modern Assert.* family added in 4.x already accepts IEnumerable<T>

    Just to be clear: Those new overloads are not yet released, correct? So the "x" in "4.x" stands for some future version, e.g. maybe 4.3?

  15. Evangelink commented on May 21, 2026

    @Evangelink
    Member

    the modern Assert.* family added in 4.x already accepts IEnumerable<T>

    Just to be clear: Those new overloads are not yet released, correct? So the "x" in "4.x" stands for some future version, e.g. maybe 4.3?

    Yes it will be shipped in 4.3 but I don't have ETA yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/assertion

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions