Skip to content

Commit 2d24434

Browse files
samcraggMeir017
authored andcommitted
Ignore dictionary types from HaveElementAt (#45)
* Ignore dictionary types from HaveElementAt * Move unit test
1 parent 02434ad commit 2d24434

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public void AssertionCallMultipleMethodWithTheSameNameAndArguments()
2525
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
2626
}
2727

28+
[TestMethod]
29+
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/44")]
30+
public void CollectionShouldHaveElementAt_ShouldIgnoreDictionaryTypes()
31+
{
32+
string source = GenerateCode.DictionaryAssertion("actual[\"key\"].Should().Be(expectedValue);");
33+
DiagnosticVerifier.VerifyCSharpDiagnostic<CollectionShouldHaveElementAtAnalyzer>(source);
34+
}
35+
2836
[TestMethod]
2937
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/13")]
3038
public void PropertyOfIndexerShouldBe_ShouldNotThrowException()

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveElementAt.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
77
using System.Composition;
8+
using System.Linq;
89

910
namespace FluentAssertions.Analyzers
1011
{
@@ -27,6 +28,16 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
2728
}
2829
}
2930

31+
protected override bool ShouldAnalyzeVariableType(ITypeSymbol type)
32+
{
33+
if (type.AllInterfaces.Any(@interface => @interface.Name == "IReadOnlyDictionary" || @interface.Name == "IDictionary"))
34+
{
35+
return false;
36+
}
37+
38+
return base.ShouldAnalyzeVariableType(type);
39+
}
40+
3041
public class ElementAtIndexShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
3142
{
3243
public ElementAtIndexShouldBeSyntaxVisitor() : base(new MemberValidator("ElementAt"), MemberValidator.Should, new MemberValidator("Be"))

0 commit comments

Comments
 (0)