Example #1
0
        private MoqStub MockMethodOrPropertyCall(StubbedCall msStub)
        {
            var trivia = new List <SyntaxTrivia>()
            {
                SyntaxFactory.CarriageReturnLineFeed
            };

            trivia.AddRange(msStub.OriginalLeadingTrivia);
            trivia.Add(SyntaxFactory.Tab);

            var stubIdentifier = MoqStub.GetStubDefinitionIdentifierName(msStub.Identifier.Identifier.ValueText)
                                 .WithTrailingTrivia(trivia);
            var setup        = SyntaxFactory.IdentifierName("Setup");
            var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, stubIdentifier, setup);

            var stubArguments = new List <ArgumentSyntax>();
            var arguments     = msStub.Stubbed.Arguments.ToList();

            foreach (var arg in msStub.Stubbed.Arguments)
            {
                var typeName = arg.ToDisplayString();

                var it                 = SyntaxFactory.IdentifierName("It");
                var isAny              = IdentifierNameHelper.CreateGenericName("IsAny", typeName);
                var anyValue           = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, it, isAny);
                var anyValueInvocation = SyntaxFactory.InvocationExpression(anyValue);

                var stubArgument = SyntaxFactory.Argument(anyValueInvocation);
                stubArguments.Add(stubArgument);
            }
            var argumentList = SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(stubArguments));

            var typeArguments    = (msStub.Stubbed.MethodOrPropertySymbol as IMethodSymbol)?.TypeArguments.Select(a => a.ToDisplayString());
            var stubLambdaAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("c"),
                                                                        IdentifierNameHelper.CreateName(msStub.Stubbed.MethodOrPropertySymbol.Name, typeArguments));
            var stubLambdaParam = SyntaxFactory.Parameter(SyntaxFactory.Identifier("c"));
            SimpleLambdaExpressionSyntax stubLambda;

            if (msStub.Stubbed.MethodOrPropertySymbol.Kind == SymbolKind.Property)
            {
                stubLambda = SyntaxFactory.SimpleLambdaExpression(stubLambdaParam, stubLambdaAccess).NormalizeWhitespace();
            }
            else
            {
                var stubLambdaBody = SyntaxFactory.InvocationExpression(stubLambdaAccess, argumentList);
                stubLambda = SyntaxFactory.SimpleLambdaExpression(stubLambdaParam, stubLambdaBody).NormalizeWhitespace();
            }

            var stubCall = SyntaxFactory
                           .InvocationExpression(memberAccess, SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(stubLambda) })))
                           .WithTrailingTrivia(trivia);

            SimpleNameSyntax ret;

            if (msStub.Stubbed.ReturnType == null)
            {
                ret = IdentifierNameHelper.CreateName("Callback", msStub.Stubbed.Arguments.Select(a => a.ToDisplayString()));
            }
            else
            {
                ret = IdentifierNameHelper.CreateName("Returns", msStub.Stubbed.Arguments.Select(a => a.ToDisplayString()));
            }

            var retArgsList = SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Argument(msStub.StubReturn));
            var retArgs     = SyntaxFactory.ArgumentList(retArgsList);

            var fullExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, stubCall, ret);
            var fullWithArgs   = SyntaxFactory.InvocationExpression(fullExpression, retArgs);

            return(new MoqStub(msStub, fullWithArgs));
        }
Example #2
0
 private GenericNameSyntax CreateMockType(string type)
 {
     return(IdentifierNameHelper.CreateGenericName("Mock", type));
 }