tpierrain/NFluent

Add check that asserts that every element of collection matches a predicate

Closed this issue · 5 comments

Currently to check that every element of collection matches a predicate I must write a code similar to:

 Check.That(collection.All(x => x.HasSomeCondition)).IsTrue();

this generates sub-optimal assertion messages. I would like you to add a method similar to HasElementThatMatches, that would check that all elements of a given collection match a predicate:

// proposal:
Check.That(collection).EveryElementMatches(x => x.HasSomeCondition)

This is already possible with a bit of predicate logic-foo, but the result is neither readable not easy to understand:

Check.That(collection).Not.HasElementThatMatches(x => !x.HasSomeCondition)

EDIT: typos

Hi
I am busy right now but it looks like ContainsOnlyElementsThatMatch is what you are looking for

Yes, looks very promising. I was looking on the Wiki and I have found there only ContainsOnlyElementThatMatch method that checks only a single element. (https://github.com/tpierrain/NFluent/wiki/enumerable-types#containsonlyelementthatmatchespredicate)

Thanks for the pointer. There was an error (now fixed) in the wiki: it should had been only and not one.

From what I see on the Wiki now there is an entry for ContainsOnlyElementThatMatches method, but what I used to solve may problem is ContainsOnlyElementsThatMatch method (notice extra s after Element, so it checks all elements not a single element). It is just a typo but it may help others.

Yes, I went back and fixed a lot of errors in this entry. Thanks again for your great help.