Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for xunit SetAsserts Subset #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@ public void AssertStartsWith_TestAnalyzer(string assertion) =>
public void AssertStartsWith_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertStartsWithCodeFix, AssertStartsWithAnalyzer>("string actual, string expected", oldAssertion, newAssertion);

[DataTestMethod]
[DataRow("Assert.Subset(expected, actual);")]
[Implemented]
public void AssertSubset_TestAnalyzer(string assertion) =>
VerifyCSharpDiagnostic<AssertSubsetAnalyzer>("ISet<string> actual, ISet<string> expected", assertion);

[DataTestMethod]
[DataRow(
/* oldAssertion: */ "Assert.Subset(expected, actual);",
/* newAssertion: */ "actual.Should().BeSubsetOf(expected);")]
[Implemented]
public void AssertSubset_TestCodeFix(string oldAssertion, string newAssertion)
=> VerifyCSharpFix<AssertSubsetCodeFix, AssertSubsetAnalyzer>("ISet<string> actual, ISet<string> expected", oldAssertion, newAssertion);

private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
{
var source = GenerateCode.XunitAssertion(methodArguments, assertion);
Expand Down
1 change: 1 addition & 0 deletions src/FluentAssertions.Analyzers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static class Xunit
public const string AssertEmpty = $"{DiagnosticProperties.IdPrefix}0714";
public const string AssertEndsWith = $"{DiagnosticProperties.IdPrefix}0715";
public const string AssertStartsWith = $"{DiagnosticProperties.IdPrefix}0716";
public const string AssertSubset = $"{DiagnosticProperties.IdPrefix}0717";
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/Xunit/AssertSubset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using FluentAssertions.Analyzers.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace FluentAssertions.Analyzers.Xunit;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AssertSubsetAnalyzer : XunitAnalyzer
{
public const string DiagnosticId = Constants.Tips.Xunit.AssertSubset;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().BeSubset()";

protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);

protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[]
{
new AssertSubsetSyntaxVisitor()
};

//public static void Subset(ISet expectedSubset, ISet? actual)
public class AssertSubsetSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AssertSubsetSyntaxVisitor() : base(
MemberValidator.ArgumentsMatch("Subset",
ArgumentValidator.Exists(),
ArgumentValidator.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_IEnumerable))
)
{
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertSubsetCodeFix)), Shared]
public class AssertSubsetCodeFix : XunitCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertSubsetAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(
ExpressionSyntax expression,
FluentAssertionsDiagnosticProperties properties)
{
switch (properties.VisitorName)
{
case nameof(AssertSubsetAnalyzer.AssertSubsetSyntaxVisitor):
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Subset", "BeSubsetOf");
default:
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
}
}
}
Loading