public ClassDeclarationSyntax StubMethod(ClassDeclarationSyntax classDclr, IMethodSymbol methodSymbol, INamedTypeSymbol stubbedInterface, SemanticModel semanticModel)
        {
            if (!methodSymbol.IsOrdinaryMethod())
            {
                return(classDclr);
            }

            MethodDeclarationSyntax methodDclr = SF.MethodDeclaration(
                SF.ParseTypeName(methodSymbol.ReturnType.GetFullyQualifiedName()), methodSymbol.GetGenericName());

            methodDclr = methodDclr.WithParameterList(methodDclr.ParameterList.AddParameters(
                                                          RoslynUtils.GetMethodParameterSyntaxList(methodSymbol).ToArray()));
            methodDclr = methodDclr.WithSemicolonToken(SF.Token(SyntaxKind.None))
                         .WithExplicitInterfaceSpecifier(
                SF.ExplicitInterfaceSpecifier(
                    SF.IdentifierName(methodSymbol.GetContainingInterfaceGenericQualifiedName())));

            string delegateTypeName = NamingUtils.GetDelegateTypeName(methodSymbol, stubbedInterface);
            string parameters       = StubbingUtils.FormatParameters(methodSymbol);
            var    outParameters    = methodSymbol.Parameters.Where(p => p.RefKind == RefKind.Out);

            var methodBlock = StubbingUtils.GetInvocationBlockSyntax(delegateTypeName, methodSymbol.GetGenericName(),
                                                                     parameters, outParameters, methodSymbol.ReturnType, semanticModel);

            classDclr = classDclr.AddMembers(methodDclr.WithBody(methodBlock));

            return(classDclr);
        }
Exemplo n.º 2
0
        public ClassDeclarationSyntax StubProperty(ClassDeclarationSyntax classDclr, IPropertySymbol propertySymbol, INamedTypeSymbol stubbedInterface, SemanticModel semanticModel)
        {
            string indexerType = propertySymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
            BasePropertyDeclarationSyntax propDclr = null;

            if (propertySymbol.GetMethod != null)
            {
                IMethodSymbol getMethodSymbol = propertySymbol.GetMethod;
                string        parameters      = StubbingUtils.FormatParameters(getMethodSymbol);

                string delegateTypeName = NamingUtils.GetDelegateTypeName(getMethodSymbol, stubbedInterface);
                var    accessorDclr     = SF.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, SF.Block(
                                                                     SF.List(new[]
                {
                    StubbingUtils.GetInvocationBlockSyntax(delegateTypeName, getMethodSymbol.Name, parameters,
                                                           Enumerable.Empty <IParameterSymbol>(), getMethodSymbol.ReturnType, semanticModel)
                })));

                propDclr = CreatePropertyDclr(getMethodSymbol, indexerType);
                propDclr = propDclr.AddAccessorListAccessors(accessorDclr);
            }

            if (propertySymbol.SetMethod != null)
            {
                var           voidType         = semanticModel.Compilation.GetTypeByMetadataName("System.Void");
                IMethodSymbol setMethodSymbol  = propertySymbol.SetMethod;
                string        parameters       = $"{StubbingUtils.FormatParameters(setMethodSymbol)}";
                string        delegateTypeName = NamingUtils.GetDelegateTypeName(setMethodSymbol, stubbedInterface);
                var           accessorDclr     = SF.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration, SF.Block(
                                                                            SF.List(new[]
                {
                    StubbingUtils.GetInvocationBlockSyntax(delegateTypeName, setMethodSymbol.Name, parameters,
                                                           Enumerable.Empty <IParameterSymbol>(), voidType, semanticModel)
                })));
                if (propDclr == null)
                {
                    propDclr = CreatePropertyDclr(setMethodSymbol, indexerType);
                }
                propDclr = propDclr.AddAccessorListAccessors(accessorDclr);
            }

            classDclr = classDclr.AddMembers(propDclr);
            return(classDclr);
        }