-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for xunit TypeAsserts IsAssignableFrom (#238)
* add support for xunit TypeAsserts IsAssignableFrom * add support for xunit TypeAsserts IsAssignableFrom
- Loading branch information
Showing
4 changed files
with
113 additions
and
1 deletion.
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
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
89 changes: 89 additions & 0 deletions
89
src/FluentAssertions.Analyzers/Tips/Xunit/AssertIsAssignableFrom.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,89 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using FluentAssertions.Analyzers.Utilities; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using SF = Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace FluentAssertions.Analyzers.Xunit; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class AssertIsAssignableFromAnalyzer : XunitAnalyzer | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Xunit.AssertIsAssignableFrom; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().BeAssignableTo()."; | ||
|
||
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[] | ||
{ | ||
new AssertIsAssignableFromGenericTypeSyntaxVisitor(), | ||
new AssertIsAssignableFromTypeSyntaxVisitor() | ||
}; | ||
|
||
//public static T IsAssignableFrom<T>(object? @object) | ||
public class AssertIsAssignableFromGenericTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertIsAssignableFromGenericTypeSyntaxVisitor() : base( | ||
MemberValidator.HasArguments("IsAssignableFrom", 1) | ||
) | ||
{ | ||
} | ||
} | ||
|
||
//public static T IsAssignableFrom(Type expectedType, object? @object) | ||
public class AssertIsAssignableFromTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertIsAssignableFromTypeSyntaxVisitor() : base( | ||
MemberValidator.HasArguments("IsAssignableFrom", 2) | ||
) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertIsAssignableFromCodeFix)), Shared] | ||
public class AssertIsAssignableFromCodeFix : XunitCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertIsAssignableFromAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression( | ||
ExpressionSyntax expression, | ||
FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
switch (properties.VisitorName) | ||
{ | ||
case nameof(AssertIsAssignableFromAnalyzer.AssertIsAssignableFromGenericTypeSyntaxVisitor): | ||
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsAssignableFrom", "BeAssignableTo"); | ||
case nameof(AssertIsAssignableFromAnalyzer.AssertIsAssignableFromTypeSyntaxVisitor): | ||
var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "IsAssignableFrom", "BeAssignableTo"); | ||
|
||
var beAssignableTo = newExpression.DescendantNodes() | ||
.OfType<MemberAccessExpressionSyntax>() | ||
.First(node => node.Name.Identifier.Text == "BeAssignableTo"); | ||
|
||
if (beAssignableTo.Parent is InvocationExpressionSyntax invocation) | ||
{ | ||
var arguments = invocation.ArgumentList.Arguments; | ||
if (arguments.Any() && arguments[0].Expression is TypeOfExpressionSyntax typeOfExpression) | ||
{ | ||
var genericBeOfType = beAssignableTo.WithName(SF.GenericName(beAssignableTo.Name.Identifier.Text) | ||
.AddTypeArgumentListArguments(typeOfExpression.Type) | ||
); | ||
newExpression = newExpression.ReplaceNode(beAssignableTo, genericBeOfType); | ||
return GetNewExpression(newExpression, NodeReplacement.RemoveFirstArgument("BeAssignableTo")); | ||
} | ||
} | ||
|
||
return newExpression; | ||
default: | ||
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); | ||
} | ||
} | ||
} |