Example #1
0
        private IReadOnlyCollection <StatementSyntax> CreateConstructorStatements(ClassInformation classInfo)
        {
            var result = new List <StatementSyntax>();

            foreach (var param in classInfo.Constructor.Parameters)
            {
                var statement = SyntaxFactory.ExpressionStatement(
                    SyntaxFactory.AssignmentExpression(
                        SyntaxKind.SimpleAssignmentExpression,
                        SyntaxFactory.IdentifierName(_valueAligner.CreateMockFieldName(param.ParameterName)),
                        SyntaxFactory.ObjectCreationExpression(
                            SyntaxFactory.GenericName(
                                SyntaxFactory.Identifier("Mock"))
                            .WithTypeArgumentList(
                                SyntaxFactory.TypeArgumentList(
                                    SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                                        SyntaxFactory.IdentifierName(param.ParameterType)))))
                        .WithArgumentList(
                            SyntaxFactory.ArgumentList())));

                result.Add(statement);
            }

            var emptyLine = SyntaxFactory.ParseStatement(string.Empty).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

            result.Add(emptyLine);

            var sutStatement = CreateSutStatement(classInfo);

            result.Add(sutStatement);

            return(result);
        }
 private FieldDeclarationSyntax CreateMockField(string typeName, string parameterName)
 {
     return(SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(
                    SyntaxFactory.GenericName(
                        SyntaxFactory.Identifier("Mock"))
                    .WithTypeArgumentList(
                        SyntaxFactory.TypeArgumentList(
                            SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                                SyntaxFactory.IdentifierName(typeName)))))
                .WithVariables(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.VariableDeclarator(
                            SyntaxFactory.Identifier(_valueAligner.CreateMockFieldName(parameterName))))))
            .WithModifiers(
                SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.PrivateKeyword),
                    SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))));
 }