From 181217200140b59fb82615788a571138dd947f9d Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Tue, 28 Nov 2023 17:06:27 +0800 Subject: [PATCH] Allow public override methods. --- ...nTestMethodAccessibilityLevelAnalyzerTests.cs | 16 ++++++++++++++++ .../NonTestMethodAccessibilityLevelAnalyzer.cs | 12 ++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/nunit.analyzers.tests/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzerTests.cs b/src/nunit.analyzers.tests/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzerTests.cs index 1180678f..74e6cb8c 100644 --- a/src/nunit.analyzers.tests/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzerTests.cs +++ b/src/nunit.analyzers.tests/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzerTests.cs @@ -172,5 +172,21 @@ public override void Dispose() { } RoslynAssert.Valid(analyzer, testCode); } + + [Test] + public void AnalyzeWhenMethodIsBaseClassOverride() + { + var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@" + public sealed class MyTestClass + { + [Test] + public void TestMethod() { } + + public override string ToString() => ""My Test Class""; + } + "); + + RoslynAssert.Valid(analyzer, testCode); + } } } diff --git a/src/nunit.analyzers/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzer.cs b/src/nunit.analyzers/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzer.cs index d35fd8cf..1c82c568 100644 --- a/src/nunit.analyzers/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzer.cs +++ b/src/nunit.analyzers/NonTestMethodAccessibilityLevel/NonTestMethodAccessibilityLevelAnalyzer.cs @@ -45,7 +45,7 @@ private static void AnalyzeType(SymbolAnalysisContext context) { if (method.IsTestRelatedMethod(context.Compilation)) hasTestMethods = true; - else if (IsPublicOrInternalMethod(method) && !IsDisposeMethod(method)) + else if (IsPublicOrInternalMethod(method) && !IsOverride(method) && !IsDisposeMethod(method)) publicNonTestMethods.Add(method); } @@ -74,13 +74,13 @@ private static bool IsPublicOrInternalMethod(IMethodSymbol method) } } - private static bool IsDisposeMethod(IMethodSymbol method) + private static bool IsOverride(IMethodSymbol method) { - if (method.OverriddenMethod is not null) - { - return IsDisposeMethod(method.OverriddenMethod); - } + return method.IsOverride; + } + private static bool IsDisposeMethod(IMethodSymbol method) + { return method.IsInterfaceImplementation("System.IDisposable"); } }