Skip to content

Commit

Permalink
refactoring test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zijianhuang committed Jun 10, 2024
1 parent 593218d commit d197eb1
Show file tree
Hide file tree
Showing 28 changed files with 81 additions and 41 deletions.
4 changes: 2 additions & 2 deletions StartPetsApi.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#Launch WebApi Website and POST a request for generating client APIs
cd $PSScriptRoot
$path = "$PSScriptRoot\PetWebApi\bin\Debug\net8.0"
$path = "$PSScriptRoot/PetWebApi"
$procArgs = @{
FilePath = "dotnet.exe"
ArgumentList = "$path\PetWebApi.dll"
ArgumentList = "run --project $path/PetWebApi.csproj --no-build"
WorkingDirectory = $path
PassThru = $true
}
Expand Down
2 changes: 1 addition & 1 deletion TestReleaseAll.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dotnet test --no-build --verbosity normal --configuration Release
dotnet test --verbosity normal --configuration Release
2 changes: 1 addition & 1 deletion TestReleaseBasic.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Test only those suites without referecing to OpenApiDir. This is also used in DotNetRelease.yml of GitHub actions
dotnet test --no-build --verbosity normal --configuration Release --filter "(FullyQualifiedName~Tests) & (FullyQualifiedName!~OpenApiDirTests)"
dotnet test --verbosity normal --configuration Release --filter "(FullyQualifiedName~Tests) & (FullyQualifiedName!~OpenApiDirTests)"
4 changes: 3 additions & 1 deletion Tests/CsOpenApi3Tests/OpenApi3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public partial class OpenApi3Tests
const string openDirName= "openapi-directory20240114";
public OpenApi3Tests(ITestOutputHelper output)
{
helper = new CSharpTestHelperForOpenApiDir(output, "openapi.yaml", OpenApi3CodeGenSettings.Default);
helper = new CSharpTestHelperForOpenApiDir(output, "openapi.yaml", OpenApi3CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success) => Assert.True(success));
}

void GenerateFromOpenApiAndBuild(string filePath, ISettings mySettings = null)
Expand Down
4 changes: 3 additions & 1 deletion Tests/CsSwagger2Tests/ODAzureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class ODAzureTests
readonly CSharpTestHelperForOpenApiDir helper;
public ODAzureTests(ITestOutputHelper output)
{
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default);
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success) => Assert.True(success));
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion Tests/CsSwagger2Tests/ODAzureTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class ODAzureTests2
readonly CSharpTestHelperForOpenApiDir helper;
public ODAzureTests2(ITestOutputHelper output)
{
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default);
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success) => Assert.True(success));
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion Tests/CsSwagger2Tests/ODMiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class ODMiscTests
readonly CSharpTestHelperForOpenApiDir helper;
public ODMiscTests(ITestOutputHelper output)
{
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default);
helper = new CSharpTestHelperForOpenApiDir(output, "swagger.yaml", Swagger2CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success) => Assert.True(success));
}

