From f603588cc61f3be77e674bd2f238dace7ed22522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 25 Jul 2025 15:53:43 +0200 Subject: [PATCH] Fix suppressors tests --- .../UnusedParameterSuppressorTests.cs | 64 ++++++++++++- ...cSuffixTestFixtureMethodSuppressorTests.cs | 94 +++++++++++++------ ...UseAsyncSuffixTestMethodSuppressorTests.cs | 8 +- 3 files changed, 133 insertions(+), 33 deletions(-) diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs index df1c94ea0d..b60b8d6403 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/UnusedParameterSuppressorTests.cs @@ -25,7 +25,7 @@ public async Task AssemblyInitializeWithUnusedTestContext_DiagnosticIsSuppressed public class SomeClass { [AssemblyInitialize] - public static void Initialize(TestContext [|context|]) + public static void Initialize(TestContext {|#0:context|}) { // TestContext parameter is unused but required by MSTest } @@ -36,12 +36,26 @@ public static void Initialize(TestContext [|context|]) await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, }.RunAsync(); // Verify issue is suppressed with suppressor await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(true), + }, }.RunAsync(); } @@ -55,7 +69,7 @@ public async Task ClassInitializeWithUnusedTestContext_DiagnosticIsSuppressed() public class SomeClass { [ClassInitialize] - public static void Initialize(TestContext [|context|]) + public static void Initialize(TestContext {|#0:context|}) { // TestContext parameter is unused but required by MSTest } @@ -66,12 +80,26 @@ public static void Initialize(TestContext [|context|]) await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, }.RunAsync(); // Verify issue is suppressed with suppressor await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(true), + }, }.RunAsync(); } @@ -85,7 +113,7 @@ public async Task TestMethodWithUnusedParameter_DiagnosticIsNotSuppressed() public class SomeClass { [TestMethod] - public void TestMethod(int [|unusedParam|]) + public void TestMethod(int {|#0:unusedParam|}) { // This should not be suppressed } @@ -96,12 +124,26 @@ public void TestMethod(int [|unusedParam|]) await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("unusedParam") + .WithIsSuppressed(false), + }, }.RunAsync(); // Verify issue is still reported with suppressor (not suppressed) await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("unusedParam") + .WithIsSuppressed(false), + }, }.RunAsync(); } @@ -114,7 +156,7 @@ public async Task RegularMethodWithUnusedTestContext_DiagnosticIsNotSuppressed() [TestClass] public class SomeClass { - public void RegularMethod(TestContext [|context|]) + public void RegularMethod(TestContext {|#0:context|}) { // This should not be suppressed as it's not AssemblyInitialize or ClassInitialize } @@ -125,12 +167,26 @@ public void RegularMethod(TestContext [|context|]) await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, }.RunAsync(); // Verify issue is still reported with suppressor (not suppressed) await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForUnusedParameters.Rule) + .WithLocation(0) + .WithArguments("context") + .WithIsSuppressed(false), + }, }.RunAsync(); } diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestFixtureMethodSuppressorTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestFixtureMethodSuppressorTests.cs index b75ac12800..8badc85830 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestFixtureMethodSuppressorTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestFixtureMethodSuppressorTests.cs @@ -26,22 +26,22 @@ public async Task AsyncTestFixtureMethodsWithoutSuffix_DiagnosticIsSuppressed() public class SomeClass { [AssemblyInitialize] - public static async Task [|AssemblyInitialize|]() { } + public static async Task {|#0:AssemblyInitialize|}() { } [AssemblyCleanup] - public static async Task [|AssemblyCleanup|]() { } + public static async Task {|#1:AssemblyCleanup|}() { } [ClassInitialize] - public static async Task [|ClassInitialize|]() { } + public static async Task {|#2:ClassInitialize|}() { } [ClassCleanup] - public static async Task [|ClassCleanup|]() { } + public static async Task {|#3:ClassCleanup|}() { } [TestInitialize] - public async Task [|TestInitialize|]() { } + public async Task {|#4:TestInitialize|}() { } [TestCleanup] - public async Task [|TestCleanup|]() { } + public async Task {|#5:TestCleanup|}() { } } "; @@ -49,11 +49,53 @@ public class SomeClass await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(0) + .WithIsSuppressed(false), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(1) + .WithIsSuppressed(false), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(2) + .WithIsSuppressed(false), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(3) + .WithIsSuppressed(false), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(4) + .WithIsSuppressed(false), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(5) + .WithIsSuppressed(false), + }, }.RunAsync(); await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = + { + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(0) + .WithIsSuppressed(true), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(1) + .WithIsSuppressed(true), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(2) + .WithIsSuppressed(true), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(3) + .WithIsSuppressed(true), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(4) + .WithIsSuppressed(true), + VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule) + .WithLocation(5) + .WithIsSuppressed(true), + }, }.RunAsync(); } @@ -61,33 +103,31 @@ public class SomeClass public async Task AsyncTestMethodWithSuffix_NoDiagnostic() { string code = """ + using System.Threading.Tasks; + using Microsoft.VisualStudio.TestTools.UnitTesting; - using System.Threading.Tasks; - using Microsoft.VisualStudio.TestTools.UnitTesting; - - [TestClass] - public class SomeClass - { - [AssemblyInitialize] - public static async Task AssemblyInitializeAsync() { } - - [AssemblyCleanup] - public static async Task AssemblyCleanupAsync() { } + [TestClass] + public class SomeClass + { + [AssemblyInitialize] + public static async Task AssemblyInitializeAsync() { } - [ClassInitialize] - public static async Task ClassInitializeAsync() { } + [AssemblyCleanup] + public static async Task AssemblyCleanupAsync() { } - [ClassCleanup] - public static async Task ClassCleanupAsync() { } + [ClassInitialize] + public static async Task ClassInitializeAsync() { } - [TestInitialize] - public async Task TestInitializeAsync() { } + [ClassCleanup] + public static async Task ClassCleanupAsync() { } - [TestCleanup] - public async Task TestCleanupAsync() { } - } + [TestInitialize] + public async Task TestInitializeAsync() { } - """; + [TestCleanup] + public async Task TestCleanupAsync() { } + } + """; await new VerifyCS.Test { diff --git a/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestMethodSuppressorTests.cs b/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestMethodSuppressorTests.cs index 1bd51439f4..95acdb7338 100644 --- a/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestMethodSuppressorTests.cs +++ b/test/UnitTests/MSTest.Analyzers.UnitTests/UseAsyncSuffixTestMethodSuppressorTests.cs @@ -28,7 +28,7 @@ public async Task AsyncTestMethodWithoutSuffix_DiagnosticIsSuppressed() public class SomeClass { [TestMethod] - public async Task [|TestMethod|]() { } + public async Task {|#0:TestMethod|}() { } } """; @@ -37,11 +37,13 @@ public class SomeClass await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = { VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule).WithLocation(0).WithIsSuppressed(false) }, }.RunAsync(); await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = { VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule).WithLocation(0).WithIsSuppressed(true) }, }.RunAsync(); } @@ -58,7 +60,7 @@ public async Task AsyncDataTestMethodWithoutSuffix_DiagnosticIsSuppressed() public class SomeClass { [DataTestMethod, DataRow(0)] - public async Task [|TestMethod|](int arg) { } + public async Task {|#0:TestMethod|}(int arg) { } } """; @@ -66,11 +68,13 @@ public class SomeClass await new VerifyCS.Test { TestState = { Sources = { code } }, + ExpectedDiagnostics = { VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule).WithLocation(0).WithIsSuppressed(false) }, }.RunAsync(); await new TestWithSuppressor { TestState = { Sources = { code } }, + ExpectedDiagnostics = { VerifyCS.Diagnostic(WarnForMissingAsyncSuffix.Rule).WithLocation(0).WithIsSuppressed(true) }, }.RunAsync(); }