Skip to content

Commit c7c65dd

Browse files
authored
Updated generator test harness to support providing a .json config file
Updated generator test harness to support providing a .json config file
2 parents 600afa4 + fea0915 commit c7c65dd

20 files changed

+129
-76
lines changed

test/Atc.Rest.ApiGenerator.Tests/Atc.Rest.ApiGenerator.Tests.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

@@ -11,6 +11,8 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
15+
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
1416
<PackageReference Include="Atc.CodeDocumentation" Version="1.1.102" />
1517
<PackageReference Include="Atc.XUnit" Version="1.1.102" />
1618
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
@@ -39,6 +41,9 @@
3941
<None Update="SyntaxGenerators\**\*.yaml">
4042
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4143
</None>
44+
<None Update="SyntaxGenerators\**\*.json">
45+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
46+
</None>
4247
</ItemGroup>
4348

4449
</Project>

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorContractInterfaceTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Atc.Rest.ApiGenerator.Tests.SyntaxGenerators.Api
1414
[UsesVerify]
1515
public class SyntaxGeneratorContractInterfaceTests : SyntaxGeneratorTestBase
1616
{
17-
public static IEnumerable<object[]> YamlFiles { get; } = AllFiles
18-
.Where(x => x.FilePath.Contains("ContractInterface", System.StringComparison.Ordinal))
17+
public static IEnumerable<object[]> TestInput { get; } = AllTestInput
18+
.Where(x => x.TestDirectory.Contains("ContractInterface", System.StringComparison.Ordinal))
1919
.Select(x => new object[] { x });
2020

2121
protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject)
@@ -38,11 +38,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
3838
}
3939

4040
[Theory(DisplayName = "Api Contract Interface")]
41-
[MemberData(nameof(YamlFiles))]
42-
public Task ExecuteGeneratorTest(YamlSpecFile specFile)
41+
[MemberData(nameof(TestInput))]
42+
public Task ExecuteGeneratorTest(GeneratorTestInput input)
4343
{
44-
Assert.NotNull(specFile.FilePath);
45-
return ExecuteTest(specFile);
44+
return VerifyGeneratedOutput(input);
4645
}
4746
}
4847
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorContractModelTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace Atc.Rest.ApiGenerator.Tests.SyntaxGenerators.Api
1313
[UsesVerify]
1414
public class SyntaxGeneratorContractModelTests : SyntaxGeneratorTestBase
1515
{
16-
public static IEnumerable<object[]> YamlFiles { get; } = AllFiles
17-
.Where(x => x.FilePath.Contains("ContractModel", System.StringComparison.Ordinal))
16+
public static IEnumerable<object[]> TestInput { get; } = AllTestInput
17+
.Where(x => x.TestDirectory.Contains("ContractModel", System.StringComparison.Ordinal))
1818
.Select(x => new object[] { x });
1919

2020
protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject)
@@ -28,11 +28,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
2828
}
2929

3030
[Theory(DisplayName = "Api Contract Model")]
31-
[MemberData(nameof(YamlFiles))]
32-
public Task ExecuteGeneratorTest(YamlSpecFile specFile)
31+
[MemberData(nameof(TestInput))]
32+
public Task ExecuteGeneratorTest(GeneratorTestInput input)
3333
{
34-
Assert.NotNull(specFile.FilePath);
35-
return ExecuteTest(specFile);
34+
return VerifyGeneratedOutput(input);
3635
}
3736
}
3837
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorContractParameterTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Atc.Rest.ApiGenerator.Tests.SyntaxGenerators.Api
1414
[UsesVerify]
1515
public class SyntaxGeneratorContractParameterTests : SyntaxGeneratorTestBase
1616
{
17-
public static IEnumerable<object[]> YamlFiles { get; } = AllFiles
18-
.Where(x => x.FilePath.Contains("ContractParameter", System.StringComparison.Ordinal))
17+
public static IEnumerable<object[]> TestInput { get; } = AllTestInput
18+
.Where(x => x.TestDirectory.Contains("ContractParameter", System.StringComparison.Ordinal))
1919
.Select(x => new object[] { x });
2020

2121
protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject)
@@ -38,11 +38,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
3838
}
3939

