[suggestion] New diagnostic: Use AllItemsConstraint for better assertion messages in case of failure
Bartleby2718 opened this issue · 1 comments
Bartleby2718 commented
Today, NUnit.Analyzers (4.2.0) doesn't flag the following code:
var numbers = new[] { 1, 2, 3 };
Assert.That(numbers.All<int>(x => x % 2 == 0));
However, NUnit has a constraint to support this use case:
Assert.That(numbers, Has.All.Matches<int>(x => x % 2 == 0));
so I think NUnit.Analyzers
should suggest a fix for a better error message.
Notes:
- It appears that the type parameter is required for
Matches
, but I'm not sure if Roslyn will be able to deduce it if the type parameter is absent (i.e. convertAssert.That(numbers.All(x => x % 2 == 0));
to the sameAssert.That(numbers, Has.All.Matches<int>(x => x % 2 == 0));
). - There's a typo in the link (
custom custom
), but I'm not sure where it's source-controlled.
Bartleby2718 commented
@manfred-brands I see that Is
and Has
are basically the same:
- https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Is.cs#L28-L33
- https://github.com/nunit/nunit/blob/master/src/NUnitFramework/framework/Has.cs#L27-L32
Questions
- Is there another one that does the same?
- Which one should we choose?