nunit/nunit.analyzers

[suggestion] New diagnostic: Use AllItemsConstraint for better assertion messages in case of failure

Bartleby2718 opened this issue · 1 comments

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:

  1. 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. convert Assert.That(numbers.All(x => x % 2 == 0)); to the same Assert.That(numbers, Has.All.Matches<int>(x => x % 2 == 0));).
  2. There's a typo in the link (custom custom), but I'm not sure where it's source-controlled.