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(); AppendUsingStatements(sb, hostProjectOptions, endpointMethodMetadata); sb.AppendLine(); GenerateCodeHelper.AppendNamespaceComment(sb, hostProjectOptions.ToolNameAndVersion); AppendNamespaceAndClassStart(sb, hostProjectOptions, endpointMethodMetadata); AppendConstructor(sb, endpointMethodMetadata); AppendTestMethod(sb, endpointMethodMetadata); AppendNamespaceAndClassEnd(sb); return(SaveFile(sb, hostProjectOptions, endpointMethodMetadata)); }
public static LogKeyValueItem GenerateGeneratedTests( DomainProjectOptions domainProjectOptions, SyntaxGeneratorHandler sgHandler) { if (domainProjectOptions == null) { throw new ArgumentNullException(nameof(domainProjectOptions)); } if (sgHandler == null) { throw new ArgumentNullException(nameof(sgHandler)); } var area = sgHandler.FocusOnSegmentName.EnsureFirstCharacterToUpper(); var nsSrc = $"{domainProjectOptions.ProjectName}.{NameConstants.Handlers}.{area}"; var nsTest = $"{domainProjectOptions.ProjectName}.Tests.{NameConstants.Handlers}.{area}.Generated"; var srcSyntaxNodeRoot = ReadCsFile(domainProjectOptions, sgHandler.FocusOnSegmentName, sgHandler); var usedInterfacesInConstructor = GetUsedInterfacesInConstructor(srcSyntaxNodeRoot); var usingStatements = GetUsedUsingStatements( srcSyntaxNodeRoot, nsSrc, usedInterfacesInConstructor.Count > 0); var sb = new StringBuilder(); foreach (var item in usingStatements) { sb.AppendLine($"using {item};"); } sb.AppendLine(); GenerateCodeHelper.AppendNamespaceComment(sb, domainProjectOptions.ToolNameAndVersion); sb.AppendLine($"namespace {nsTest}"); sb.AppendLine("{"); GenerateCodeHelper.AppendGeneratedCodeAttribute(sb, domainProjectOptions.ToolName, domainProjectOptions.ToolVersion); sb.AppendLine(4, $"public class {sgHandler.HandlerTypeName}GeneratedTests"); sb.AppendLine(4, "{"); AppendInstantiateConstructor(sb, sgHandler, usedInterfacesInConstructor); if (sgHandler.HasParametersOrRequestBody) { sb.AppendLine(); AppendParameterArgumentNullCheck(sb, sgHandler, usedInterfacesInConstructor); } sb.AppendLine(4, "}"); sb.AppendLine("}"); var pathGenerated = Path.Combine(Path.Combine(domainProjectOptions.PathForTestHandlers !.FullName, area), "Generated"); var fileGenerated = new FileInfo(Path.Combine(pathGenerated, $"{sgHandler.HandlerTypeName}GeneratedTests.cs")); return(TextFileHelper.Save(fileGenerated, 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.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())); }