コード例 #1
0
ファイル: MsTestStub2Moq.cs プロジェクト: molinch/kickmstest
        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));
        }
コード例 #2
0
ファイル: MsTestStub2Moq.cs プロジェクト: molinch/kickmstest
        public SyntaxNode SwitchType(AssignmentExpressionSyntax assignment)
        {
            var left = assignment.Left as MemberAccessExpressionSyntax;

            if (left == null)
            {
                return(null);
            }

            var right = assignment.Right as ObjectCreationExpressionSyntax;

            var declarationTypeSymbol = semanticModel.GetTypeInfo(left).ConvertedType as INamedTypeSymbol;

            if (declarationTypeSymbol == null)
            {
                return(null);
            }

            string typeToMock;

            if (msTestHelper.IsStub(declarationTypeSymbol, out typeToMock))
            {
                if (typeToMock == null)
                {
                    return(null);
                }

                var identifierName     = left.Name.Identifier.ValueText;
                var stubName           = char.ToLowerInvariant(identifierName[0]) + identifierName.Substring(1);
                var stubDefDeclaration = CreateStubDefinitionDeclaration(stubName, typeToMock)
                                         .NormalizeWhitespace()
                                         .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

                var initializerExpression = CreateStubInitializerDeclarations(assignment, stubName, declarationTypeSymbol);

                var newAssignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, MoqStub.GetStubDefinitionIdentifierName(stubName))
                                    .NormalizeWhitespace()
                                    .WithLeadingTrivia(assignment.GetLeadingTrivia());

                var statements = new SyntaxList <StatementSyntax>();
                statements = statements.AddRange(new StatementSyntax[] { stubDefDeclaration }.Union(initializerExpression).Union(new StatementSyntax[] { SyntaxFactory.ExpressionStatement(newAssignment) }));
                var wrapper = SyntaxFactory.Block(statements);
                wrapper = wrapper.WithOpenBraceToken(SyntaxFactory.MissingToken(SyntaxKind.OpenBraceToken)) // to remove scope {}
                          .WithCloseBraceToken(SyntaxFactory.MissingToken(SyntaxKind.CloseBraceToken));

                return(wrapper
                       .WithLeadingTrivia(assignment.GetLeadingTrivia())
                       .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed));
            }

            return(null);
        }
コード例 #3
0
ファイル: MsTestStub2Moq.cs プロジェクト: molinch/kickmstest
        private VariableDeclarationSyntax CreateStubDeclaration(SyntaxNode node, string variableName, string variableIdentifierName)
        {
            var stubDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName("var"))
                                  .WithVariables(SyntaxFactory.SingletonSeparatedList(
                                                     SyntaxFactory.VariableDeclarator(variableIdentifierName)
                                                     .WithInitializer(
                                                         SyntaxFactory.EqualsValueClause(
                                                             SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, MoqStub.GetStubDefinitionIdentifierName(variableName), SyntaxFactory.IdentifierName("Object"))
                                                             )
                                                         )
                                                     ))
                                  .NormalizeWhitespace()
                                  .WithLeadingTrivia(node.GetLeadingTrivia());

            return(stubDeclaration);
        }