4040
[Theory(DisplayName = "Api Contract Parameter")]
41-
[MemberData(nameof(YamlFiles))]
42-
public Task ExecuteGeneratorTest(YamlSpecFile specFile)
41+
[MemberData(nameof(TestInput))]
42+
public Task ExecuteGeneratorTest(GeneratorTestInput input)
4343
{
44-
Assert.NotNull(specFile.FilePath);
45-
return ExecuteTest(specFile);
44+
return VerifyGeneratedOutput(input);
4645
}
4746
}
4847
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorContractResultTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Atc.Rest.ApiGenerator.Tests.SyntaxGenerators.Api
1414
[UsesVerify]
1515
public class SyntaxGeneratorContractResultTests : SyntaxGeneratorTestBase
1616
{
17-
public static IEnumerable<object[]> YamlFiles { get; } = AllFiles
18-
.Where(x => x.FilePath.Contains("ContractResult", System.StringComparison.Ordinal))
17+
public static IEnumerable<object[]> TestInput { get; } = AllTestInput
18+
.Where(x => x.TestDirectory.Contains("ContractResult", System.StringComparison.Ordinal))
1919
.Select(x => new object[] { x });
2020

2121
protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject)
@@ -36,11 +36,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
3636
}
3737

3838
[Theory(DisplayName = "Api Contract Result")]
39-
[MemberData(nameof(YamlFiles))]
40-
public Task ExecuteGeneratorTest(YamlSpecFile specFile)
39+
[MemberData(nameof(TestInput))]
40+
public Task ExecuteGeneratorTest(GeneratorTestInput input)
4141
{
42-
Assert.NotNull(specFile.FilePath);
43-
return ExecuteTest(specFile);
42+
return VerifyGeneratedOutput(input);
4443
}
4544
}
4645
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorEndpointControllersTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Atc.Rest.ApiGenerator.Helpers;
@@ -14,8 +15,8 @@ namespace Atc.Rest.ApiGenerator.Tests.SyntaxGenerators.Api
1415
[UsesVerify]
1516
public class SyntaxGeneratorEndpointControllersTests : SyntaxGeneratorTestBase
1617
{
17-
public static IEnumerable<object[]> YamlFiles { get; } = AllFiles
18-
.Where(x => x.FilePath.Contains("EndpointControllers", System.StringComparison.Ordinal))
18+
public static IEnumerable<object[]> TestInput { get; } = AllTestInput
19+
.Where(x => x.TestDirectory.Contains("EndpointControllers", System.StringComparison.Ordinal))
1920
.Select(x => new object[] { x });
2021

2122
protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject)
@@ -30,11 +31,10 @@ protected override ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions api
3031
}
3132

3233
[Theory(DisplayName = "Api Contract Controllers")]
33-
[MemberData(nameof(YamlFiles))]
34-
public Task ExecuteGeneratorTest(YamlSpecFile specFile)
34+
[MemberData(nameof(TestInput))]
35+
public Task ExecuteGeneratorTest(GeneratorTestInput input)
3536
{
36-
Assert.NotNull(specFile.FilePath);
37-
return ExecuteTest(specFile);
37+
return VerifyGeneratedOutput(input);
3838
}
3939
}
4040
}

test/Atc.Rest.ApiGenerator.Tests/SyntaxGenerators/Api/SyntaxGeneratorTestBase.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class SyntaxGeneratorTestBase
1919
private const string ProjectPrefix = "TestProject";
2020
private const string ProjectSuffix = "AtcTest";
2121

22-
protected static IReadOnlyList<YamlSpecFile> AllFiles { get; } = GetYamlFiles();
22+
protected static IReadOnlyList<GeneratorTestInput> AllTestInput { get; } = GetTestInput();
2323

