コード例 #1
0
        public override void VisitInvocationExpression(InvocationExpressionSyntax invocationExpr)
        {
            var memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;

            if (memberAccessExpr != null)
            {
                var symbolInfo   = _semantics.GetSymbolInfo(memberAccessExpr);
                var memberSymbol = symbolInfo.Symbol as IMethodSymbol ?? symbolInfo.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault();
                if (memberSymbol != null)
                {
                    if (RhinoRecognizer.IsReturnMethod(memberSymbol))
                    {
                        SetTargetNode(memberAccessExpr);
                    }
                    else if (RhinoRecognizer.IsExpectMethod(memberSymbol) || RhinoRecognizer.IsStubMethod(memberSymbol))
                    {
                        SetTargetNode(memberAccessExpr);
                        if (memberAccessExpr.Expression is IdentifierNameSyntax identifier)
                        {
                            var fieldOrProperty = _semantics.GetSymbolInfo(identifier).Symbol;
                            if (fieldOrProperty is IFieldSymbol || fieldOrProperty is IPropertySymbol)
                            {
                                _localScope = false;
                            }
                        }
                    }
                    else if (RhinoRecognizer.IsGenerateMockMethod(memberSymbol))
                    {
                        SetTargetNode(memberAccessExpr);
                    }
                    else if (RhinoRecognizer.IsGenerateStubMethod(memberSymbol))
                    {
                        SetTargetNode(memberAccessExpr);
                    }
                    else if (RhinoRecognizer.IsThrowMethod(memberSymbol))
                    {
                        SetTargetNode(memberAccessExpr);
                    }
                }
            }
            base.VisitInvocationExpression(invocationExpr);
        }
コード例 #2
0
        public override void VisitArgument(ArgumentSyntax node)
        {
            var argumentExpr = node.Expression as MemberAccessExpressionSyntax;

            if (argumentExpr != null)
            {
                var argumentSymbol = _semantics.GetSymbolInfo(argumentExpr).Symbol as IPropertySymbol;
                if (argumentSymbol != null)
                {
                    if (RhinoRecognizer.IsAnythingProperty(argumentSymbol))
                    {
                        var innerSymbol = _semantics.GetSymbolInfo(argumentExpr.Expression).Symbol as IPropertySymbol;
                        if (RhinoRecognizer.IsIsArgProperty(innerSymbol))
                        {
                            SetTargetNode(argumentExpr);
                        }
                    }
                }
            }
            base.VisitArgument(node);
        }
