Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Bartleby2718 opened this issue Jun 2, 2024 · 1 comment

Comments

@Bartleby2718
Copy link
Contributor

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.
@Bartleby2718
Copy link
Contributor Author

@manfred-brands I see that Is and Has are basically the same:

Questions

  1. Is there another one that does the same?
  2. Which one should we choose?

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

No branches or pull requests

2 participants