testfx icon indicating copy to clipboard operation
testfx copied to clipboard

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

Open Copilot opened this issue 8 months ago • 1 comments

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.

Copilot avatar Jun 16 '25 20:06 Copilot

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:

Copilot avatar Jun 18 '25 09:06 Copilot

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

[!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:

  • s4uvsblobprodcus326.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.25326.3.txt (dns block)

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

Copilot avatar Jun 30 '25 08:06 Copilot