private MemberDeclarationSyntax MockSetVirtualMethod()
        {
            var uniquifier = new Uniquifier(Symbol.Parameters.Select(p => p.Name));

            var parameterList = F.SeparatedList(Symbol.Parameters.Select(a =>
                                                                         F.Parameter(F.Identifier(a.Name)).WithType(TypesForSymbols.ParseTypeName(a.Type, a.NullableOrOblivious()))))
                                .Add(F.Parameter(F.Identifier(uniquifier.GetUniqueName("value"))).WithType(ValueTypeSyntax));

            return(F.MethodDeclaration(F.PredefinedType(F.Token(SyntaxKind.VoidKeyword)), F.Identifier(MemberMockName))
                   .WithModifiers(F.TokenList(F.Token(SyntaxKind.ProtectedKeyword), F.Token(SyntaxKind.VirtualKeyword)))
                   .WithParameterList(F.ParameterList(parameterList))
                   .WithBody(F.Block(ThrowMockMissingStatement("VirtualIndexerSet"))));
        }
        private MemberDeclarationSyntax ExplicitInterfaceMember()
        {
            var mockedIndexer = F.IndexerDeclaration(ValueWithReadonlyTypeSyntax)
                                .WithParameterList(F.BracketedParameterList(F.SeparatedList(Symbol.Parameters.Select(a =>
                                                                                                                     F.Parameter(F.Identifier(a.Name)).WithType(TypesForSymbols.ParseTypeName(a.Type, a.NullableOrOblivious()))))))
                                .WithExplicitInterfaceSpecifier(F.ExplicitInterfaceSpecifier(TypesForSymbols.ParseName(InterfaceSymbol)));

            if (Symbol.IsReadOnly)
            {
                ExpressionSyntax invocation = F.InvocationExpression(F.IdentifierName(MemberMockName),
                                                                     F.ArgumentList(F.SeparatedList(Symbol.Parameters.Select(a => F.Argument(F.IdentifierName(a.Name))))));
                if (Symbol.ReturnsByRef || Symbol.ReturnsByRefReadonly)
                {
                    invocation = F.RefExpression(invocation);
                }

                mockedIndexer = mockedIndexer
                                .WithExpressionBody(F.ArrowExpressionClause(invocation))
                                .WithSemicolonToken(F.Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                if (!Symbol.IsWriteOnly)
                {
                    var argumentList = F.SeparatedList(Symbol.Parameters.Select(a => F.Argument(F.IdentifierName(a.Name))));

                    mockedIndexer = mockedIndexer.AddAccessorListAccessors(F.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                                                                           .WithExpressionBody(F.ArrowExpressionClause(F.InvocationExpression(F.IdentifierName(MemberMockName))
                                                                                                                       .WithArgumentList(F.ArgumentList(argumentList))))
                                                                           .WithSemicolonToken(F.Token(SyntaxKind.SemicolonToken))
                                                                           );
                }

                if (!Symbol.IsReadOnly)
                {
                    var argumentList = F.SeparatedList(Symbol.Parameters.Select(a => F.Argument(F.IdentifierName(a.Name))))
                                       .Add(F.Argument(F.IdentifierName("value")));

                    mockedIndexer = mockedIndexer.AddAccessorListAccessors(F.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                                                                           .WithExpressionBody(F.ArrowExpressionClause(F.InvocationExpression(F.IdentifierName(MemberMockName),
                                                                                                                                              F.ArgumentList(argumentList))))
                                                                           .WithSemicolonToken(F.Token(SyntaxKind.SemicolonToken))
                                                                           );
                }
            }

            return(mockedIndexer);
        }
 private MemberDeclarationSyntax MockGetVirtualMethod()
 {
     return(F.MethodDeclaration(ValueTypeSyntax, F.Identifier(MemberMockName))
            .WithModifiers(F.TokenList(F.Token(SyntaxKind.ProtectedKeyword), F.Token(SyntaxKind.VirtualKeyword)))
            .WithParameterList(F.ParameterList(F.SeparatedList(Symbol.Parameters.Select(a =>
                                                                                        F.Parameter(F.Identifier(a.Name)).WithType(TypesForSymbols.ParseTypeName(a.Type, a.NullableOrOblivious()))))))
            .WithBody(F.Block(ThrowMockMissingStatement("VirtualIndexerGet"))));
 }
示例#4
0
        protected MemberDeclarationSyntax ExplicitInterfaceMember()
        {
            var baseReturnType = Symbol.ReturnsVoid
                ? F.PredefinedType(F.Token(SyntaxKind.VoidKeyword))
                : TypesForSymbols.ParseTypeName(Symbol.ReturnType, Symbol.ReturnTypeIsNullableOrOblivious());
            var returnType = baseReturnType;

            if (Symbol.ReturnsByRef)
            {
                returnType = F.RefType(returnType);
            }
            else if (Symbol.ReturnsByRefReadonly)
            {
                returnType = F.RefType(returnType).WithReadOnlyKeyword(F.Token(SyntaxKind.ReadOnlyKeyword));
            }

            var mockedMethod = ExplicitInterfaceMemberMethodDeclaration(returnType);

            var memberMockInstance = ExplicitInterfaceMemberMemberMockInstance();

            ExpressionSyntax invocation = F.InvocationExpression(
                F.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                         memberMockInstance, F.IdentifierName("Call")))
                                          .WithExpressionsAsArgumentList(ParametersType.BuildArgumentListWithOriginalNames());

            // look at the return parameters. If we don't have any we can just make the call.
            // if we only have one and that's the return value, we can just return it.
            if (ReturnValuesType.Count == 0 ||
                ReturnValuesType.Count == 1 && ReturnValuesType[0].IsReturnValue)
            {
                if (Symbol.ReturnsByRef || Symbol.ReturnsByRefReadonly)
                {
                    invocation = TypesForSymbols.WrapByRef(invocation, baseReturnType);
                }

                mockedMethod = mockedMethod.WithExpressionBody(F.ArrowExpressionClause(invocation))
                               .WithSemicolonToken(F.Token(SyntaxKind.SemicolonToken));
            }
            // if we only have one and that's not a return value, we can just assign it to the out or ref parameter it corresponds to.
            else if (ReturnValuesType.Count == 1)
            {
                mockedMethod = mockedMethod.WithBody(F.Block(F.ExpressionStatement(F.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                                          F.IdentifierName(ReturnValuesType[0].OriginalName), invocation))));
            }
            else
            {
                // if we have more than one, put it in a temporary variable. (consider name clashes with method parameter names)
                var    x   = new Uniquifier(Symbol.Parameters.Select(m => m.Name));
                string tmp = x.GetUniqueName("tmp");

                var statements = new List <StatementSyntax>
                {
                    F.LocalDeclarationStatement(F.VariableDeclaration(F.IdentifierName("var")).WithVariables(
                                                    F.SingletonSeparatedList(F.VariableDeclarator(F.Identifier(tmp)).WithInitializer(F.EqualsValueClause(invocation)))))
                };

                // then for any out or ref parameters, set their values from the temporary variable.
                foreach (var rv in ReturnValuesType.Where(a => !a.IsReturnValue))
                {
                    statements.Add(F.ExpressionStatement(F.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
                                                                                F.IdentifierName(rv.OriginalName),
                                                                                F.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, F.IdentifierName(tmp),
                                                                                                         F.IdentifierName(rv.TupleSafeName)))));
                }

                // finally, if there is a 'proper' return type, return the corresponding value from the temporary variable.
                foreach (var rv in ReturnValuesType.Where(a => a.IsReturnValue))
                {
                    ExpressionSyntax memberAccess = F.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, F.IdentifierName(tmp),
                                                                             F.IdentifierName(rv.TupleSafeName));

                    if (Symbol.ReturnsByRef || Symbol.ReturnsByRefReadonly)
                    {
                        memberAccess = TypesForSymbols.WrapByRef(memberAccess, baseReturnType);
                    }

                    statements.Add(F.ReturnStatement(memberAccess));
                }

                mockedMethod = mockedMethod.WithBody(F.Block(statements));
            }

            return(mockedMethod);
        }
        private MemberDeclarationSyntax ExplicitInterfaceMember()
        {
            var parameters = F.SeparatedList(Symbol.Parameters.Select(p => TypesForSymbols.AsParameterSyntax(p)));

            if (ArglistParameterName != null)
            {
                parameters = parameters.Add(F.Parameter(F.Token(SyntaxKind.ArgListKeyword)));
            }

            var arguments = Symbol.Parameters.AsArgumentList();

            if (ArglistParameterName != null && TypesForSymbols.RuntimeArgumentHandle() != null)
            {
                arguments = arguments.Add(F.Argument(F.LiteralExpression(SyntaxKind.ArgListExpression, F.Token(SyntaxKind.ArgListKeyword))));
            }

            var mockedMethod = F.MethodDeclaration(ReturnType, Symbol.Name)
                               .WithParameterList(F.ParameterList(parameters))
                               .WithExplicitInterfaceSpecifier(F.ExplicitInterfaceSpecifier(TypesForSymbols.ParseName(InterfaceSymbol)));

            if (Symbol.TypeParameters.Any())
            {
                mockedMethod = mockedMethod.WithTypeParameterList(TypeParameterList());
            }

            var invocation = Symbol.TypeParameters.Any()
                ? (ExpressionSyntax)F.GenericName(MemberMockName)
                             .WithTypeArgumentList(F.TypeArgumentList(
                                                       F.SeparatedList(Symbol.TypeParameters.Select(typeParameter =>
                                                                                                    TypesForSymbols.ParseTypeName(typeParameter, false)))))
                : F.IdentifierName(MemberMockName);

            invocation = F.InvocationExpression(invocation, F.ArgumentList(arguments));

            if (Symbol.ReturnsByRef || Symbol.ReturnsByRefReadonly)
            {
                invocation = F.RefExpression(invocation);
            }

            mockedMethod = mockedMethod
                           .WithExpressionBody(F.ArrowExpressionClause(invocation))
                           .WithSemicolonToken(F.Token(SyntaxKind.SemicolonToken));

            return(mockedMethod);
        }
 public void AddReturnValue(ITypeSymbol returnType, bool nullable)
 {
     Items.Add(new BuilderEntry("returnValue", TypesForSymbols.ParseTypeName(returnType, nullable), true));
 }
        public void AddParameter(IParameterSymbol parameter)
        {
            var x = TypesForSymbols.ParseTypeName(parameter.Type, parameter.NullableOrOblivious());

            Items.Add(new BuilderEntry(parameter.Name, x, false));
        }