Skip to content

Commit 0441304

Browse files
authored
add support for xunit SetAsserts Subset (#236)
* add support for xunit SetAsserts Subset * add support for xunit SetAsserts Subset
1 parent 4364eda commit 0441304

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/XunitTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,20 @@ public void AssertStartsWith_TestAnalyzer(string assertion) =>
540540
public void AssertStartsWith_TestCodeFix(string oldAssertion, string newAssertion)
541541
=> VerifyCSharpFix<AssertStartsWithCodeFix, AssertStartsWithAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
542542

543+
[DataTestMethod]
544+
[DataRow("Assert.Subset(expected, actual);")]
545+
[Implemented]
546+
public void AssertSubset_TestAnalyzer(string assertion) =>
547+
VerifyCSharpDiagnostic<AssertSubsetAnalyzer>("ISet<string> actual, ISet<string> expected", assertion);
548+
549+
[DataTestMethod]
550+
[DataRow(
551+
/* oldAssertion: */ "Assert.Subset(expected, actual);",
552+
/* newAssertion: */ "actual.Should().BeSubsetOf(expected);")]
553+
[Implemented]
554+
public void AssertSubset_TestCodeFix(string oldAssertion, string newAssertion)
555+
=> VerifyCSharpFix<AssertSubsetCodeFix, AssertSubsetAnalyzer>("ISet<string> actual, ISet<string> expected", oldAssertion, newAssertion);
556+
543557
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
544558
{
545559
var source = GenerateCode.XunitAssertion(methodArguments, assertion);

src/FluentAssertions.Analyzers/Constants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public static class Xunit
136136
public const string AssertEmpty = $"{DiagnosticProperties.IdPrefix}0714";
137137
public const string AssertEndsWith = $"{DiagnosticProperties.IdPrefix}0715";
138138
public const string AssertStartsWith = $"{DiagnosticProperties.IdPrefix}0716";
139+
public const string AssertSubset = $"{DiagnosticProperties.IdPrefix}0717";
139140
}
140141
}
141142

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections.Generic;
2+
using System.Collections.Immutable;
3+
using System.Composition;
4+
using FluentAssertions.Analyzers.Utilities;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CodeFixes;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
8+
using Microsoft.CodeAnalysis.Diagnostics;
9+
10+
namespace FluentAssertions.Analyzers.Xunit;
11+
12+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13+
public class AssertSubsetAnalyzer : XunitAnalyzer
14+
{
15+
public const string DiagnosticId = Constants.Tips.Xunit.AssertSubset;
16+
public const string Category = Constants.Tips.Category;
17+
18+
public const string Message = "Use .Should().BeSubset()";
19+
20+
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
21+
22+
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[]
23+
{
24+
new AssertSubsetSyntaxVisitor()
25+
};
26+
27+
//public static void Subset(ISet expectedSubset, ISet? actual)
28+
public class AssertSubsetSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
29+
{
30+
public AssertSubsetSyntaxVisitor() : base(
31+
MemberValidator.ArgumentsMatch("Subset",
32+
ArgumentValidator.Exists(),
33+
ArgumentValidator.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_IEnumerable))
34+
)
35+
{
36+
}
37+
}
38+
}
39+
40+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertSubsetCodeFix)), Shared]
41+
public class AssertSubsetCodeFix : XunitCodeFixProvider
42+
{
43+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertSubsetAnalyzer.DiagnosticId);
44+
45+
protected override ExpressionSyntax GetNewExpression(
46+
ExpressionSyntax expression,
47+
FluentAssertionsDiagnosticProperties properties)
48+
{
49+
switch (properties.VisitorName)
50+
{
51+
case nameof(AssertSubsetAnalyzer.AssertSubsetSyntaxVisitor):
52+
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Subset", "BeSubsetOf");
53+
default:
54+
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)