Skip to content

Commit a5532be

Browse files
authored
bugfix: fix issue 290 (#292)
* tests: sanity test for issue #290 * bugfix: fix issue #290 * bugfix: fix issue #290
1 parent 2e2d70c commit a5532be

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

Diff for: src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

+57
Original file line numberDiff line numberDiff line change
@@ -448,5 +448,62 @@ public class MyCollectionType { }";
448448
})
449449
);
450450
}
451+
452+
[TestMethod]
453+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/290")]
454+
public void ShouldNotReportIssue290()
455+
{
456+
const string source = @"
457+
using FluentAssertions;
458+
using FluentAssertions.Extensions;
459+
using System;
460+
using System.Collections.Generic;
461+
using System.Linq;
462+
public class TestClass
463+
{
464+
public static void Main()
465+
{
466+
IEnumerable<Item> expectedOrderedNames = new[] { new Item(""Alpha""), new Item(""Bravo""), new Item(""Charlie"") };
467+
IEnumerable<Parent> actual = GetSortedItems();
468+
469+
actual.Select(x => x.Item).Should().Equal(expectedOrderedNames);
470+
}
471+
472+
static IEnumerable<Parent> GetSortedItems()
473+
{
474+
yield return new Parent(""Bravo"");
475+
yield return new Parent(""Charlie"");
476+
yield return new Parent(""Alpha"");
477+
}
478+
}
479+
480+
public class Item
481+
{
482+
public string Name { get; set; }
483+
public Guid Id { get; set; }
484+
485+
public Item(string name)
486+
{
487+
Name = name;
488+
Id = Guid.NewGuid();
489+
}
490+
}
491+
492+
public class Parent
493+
{
494+
public Item Item { get; set; }
495+
496+
public Parent(string name)
497+
{
498+
Item = new Item(name);
499+
}
500+
}";
501+
502+
DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments()
503+
.WithSources(source)
504+
.WithAllAnalyzers()
505+
.WithPackageReferences(PackageReference.FluentAssertions_6_12_0)
506+
);
507+
}
451508
}
452509
}

Diff for: src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s
4747
public static DiagnosticMetadata CollectionShouldHaveElementAt_SkipFirstShouldBe { get; } = new("Use .Should().HaveElementAt()", GetHelpLink("Collections-26"));
4848
public static DiagnosticMetadata CollectionShouldBeInAscendingOrder_OrderByShouldEqual { get; } = new("Use .Should().BeInAscendingOrder()", GetHelpLink("Collections-27"));
4949
public static DiagnosticMetadata CollectionShouldBeInDescendingOrder_OrderByDescendingShouldEqual { get; } = new("Use .Should().BeInDescendingOrder()", GetHelpLink("Collections-28"));
50-
public static DiagnosticMetadata CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect { get; } = new("Use .Should().BeEquivalentTo()", GetHelpLink("Collections-29"));
50+
public static DiagnosticMetadata CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect { get; } = new("Use .Should().Equal()", GetHelpLink("Collections-29"));
5151
public static DiagnosticMetadata CollectionShouldNotIntersectWith_IntersectShouldBeEmpty { get; } = new("Use .Should().NotIntersectWith()", GetHelpLink("Collections-30"));
5252
public static DiagnosticMetadata CollectionShouldIntersectWith_IntersectShouldNotBeEmpty { get; } = new("Use .Should().IntersectWith()", GetHelpLink("Collections-31"));
5353
public static DiagnosticMetadata CollectionShouldNotContainNulls_SelectShouldNotContainNulls { get; } = new("Use .Should().NotContainNulls()", GetHelpLink("Collections-32"));

Diff for: src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
179179
case nameof(Enumerable.OrderByDescending) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata) && invocationBeforeShould.Arguments[0].IsSameArgumentReference(assertion.Arguments[0]):
180180
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldBeInDescendingOrder_OrderByDescendingShouldEqual));
181181
return;
182-
case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata):
182+
case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata)
183+
&& assertion.Arguments[0].Value is IInvocationOperation { TargetMethod.Name: nameof(Enumerable.Select), Arguments.Length: 2 } expectedInvocation && expectedInvocation.Arguments[1].IsLambda():
183184
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect));
184185
return;
185186
}

0 commit comments

Comments
 (0)