Skip to content

Conversation

@AtolagbeMuiz
Copy link
Contributor

This PR fixes #6585

This Implementation is to modify the DoNotNegateBoolean Analyzer code for to suggest better and cleaner code when developers negate boolean arguments.

For example;

When a code is wrriten as Assert.IsTrue(!condition) , then the Analyzer would suggest Assert.IsFalse(condition)

Assert.IsFalse(!condition) Analyzer would suggest Assert.IsTrue(condition).

other edge cases were also considered, for example;
Assert.IsTrue(!(a && b)) Analyzer would suggest Assert.IsFalse(a && b)

Assert.IsFalse(!(x == y)), Analyzer would suggest Assert.IsTrue(x == y)

Assert.IsTrue(!!condition) Analyzer would suggest Assert.IsFalse(condition)

@Evangelink Evangelink requested a review from Copilot October 27, 2025 05:48
Copy link
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 implements a code fix for analyzer MSTEST0023, which suggests using the appropriate Assert method instead of negating boolean expressions in test assertions. For example, Assert.IsTrue(!condition) is replaced with Assert.IsFalse(condition).

Key Changes:

  • Adds a new code fix provider DoNotNegateBooleanAssertionFixer that automatically corrects negated boolean assertions
  • Updates the analyzer to include diagnostic properties that guide the code fix
  • Adds comprehensive test coverage for various negation scenarios including simple negations, double negations, complex expressions, and method calls

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Analyzers/MSTest.Analyzers.CodeFixes/DoNotNegateBooleanAssertionFixer.cs New code fix provider that removes negations and swaps Assert.IsTrue/IsFalse methods
src/Analyzers/MSTest.Analyzers/DoNotNegateBooleanAssertionAnalyzer.cs Updated analyzer to include proper assert method name in diagnostic properties
src/Analyzers/MSTest.Analyzers/Resources.resx Added localization strings for code fix titles
src/Analyzers/MSTest.Analyzers/xlf/Resources.*.xlf Added localization entries for all supported languages
test/UnitTests/MSTest.Analyzers.UnitTests/DoNotNegateBooleanAssertionAnalyzerTests.cs Added comprehensive test cases for code fix functionality

Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

Let's replace all calls to VerifyAnalyzerAsync from existing tests please

@AtolagbeMuiz
Copy link
Contributor Author

AtolagbeMuiz commented Oct 27, 2025

Let's replace all calls to VerifyAnalyzerAsync from existing tests please

you want me to replace all VerifyAnalyzerAsync from the test with VerifyCodeFixAsync ?... is this just for pre-exisitng test cases which are just two.. or including the test cases I wrote? @Evangelink

@Evangelink
Copy link
Member

Ideally everything should always use the code fix verifier API. This helps with ensuring we test as many cases as possible.

@AtolagbeMuiz
Copy link
Contributor Author

AtolagbeMuiz commented Oct 31, 2025

Ideally everything should always use the code fix verifier API. This helps with ensuring we test as many cases as possible.

All the test cases has been updated to use VerifyCodeFixAsync @Evangelink .. pull request has been updated as well

Copy link
Member

@Evangelink Evangelink left a comment

Choose a reason for hiding this comment

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

We have had a few issues with trivias on our code fixers, I'd be great to add a few tests like:

Assert.IsTrue(!false,
    "some explanation");

Assert.IsTrue(/* some comment */ !false /* some other comment */);

return expression;
}

private sealed class FixAll : DocumentBasedFixAllProvider
Copy link
Member

Choose a reason for hiding this comment

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

I think this is unused and can be removed.

}

[TestMethod]
public async Task WhenAssertIsTrueWithNegation_Diagnostic()
Copy link
Member

Choose a reason for hiding this comment

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

I think this test case can be dropped as it's already covered above.

}

[TestMethod]
public async Task WhenAssertIsFalseWithNegation_Diagnostic()
Copy link
Member

Choose a reason for hiding this comment

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

Same remark for this test case.

Copy link
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

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Comment on lines +30 to +31
public override FixAllProvider GetFixAllProvider()
=> WellKnownFixAllProviders.BatchFixer;
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

The GetFixAllProvider method returns WellKnownFixAllProviders.BatchFixer, but there's a complete FixAll class implementation (lines 235-367) that is never used. Either the method should return new FixAll() to use the custom implementation, or the unused FixAll class should be removed if BatchFixer is sufficient for this scenario.

Copilot uses AI. Check for mistakes.
// Get the actual argument from the invocation
conditionArgument = invocation.ArgumentList.Arguments[conditionArgumentIndex];

string title = string.Format(System.Globalization.CultureInfo.InvariantCulture, Resources.DoNotNegateBooleanAssertionFix, properAssertMethodName);
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Use a using directive for System.Globalization instead of fully qualifying CultureInfo.InvariantCulture. This aligns with the coding guidelines that prefer using directives and is consistent with other code fixers in the codebase.

Copilot uses AI. Check for mistakes.
Comment on lines +235 to +237
private sealed class FixAll : DocumentBasedFixAllProvider
{
public static readonly FixAll Instance = new();
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

The entire FixAll class (lines 235-367) is defined but never instantiated or used since GetFixAllProvider() returns WellKnownFixAllProviders.BatchFixer. This adds 133 lines of dead code. Remove this class unless you intend to use it by changing GetFixAllProvider() to return new FixAll().

Copilot uses AI. Check for mistakes.
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.

Implement a codefix for MSTEST0023

3 participants