Skip to content

Add analyzer and code fix to migrate from StringAssert to Assert APIs #5792

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 16, 2025

This PR implements an analyzer and code fix to help migrate from StringAssert APIs to equivalent Assert APIs with swapped parameter order.

Changes Made

1. Added Missing Assert Methods

Created Assert.StringMethods.cs with new methods that have swapped parameter order compared to StringAssert:

// Before (StringAssert)
StringAssert.Contains(value, substring);
StringAssert.StartsWith(value, substring);
StringAssert.EndsWith(value, substring);
StringAssert.Matches(value, pattern);
StringAssert.DoesNotMatch(value, pattern);

// After (Assert - parameters swapped)
Assert.Contains(substring, value);
Assert.StartsWith(substring, value);
Assert.EndsWith(substring, value);
Assert.Matches(pattern, value);
Assert.DoesNotMatch(pattern, value);

All methods include comprehensive overloads supporting:

  • String comparison types
  • Custom messages and formatting parameters
  • Proper parameter validation and error messaging

2. Extended UseProperAssertMethodsAnalyzer

  • Added detection for StringAssert method calls alongside existing Assert method analysis
  • Added CodeFixModeSwapArguments for the new transformation pattern
  • Maps all StringAssert methods to their equivalent Assert counterparts

3. Extended UseProperAssertMethodsFixer

  • Added FixAssertMethodForSwapArgumentsModeAsync method to handle the transformation
  • Changes class name from StringAssert to Assert
  • Swaps the first two arguments while preserving additional parameters
  • Handles all StringAssert method overloads correctly

4. Added Comprehensive Tests

Added test cases covering:

  • All StringAssert methods (Contains, StartsWith, EndsWith, Matches, DoesNotMatch)
  • Parameter swapping verification
  • Message and additional parameter preservation
  • Proper diagnostic reporting

Example Transformation

// Before
StringAssert.Contains(actualString, expectedSubstring, "Should contain substring");

// After (with code fix applied)
Assert.Contains(expectedSubstring, actualString, "Should contain substring");

The analyzer reports this as an info-level diagnostic (MSTEST0037) with the message:
"Use 'Assert.Contains' instead of 'StringAssert.Contains'"

This helps developers migrate to the more consistent Assert API where the expected value comes first, matching the pattern used in other Assert methods like Assert.AreEqual(expected, actual).

Fixes #5791.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Analyzer + code fix to move off of StringAssert APIs Add analyzer and code fix to migrate from StringAssert to Assert APIs Jun 16, 2025
@Copilot Copilot AI requested a review from Evangelink June 16, 2025 20:40
Copilot finished work on behalf of Evangelink June 16, 2025 20:40
Copy link
Member

@Youssef1313 Youssef1313 left a comment

Choose a reason for hiding this comment

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

Don't update the existing analyzer. Add a new one.

…sting analyzer

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 18, 2025

Don't update the existing analyzer. Add a new one.

Created a new StringAssertToAssertAnalyzer (MSTEST0045) instead of extending the existing UseProperAssertMethodsAnalyzer. The changes to the original analyzer have been reverted and the StringAssert functionality is now in a separate analyzer with its own code fixer and tests. Commit: 05f98b9

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cflvsblobprodcus383.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/10.0.0-beta.25313.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Copilot finished work on behalf of Youssef1313 June 18, 2025 09:52
@Copilot Copilot AI requested a review from Youssef1313 June 18, 2025 09:52
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 fix to move off of StringAssert APIs
3 participants