예제 #1
0
        private static List <string> GetUsingStatements(HostProjectOptions hostProjectOptions, EndpointMethodMetadata endpointMethodMetadata)
        {
            var systemList = new List <string>
            {
                "System",
                "System.CodeDom.Compiler",
                "System.Collections.Generic",
                "System.Net",
                "System.Net.Http",
                "System.Text",
                "System.Threading.Tasks",
            }.OrderBy(x => x);

            var generalList = new List <string>
            {
                "FluentAssertions",
                "Xunit",
                $"{hostProjectOptions.ProjectName}.Generated.Contracts",
                $"{hostProjectOptions.ProjectName}.Generated.Contracts.{endpointMethodMetadata.SegmentName}",
            };

            if (endpointMethodMetadata.IsPaginationUsed())
            {
                generalList.Add("Atc.Rest.Results");
            }

            return(systemList
                   .OrderBy(x => x)
                   .Concat(generalList
                           .OrderBy(x => x))
                   .ToList());
        }
예제 #2
0
        private static void AppendUsingStatements(
            StringBuilder sb,
            HostProjectOptions hostProjectOptions,
            EndpointMethodMetadata endpointMethodMetadata)
        {
            sb.AppendLine("using System;");
            sb.AppendLine("using System.CodeDom.Compiler;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Threading;");
            sb.AppendLine("using System.Threading.Tasks;");
            if (endpointMethodMetadata.IsPaginationUsed())
            {
                sb.AppendLine("using Atc.Rest.Results;");
            }

            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts;");
            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts.{endpointMethodMetadata.SegmentName};");
        }
