From 222715082f673a7ba6b111cf6d042539c486b960 Mon Sep 17 00:00:00 2001 From: Martin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:06:07 +0100 Subject: [PATCH] S2699 Reproducer for AssertionMethodAttribute is ignored when assertion method is inherited (#6528) * Add test case for derived methods * Add a test case for CustomAssertion on overridden method. * Remove generics * Remove sealed --- ...TestMethodShouldContainAssertion.Custom.cs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs index 2aa8bb44138..982c4c2dc55 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs @@ -7,11 +7,14 @@ namespace CustomTests using TestFramework.Attributes; [TestClass] - public class Program + public class BaseTest { + [AssertionMethod] + protected virtual void CustomAssertionMethod() { } + [TestMethod] public void TestMethod1() // Noncompliant {{Add at least one assertion to this test case.}} -// ^^^^^^^^^^^ + // ^^^^^^^^^^^ { var x = 42; } @@ -56,6 +59,20 @@ public class Program var x = 42; } } + + [TestClass] + public class DerivedTest : BaseTest + { + [TestMethod] + public void Derived() // Noncompliant FP: The overridden method needs to be annotated because Roslyn does not respect AttributeUsage.Inherited in ISymbol.GetAttributes + { + CustomAssertionMethod(); + } + + protected override void CustomAssertionMethod() + { + } + } } namespace TestFramework