private static MethodDeclarationSyntax CreateTypeRequestWithMessageAllowNull(
     string className,
     HttpStatusCode httpStatusCode,
     string typeRequestName,
     string parameterName = "message")
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithParameterList(
                SyntaxFactory.ParameterList(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName))
                        .WithType(SyntaxFactory.NullableType(SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword())))
                        .WithDefault(
                            SyntaxFactory.EqualsValueClause(
                                SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))))))
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxObjectCreationExpressionFactory.Create(className)
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxObjectCreationExpressionFactory.Create(typeRequestName)
                                    .WithArgumentList(
                                        SyntaxArgumentListFactory.CreateWithOneItem(parameterName))))))))
            .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
 }
예제 #2
0
        public void CreateWithThreeArgumentItems()
        {
            // Arrange
            var expected = SyntaxFactory.ArgumentList(
                SyntaxFactory.SeparatedList <ArgumentSyntax>(
                    new SyntaxNodeOrToken[]
            {
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("foo")),
                SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.CommaToken, new SyntaxTriviaList(SyntaxFactory.Space)),
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("bar")),
                SyntaxFactory.Token(SyntaxTriviaList.Empty, SyntaxKind.CommaToken, new SyntaxTriviaList(SyntaxFactory.Space)),
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("baz")),
            }));

            // Act
            var actual = SyntaxArgumentListFactory.CreateWithThreeArgumentItems(
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("foo")),
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("bar")),
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("baz")));

            // Assert
            Assert.Equal(expected.ToFullString(), actual.ToFullString());
        }
예제 #3
0
 private static List <MemberDeclarationSyntax> CreateConstructor(
     string className,
     string parameterName)
 {
     return(new MemberDeclarationSyntax[]
     {
         SyntaxFactory.GlobalStatement(
             SyntaxFactory.LocalFunctionStatement(
                 SyntaxFactory.IdentifierName(className),
                 SyntaxFactory.MissingToken(SyntaxKind.IdentifierToken))
             .WithModifiers(SyntaxFactory.TokenList(SyntaxTokenFactory.PrivateKeyword()))
             .WithParameterList(SyntaxParameterListFactory.CreateWithOneItem(nameof(ActionResult), parameterName))
             .WithSemicolonToken(
                 SyntaxFactory.MissingToken(
                     SyntaxFactory.TriviaList(),
                     SyntaxKind.SemicolonToken,
                     SyntaxFactory.TriviaList(
                         SyntaxFactory.Trivia(
                             SyntaxFactory.SkippedTokensTrivia()
                             .WithTokens(
                                 SyntaxFactory.TokenList(
                                     SyntaxTokenFactory.Colon()))))))),
         SyntaxFactory.GlobalStatement(
             SyntaxFactory.ExpressionStatement(
                 SyntaxFactory.InvocationExpression(
                     SyntaxFactory.BaseExpression())
                 .WithArgumentList(
                     SyntaxArgumentListFactory.CreateWithOneItem(parameterName)))
             .WithSemicolonToken(SyntaxFactory.MissingToken(SyntaxKind.SemicolonToken))),
         SyntaxFactory.GlobalStatement(SyntaxFactory.Block()),
     }.ToList());
 }
    private MemberDeclarationSyntax CreateExecuteAsyncMethod(
        string parameterTypeName,
        string resultTypeName,
        bool hasParameters)
    {
        var arguments = hasParameters
            ? new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
            SyntaxTokenFactory.Comma(),
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        }
            : new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        };

        SyntaxTokenList methodModifiers;
        BlockSyntax     codeBlockSyntax;

        if (hasParameters)
        {
            methodModifiers = SyntaxTokenListFactory.PublicKeyword();

            codeBlockSyntax = SyntaxFactory.Block(
                SyntaxIfStatementFactory.CreateParameterArgumentNullCheck("parameters", false),
                SyntaxFactory.ReturnStatement(
                    SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName("InvokeExecuteAsync"))
                    .WithArgumentList(
                        SyntaxArgumentListFactory.CreateWithTwoItems("parameters", nameof(CancellationToken).EnsureFirstCharacterToLower()))));
        }
        else
        {
            methodModifiers = SyntaxTokenListFactory.PublicAsyncKeyword();

            var bodyBlockSyntaxStatements = CreateBodyBlockSyntaxStatements(resultTypeName);
            codeBlockSyntax = SyntaxFactory.Block(bodyBlockSyntaxStatements);
        }

        return(SyntaxFactory.MethodDeclaration(
                   SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                   .WithTypeArgumentList(
                       SyntaxFactory.TypeArgumentList(
                           SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                               SyntaxFactory.IdentifierName(EndpointResultTypeName)))),
                   SyntaxFactory.Identifier("ExecuteAsync"))
               .WithModifiers(methodModifiers)
               .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
               .WithBody(codeBlockSyntax));
    }
예제 #5
0
        public void CreateWithOneItem()
        {
            // Arrange
            var expected = SyntaxFactory.ArgumentList(
                SyntaxFactory.SingletonSeparatedList(
                    SyntaxFactory.Argument(
                        SyntaxFactory.IdentifierName("hallo"))));

            // Act
            var actual = SyntaxArgumentListFactory.CreateWithOneItem("hallo");

            // Assert
            Assert.Equal(expected.ToFullString(), actual.ToFullString());
        }
예제 #6
0
        public void CreateWithOneExpressionItem()
        {
            // Arrange
            var expected = SyntaxFactory.ArgumentList(
                SyntaxFactory.SeparatedList <ArgumentSyntax>(
                    new SyntaxNodeOrToken[]
            {
                SyntaxFactory.Argument(
                    SyntaxFactory.IdentifierName("foo")),
            }));

            // Act
            var actual = SyntaxArgumentListFactory.CreateWithOneExpressionItem(
                SyntaxFactory.IdentifierName("foo"));

            // Assert
            Assert.Equal(expected.ToFullString(), actual.ToFullString());
        }
        private static MethodDeclarationSyntax CreateTypeRequestObjectResult(
            string className,
            string methodName,
            string parameterTypeName,
            string parameterName     = "response",
            bool asGenericList       = false,
            bool asGenericPagination = false)
        {
            string?genericListTypeName = null;

            if (asGenericList)
            {
                genericListTypeName = Microsoft.OpenApi.Models.NameConstants.List;
            }
            else if (asGenericPagination)
            {
                genericListTypeName = Microsoft.OpenApi.Models.NameConstants.Pagination;
            }

            if (string.IsNullOrEmpty(parameterTypeName))
            {
                parameterTypeName = "string";
            }

            return(SyntaxFactory.MethodDeclaration(
                       SyntaxFactory.IdentifierName(className),
                       SyntaxFactory.Identifier(methodName))
                   .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
                   .WithParameterList(SyntaxParameterListFactory.CreateWithOneItem(parameterTypeName, parameterName, genericListTypeName))
                   .WithExpressionBody(
                       SyntaxFactory.ArrowExpressionClause(
                           SyntaxObjectCreationExpressionFactory.Create(className)
                           .WithArgumentList(
                               SyntaxFactory.ArgumentList(
                                   SyntaxFactory.SingletonSeparatedList(
                                       SyntaxFactory.Argument(
                                           SyntaxObjectCreationExpressionFactory.Create(methodName + nameof(ObjectResult))
                                           .WithArgumentList(SyntaxArgumentListFactory.CreateWithOneItem(parameterName))))))))
                   .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
        }