コード例 #3
0
        public override void VisitInvocationExpression(InvocationExpressionSyntax invocationExpr)
        {
            using (_invocationContext.Enter())
            {
                var memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;
                if (memberAccessExpr != null)
                {
                    var originalSymbolInfo = _originalSemantics.GetSymbolInfo(memberAccessExpr);

                    base.VisitInvocationExpression(invocationExpr);
                    memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;

                    var originalMemberSymbol = originalSymbolInfo.Symbol as IMethodSymbol ?? originalSymbolInfo.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault();
                    if (originalMemberSymbol != null)
                    {
                        if (RhinoRecognizer.IsReturnMethod(originalMemberSymbol))
                        {
                            _invocationContext.Current.HasReturn = true;
                            return;
                        }
                        if (RhinoRecognizer.IsExpectMethod(originalMemberSymbol) || RhinoRecognizer.IsStubMethod(originalMemberSymbol))
                        {
                            ExtractExpectAndStubInvocation(memberAccessExpr);
                            return;
                        }
                        if (RhinoRecognizer.IsGenerateMockMethod(originalMemberSymbol) || RhinoRecognizer.IsGenerateStubMethod(originalMemberSymbol))
                        {
                            return;
                        }
                        if (RhinoRecognizer.IsThrowMethod(originalMemberSymbol))
                        {
                            _invocationContext.Current.UseExceptionExtensions = true;
                            _invocationContext.Current.HasThrow = true;
                            return;
                        }
                        if (RhinoRecognizer.IsIgnoreArgumentsMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax ignoreArgumentsMemberExpression &&
                            ignoreArgumentsMemberExpression.Expression is InvocationExpressionSyntax innerIgnoreArgumentsInvocationExpression)
                        {
                            _invocationContext.Current.UseAnyArgs = true;
                            return;
                        }
                        if (RhinoRecognizer.IsAnyRepeatOptionsMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax repeatOptionMemberAccess &&
                            repeatOptionMemberAccess.Expression is MemberAccessExpressionSyntax repeatMemberAccess &&
                            repeatMemberAccess.Expression != null)
                        {
                            return;
                        }
                        if (RhinoRecognizer.IsOutRefProperty(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax outRefMemberExpression &&
                            outRefMemberExpression.Expression is InvocationExpressionSyntax outRefInnerInvocationExpression)
                        {
                            _invocationContext.Current.OutRefArguments.AddRange(invocationExpr.ArgumentList.Arguments.Select(x => x.Expression));
                            return;
                        }
                        if (RhinoRecognizer.IsVerifyAllExpectationsMethod(originalMemberSymbol))
                        {
                            return;
                        }
                        if (RhinoRecognizer.IsAssertWasCalledMethod(originalMemberSymbol))
                        {
                            return;
                        }
                        if (RhinoRecognizer.IsAssertWasNotCalledMethod(originalMemberSymbol))
                        {
                            return;
                        }
                        if (RhinoRecognizer.IsPropertyBehavior(originalMemberSymbol))
                        {
                            RemoveInvocation(invocationExpr);
                            return;
                        }
                        if (RhinoRecognizer.IsWhenCalledMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax whenCalledMemberAccess &&
                            invocationExpr.ArgumentList.Arguments.FirstOrDefault().Expression is SimpleLambdaExpressionSyntax whenCalledLambda)
                        {
                            _invocationContext.Current.WhenCalledLambda = whenCalledLambda;
                            return;
                        }
                    }
                }
            }
        }
コード例 #4
0
        public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax invocationExpr)
        {
            using (_invocationContext.Enter(invocationExpr))
            {
                var memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;
                if (memberAccessExpr != null)
                {
                    var originalSymbolInfo = _originalSemantics.GetSymbolInfo(memberAccessExpr);

                    invocationExpr   = base.VisitInvocationExpression(invocationExpr) as InvocationExpressionSyntax;
                    memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;

                    var originalMemberSymbol = originalSymbolInfo.Symbol as IMethodSymbol ?? originalSymbolInfo.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault();
                    if (originalMemberSymbol != null)
                    {
                        if (RhinoRecognizer.IsReturnMethod(originalMemberSymbol))
                        {
                            invocationExpr = invocationExpr.WithArgumentList(ReWriteArguments(invocationExpr));
                            if (_invocationContext.Current.UseAnyArgs)
                            {
                                return(invocationExpr.WithExpression(UseReturnsForAnyArgs(memberAccessExpr)));
                            }
                            else
                            {
                                return(invocationExpr.WithExpression(UseReturns(memberAccessExpr)));
                            }
                        }
                        if (RhinoRecognizer.IsExpectMethod(originalMemberSymbol) || RhinoRecognizer.IsStubMethod(originalMemberSymbol))
                        {
                            return(ExtractExpectAndStubInvocation(memberAccessExpr));
                        }
                        if (RhinoRecognizer.IsGenerateMockMethod(originalMemberSymbol) || RhinoRecognizer.IsGenerateStubMethod(originalMemberSymbol))
                        {
                            return(UseSubstituteFor(memberAccessExpr));
                        }
                        if (RhinoRecognizer.IsThrowMethod(originalMemberSymbol))
                        {
                            UseExceptionExtensions = true;
                            if (_invocationContext.Current.UseAnyArgs)
                            {
                                return(invocationExpr.WithExpression(UseThrowsForAnyArgs(memberAccessExpr)));
                            }
                            else
                            {
                                return(invocationExpr.WithExpression(UseThrows(memberAccessExpr)));
                            }
                        }
                        if (RhinoRecognizer.IsIgnoreArgumentsMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax ignoreArgumentsMemberExpression &&
                            ignoreArgumentsMemberExpression.Expression is InvocationExpressionSyntax innerIgnoreArgumentsInvocationExpression)
                        {
                            return(innerIgnoreArgumentsInvocationExpression);
                        }
                        if (RhinoRecognizer.IsAnyRepeatOptionsMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax repeatOptionMemberAccess &&
                            repeatOptionMemberAccess.Expression is MemberAccessExpressionSyntax repeatMemberAccess &&
                            repeatMemberAccess.Expression != null)
                        {
                            return(repeatMemberAccess.Expression);
                        }
                        if (RhinoRecognizer.IsOutRefProperty(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax outRefMemberExpression &&
                            outRefMemberExpression.Expression is InvocationExpressionSyntax outRefInnerInvocationExpression)
                        {
                            return(outRefInnerInvocationExpression);
                        }
                        if (RhinoRecognizer.IsVerifyAllExpectationsMethod(originalMemberSymbol))
                        {
                            return(UseReceivedOnMethodContext(invocationExpr));
                        }
                        if (RhinoRecognizer.IsAssertWasCalledMethod(originalMemberSymbol))
                        {
                            return(ExtractAssertCalledInvocation(memberAccessExpr, "AssertWasCalled", "Received"));
                        }
                        if (RhinoRecognizer.IsAssertWasNotCalledMethod(originalMemberSymbol))
                        {
                            return(ExtractAssertCalledInvocation(memberAccessExpr, "AssertWasNotCalled", "DidNotReceive"));
                        }
                        if (RhinoRecognizer.IsPropertyBehavior(originalMemberSymbol))
                        {
                            return(RemoveInvocation(invocationExpr));
                        }
                        if (RhinoRecognizer.IsWhenCalledMethod(originalMemberSymbol) &&
                            invocationExpr.Expression is MemberAccessExpressionSyntax whenCalledMemberAccess &&
                            invocationExpr.ArgumentList.Arguments.FirstOrDefault().Expression is SimpleLambdaExpressionSyntax whenCalledLambda)
                        {
                            return(UseInnerCallOrWhenDo(invocationExpr, whenCalledMemberAccess));
                        }
                    }
                }
                return(invocationExpr);
            }
        }
コード例 #5
0
        public override SyntaxNode VisitArgument(ArgumentSyntax node)
        {
            if (node.Expression is MemberAccessExpressionSyntax argumentExpr)
            {
                var argumentSymbol = _originalSemantics.GetSymbolInfo(argumentExpr).Symbol;
                if (argumentSymbol is IPropertySymbol propertySymbol)
                {
                    if (RhinoRecognizer.IsAnythingProperty(propertySymbol))
                    {
                        var innerSymbol = _originalSemantics.GetSymbolInfo(argumentExpr.Expression).Symbol as IPropertySymbol;
                        if (RhinoRecognizer.IsIsArgProperty(innerSymbol))
                        {
                            return(UseArgsAny(node));
                        }
                    }
                    if (RhinoRecognizer.IsNullArgProperty(propertySymbol))
                    {
                        var innerSymbol = _originalSemantics.GetSymbolInfo(argumentExpr.Expression).Symbol as IPropertySymbol;
                        if (RhinoRecognizer.IsIsArgProperty(innerSymbol))
                        {
                            return(UseArgWith(node, GetEqualsNullArgument(_methodContext.Current.UnusedLambdaToken)));
                        }
                    }
                    if (RhinoRecognizer.IsNotNullArgProperty(propertySymbol))
                    {
                        var innerSymbol = _originalSemantics.GetSymbolInfo(argumentExpr.Expression).Symbol as IPropertySymbol;
                        if (RhinoRecognizer.IsIsArgProperty(innerSymbol))
                        {
                            return(UseArgWith(node, GetNotEqualsNullArgument(_methodContext.Current.UnusedLambdaToken)));
                        }
                    }
                }

                if (node.RefKindKeyword.IsKind(SyntaxKind.OutKeyword) && argumentSymbol is IFieldSymbol fieldSymbol)
                {
                    if (RhinoRecognizer.IsDummyField(fieldSymbol))
                    {
                        var outMethodSymbol = _originalSemantics.GetSymbolInfo(argumentExpr.Expression).Symbol as IMethodSymbol;
                        if (RhinoRecognizer.IsOutArgMethod(outMethodSymbol) && argumentExpr.Expression is InvocationExpressionSyntax outMethodInvocation)
                        {
                            _invocationContext.Current.OutRefArguments.Add(outMethodInvocation.ArgumentList.Arguments.First().Expression);
                            return(UseArgsAny(node, true));
                        }
                    }
                }
            }

            if (node.Expression is InvocationExpressionSyntax argumentMethodInvocation)
            {
                var argumentSymbol = _originalSemantics.GetSymbolInfo(argumentMethodInvocation.Expression).Symbol;
                if (argumentSymbol is IMethodSymbol methodSymbol)
                {
                    if (RhinoRecognizer.IsEqualArgMethod(methodSymbol))
                    {
                        var equalsTo = argumentMethodInvocation.ArgumentList.Arguments.First().Expression;
                        return(UseArgWith(node, GetEqualsToGivenArgument(_methodContext.Current.UnusedLambdaToken, equalsTo)));
                    }
                    if (RhinoRecognizer.IsSameArgMethod(methodSymbol))
                    {
                        var sameTo = argumentMethodInvocation.ArgumentList.Arguments.First().Expression;
                        return(UseArgWith(node, GetReferenceEqualsToGivenArgument(_methodContext.Current.UnusedLambdaToken, sameTo)));
                    }
                    if (RhinoRecognizer.IsMatchesArgMethod(methodSymbol))
                    {
                        var lambdaArgument = argumentMethodInvocation.ArgumentList.Arguments.First().Expression;
                        return(UseArgWith(node, GetLambdaAsArgument(_methodContext.Current.UnusedLambdaToken, lambdaArgument)));
                    }
                }
            }

            return(base.VisitArgument(node));
        }