2424
protected abstract ISyntaxCodeGenerator CreateApiGenerator(ApiProjectOptions apiProject);
2525

@@ -28,11 +28,11 @@ protected static Task VerifyGeneratedCode(string generatedCode, VerifySettings v
2828
return Verifier.Verify(generatedCode, verifySettings);
2929
}
3030

31-
protected async Task ExecuteTest(YamlSpecFile specFile)
31+
protected async Task VerifyGeneratedOutput(GeneratorTestInput input)
3232
{
3333
// Arrange
34-
var apiProject = await CreateApiProjectAsync(specFile);
35-
var verifySettings = CreateVerifySettings(specFile, apiProject);
34+
var apiProject = await CreateApiProjectAsync(input);
35+
var verifySettings = CreateVerifySettings(input, apiProject);
3636

3737
var sut = CreateApiGenerator(apiProject);
3838

@@ -43,27 +43,36 @@ protected async Task ExecuteTest(YamlSpecFile specFile)
4343
await VerifyGeneratedCode(generatedCode, verifySettings);
4444
}
4545

46-
private VerifySettings CreateVerifySettings(YamlSpecFile yamlFile, ApiProjectOptions apiOptions)
46+
private VerifySettings CreateVerifySettings(GeneratorTestInput yamlFile, ApiProjectOptions apiOptions)
4747
{
4848
var settings = new VerifySettings();
49-
settings.UseDirectory(yamlFile.DirectoryName);
50-
settings.UseFileName(yamlFile.FileName);
49+
settings.UseDirectory(yamlFile.TestDirectory);
50+
settings.UseFileName(yamlFile.TestName);
5151
settings.UseExtension("cs");
5252
settings.AddScrubber(input => input.Replace(apiOptions.ToolVersion.ToString(), "x.x.x.x"));
5353
return settings;
5454
}
5555

56-
private static IReadOnlyList<YamlSpecFile> GetYamlFiles([CallerFilePath] string sourceFilePath = "")
56+
private static IReadOnlyList<GeneratorTestInput> GetTestInput([CallerFilePath] string sourceFilePath = "")
5757
{
5858
var directory = Path.GetDirectoryName(sourceFilePath);
5959
return Directory.EnumerateFiles(directory, "*.yaml", SearchOption.AllDirectories)
60-
.Select(x => new YamlSpecFile(new FileInfo(x)))
60+
.Select(x =>
61+
{
62+
var specFile = new FileInfo(x);
63+
var configFilePath = Path.Combine(specFile.DirectoryName, Path.GetFileNameWithoutExtension(specFile.Name) + ".json");
64+
var configFile = File.Exists(configFilePath)
65+
? new FileInfo(configFilePath)
66+
: null;
67+
return new GeneratorTestInput(specFile, configFile);
68+
})
6169
.ToArray();
6270
}
6371

64-
private async Task<ApiProjectOptions> CreateApiProjectAsync(YamlSpecFile specFile)
72+
private async Task<ApiProjectOptions> CreateApiProjectAsync(GeneratorTestInput testInput)
6573
{
66-
var spec = await specFile.LoadFileContentAsync();
74+
var spec = await testInput.LoadYamlSpecContentAsync();
75+
var options = testInput.GeneratorOptions.Value;
6776
var document = GenerateApiDocument(spec);
6877

6978
return new ApiProjectOptions(
@@ -73,7 +82,7 @@ private async Task<ApiProjectOptions> CreateApiProjectAsync(YamlSpecFile specFil
7382
new FileInfo("resources/dummySpec.yaml"),
7483
ProjectPrefix,
7584
ProjectSuffix,
76-
new Models.ApiOptions.ApiOptions());
85+
options);
7786
}
7887

7988
private OpenApiDocument GenerateApiDocument(string spec)
@@ -89,4 +98,4 @@ private OpenApiDocument GenerateApiDocument(string spec)
8998
return openApiStreamReader.Read(memoryStream, out _);
9099
}
91100
}
92-
}
101+
}

0 commit comments

Comments
 (0)