Skip to content

Commit dba288b

Browse files
authored
add support for StringAsserts DoesNotContain (#223)
1 parent 53a18f2 commit dba288b

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ public void AssertStringContains_TestAnalyzer(string assertion) =>
354354
public void AssertStringContains_TestCodeFix(string oldAssertion, string newAssertion)
355355
=> VerifyCSharpFix<AssertContainsCodeFix, AssertContainsAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
356356

357+
[DataTestMethod]
358+
[DataRow("Assert.DoesNotContain(expected, actual);")]
359+
[Implemented]
360+
public void AssertStringDoesNotContain_TestAnalyzer(string assertion) =>
361+
VerifyCSharpDiagnostic<AssertDoesNotContainAnalyzer>("string actual, string expected", assertion);
362+
363+
[DataTestMethod]
364+
[DataRow(
365+
/* oldAssertion: */ "Assert.DoesNotContain(expected, actual);",
366+
/* newAssertion: */ "actual.Should().NotContain(expected);")]
367+
[Implemented]
368+
public void AssertStringDoesNotContain_TestCodeFix(string oldAssertion, string newAssertion)
369+
=> VerifyCSharpFix<AssertDoesNotContainCodeFix, AssertDoesNotContainAnalyzer>("string actual, string expected", oldAssertion, newAssertion);
370+
371+
357372
private void VerifyCSharpDiagnostic<TDiagnosticAnalyzer>(string methodArguments, string assertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
358373
{
359374
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
@@ -130,6 +130,7 @@ public static class Xunit
130130
public const string AssertNull = $"{DiagnosticProperties.IdPrefix}0708";
131131
public const string AssertNotNull = $"{DiagnosticProperties.IdPrefix}0709";
132132
public const string AssertContains = $"{DiagnosticProperties.IdPrefix}0710";
133+
public const string AssertDoesNotContain = $"{DiagnosticProperties.IdPrefix}0711";
133134
}
134135
}
135136

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

0 commit comments

Comments
 (0)