Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore dictionary types from HaveElementAt #45

Merged
merged 2 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public void AssertionCallMultipleMethodWithTheSameNameAndArguments()
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
}

[TestMethod]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/44")]
public void CollectionShouldHaveElementAt_ShouldIgnoreDictionaryTypes()
{
string source = GenerateCode.DictionaryAssertion("actual[\"key\"].Should().Be(expectedValue);");
DiagnosticVerifier.VerifyCSharpDiagnostic<CollectionShouldHaveElementAtAnalyzer>(source);
}

[TestMethod]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/13")]
public void PropertyOfIndexerShouldBe_ShouldNotThrowException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;

namespace FluentAssertions.Analyzers
{
Expand All @@ -27,6 +28,16 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
}
}

protected override bool ShouldAnalyzeVariableType(ITypeSymbol type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this to the collection analyzer without breaking tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do want some of the collection ones to still trigger on dictionaries (e.g. Should().BeEmpty())

{
if (type.AllInterfaces.Any(@interface => @interface.Name == "IReadOnlyDictionary" || @interface.Name == "IDictionary"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the dictionary assertions are on the type IDictionary I think this should not look for the readonly one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct, however, this is preventing the HaveIndexAt one from incorrectly firing on dictionaries. Since IDictionary doesn't inherit IReadOnlyDictionary I've done two checks (I use IReadOnlyDictionary in my interfaces which is where I found the bug).

{
return false;
}

return base.ShouldAnalyzeVariableType(type);
}

public class ElementAtIndexShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public ElementAtIndexShouldBeSyntaxVisitor() : base(new MemberValidator("ElementAt"), MemberValidator.Should, new MemberValidator("Be"))
Expand Down