Skip to content

Commit ad3e43a

Browse files
committed
Fix TestInitialize and TestCleanup analyzers to allow generic class
1 parent a511e47 commit ad3e43a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Analyzers/MSTest.Analyzers/TestCleanupShouldBeValidAnalyzer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
5555
var methodSymbol = (IMethodSymbol)context.Symbol;
5656
if (methodSymbol.IsTestCleanupMethod(testCleanupAttributeSymbol)
5757
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
58-
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
58+
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
5959
{
6060
context.ReportDiagnostic(isFixable
6161
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)

src/Analyzers/MSTest.Analyzers/TestInitializeShouldBeValidAnalyzer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
5555
var methodSymbol = (IMethodSymbol)context.Symbol;
5656
if (methodSymbol.IsTestInitializeMethod(testInitializeAttributeSymbol)
5757
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
58-
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
58+
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
5959
{
6060
context.ReportDiagnostic(isFixable
6161
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)

test/UnitTests/MSTest.Analyzers.UnitTests/TestCleanupShouldBeValidAnalyzerTests.cs

+18
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,22 @@ public void TestCleanup()
515515

516516
await VerifyCS.VerifyAnalyzerAsync(code);
517517
}
518+
519+
public async Task WhenTestCleanupIsOnGenericClass_NoDiagnostic()
520+
{
521+
string code = """
522+
using Microsoft.VisualStudio.TestTools.UnitTesting;
523+
524+
[TestClass]
525+
public class MyTestClass<T>
526+
{
527+
[TestCleanup]
528+
public void TestCleanup()
529+
{
530+
}
531+
}
532+
""";
533+
534+
await VerifyCS.VerifyAnalyzerAsync(code);
535+
}
518536
}

test/UnitTests/MSTest.Analyzers.UnitTests/TestInitializeShouldBeValidAnalyzerTests.cs

+18
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,22 @@ public void TestInitialize()
513513

514514
await VerifyCS.VerifyAnalyzerAsync(code);
515515
}
516+
517+
public async Task WhenTestInitializeIsOnGenericClass_NoDiagnostic()
518+
{
519+
string code = """
520+
using Microsoft.VisualStudio.TestTools.UnitTesting;
521+
522+
[TestClass]
523+
public class MyTestClass<T>
524+
{
525+
[TestInitialize]
526+
public void TestInitialize()
527+
{
528+
}
529+
}
530+
""";
531+
532+
await VerifyCS.VerifyAnalyzerAsync(code);
533+
}
516534
}

0 commit comments

Comments
 (0)