Skip to content

Commit 9680733

Browse files
authored
add support for xunit StringAsserts EndsWith (#227)
* add support for xunit StringAsserts EndsWith * add support for xunit StringAsserts EndsWith * add support for xunit StringAsserts EndsWith
1 parent bb52ea8 commit 9680733

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@ public void AssertEmpty_String_TestAnalyzer(string assertion) =>
438438
public void AssertEmpty_String_TestCodeFix(string oldAssertion, string newAssertion)
439439
=> VerifyCSharpFix<AssertEmptyCodeFix, AssertEmptyAnalyzer>("string actual", oldAssertion, newAssertion);
440440

441+
[DataTestMethod]
442+
[DataRow("Assert.EndsWith(expected, actual);")]
443+
[Implemented]
444+
public void AssertEndsWith_TestAnalyzer(string assertion) =>
445+
VerifyCSharpDiagnostic<AssertEndsWithAnalyzer>("string actual, string expected", assertion);
446+
447+
[DataTestMethod]
448+
[DataRow(
449+
/* oldAssertion: */ "Assert.EndsWith(expected, actual);",
450+
/* newAssertion: */ "actual.Should().EndWith(expected);")]
451+
[Implemented]
452+
public void AssertEndsWith_TestCodeFix(string oldAssertion, string newAssertion)
453+
=> VerifyCSharpFix<AssertEndsWithCodeFix, AssertEndsWithAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
454+
441455
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
442456
{
443457
var source = GenerateCode.XunitAssertion(methodArguments, assertion);

src/FluentAssertions.Analyzers/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public static class Xunit
134134
public const string AssertMatches = $"{DiagnosticProperties.IdPrefix}0712";
135135
public const string AssertDoesNotMatch = $"{DiagnosticProperties.IdPrefix}0713";
136136
public const string AssertEmpty = $"{DiagnosticProperties.IdPrefix}0714";
137+
public const string AssertEndsWith = $"{DiagnosticProperties.IdPrefix}0715";
137138
}
138139
}
139140

src/FluentAssertions.Analyzers/Tips/Xunit/AssertEmpty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AssertEmptyAnalyzer : XunitAnalyzer
1515
public const string DiagnosticId = Constants.Tips.Xunit.AssertEmpty;
1616
public const string Category = Constants.Tips.Category;
1717

18-
public const string Message = "Use .Should().NotMatchRegex()";
18+
public const string Message = "Use .Should().BeEmpty()";
1919

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

Lines changed: 57 additions & 0 deletions
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 AssertEndsWithAnalyzer : XunitAnalyzer
14+
{
15+
public const string DiagnosticId = Constants.Tips.Xunit.AssertEndsWith;
16+
public const string Category = Constants.Tips.Category;
17+
18+
public const string Message = "Use .Should().EndWith()";
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 AssertEndsWithStringSyntaxVisitor()
25+
};
26+
27+
//public static void EndsWith(string? expectedEndString, string? actualString)
28+
public class AssertEndsWithStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
29+
{
30+
public AssertEndsWithStringSyntaxVisitor() : base(
31+
MemberValidator.ArgumentsMatch("EndsWith",
32+
ArgumentValidator.IsType(TypeSelector.GetStringType),
33+
ArgumentValidator.IsType(TypeSelector.GetStringType))
34+
)
35+
{
36+
}
37+
}
38+
}
39+
40+
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertEndsWithCodeFix)), Shared]
41+
public class AssertEndsWithCodeFix : XunitCodeFixProvider
42+
{
43+
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertEndsWithAnalyzer.DiagnosticId);
44+
45+
protected override ExpressionSyntax GetNewExpression(
46+
ExpressionSyntax expression,
47+
FluentAssertionsDiagnosticProperties properties)
48+
{
49+
switch (properties.VisitorName)
50+
{
51+
case nameof(AssertEndsWithAnalyzer.AssertEndsWithStringSyntaxVisitor):
52+
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "EndsWith", "EndWith");
53+
default:
54+
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)