Skip to content

Add MSTEST0068 - CollectionAssert to Assert analyzer and code fix#8768

Open
Evangelink wants to merge 2 commits into
microsoft:mainfrom
Evangelink:dev/amauryleve/collectionassert-to-assert-analyzer
Open

Add MSTEST0068 - CollectionAssert to Assert analyzer and code fix#8768
Evangelink wants to merge 2 commits into
microsoft:mainfrom
Evangelink:dev/amauryleve/collectionassert-to-assert-analyzer

Conversation

@Evangelink
Copy link
Copy Markdown
Member

Fixes #8764.

Adds the MSTEST0068 analyzer + code fix to migrate legacy CollectionAssert.* calls to their modern Assert.* equivalents.

Mappings

CollectionAssert Assert
AreEqual / AreNotEqual AreSequenceEqual / AreNotSequenceEqual
AreEquivalent / AreNotEquivalent AreSequenceEqual / AreNotSequenceEqual with SequenceOrder.InAnyOrder
AllItemsAreNotNull AreAllNotNull
AllItemsAreUnique AreAllDistinct
AllItemsAreInstancesOfType(coll, type) AreAllOfType(type, coll)
Contains(coll, x) Contains(x, coll)
DoesNotContain(coll, x) DoesNotContain(x, coll)

Skipped overloads

  • AreEqual / AreNotEqual overloads taking an IComparer are not migrated (no equivalent on Assert).
  • AreEquivalent / AreNotEquivalent overloads taking an IEqualityComparer<T> are not migrated.
  • IsSubsetOf / IsNotSubsetOf have no Assert equivalent and are not migrated.

Notes

  • Diagnostic MSTEST0068, category Usage, severity Info.
  • Argument-swap and SequenceOrder insertion are operation-based, so named/out-of-order arguments are normalized correctly.
  • SequenceOrder.InAnyOrder is emitted fully-qualified to avoid breaking files that lack the Microsoft.VisualStudio.TestTools.UnitTesting using directive.
  • AllItemsAreInstancesOfType -> AreAllOfType changes null-element semantics: CollectionAssert.AllItemsAreInstancesOfType allowed nulls, while Assert.AreAllOfType treats them as failures. The new behavior is arguably more correct and matches the issue mandate.

Implements the MSTEST0068 analyzer and code fix to migrate legacy

`CollectionAssert.*` calls to their modern `Assert.*` equivalents.

Mappings:

 - AreEqual/AreNotEqual -> AreSequenceEqual/AreNotSequenceEqual

 - AreEquivalent/AreNotEquivalent -> AreSequenceEqual/AreNotSequenceEqual with SequenceOrder.InAnyOrder

 - AllItemsAreNotNull -> AreAllNotNull

 - AllItemsAreUnique -> AreAllDistinct

 - AllItemsAreInstancesOfType -> AreAllOfType (swap args)

 - Contains/DoesNotContain -> Contains/DoesNotContain (swap args)

IComparer/IEqualityComparer<T> overloads, IsSubsetOf and IsNotSubsetOf are intentionally not migrated.

Fixes microsoft#8764

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 2, 2026 14:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds MSTEST0068 to MSTest.Analyzers (and a corresponding C# code fix in MSTest.Analyzers.CodeFixes) to migrate supported CollectionAssert.* calls to modern Assert.* equivalents, plus unit tests and localized resources.

Changes:

  • Introduce CollectionAssertToAssertAnalyzer (MSTEST0068) with method-to-method mappings and skipped overload handling.
  • Add CollectionAssertToAssertFixer implementing argument reordering and SequenceOrder.InAnyOrder insertion.
  • Add unit tests and update analyzer/code-fix resources and release tracking entries.
Show a summary per file
File Description
test/UnitTests/MSTest.Analyzers.UnitTests/CollectionAssertToAssertAnalyzerTests.cs Adds analyzer+fixer unit tests covering supported mappings and some formatting/argument-order scenarios.
src/Analyzers/MSTest.Analyzers/Resources.resx Adds MSTEST0068 title/message resources.
src/Analyzers/MSTest.Analyzers/Helpers/DiagnosticIds.cs Reserves diagnostic ID MSTEST0068 constant.
src/Analyzers/MSTest.Analyzers/CollectionAssertToAssertAnalyzer.cs Implements MSTEST0068 analyzer and mapping logic.
src/Analyzers/MSTest.Analyzers/AnalyzerReleases.Unshipped.md Registers MSTEST0068 in unshipped analyzer release notes.
src/Analyzers/MSTest.Analyzers/xlf/Resources.cs.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.de.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.es.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.fr.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.it.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.ja.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.ko.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.pl.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.pt-BR.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.ru.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.tr.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hans.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers/xlf/Resources.zh-Hant.xlf Localization update for new MSTEST0068 analyzer strings.
src/Analyzers/MSTest.Analyzers.CodeFixes/CodeFixResources.resx Adds code-fix title resource for CollectionAssert→Assert migration.
src/Analyzers/MSTest.Analyzers.CodeFixes/CollectionAssertToAssertFixer.cs Implements the C# code fix for MSTEST0068 (argument normalization + rewrite strategies).
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.cs.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.de.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.es.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.fr.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.it.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ja.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ko.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.pl.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.pt-BR.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.ru.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.tr.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.zh-Hans.xlf Localization update for new code-fix title resource.
src/Analyzers/MSTest.Analyzers.CodeFixes/xlf/CodeFixResources.zh-Hant.xlf Localization update for new code-fix title resource.

Copilot's findings

  • Files reviewed: 33/33 changed files
  • Comments generated: 2

Comment thread src/Analyzers/MSTest.Analyzers.CodeFixes/CollectionAssertToAssertFixer.cs Outdated
@Evangelink
Copy link
Copy Markdown
Member Author

@copilot address review comments

… Assert

The fixer used to unconditionally replace `<qualifier>.CollectionAssert.<Method>` with the unqualified `Assert.<ProperMethod>`. That breaks fully-qualified calls like `Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEqual(...)` in files without the `using Microsoft.VisualStudio.TestTools.UnitTesting;` directive: the rewritten `Assert` would either fail to bind or bind to a different `Assert` type in scope.

Now preserve the original qualifier and only swap the trailing `CollectionAssert` identifier for `Assert`. Handles unqualified, namespace-qualified, `global::`-qualified, and `alias::`-qualified forms.

Added two regression tests covering `Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.X(...)` (no using directive) and `global::Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.X(...)`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

Analyzer + code fixer: migrate CollectionAssert.* to Assert.*

2 participants