[Fact]
Expand Down
11 changes: 7 additions & 4 deletions Tests/CsTestHelpers/CSharpTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace Fonlow.OpenApiClientGen.TestHelpers
Expand All @@ -15,11 +14,15 @@ public class CSharpTestHelper: ITestHelper
{
readonly protected ITestOutputHelper output;
readonly protected ISettings defaultSettings;
readonly protected Action<string, string> assertEqual;
readonly protected Action<bool> assertTrue;

public CSharpTestHelper(ITestOutputHelper output, ISettings defaultSettings)
public CSharpTestHelper(ITestOutputHelper output, ISettings defaultSettings, Action<string, string> assertEqual, Action<bool> assertTrue)
{
this.output = output;
this.defaultSettings = defaultSettings;
this.assertEqual = assertEqual;
this.assertTrue = assertTrue;
}

static OpenApiDocument ReadDef(string filePath)
Expand Down Expand Up @@ -64,7 +67,7 @@ public void GenerateAndAssertAndBuild(string filePath, string expectedFile, ISet
else
{
string expected = ReadFromResults(expectedFile);
Assert.Equal(expected, s, ignoreLineEndingDifferences: true);
assertEqual(expected, s);//, ignoreLineEndingDifferences: true);
}

if (TestingSettings.Instance.Build)
Expand All @@ -80,7 +83,7 @@ public void GenerateAndAssertAndBuild(string filePath, string expectedFile, ISet
}
}

Assert.True(r.Success);
assertTrue(r.Success);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/CsTestHelpers/CSharpTestHelperForOpenApiDir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using System.IO;
using Xunit;
using Fonlow.OpenApiClientGen.CS;
using Xunit.Abstractions;
using System;
Expand All @@ -12,7 +11,8 @@ namespace Fonlow.OpenApiClientGen.TestHelpers
{
public class CSharpTestHelperForOpenApiDir: CSharpTestHelper, IOpenApiDirTestHelper
{
public CSharpTestHelperForOpenApiDir(ITestOutputHelper output, string defaultDefName, ISettings defaultSettings) : base(output, defaultSettings)
public CSharpTestHelperForOpenApiDir(ITestOutputHelper output, string defaultDefName,
ISettings defaultSettings, Action<string, string> assertEqual, Action<bool> assertTrue) : base(output, defaultSettings, assertEqual, assertTrue)
{
this.defaultDefName = defaultDefName;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ public void GenerateFromOpenApiAndBuild(string openapiDir, ISettings mySettings=
else
{
string expected = File.ReadAllText(csFilePath);
Assert.Equal(expected, s, ignoreLineEndingDifferences: true);
assertEqual(expected, s);
}

if (TestingSettings.Instance.Build)
Expand All @@ -70,7 +70,7 @@ public void GenerateFromOpenApiAndBuild(string openapiDir, ISettings mySettings=
}
}

Assert.True(r.Success);
assertTrue(r.Success);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/CsTestHelpers/CsTestHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion Tests/NG2OpenApi3Tests/OpenApi3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public partial class OpenApi3Tests
const string openDirName= "openapi-directory20240114";
public OpenApi3Tests(ITestOutputHelper output)
{
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output);
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual) => Assert.Equal(expected, actual));
}
}
}
4 changes: 3 additions & 1 deletion Tests/NG2Tests/ODAzureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public partial class ODAzureTests
readonly NG2OpenApiDirTestHelper helper;
public ODAzureTests(ITestOutputHelper output)
{
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output, "swagger.yaml");
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual) => Assert.Equal(expected, actual), "swagger.yaml");
}

}
Expand Down
4 changes: 3 additions & 1 deletion Tests/NG2Tests/ODAzureTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public partial class ODAzureTests2
readonly NG2OpenApiDirTestHelper helper;
public ODAzureTests2(ITestOutputHelper output)
{
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output, "swagger.yaml");
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual) => Assert.Equal(expected, actual), "swagger.yaml");
}
}
}
4 changes: 3 additions & 1 deletion Tests/NG2Tests/ODMiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public partial class ODMiscTests
readonly NG2OpenApiDirTestHelper helper;
public ODMiscTests(ITestOutputHelper output)
{
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output, "swagger.yaml");
helper = new NG2OpenApiDirTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual) => Assert.Equal(expected, actual), "swagger.yaml");
}
}
}
8 changes: 5 additions & 3 deletions Tests/SwagTests/CodeGenCsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class CodeGenCsTests
{
public CodeGenCsTests(ITestOutputHelper output)
{
helper = new CSharpTestHelper(output, CodeGenSettings.Default);
helper = new CSharpTestHelper(output, CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success)=> Assert.True(success));
}

readonly CSharpTestHelper helper;
Expand Down Expand Up @@ -465,7 +467,7 @@ public void TestGoogleBooks()
GenerateAndAssertAndBuild("SwagMock/googleBooksOpenApi.yaml", "Results/googleBooksOpenApi.txt");
}

[Fact(Skip ="Not sure")]
[Fact(Skip = "Not sure")]
public void TestAzureSecurityInsights()
{
GenerateAndAssertAndBuild("SwagMock/AzureSecurityInsights.yaml", "Results/AzureSecurityInsights.txt");
Expand All @@ -477,7 +479,7 @@ public void TestAzureSecurityInsights()
/// <summary>
/// OpenApi v3.1
/// </summary>
[Fact(Skip ="Until OpenApi v3.1 is supported by Microsoft.OpenApi.Readers")]
[Fact(Skip = "Until OpenApi v3.1 is supported by Microsoft.OpenApi.Readers")]
public void Testwebhook31()
{
GenerateAndAssertAndBuild("SwagMock/webhook31.yaml", "Results/webhook31.txt");
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwagTests/VstsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public class VstsCsTests
{
public VstsCsTests(ITestOutputHelper output)
{
helper = new CSharpTestHelper(output, CodeGenSettings.Default);
helper = new CSharpTestHelper(output, CodeGenSettings.Default,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(bool success) => Assert.True(success));
}

readonly CSharpTestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/CodeGenAureliaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class CodeGenAureliaTests
{
public CodeGenAureliaTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsAureliaClientApiGen), TestingSettings.Instance);
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsAureliaClientApiGen), TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/CodeGenAxiosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class CodeGenAxiosTests
{
public CodeGenAxiosTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsAxiosClientApiGen), TestingSettings.Instance);
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsAxiosClientApiGen), TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/CodeGenFetchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class CodeGenFetchTests
{
public CodeGenFetchTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsFetchClientApiGen), TestingSettings.Instance);
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsFetchClientApiGen), TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/CodeGenJqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class CodeGenJqTests
{
public CodeGenJqTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsJqClientApiGen), TestingSettings.Instance);
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsJqClientApiGen), TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwagTsTests/CodeGenNG2BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class CodeGenNG2BuildTests
{
public CodeGenNG2BuildTests(ITestOutputHelper output)
{
helper = new NG2TestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2ClientApiGen), output, TestingSettings.Instance);
helper = new NG2TestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2ClientApiGen), output, TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual)=> Assert.Equal(expected, actual));
}

