Skip to content

Commit

Permalink
Migrate to TUnit (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored Oct 17, 2024
1 parent 1954d1a commit 2771eee
Show file tree
Hide file tree
Showing 23 changed files with 242 additions and 259 deletions.
4 changes: 4 additions & 0 deletions tests/Immediate.Apis.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[*.cs]

dotnet_diagnostic.CA1822.severity = none # CA1822: Mark members as static

[*.g.verified.cs]

# Mark code as excluded and generated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
Expand All @@ -7,7 +8,7 @@ namespace Immediate.Apis.Tests.AnalyzerTests;
public static class AnalyzerTestHelpers
{
public static CSharpAnalyzerTest<TAnalyzer, DefaultVerifier> CreateAnalyzerTest<TAnalyzer>(
string inputSource
[StringSyntax("c#-test")] string inputSource
)
where TAnalyzer : DiagnosticAnalyzer, new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Immediate.Apis.Tests.AnalyzerTests;

public sealed class CustomizeEndpointUsageAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -39,8 +39,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task MultipleDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -77,8 +77,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidAccessibilityShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -112,8 +112,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InstanceMethodShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -147,8 +147,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidReturnShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -182,8 +182,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidParameterTypeShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ namespace Immediate.Apis.Tests.AnalyzerTests;

public sealed class InvalidAuthorizeAttributeAnalyzerTests
{
[Fact]
[Test]
public async Task AuthorizeAloneShouldNotError() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
"""
using System.Threading;
using System.Threading.Tasks;
using Immediate.Apis.Shared;
Expand All @@ -32,8 +32,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizeRolesShouldError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down Expand Up @@ -62,8 +62,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizeAuthenticationSchemesShouldError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down Expand Up @@ -92,8 +92,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizeShouldNotError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down Expand Up @@ -122,8 +122,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizeConstructorShouldNotError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down Expand Up @@ -152,8 +152,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizePolicyShouldNotError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down Expand Up @@ -182,8 +182,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task AuthorizeAndAllowAnonymousShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<InvalidAuthorizeAttributeAnalyzer>(
$$"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Immediate.Apis.Tests.AnalyzerTests;
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Test names")]
public sealed class MissingCustomizeEndpointMethodAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinitionShouldRaiseHiddenDiagnostic(string method)
{
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingCustomizeEndpointMethodAnalyzer>(
Expand Down Expand Up @@ -40,8 +40,8 @@ private static async ValueTask<int> Handle(
).RunAsync();
}

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinition_WithExistingCustomizeEndpointMethod_ShouldNotRaiseHiddenDiagnostic(string method)
{
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingCustomizeEndpointMethodAnalyzer>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Immediate.Apis.Tests.AnalyzerTests;

public sealed class MissingHandlerAttributeAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task MissingHandlerAttributeShouldError(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingHandlerAttributeAnalyzer>(
$$"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Immediate.Apis.Tests.AnalyzerTests;
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Test names")]
public sealed class MissingTransformResultMethodAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinitionShouldRaiseHiddenDiagnostic(string method)
{
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingTransformResultMethodAnalyzer>(
Expand Down Expand Up @@ -40,8 +40,8 @@ private static async ValueTask<int> Handle(
).RunAsync();
}

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinition_WithExistingTransformResultMethod_ShouldNotRaiseHiddenDiagnostic(string method)
{
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingTransformResultMethodAnalyzer>(
Expand Down Expand Up @@ -77,8 +77,8 @@ private static async ValueTask<int> Handle(
).RunAsync();
}

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidDefinition_ShouldNotRaiseHiddenDiagnostic(string method)
{
await AnalyzerTestHelpers.CreateAnalyzerTest<MissingTransformResultMethodAnalyzer>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Immediate.Apis.Tests.AnalyzerTests;

public sealed class TransformResultUsageAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -34,8 +34,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task MultipleDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -65,8 +65,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidAccessibilityShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -95,8 +95,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InstanceMethodShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -125,8 +125,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task InvalidParameterTypeShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down Expand Up @@ -155,8 +155,8 @@ private static async ValueTask<int> Handle(
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task VoidReturnShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<TransformResultUsageAnalyzer>(
$$"""
Expand Down
5 changes: 3 additions & 2 deletions tests/Immediate.Apis.Tests/CodeFixTests/CodeFixTestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -19,8 +20,8 @@ public static class CodeFixTestHelper
""";

public static CSharpCodeFixTest<TAnalyzer, TCodeFix, DefaultVerifier> CreateCodeFixTest<TAnalyzer, TCodeFix>(
string inputSource,
string fixedSource,
[StringSyntax("c#-test")] string inputSource,
[StringSyntax("c#-test")] string fixedSource,
int codeActionIndex = 0
)
where TAnalyzer : DiagnosticAnalyzer, new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Immediate.Apis.Tests.CodeFixTests;
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Test names")]
public sealed class MissingCustomizeEndpointMethodCodeFixTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinition_ShouldAddCustomizeEndpointMethod(string method)
{
await CodeFixTestHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Immediate.Apis.Tests.CodeFixTests;
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Test names")]
public sealed class MissingHandlerAttributeCodeFixTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task MissingHandlerAttribute_ShouldAddAttributeAndUsing(string method)
{
await CodeFixTestHelper
Expand Down Expand Up @@ -58,8 +58,8 @@ private static async ValueTask<int> Handle(
).RunAsync();
}

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task MissingHandlerAttributeWithUsing_ShouldAddAttributeOnly(string method)
{
await CodeFixTestHelper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Immediate.Apis.Tests.CodeFixTests;
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Test names")]
public sealed class MissingTransformResultMethodCodeFixTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
[Test]
[MethodDataSource(typeof(Utility), nameof(Utility.Methods))]
public async Task ValidDefinition_ShouldAddTransformResultMethod(string method)
{
await CodeFixTestHelper
Expand Down
6 changes: 2 additions & 4 deletions tests/Immediate.Apis.Tests/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.11.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="MinVer" Version="5.0.0" />
<PackageVersion Include="PolySharp" Version="1.14.1" />
<PackageVersion Include="Scriban" Version="5.10.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageVersion Include="TUnit" Version="0.1.1065" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.5.0" />
<PackageVersion Include="Verify.Xunit" Version="26.6.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="Verify.TUnit" Version="27.0.1" />
</ItemGroup>
</Project>
Loading

0 comments on commit 2771eee

Please sign in to comment.