Skip to content

Commit 593b7b8

Browse files
authored
analyzer cleanups (#265)
1 parent ff25189 commit 593b7b8

File tree

5 files changed

+94
-133
lines changed

5 files changed

+94
-133
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,15 @@ public class CollectionTests
305305
[AssertionDiagnostic("actual.ToList().Count().Should().Be(0{0}).And.ToString();")]
306306
[AssertionDiagnostic("actual.ToArray().Count().Should().Be(0{0}).And.ToString();")]
307307
[Implemented]
308-
public void CollectionShouldHaveCount_CountShouldBe0_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe0);
308+
public void CollectionShouldHaveCount_CountShouldBe0_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0);
309309

310310
[DataTestMethod]
311311
[AssertionDiagnostic("actual.Count().Should().Be(1{0});")]
312312
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(1{0}).And.ToString();")]
313313
[AssertionDiagnostic("actual.ToList().Count().Should().Be(1{0}).And.ToString();")]
314314
[AssertionDiagnostic("actual.ToArray().Count().Should().Be(1{0}).And.ToString();")]
315315
[Implemented]
316-
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe1);
316+
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1);
317317

318318
[DataTestMethod]
319319
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(1{0}).And.ToString();")]

src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
3030
public static DiagnosticMetadata CollectionShouldHaveCountLessThan_CountShouldBeLessThan { get; } = new("Use .Should().HaveCountLessThan()", GetHelpLink("Collections-12"));
3131
public static DiagnosticMetadata CollectionShouldHaveCountLessOrEqualTo_CountShouldBeLessOrEqualTo { get; } = new("Use .Should().HaveCountLessOrEqualTo()", GetHelpLink("Collections-13"));
3232
public static DiagnosticMetadata CollectionShouldNotHaveCount_CountShouldNotBe { get; } = new("Use .Should().NotHaveCount()", GetHelpLink("Collections-14"));
33-
public static DiagnosticMetadata CollectionShouldContainSingle_ShouldHaveCount1 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-15"));
34-
public static DiagnosticMetadata CollectionShouldHaveCount_CountShouldBe1 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-15"));
33+
public static DiagnosticMetadata CollectionShouldContainSingle_ShouldHaveCount1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
34+
public static DiagnosticMetadata CollectionShouldContainSingle_CountShouldBe1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
3535
public static DiagnosticMetadata CollectionShouldBeEmpty_ShouldHaveCount0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
36-
public static DiagnosticMetadata CollectionShouldHaveCount_CountShouldBe0 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-16"));
36+
public static DiagnosticMetadata CollectionShouldBeEmpty_CountShouldBe0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
3737
public static DiagnosticMetadata CollectionShouldHaveSameCount_ShouldHaveCountOtherCollectionCount { get; } = new("Use .Should().HaveSameCount()", GetHelpLink("Collections-17"));
3838
public static DiagnosticMetadata CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount { get; } = new("Use .Should().NotHaveSameCount()", GetHelpLink("Collections-18"));
3939
public static DiagnosticMetadata CollectionShouldContainProperty_WhereShouldNotBeEmpty { get; } = new("Use .Should().NotBeEmpty()", GetHelpLink("Collections-19"));
@@ -95,7 +95,6 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
9595
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowWhichMessageShouldEndWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));
9696
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowAndMessageShouldStartWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));
9797
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowWhichMessageShouldStartWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));
98-
9998
public static DiagnosticMetadata ExceptionShouldThrowWithInnerException_ShouldThrowAndInnerExceptionShouldBeOfType = new("Use .Should().Throw<TException>().WithInnerException<TInnerException>()", string.Empty);
10099
public static DiagnosticMetadata ExceptionShouldThrowWithInnerException_ShouldThrowWhichInnerExceptionShouldBeOfType = new("Use .Should().Throw<TException>().WithInnerException<TInnerException>()", string.Empty);
101100
public static DiagnosticMetadata ExceptionShouldThrowExactlyWithInnerException_ShouldThrowExactlyAndInnerExceptionShouldBeOfType = new("Use .Should().ThrowExactly<TException>().WithInnerException<TInnerException>()", string.Empty);

src/FluentAssertions.Analyzers/Tips/FluentAssertionsCodeFix.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ ExpressionSyntax GetCombinedAssertionsWithArgumentsReversedOrder(string remove,
105105
return RemoveAndRenameWithoutFirstArgumentWithArgumentsFromRemoved(remove: "Where", rename: "HaveCount", newName: "ContainSingle");
106106
case nameof(DiagnosticMetadata.CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect):
107107
return GetNewExpressionForSelectShouldEqualOtherCollectionSelectSyntaxVisitor(expression);
108-
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe0):
108+
case nameof(DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0):
109109
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "BeEmpty"));
110-
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe1):
110+
case nameof(DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1):
111111
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "ContainSingle"));
112112
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe):
113113
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));

src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.Utils.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ private static bool IsEnumerableMethodWithoutArguments(IInvocationOperation invo
1515
private static bool IsEnumerableMethodWithPredicate(IInvocationOperation invocation, FluentAssertionsMetadata metadata)
1616
=> invocation.IsContainedInType(metadata.Enumerable) && invocation.Arguments.Length == 2 && invocation.Arguments[1].IsLambda(); // invocation.Arguments[0] is `this` argument
1717

18-
private static bool TryGetExceptionPropertyAssertion(IInvocationOperation assertion, string fluentAssertionProperty, string exceptionProperty, out IInvocationOperation nextAssertion)
18+
private static bool TryGetExceptionPropertyAssertion(IInvocationOperation assertion, out string fluentAssertionProperty, out string exceptionProperty, out IInvocationOperation nextAssertion)
1919
{
20-
if (assertion.Parent is IPropertyReferenceOperation chainProperty && chainProperty.Property.Name.Equals(fluentAssertionProperty)
21-
&& chainProperty.Parent is IPropertyReferenceOperation exception && exception.Property.Name.Equals(exceptionProperty)
20+
if (assertion.Parent is IPropertyReferenceOperation chainProperty
21+
&& chainProperty.Parent is IPropertyReferenceOperation exception
2222
&& exception.Parent.UnwrapParentConversion() is IArgumentOperation argument
2323
&& argument.Parent is IInvocationOperation { TargetMethod.Name: "Should" } should)
2424
{
2525
nextAssertion = should.Parent as IInvocationOperation;
26+
fluentAssertionProperty = chainProperty.Property.Name;
27+
exceptionProperty = exception.Property.Name;
2628
return nextAssertion is not null;
2729
}
2830

2931
nextAssertion = default;
32+
fluentAssertionProperty = default;
33+
exceptionProperty = default;
3034
return false;
3135
}
3236

0 commit comments

Comments
 (0)