readonly NG2TestHelper helper;
Expand Down
4 changes: 3 additions & 1 deletion Tests/SwagTsTests/CodeGenNG2FormGroupBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class CodeGenNG2FormGroupBuildTests
{
public CodeGenNG2FormGroupBuildTests(ITestOutputHelper output)
{
helper = new NG2TestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output, TestingSettings.Instance);
helper = new NG2TestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), output, TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true),
(int expected, int actual) => Assert.Equal(expected, actual));
}

readonly NG2TestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/ComponentsToTsTypesFormGroupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class ComponentsToTsTypesFormGroupTests
{
public ComponentsToTsTypesFormGroupTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), TestingSettings.Instance);
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2FormGroupClientApiGen), TestingSettings.Instance,
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
3 changes: 2 additions & 1 deletion Tests/SwagTsTests/ComponentsToTsTypesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class ComponentsToTsTypesTests
{
public ComponentsToTsTypesTests()
{
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2ClientApiGen), new CasualTestingSettings(false, false));
helper = new TsTestHelper(typeof(Fonlow.CodeDom.Web.Ts.ControllersTsNG2ClientApiGen), new CasualTestingSettings(false, false),
(string expected, string s) => Assert.Equal(expected, s, ignoreLineEndingDifferences: true));
}

readonly TsTestHelper helper;
Expand Down
6 changes: 4 additions & 2 deletions Tests/TsTestHelpers/NG2OpenApiDirTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public class NG2OpenApiDirTestHelper: NG2TestHelper, IOpenApiDirTestHelper
/// <param name="genType"></param>
/// <param name="output"></param>
/// <param name="defaultDefFile">default is openapi.yaml</param>
public NG2OpenApiDirTestHelper(Type genType, ITestOutputHelper output, string defaultDefFile="openapi.yaml", ISettings defaultSettings=null) : base(genType, output,
new CasualTestingSettings(TestHelpers.TestingSettings.Instance.Build, TestHelpers.TestingSettings.Instance.UpdateGenerated))
public NG2OpenApiDirTestHelper(Type genType, ITestOutputHelper output,
Action<string, string> assertEqual, Action<int, int> assertEqualInt, string defaultDefFile="openapi.yaml", ISettings defaultSettings=null)
: base(genType, output,
new CasualTestingSettings(TestHelpers.TestingSettings.Instance.Build, TestHelpers.TestingSettings.Instance.UpdateGenerated), assertEqual, assertEqualInt)
{
this.defaultDefFile = defaultDefFile;
this.defaultSettings = defaultSettings;
Expand Down
9 changes: 6 additions & 3 deletions Tests/TsTestHelpers/NG2TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class NG2TestHelper : TsTestHelper, ITestHelper
{
readonly ITestOutputHelper output;
readonly bool buildToValidate;
readonly Action<int, int> assertEqualInt;

public NG2TestHelper(Type genType, ITestOutputHelper output, ITestingSettings testingSettings) : base(genType, testingSettings)
public NG2TestHelper(Type genType, ITestOutputHelper output, ITestingSettings testingSettings, Action<string, string> assertEqual, Action<int, int> assertEqualInt)
: base(genType, testingSettings, assertEqual)
{
this.output = output;
this.buildToValidate = testingSettings.Build;
this.assertEqualInt = assertEqualInt;
}

public void GenerateAndAssertAndBuild(string openApiFile, string expectedFile, ISettings settings = null)
Expand All @@ -41,12 +44,12 @@ public void GenerateAndAssertAndBuild(string openApiFile, string expectedFile, I
else
{
string expected = ReadFromResults(expectedFile);
Assert.Equal(expected, s, ignoreLineEndingDifferences: true);
assertEqual(expected, s);
}

if (buildToValidate)
{
Assert.Equal(0, CheckNGBuild(s));
assertEqualInt(0, CheckNGBuild(s));
}
}

Expand Down
6 changes: 4 additions & 2 deletions Tests/TsTestHelpers/TsTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ public class TsTestHelper
///
/// </summary>
/// <param name="codeGenType">Code gen Type to be instantiated.</param>
public TsTestHelper(Type codeGenType, ITestingSettings testingSettings)
public TsTestHelper(Type codeGenType, ITestingSettings testingSettings, Action<string, string> assertEqual)
{
this.codeGenType = codeGenType;
this.testingSettings = testingSettings;
this.assertEqual = assertEqual;
}

readonly Type codeGenType;
protected readonly ITestingSettings testingSettings;
protected readonly Action<string, string> assertEqual;

static OpenApiDocument ReadOpenApiDef(string filePath)
{
Expand Down Expand Up @@ -116,7 +118,7 @@ public void GenerateAndAssert(string openApiFile, string expectedFile, ISettings
}

string expected = ReadFromResults(expectedFile);
Assert.Equal(expected, s, ignoreLineEndingDifferences: true);
assertEqual(expected, s);
}
}

Expand Down
Loading

0 comments on commit d197eb1

Please sign in to comment.