Skip to content

Conversation

AtolagbeMuiz
Copy link
Contributor

@AtolagbeMuiz AtolagbeMuiz commented Oct 10, 2025

This Pull request fixes #6360

This implementation involves changes to the Analyzer to suggest a much cleaner approach to write Assertions that involves the use of Predicate functions. for example

Assert.IsTrue(_enumerable.Where(x => x == 1).Any());
Assert.IsTrue(_enumerable.Any(x => x == 1));
Assert.IsTrue(_enumerable.Count(x => x == 1) > 0);

Analyzer will suggest (Expected Behaviour)
Assert.Contains(x => x == 1, _enumerable);

Example 2:

Assert.IsFalse(_enumerable.Where(x => x == 1).Any());
Assert.IsFalse(_enumerable.Any(x => x == 1));
Assert.IsFalse(_enumerable.Count(x => x == 1) > 0);

Analyzer will suggest (Expected Behaviour)
Assert.DoesNotContain(x => x == 1, _enumerable);

Unknown,
Any,
Count,
WhereAny,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding SingleAny and SingleCount for the .Single() Linq extension method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this just to add it as part of the enums because, there is no use of it for now in the codebase except if we have an issue that wants the analyzer to check for predicate that uses Single Linq

@Youssef1313 thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant full support of .Single() in this analyzer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh okay.. please can you provide sample code of how it can be used and the expected suggestion code we want analyzer to give..

just to make sure we are on the same page. thank you.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see a few cases, but I'm not sure if they make sense. In all those cases the thrown exception would change at least in one of the possible failure cases. (And those old assertions are not good to start with.)
That's also why I didn't include those when I initially wrote the issue.

// Expected analyzer suggestion:
//Assert.ContainsSingle(x => x == 1, _enumerable);

// Questionable:
Assert.IsNotNull(_enumerable.Where(x => x == 1).SingleOrDefault());
Assert.IsNotNull(_enumerable.SingleOrDefault(x => x == 1));

// Even more questionable:
Assert.IsNotNull(_enumerable.Where(x => x == 1).Single());
Assert.IsNotNull(_enumerable.Single(x => x == 1));
// Expected analyzer suggestion:
//Assert.DoesNotContain(x => x == 1, _enumerable);

// Questionable:
Assert.IsNull(_enumerable.Where(x => x == 1).SingleOrDefault());
Assert.IsNull(_enumerable.SingleOrDefault(x => x == 1));

Copy link
Contributor Author

@AtolagbeMuiz AtolagbeMuiz Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the pull request is now updated with the requested changes.. analyzer has been updated for Single and SingleOrDefault @stan-sz @cremor

…Null and Asset.IsNotNull using Single, SingleOrDefaut, WhereSingle and WhereSingleOrDefualt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSTEST0037 analyzer should suggest Contains/DoesNotContain/ContainsSingle for overloads taking a predicate

3 participants