-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xUnit2032 to soft-deprecate Assert.IsAssignableFrom
- Loading branch information
1 parent
c707020
commit 350ab93
Showing
14 changed files
with
286 additions
and
16 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
79 changes: 79 additions & 0 deletions
79
src/xunit.analyzers.fixes/X2000/AssignableFromAssertionIsConfusinglyNamedFixer.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,79 @@ | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Xunit.Analyzers.Fixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp), Shared] | ||
public class AssignableFromAssertionIsConfusinglyNamedFixer : BatchedCodeFixProvider | ||
{ | ||
public const string Key_UseIsType = "xUnit2032_UseIsType"; | ||
|
||
public AssignableFromAssertionIsConfusinglyNamedFixer() : | ||
base(Descriptors.X2032_AssignableFromAssertionIsConfusinglyNamed.Id) | ||
{ } | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
if (root is null) | ||
return; | ||
|
||
var invocation = root.FindNode(context.Span).FirstAncestorOrSelf<InvocationExpressionSyntax>(); | ||
if (invocation is null) | ||
return; | ||
|
||
var simpleNameSyntax = invocation.GetSimpleName(); | ||
if (simpleNameSyntax is null) | ||
return; | ||
|
||
var methodName = simpleNameSyntax.Identifier.Text; | ||
if (!AssignableFromAssertionIsConfusinglyNamed.ReplacementMethods.TryGetValue(methodName, out var replacementName)) | ||
return; | ||
|
||
context.RegisterCodeFix( | ||
XunitCodeAction.Create( | ||
ct => UseIsType(context.Document, invocation, simpleNameSyntax, replacementName, ct), | ||
Key_UseIsType, | ||
"Use Assert.{0}", replacementName | ||
), | ||
context.Diagnostics | ||
); | ||
} | ||
|
||
static async Task<Document> UseIsType( | ||
Document document, | ||
InvocationExpressionSyntax invocation, | ||
SimpleNameSyntax simpleName, | ||
string replacementName, | ||
CancellationToken cancellationToken) | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); | ||
|
||
editor.ReplaceNode( | ||
invocation, | ||
invocation | ||
.ReplaceNode( | ||
simpleName, | ||
simpleName.WithIdentifier(Identifier(replacementName)) | ||
) | ||
.WithArgumentList( | ||
invocation | ||
.ArgumentList | ||
.AddArguments( | ||
ParseArgumentList("false") | ||
.Arguments[0] | ||
.WithNameColon(NameColon("exactMatch")) | ||
) | ||
) | ||
); | ||
|
||
return editor.GetChangedDocument(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/xunit.analyzers.tests/Analyzers/X2000/AssignableFromAssertionIsConfusinglyNamedTests.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,76 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
using Xunit.Analyzers; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.AssignableFromAssertionIsConfusinglyNamed>; | ||
using Verify_v2_Pre2_9_3 = CSharpVerifier<AssignableFromAssertionIsConfusinglyNamedTests.Analyzer_v2_Pre2_9_3>; | ||
using Verify_v3_Pre0_6_0 = CSharpVerifier<AssignableFromAssertionIsConfusinglyNamedTests.Analyzer_v3_Pre0_6_0>; | ||
|
||
public class AssignableFromAssertionIsConfusinglyNamedTests | ||
{ | ||
public static TheoryData<string, string> Methods = new() | ||
{ | ||
{ "IsAssignableFrom", "IsType" }, | ||
{ "IsNotAssignableFrom", "IsNotType"}, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(Methods))] | ||
public async Task WhenReplacementAvailable_Triggers( | ||
string method, | ||
string replacement) | ||
{ | ||
var source = string.Format(/* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
class TestClass {{ | ||
void TestMethod() {{ | ||
{{|#0:Assert.{0}<object>(new object())|}}; | ||
{{|#1:Assert.{0}(typeof(object), new object())|}}; | ||
}} | ||
}} | ||
""", method); | ||
var expected = new[] { | ||
Verify.Diagnostic().WithLocation(0).WithArguments(method, replacement), | ||
Verify.Diagnostic().WithLocation(1).WithArguments(method, replacement), | ||
}; | ||
|
||
await Verify.VerifyAnalyzer(source, expected); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Methods))] | ||
public async Task WhenReplacementNotAvailable_DoesNotTriggers( | ||
string method, | ||
string _) | ||
{ | ||
var source = string.Format(/* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
class TestClass {{ | ||
void TestMethod() {{ | ||
Assert.{0}<object>(new object()); | ||
Assert.{0}(typeof(object), new object()); | ||
}} | ||
}} | ||
""", method); | ||
|
||
await Verify_v2_Pre2_9_3.VerifyAnalyzer(source); | ||
await Verify_v3_Pre0_6_0.VerifyAnalyzer(source); | ||
} | ||
|
||
internal class Analyzer_v2_Pre2_9_3 : AssignableFromAssertionIsConfusinglyNamed | ||
{ | ||
protected override XunitContext CreateXunitContext(Compilation compilation) => | ||
XunitContext.ForV2(compilation, new Version(2, 9, 2)); | ||
} | ||
|
||
internal class Analyzer_v3_Pre0_6_0 : AssignableFromAssertionIsConfusinglyNamed | ||
{ | ||
protected override XunitContext CreateXunitContext(Compilation compilation) => | ||
XunitContext.ForV3(compilation, new Version(0, 5, 999)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/xunit.analyzers.tests/Fixes/X2000/AssignableFromAssertionIsConfusinglyNamedFixerTests.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,42 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Analyzers.Fixes; | ||
using Verify = CSharpVerifier<Xunit.Analyzers.AssignableFromAssertionIsConfusinglyNamed>; | ||
|
||
public class AssignableFromAssertionIsConfusinglyNamedFixerTests | ||
{ | ||
[Fact] | ||
public async Task Conversions() | ||
{ | ||
var before = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = "Hello world"; | ||
[|Assert.IsAssignableFrom(typeof(object), data)|]; | ||
[|Assert.IsAssignableFrom<object>(data)|]; | ||
} | ||
} | ||
"""; | ||
var after = /* lang=c#-test */ """ | ||
using System; | ||
using Xunit; | ||
public class TestClass { | ||
[Fact] | ||
public void TestMethod() { | ||
var data = "Hello world"; | ||
Assert.IsType(typeof(object), data, exactMatch: false); | ||
Assert.IsType<object>(data, exactMatch: false); | ||
} | ||
} | ||
"""; | ||
|
||
await Verify.VerifyCodeFix(before, after, AssignableFromAssertionIsConfusinglyNamedFixer.Key_UseIsType); | ||
} | ||
} |
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
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
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
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
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
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
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
46 changes: 46 additions & 0 deletions
46
src/xunit.analyzers/X2000/AssignableFromAssertionIsConfusinglyNamed.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,46 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace Xunit.Analyzers; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class AssignableFromAssertionIsConfusinglyNamed : AssertUsageAnalyzerBase | ||
{ | ||
public static readonly Dictionary<string, string> ReplacementMethods = new() | ||
{ | ||
{ Constants.Asserts.IsAssignableFrom, Constants.Asserts.IsType }, | ||
{ Constants.Asserts.IsNotAssignableFrom, Constants.Asserts.IsNotType }, | ||
}; | ||
|
||
public AssignableFromAssertionIsConfusinglyNamed() : | ||
base(Descriptors.X2032_AssignableFromAssertionIsConfusinglyNamed, ReplacementMethods.Keys) | ||
{ } | ||
|
||
protected override void AnalyzeInvocation( | ||
OperationAnalysisContext context, | ||
XunitContext xunitContext, | ||
IInvocationOperation invocationOperation, | ||
IMethodSymbol method) | ||
{ | ||
Guard.ArgumentNotNull(xunitContext); | ||
Guard.ArgumentNotNull(invocationOperation); | ||
Guard.ArgumentNotNull(method); | ||
|
||
if (!xunitContext.Assert.SupportsInexactTypeAssertions) | ||
return; | ||
|
||
if (!ReplacementMethods.TryGetValue(invocationOperation.TargetMethod.Name, out var replacement)) | ||
return; | ||
|
||
context.ReportDiagnostic( | ||
Diagnostic.Create( | ||
Descriptors.X2032_AssignableFromAssertionIsConfusinglyNamed, | ||
invocationOperation.Syntax.GetLocation(), | ||
invocationOperation.TargetMethod.Name, | ||
replacement | ||
) | ||
); | ||
} | ||
} |
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
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