Skip to content

Commit

Permalink
analyzer cleanups (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored Dec 18, 2023
1 parent ff25189 commit 593b7b8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 133 deletions.
4 changes: 2 additions & 2 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,15 @@ public class CollectionTests
[AssertionDiagnostic("actual.ToList().Count().Should().Be(0{0}).And.ToString();")]
[AssertionDiagnostic("actual.ToArray().Count().Should().Be(0{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveCount_CountShouldBe0_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe0);
public void CollectionShouldHaveCount_CountShouldBe0_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0);

[DataTestMethod]
[AssertionDiagnostic("actual.Count().Should().Be(1{0});")]
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(1{0}).And.ToString();")]
[AssertionDiagnostic("actual.ToList().Count().Should().Be(1{0}).And.ToString();")]
[AssertionDiagnostic("actual.ToArray().Count().Should().Be(1{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe1);
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1);

[DataTestMethod]
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(1{0}).And.ToString();")]
Expand Down
7 changes: 3 additions & 4 deletions src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
public static DiagnosticMetadata CollectionShouldHaveCountLessThan_CountShouldBeLessThan { get; } = new("Use .Should().HaveCountLessThan()", GetHelpLink("Collections-12"));
public static DiagnosticMetadata CollectionShouldHaveCountLessOrEqualTo_CountShouldBeLessOrEqualTo { get; } = new("Use .Should().HaveCountLessOrEqualTo()", GetHelpLink("Collections-13"));
public static DiagnosticMetadata CollectionShouldNotHaveCount_CountShouldNotBe { get; } = new("Use .Should().NotHaveCount()", GetHelpLink("Collections-14"));
public static DiagnosticMetadata CollectionShouldContainSingle_ShouldHaveCount1 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-15"));
public static DiagnosticMetadata CollectionShouldHaveCount_CountShouldBe1 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-15"));
public static DiagnosticMetadata CollectionShouldContainSingle_ShouldHaveCount1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
public static DiagnosticMetadata CollectionShouldContainSingle_CountShouldBe1 { get; } = new("Use .Should().ContainSingle()", GetHelpLink("Collections-15"));
public static DiagnosticMetadata CollectionShouldBeEmpty_ShouldHaveCount0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
public static DiagnosticMetadata CollectionShouldHaveCount_CountShouldBe0 { get; } = new("Use .Should().HaveCount()", GetHelpLink("Collections-16"));
public static DiagnosticMetadata CollectionShouldBeEmpty_CountShouldBe0 { get; } = new("Use .Should().BeEmpty()", GetHelpLink("Collections-16"));
public static DiagnosticMetadata CollectionShouldHaveSameCount_ShouldHaveCountOtherCollectionCount { get; } = new("Use .Should().HaveSameCount()", GetHelpLink("Collections-17"));
public static DiagnosticMetadata CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount { get; } = new("Use .Should().NotHaveSameCount()", GetHelpLink("Collections-18"));
public static DiagnosticMetadata CollectionShouldContainProperty_WhereShouldNotBeEmpty { get; } = new("Use .Should().NotBeEmpty()", GetHelpLink("Collections-19"));
Expand Down Expand Up @@ -95,7 +95,6 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowWhichMessageShouldEndWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowAndMessageShouldStartWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));
public static DiagnosticMetadata ExceptionShouldThrowWithMessage_ShouldThrowWhichMessageShouldStartWith = new("Use .Should().Throw<TException>().WithMessage()", GetHelpLink("Exceptions-2"));

public static DiagnosticMetadata ExceptionShouldThrowWithInnerException_ShouldThrowAndInnerExceptionShouldBeOfType = new("Use .Should().Throw<TException>().WithInnerException<TInnerException>()", string.Empty);
public static DiagnosticMetadata ExceptionShouldThrowWithInnerException_ShouldThrowWhichInnerExceptionShouldBeOfType = new("Use .Should().Throw<TException>().WithInnerException<TInnerException>()", string.Empty);
public static DiagnosticMetadata ExceptionShouldThrowExactlyWithInnerException_ShouldThrowExactlyAndInnerExceptionShouldBeOfType = new("Use .Should().ThrowExactly<TException>().WithInnerException<TInnerException>()", string.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ ExpressionSyntax GetCombinedAssertionsWithArgumentsReversedOrder(string remove,
return RemoveAndRenameWithoutFirstArgumentWithArgumentsFromRemoved(remove: "Where", rename: "HaveCount", newName: "ContainSingle");
case nameof(DiagnosticMetadata.CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect):
return GetNewExpressionForSelectShouldEqualOtherCollectionSelectSyntaxVisitor(expression);
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe0):
case nameof(DiagnosticMetadata.CollectionShouldBeEmpty_CountShouldBe0):
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "BeEmpty"));
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe1):
case nameof(DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1):
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "ContainSingle"));
case nameof(DiagnosticMetadata.CollectionShouldHaveCount_CountShouldBe):
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ private static bool IsEnumerableMethodWithoutArguments(IInvocationOperation invo
private static bool IsEnumerableMethodWithPredicate(IInvocationOperation invocation, FluentAssertionsMetadata metadata)
=> invocation.IsContainedInType(metadata.Enumerable) && invocation.Arguments.Length == 2 && invocation.Arguments[1].IsLambda(); // invocation.Arguments[0] is `this` argument

private static bool TryGetExceptionPropertyAssertion(IInvocationOperation assertion, string fluentAssertionProperty, string exceptionProperty, out IInvocationOperation nextAssertion)
private static bool TryGetExceptionPropertyAssertion(IInvocationOperation assertion, out string fluentAssertionProperty, out string exceptionProperty, out IInvocationOperation nextAssertion)
{
if (assertion.Parent is IPropertyReferenceOperation chainProperty && chainProperty.Property.Name.Equals(fluentAssertionProperty)
&& chainProperty.Parent is IPropertyReferenceOperation exception && exception.Property.Name.Equals(exceptionProperty)
if (assertion.Parent is IPropertyReferenceOperation chainProperty
&& chainProperty.Parent is IPropertyReferenceOperation exception
&& exception.Parent.UnwrapParentConversion() is IArgumentOperation argument
&& argument.Parent is IInvocationOperation { TargetMethod.Name: "Should" } should)
{
nextAssertion = should.Parent as IInvocationOperation;
fluentAssertionProperty = chainProperty.Property.Name;
exceptionProperty = exception.Property.Name;
return nextAssertion is not null;
}

nextAssertion = default;
fluentAssertionProperty = default;
exceptionProperty = default;
return false;
}

Expand Down
Loading

0 comments on commit 593b7b8

Please sign in to comment.