Skip to content

Commit 9dd2e5d

Browse files
authored
add support for xunit StringAsserts StartsWith (#228)
1 parent 9680733 commit 9dd2e5d

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,20 @@ public void AssertEndsWith_TestAnalyzer(string assertion) =>
452452
public void AssertEndsWith_TestCodeFix(string oldAssertion, string newAssertion)
453453
=> VerifyCSharpFix<AssertEndsWithCodeFix, AssertEndsWithAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
454454

455+
[DataTestMethod]
456+
[DataRow("Assert.StartsWith(expected, actual);")]
457+
[Implemented]
458+
public void AssertStartsWith_TestAnalyzer(string assertion) =>
459+
VerifyCSharpDiagnostic<AssertStartsWithAnalyzer>("string actual, string expected", assertion);
460+
461+
[DataTestMethod]
462+
[DataRow(
463+
/* oldAssertion: */ "Assert.StartsWith(expected, actual);",
464+
/* newAssertion: */ "actual.Should().StartWith(expected);")]
465+
[Implemented]
466+
public void AssertStartsWith_TestCodeFix(string oldAssertion, string newAssertion)
467+
=> VerifyCSharpFix<AssertStartsWithCodeFix, AssertStartsWithAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
468+
455469
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
456470
{
457471
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
@@ -135,6 +135,7 @@ public static class Xunit
135135
public const string AssertDoesNotMatch = $"{DiagnosticProperties.IdPrefix}0713";
136136
public const string AssertEmpty = $"{DiagnosticProperties.IdPrefix}0714";
137137
public const string AssertEndsWith = $"{DiagnosticProperties.IdPrefix}0715";
138+
public const string AssertStartsWith = $"{DiagnosticProperties.IdPrefix}0716";
138139
}
139140
}
140141

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

0 commit comments

Comments
 (0)