예제 #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 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;
                        }
                    }
                }
            }
        }
        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);
            }
        }