From fefd4a9a9182955bf1a7c6078b1457b6dd226a91 Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 13 Dec 2022 10:03:15 +0100 Subject: [PATCH 1/4] Add test case for derived methods --- .../TestMethodShouldContainAssertion.Custom.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs index 2aa8bb44138..0e42ada8882 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs @@ -41,6 +41,13 @@ public class Program validator.InstanceWithAttribute(); } + [TestMethod] + public void DerivedValidationMethod() // Compliant + { + var validator = new DerivedValidator(); + validator.InstanceWithAttribute(); + } + [TestMethod] public void TestMethod6() => // Compliant new Validator().InstanceWithAttributeAndArg(null); @@ -79,6 +86,10 @@ public class Validator public bool InstanceWithAttributeAndArg(object arg) => true; } + public class DerivedValidator: Validator + { + } + [AssertionMethod] // Missused attribute public static class AttributedType { @@ -88,7 +99,8 @@ public static class AttributedType namespace TestFramework.Attributes { - public class AssertionMethodAttribute : Attribute { } + [AttributeUsage(AttributeTargets.Method | /* for testing misplaced attributes */ AttributeTargets.Class, Inherited = true)] + public sealed class AssertionMethodAttribute : Attribute { } public class NotAssertionMethodAttribute : Attribute { } // AssertionMethodAttribute doesn't count as an assertion method attribute public class DerivedExpectedExceptionAttribute : ExpectedExceptionBaseAttribute { protected override void Verify(Exception exception) { } } } From 2c6102718e5e80435f9a880262bb6580496717cf Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 13 Dec 2022 12:26:37 +0100 Subject: [PATCH 2/4] Add a test case for CustomAssertion on overridden method. --- ...TestMethodShouldContainAssertion.Custom.cs | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs index 0e42ada8882..fb6fe0366a2 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(T t) { } + [TestMethod] public void TestMethod1() // Noncompliant {{Add at least one assertion to this test case.}} -// ^^^^^^^^^^^ + // ^^^^^^^^^^^ { var x = 42; } @@ -41,13 +44,6 @@ public class Program validator.InstanceWithAttribute(); } - [TestMethod] - public void DerivedValidationMethod() // Compliant - { - var validator = new DerivedValidator(); - validator.InstanceWithAttribute(); - } - [TestMethod] public void TestMethod6() => // Compliant new Validator().InstanceWithAttributeAndArg(null); @@ -63,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(new object()); + } + + protected override void CustomAssertionMethod(T t) + { + } + } } namespace TestFramework @@ -86,10 +96,6 @@ public class Validator public bool InstanceWithAttributeAndArg(object arg) => true; } - public class DerivedValidator: Validator - { - } - [AssertionMethod] // Missused attribute public static class AttributedType { @@ -99,7 +105,6 @@ public static class AttributedType namespace TestFramework.Attributes { - [AttributeUsage(AttributeTargets.Method | /* for testing misplaced attributes */ AttributeTargets.Class, Inherited = true)] public sealed class AssertionMethodAttribute : Attribute { } public class NotAssertionMethodAttribute : Attribute { } // AssertionMethodAttribute doesn't count as an assertion method attribute public class DerivedExpectedExceptionAttribute : ExpectedExceptionBaseAttribute { protected override void Verify(Exception exception) { } } From f2137e355cc64503b26d2145f568e556ec1f52ac Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 13 Dec 2022 12:28:18 +0100 Subject: [PATCH 3/4] Remove generics --- .../TestCases/TestMethodShouldContainAssertion.Custom.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs index fb6fe0366a2..90f4b648a7d 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs @@ -10,7 +10,7 @@ namespace CustomTests public class BaseTest { [AssertionMethod] - protected virtual void CustomAssertionMethod(T t) { } + protected virtual void CustomAssertionMethod() { } [TestMethod] public void TestMethod1() // Noncompliant {{Add at least one assertion to this test case.}} @@ -66,10 +66,10 @@ 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(new object()); + CustomAssertionMethod(); } - protected override void CustomAssertionMethod(T t) + protected override void CustomAssertionMethod() { } } From ecfc13e45cf67f7f433f165fbd33c9ed1f09143b Mon Sep 17 00:00:00 2001 From: Martin Strecker Date: Tue, 13 Dec 2022 12:31:34 +0100 Subject: [PATCH 4/4] Remove sealed --- .../TestCases/TestMethodShouldContainAssertion.Custom.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs index 90f4b648a7d..982c4c2dc55 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/TestMethodShouldContainAssertion.Custom.cs @@ -105,7 +105,7 @@ public static class AttributedType namespace TestFramework.Attributes { - public sealed class AssertionMethodAttribute : Attribute { } + public class AssertionMethodAttribute : Attribute { } public class NotAssertionMethodAttribute : Attribute { } // AssertionMethodAttribute doesn't count as an assertion method attribute public class DerivedExpectedExceptionAttribute : ExpectedExceptionBaseAttribute { protected override void Verify(Exception exception) { } } }