-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analyzer and fixer for test classes nested in generic classes (#159)
- Loading branch information
1 parent
49851f3
commit 34d10d5
Showing
6 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/xunit.analyzers.fixes/X1000/TestClassCannotBeNestedInGenericClassFixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Xunit.Analyzers.Fixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class TestClassCannotBeNestedInGenericClassFixer : BatchedCodeFixProvider | ||
{ | ||
public const string Key_ExtractTestClass = "xUnit1032_TestClassCannotBeNestedInGenericClass"; | ||
|
||
public TestClassCannotBeNestedInGenericClassFixer() : | ||
base(Descriptors.X1032_TestClassCannotBeNestedInGenericClass.Id) | ||
{ } | ||
|
||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
if (root is null) | ||
return; | ||
|
||
var classDeclaration = root.FindNode(context.Span).FirstAncestorOrSelf<ClassDeclarationSyntax>(); | ||
if (classDeclaration is null) | ||
return; | ||
|
||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
"Extract test class from parent class", | ||
ct => context.Document.ExtractNodeFromParent(classDeclaration, ct), | ||
Key_ExtractTestClass | ||
), | ||
context.Diagnostics | ||
); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/xunit.analyzers.tests/Analyzers/X1000/TestClassCannotBeNestedInGenericClassTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.TestClassCannotBeNestedInGenericClass>; | ||
|
||
public class TestClassCannotBeNestedInGenericClassTests | ||
{ | ||
[Fact] | ||
public async Task ReportsDiagnostic_WhenTestClassIsNestedInOpenGenericType() | ||
{ | ||
var source = @" | ||
public abstract class OpenGenericType<T> | ||
{ | ||
public class NestedTestClass | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
} | ||
}"; | ||
|
||
var expected = | ||
Verify | ||
.Diagnostic() | ||
.WithLocation(4, 18); | ||
|
||
await Verify.VerifyAnalyzer(source, expected); | ||
} | ||
|
||
[Fact] | ||
public async Task ReportsDiagnostic_WhenDerivedTestClassIsNestedInOpenGenericType() | ||
{ | ||
var source = @" | ||
public abstract class BaseTestClass | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
} | ||
public abstract class OpenGenericType<T> | ||
{ | ||
public class NestedTestClass : BaseTestClass | ||
{ | ||
} | ||
}"; | ||
|
||
var expected = | ||
Verify | ||
.Diagnostic() | ||
.WithLocation(10, 18); | ||
|
||
await Verify.VerifyAnalyzer(source, expected); | ||
} | ||
|
||
[Fact] | ||
public async Task DoesNotReportDiagnostic_WhenTestClassIsNestedInClosedGenericType() | ||
{ | ||
var source = @" | ||
public abstract class OpenGenericType<T> | ||
{ | ||
} | ||
public abstract class ClosedGenericType : OpenGenericType<int> | ||
{ | ||
public class NestedTestClass | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
} | ||
}"; | ||
|
||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Fact] | ||
public async Task DoesNotReportDiagnostic_WhenNestedClassIsNotTestClass() | ||
{ | ||
var source = @" | ||
public abstract class OpenGenericType<T> | ||
{ | ||
public class NestedClass | ||
{ | ||
} | ||
}"; | ||
|
||
await Verify.VerifyAnalyzer(source); | ||
} | ||
|
||
[Fact] | ||
public async Task DoesNotReportDiagnostic_WhenTestClassIsNotNestedInOpenGenericType() | ||
{ | ||
var source = @" | ||
public abstract class NonGenericType | ||
{ | ||
public class NestedTestClass | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
} | ||
}"; | ||
|
||
await Verify.VerifyAnalyzer(source); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/xunit.analyzers.tests/Fixes/X1000/TestClassCannotBeNestedInGenericClassFixerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Xunit; | ||
using Xunit.Analyzers.Fixes; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.TestClassCannotBeNestedInGenericClass>; | ||
|
||
public class TestClassCannotBeNestedInGenericClassFixerTests | ||
{ | ||
[Fact] | ||
public async void MovesTestClassOutOfGenericParent() | ||
{ | ||
const string before = @" | ||
public abstract class OpenGenericType<T> | ||
{ | ||
public class [|NestedTestClass|] | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
} | ||
}"; | ||
const string after = @" | ||
public abstract class OpenGenericType<T> | ||
{ | ||
} | ||
public class NestedTestClass | ||
{ | ||
[Xunit.Fact] | ||
public void TestMethod() { } | ||
}"; | ||
|
||
await Verify.VerifyCodeFix(before, after, TestClassCannotBeNestedInGenericClassFixer.Key_ExtractTestClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/xunit.analyzers/X1000/TestClassCannotBeNestedInGenericClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class TestClassCannotBeNestedInGenericClass : XunitDiagnosticAnalyzer | ||
{ | ||
public TestClassCannotBeNestedInGenericClass() : | ||
base(Descriptors.X1032_TestClassCannotBeNestedInGenericClass) | ||
{ } | ||
|
||
public override void AnalyzeCompilation( | ||
CompilationStartAnalysisContext context, | ||
XunitContext xunitContext) | ||
{ | ||
context.RegisterSymbolAction(context => | ||
{ | ||
if (xunitContext.Core.FactAttributeType is null) | ||
return; | ||
if (context.Symbol is not INamedTypeSymbol classSymbol) | ||
return; | ||
if (classSymbol.ContainingType is null) | ||
return; | ||
if (!classSymbol.ContainingType.IsGenericType) | ||
return; | ||
var doesClassContainTests = DoesInheritenceTreeContainTests(classSymbol, xunitContext, depth: 3); | ||
if (!doesClassContainTests) | ||
return; | ||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
Descriptors.X1032_TestClassCannotBeNestedInGenericClass, | ||
classSymbol.Locations.First() | ||
) | ||
); | ||
}, SymbolKind.NamedType); | ||
} | ||
|
||
private bool DoesInheritenceTreeContainTests( | ||
INamedTypeSymbol classSymbol, | ||
XunitContext xunitContext, | ||
int depth) | ||
{ | ||
var doesClassContainTests = | ||
classSymbol | ||
.GetMembers() | ||
.OfType<IMethodSymbol>() | ||
.Any(m => m.GetAttributes().Any(a => xunitContext.Core.FactAttributeType.IsAssignableFrom(a.AttributeClass))); | ||
|
||
if (!doesClassContainTests && classSymbol.BaseType is not null && depth > 0) | ||
return DoesInheritenceTreeContainTests(classSymbol.BaseType, xunitContext, depth - 1); | ||
|
||
return doesClassContainTests; | ||
} | ||
} |