Skip to content

Commit a88c088

Browse files
committed
feat: improve TryGetFirstDescendent logic
1 parent 2d2b6e6 commit a88c088

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,14 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
763763
[Implemented]
764764
public void CollectionShouldHaveElementAt_ElementAtIndexShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveElementAt_ElementAtIndexShouldBe);
765765

766+
[DataTestMethod]
767+
[AssertionDiagnostic("actual.ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
768+
[AssertionDiagnostic("actual.ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
769+
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
770+
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
771+
[Implemented]
772+
public void CollectionShouldHaveElementAt_ElementAtIndexShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));
773+
766774
[DataTestMethod]
767775
[AssertionDiagnostic("actual[k].Should().Be(expectedItem{0});")]
768776
[AssertionDiagnostic("actual[6].Should().Be(expectedItem{0});")]

src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
301301
return;
302302
case "OnlyHaveUniqueItems" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3):
303303
{
304-
if (!invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould)) return;
304+
if (!invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould)) return;
305305
switch (invocationBeforeShould.TargetMethod.Name)
306306
{
307307
case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata):
@@ -370,7 +370,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
370370
}
371371
case "Be" when assertion.IsContainedInType(metadata.ObjectAssertionsOfT2):
372372
{
373-
if (invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould))
373+
if (invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould))
374374
{
375375
switch (invocationBeforeShould.TargetMethod.Name)
376376
{

0 commit comments

Comments
 (0)