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

HaveElementAt #13

Closed
jnyrup opened this issue Oct 23, 2017 · 6 comments
Closed

HaveElementAt #13

jnyrup opened this issue Oct 23, 2017 · 6 comments
Labels

Comments

@jnyrup
Copy link
Member

jnyrup commented Oct 23, 2017

I suspect this might be related to the other issue about the syntax tree?

public class MyClass
{
    public string Message { get; }
}

var list = new List<MyClass>();
list[0].Message.Should().Be(""); // throws exception
System.InvalidCastException : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.MemberAccessExpressionSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ElementAccessExpressionSyntax'.
   at FluentAssertions.Analyzers.NodeReplacement.RemoveAndRetrieveIndexerArgumentsNodeReplacement.ComputeNew(LinkedListNode`1 listNode)
   at FluentAssertions.Analyzers.FluentAssertionsCodeFixProvider.GetNewStatement(ExpressionStatementSyntax statement,NodeReplacement replacement)
   at FluentAssertions.Analyzers.CollectionShouldHaveElementAtCodeFix.GetNewStatement(ExpressionStatementSyntax statement,FluentAssertionsDiagnosticProperties properties)
   at async FluentAssertions.Analyzers.FluentAssertionsCodeFixProvider.RewriteAssertion(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetChangedSolutionAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputeOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.ComputePreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.CodeActions.CodeAction.GetPreviewOperationsAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.GetPreviewResultAsync(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedActionWithNestedFlavors.<>c__DisplayClass11_0.<GetPreviewAsync>b__0(<Unknown Parameters>)
   at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T](<Unknown Parameters>)

If I rewrite the assertion to be valid for all enumerables

list.ElementAt(0).Message.Should().Be("");

it is simplified into the invalid

list.Message.Should().HaveElementAt(0, "");
Meir017 added a commit that referenced this issue Oct 23, 2017
@Meir017
Copy link
Member

Meir017 commented Oct 23, 2017

just to be sure,

list.ElementAt(0).Message.Should().Be("");

should not trigger any analyzers, right?

@Meir017
Copy link
Member

Meir017 commented Oct 23, 2017

right now the way to discover this scenario is if the SimpleMemberAccessExpression representing the Should method doesn't have an InvocationExpression as it's Expression then we have a property accessor and should not trigger the analyzer.

image

The exception I see is having cases like this

list.ElementAt(0).ToString().Should().Be(flag);

where the user is doing something weired...

@Meir017 Meir017 added the bug label Oct 23, 2017
@jnyrup
Copy link
Member Author

jnyrup commented Oct 23, 2017

You are quite good at giving hints :)
A simplified version of the code causing the exception:

string str = "a, b";
string[] splitLines = str.Split(',');

splitLines[0].Trim().Should().Be("a");
splitLines[1].Trim().Should().Be("b");

@Meir017
Copy link
Member

Meir017 commented Oct 23, 2017

I created a branch for testing stuff, I have a lead on the new visitor algorithm - https://github.com/fluentassertions/fluentassertions.analyzers/blob/playground/src/FluentAssertions.Analyzers/Poc/PocAnalyzer.cs

@Meir017
Copy link
Member

Meir017 commented Oct 23, 2017

@jnyrup I managed to create an analyzer using the new visitor 052f272 .

I think the next stage will be to add positive tests that trigger the analyzers and negative tests that don't trigger any analyzers

@Meir017
Copy link
Member

Meir017 commented Oct 24, 2017

@jnyrup notice the tests in the file https://github.com/fluentassertions/fluentassertions.analyzers/blob/feature/GH-13/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs

Are there cases related to the exception in this issue you can think of that I missed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants