Extend MSTEST0037 to cover array.Length, enumerable.Count(), Any(), and Contains with comparer#7540
Extend MSTEST0037 to cover array.Length, enumerable.Count(), Any(), and Contains with comparer#7540
Conversation
…and Contains with comparer Co-authored-by: Evangelink <[email protected]>
…inqAnyNoPredicate Co-authored-by: Evangelink <[email protected]>
Youssef1313
left a comment
There was a problem hiding this comment.
This analyzer is really getting unnecessarily too complicated and handling too much stuff 😕
We can split to have 1 rule per target refactoring but I am not so sure this is helping much (user pov or even for our maintenance). Do you have some other ideas? |
Nothing immediately in mind. I'll try to look deeper in the current implementation later. Fine for me for now to make the fix here. |
|
@copilot there are some issues: azure-pipelines test/UnitTests/MSTest.Analyzers.UnitTests/Verifiers/CSharpCodeFixVerifier`2.cs(78,1): error : [WhenAssertIsTrueWithLinqContainsAndComparer] [net8.0] Test method MSTest.Analyzers.Test.UseProperAssertMethodsAnalyzerTests.WhenAssertIsTrueWithLinqContainsAndComparer threw exception: Expected diagnostic: Actual diagnostic: |
Co-authored-by: Evangelink <[email protected]>
Fixed in 0d0776f — the test code snippets for |
|
@copilot please ensure everything builds and all tests are passing. THis test is failing: test/UnitTests/MSTest.Analyzers.UnitTests/Verifiers/CSharpCodeFixVerifier`2.cs#L78 test/UnitTests/MSTest.Analyzers.UnitTests/Verifiers/CSharpCodeFixVerifier`2.cs(78,1): error : [WhenAssertIsFalseWithLinqContainsAndComparer] [net8.0] Test method MSTest.Analyzers.Test.UseProperAssertMethodsAnalyzerTests.WhenAssertIsFalseWithLinqContainsAndComparer threw exception: [TestClass]
|
…th-comparer Co-authored-by: Evangelink <[email protected]>
Fixed in 49076b6. The root cause was that Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
MSTEST0037 was missing several common patterns equivalent to the already-handled
collection.Countandcollection.Contains()cases. This extends the analyzer and code fixer to cover those gaps.New patterns detected and fixed
enumerable.Any()(no predicate)Assert.IsTrue(e.Any())→Assert.IsNotEmpty(e)Assert.IsFalse(e.Any())→Assert.IsEmpty(e)enumerable.Count()in boolean expressionsAssert.IsTrue/IsFalse(e.Count() > 0 / != 0 / == 0)→IsNotEmpty/IsEmptyenumerable.Count()andarray.Lengthin AreEqual/AreNotEqualAssert.AreEqual(0, e.Count())→Assert.IsEmpty(e)Assert.AreEqual(n, e.Count())→Assert.HasCount(n, e)Assert.AreNotEqual(0, e.Count()/array.Length/collection.Count)→Assert.IsNotEmpty(e)(was previously not flagged)Enumerable.Containswith comparerAssert.IsTrue(e.Contains(item, comparer))→Assert.Contains(item, e, comparer)Assert.IsFalse(e.Contains(item, comparer))→Assert.DoesNotContain(item, e, comparer)Implementation notes
TryGetLinqCountNoPredicate/TryGetLinqAnyNoPredicatehelpers that match zero-predicate LINQ extension calls viaenumerableTypeSymbol(consistent with existing LINQ detection).RecognizeCountCheckoverloads to acceptenumerableTypeSymboland handleIInvocationOperationin addition toIPropertyReferenceOperation.CollectionCheckStatus.ContainsWithComparerand a newCodeFixModeAddTwoArgumentscode-fix mode that rewrites the condition argument into three new arguments(item, collection, comparer).AreNotEqual(0, count)now triggersIsNotEmpty; the previously no-op testWhenAssertAreNotEqualWithCollectionCountZerois updated accordingly.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
x3yvsblobprodcus370.vsblob.vsassets.io/home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Release/net8.0/MSTest.Analyzers.UnitTests /home/REDACTED/work/testfx/testfx/artifacts/bin/MSTest.Analyzers.UnitTests/Release/net8.0/MSTest.Analyzers.UnitTests --diagnostic --diagnostic-output-directory /home/REDACTED/work/testfx/testfx/artifacts/log/Release --diagnostic-file-prefix MSTest.Analyzers.UnitTests_net8.0_Release_x64 --diagnostic-verbosity trace --crashdump --hangdump --hangdump-timeout 15m --report-azdo --coverage --coverage-settings /home/REDACTED/work/testfx/testfx/test/coverage.config --coverage-output MSTest.Analyzers.UnitTests_net8.0_Release_x64.coverage --filter UseProperAssertMethodsAnalyzerTests(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.