예제 #3
0
        public static LogKeyValueItem Generate(HostProjectOptions hostProjectOptions, EndpointMethodMetadata endpointMethodMetadata)
        {
            if (hostProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(hostProjectOptions));
            }

            if (endpointMethodMetadata == null)
            {
                throw new ArgumentNullException(nameof(endpointMethodMetadata));
            }

            var sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using System.CodeDom.Compiler;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Net;");
            sb.AppendLine("using System.Net.Http;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using System.Threading.Tasks;");
            sb.AppendLine("using FluentAssertions;");
            if (endpointMethodMetadata.IsPaginationUsed())
            {
                sb.AppendLine("using Atc.Rest.Results;");
            }

            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts;");
            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts.{endpointMethodMetadata.SegmentName};");
            sb.AppendLine("using Xunit;");
            sb.AppendLine();
            GenerateCodeHelper.AppendNamespaceComment(sb, hostProjectOptions.ToolNameAndVersion);
            sb.AppendLine($"namespace {hostProjectOptions.ProjectName}.Tests.Endpoints.{endpointMethodMetadata.SegmentName}.Generated");
            sb.AppendLine("{");
            GenerateCodeHelper.AppendGeneratedCodeAttribute(sb, hostProjectOptions.ToolName, hostProjectOptions.ToolVersion);
            sb.AppendLine(4, "[Collection(\"Sequential-Endpoints\")]");
            sb.AppendLine(4, $"public class {endpointMethodMetadata.MethodName}Tests : WebApiControllerBaseTest");
            sb.AppendLine(4, "{");
            sb.AppendLine(8, $"public {endpointMethodMetadata.MethodName}Tests(WebApiStartupFactory fixture) : base(fixture) {{ }}");
            foreach (var contractReturnTypeName in endpointMethodMetadata.ContractReturnTypeNames)
            {
                switch (contractReturnTypeName.Item1)
                {
                case HttpStatusCode.OK:
                    AppendTest200Ok(sb, endpointMethodMetadata, contractReturnTypeName !);
                    break;

                case HttpStatusCode.Created:
                    AppendTest201Created(sb, endpointMethodMetadata, contractReturnTypeName !);
                    break;

                case HttpStatusCode.BadRequest:
                    AppendTest400BadRequestInPath(sb, endpointMethodMetadata, contractReturnTypeName !);
                    AppendTest400BadRequestInHeader(sb, endpointMethodMetadata, contractReturnTypeName !);
                    AppendTest400BadRequestInQuery(sb, endpointMethodMetadata, contractReturnTypeName !);
                    AppendTest400BadRequestInBody(sb, endpointMethodMetadata, contractReturnTypeName !);
                    break;
                }
            }

            sb.AppendLine(4, "}");
            sb.AppendLine("}");

            var pathA    = Path.Combine(hostProjectOptions.PathForTestGenerate !.FullName, "Endpoints");
            var pathB    = Path.Combine(pathA, endpointMethodMetadata.SegmentName);
            var pathC    = Path.Combine(pathB, "Generated");
            var fileName = $"{endpointMethodMetadata.MethodName}Tests.cs";
            var file     = new FileInfo(Path.Combine(pathC, fileName));

            return(TextFileHelper.Save(file, sb.ToString()));
        }
        public static LogKeyValueItem Generate(
            HostProjectOptions hostProjectOptions,
            EndpointMethodMetadata endpointMethodMetadata)
        {
            if (hostProjectOptions == null)
            {
                throw new ArgumentNullException(nameof(hostProjectOptions));
            }

            if (endpointMethodMetadata == null)
            {
                throw new ArgumentNullException(nameof(endpointMethodMetadata));
            }

            var sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using System.CodeDom.Compiler;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Threading;");
            sb.AppendLine("using System.Threading.Tasks;");
            if (endpointMethodMetadata.IsPaginationUsed())
            {
                sb.AppendLine("using Atc.Rest.Results;");
            }

            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts;");
            sb.AppendLine($"using {hostProjectOptions.ProjectName}.Generated.Contracts.{endpointMethodMetadata.SegmentName};");
            sb.AppendLine();
            GenerateCodeHelper.AppendNamespaceComment(sb, hostProjectOptions.ToolNameAndVersion);
            sb.AppendLine($"namespace {hostProjectOptions.ProjectName}.Tests.Endpoints.{endpointMethodMetadata.SegmentName}.Generated");
            sb.AppendLine("{");
            GenerateCodeHelper.AppendGeneratedCodeAttribute(sb, hostProjectOptions.ToolName, hostProjectOptions.ToolVersion);
            sb.AppendLine(4, $"public class {endpointMethodMetadata.MethodName}HandlerStub : {endpointMethodMetadata.ContractInterfaceHandlerTypeName}");
            sb.AppendLine(4, "{");
            sb.AppendLine(8, endpointMethodMetadata.ContractParameterTypeName == null
                ? $"public Task<{endpointMethodMetadata.ContractResultTypeName}> ExecuteAsync(CancellationToken cancellationToken = default)"
                : $"public Task<{endpointMethodMetadata.ContractResultTypeName}> ExecuteAsync({endpointMethodMetadata.ContractParameterTypeName} parameters, CancellationToken cancellationToken = default)");
            sb.AppendLine(8, "{");
            if (endpointMethodMetadata.ContractReturnTypeNames.FirstOrDefault(x => x.Item1 == HttpStatusCode.OK) != null)
            {
                AppendContentForExecuteAsync(sb, endpointMethodMetadata, HttpStatusCode.OK);
            }
            else if (endpointMethodMetadata.ContractReturnTypeNames.FirstOrDefault(x => x.Item1 == HttpStatusCode.Created) != null)
            {
                AppendContentForExecuteAsync(sb, endpointMethodMetadata, HttpStatusCode.Created);
            }
            else
            {
                sb.AppendLine(12, "throw new System.NotImplementedException();");
            }

            sb.AppendLine(8, "}");
            sb.AppendLine(4, "}");
            sb.AppendLine("}");

            var pathA    = Path.Combine(hostProjectOptions.PathForTestGenerate !.FullName, "Endpoints");
            var pathB    = Path.Combine(pathA, endpointMethodMetadata.SegmentName);
            var pathC    = Path.Combine(pathB, "Generated");
            var fileName = $"{endpointMethodMetadata.ContractInterfaceHandlerTypeName.Substring(1)}Stub.cs";
            var file     = new FileInfo(Path.Combine(pathC, fileName));

            return(TextFileHelper.Save(file, sb.ToString()));
        }