Skip to content

Commit

Permalink
Fix TestInitialize and TestCleanup analyzers to allow generic class (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Aug 2, 2024
1 parent 71a7f31 commit b095754
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
var methodSymbol = (IMethodSymbol)context.Symbol;
if (methodSymbol.IsTestCleanupMethod(testCleanupAttributeSymbol)
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
{
context.ReportDiagnostic(isFixable
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context, INamedTypeSymbo
var methodSymbol = (IMethodSymbol)context.Symbol;
if (methodSymbol.IsTestInitializeMethod(testInitializeAttributeSymbol)
&& !methodSymbol.HasValidFixtureMethodSignature(taskSymbol, valueTaskSymbol, canDiscoverInternals, shouldBeStatic: false,
allowGenericType: false, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
allowGenericType: true, testContextSymbol: null, testClassAttributeSymbol, fixtureAllowInheritedTestClass: true, out bool isFixable))
{
context.ReportDiagnostic(isFixable
? methodSymbol.CreateDiagnostic(Rule, methodSymbol.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,22 @@ public void TestCleanup()

await VerifyCS.VerifyAnalyzerAsync(code);
}

public async Task WhenTestCleanupIsOnGenericClass_NoDiagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass<T>
{
[TestCleanup]
public void TestCleanup()
{
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,22 @@ public void TestInitialize()

await VerifyCS.VerifyAnalyzerAsync(code);
}

public async Task WhenTestInitializeIsOnGenericClass_NoDiagnostic()
{
string code = """
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass<T>
{
[TestInitialize]
public void TestInitialize()
{
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
}
}

0 comments on commit b095754

Please sign in to comment.