fluentassertions / fluentassertions

A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, as well as .NET Core 2.1, .NET Core 3.0, .NET 6, .NET Standard 2.0 and 2.1. Supports the unit test frameworks MSTest2, NUnit3, XUnit2, MSpec, and NSpec3.

Home Page:https://www.fluentassertions.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add AllSatisfyOrEmpty for GenericCollection

dtila opened this issue · comments

Background and motivation

Currently if you want to assert all items or none satisfy a condition, there is no way. AllSatisfy throws exception when collection is empty,

API Proposal

    public AndConstraint<CustomCollectionAssertions<T>> AllSatisfyOrEmpty(Action<T> action, string because = "", params object[] becauseArgs)

API Usage

collection.Should()
.HaveCount(2)
.And
.AllSatisfyOrEmpty(li => li.item.Should().HaveCount(0))

Alternative Designs

No response

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

No

I propose that we name this one BeEmptyOrAllSatisfy. What do you think @jnyrup @arocheleau ?

Hmm.. Why do we need an extra method for this? With #2321 (in v7) an empty collection does not fail anymore.

I crap, you're right. Now I remember the previous discussion.

@dtila It's already there in the upcoming v7 release. If you can't wait, consider using the latest alpha version.

I see better consistency with the .NET environment if #2321 is fixed
Actually when I discovered this behavior I was surprised and look into the code since I wasn't expecting like using All to throw exceptions.

Bug indeed can be closed. Thanks guys!