Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Mar 21, 2024
1 parent 4843c4f commit 8cffd05
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed class CustomizeEndpointUsageAnalyzerTests
{
[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public async Task ValidDefinitionShouldNotError(string method) =>
public async Task ValidDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
using System.Threading;
Expand Down Expand Up @@ -43,7 +43,44 @@ private static async ValueTask<int> Handle(

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public async Task InvalidAccessibilityShouldError(string method) =>
public async Task MultipleDefinitionShouldNotWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
using System.Threading;
using System.Threading.Tasks;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
namespace Dummy;
[Handler]
[Map{{method}}("/test")]
[Authorize(Roles = "")]
public static class GetUsersQuery
{
internal static void CustomizeEndpoint(Microsoft.AspNetCore.{|CS0234:Builder|}.IEndpointConventionBuilder endpoint)
=> endpoint
.WithDescription("");
internal static void CustomizeEndpoint(int id)
=> id.ToString();
public record Query;
private static async ValueTask<int> Handle(
Query _,
CancellationToken token)
{
return 0;
}
}
"""
).RunAsync();

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public async Task InvalidAccessibilityShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
using System.Threading;
Expand Down Expand Up @@ -77,7 +114,7 @@ private static async ValueTask<int> Handle(

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public async Task InstanceMethodShouldError(string method) =>
public async Task InstanceMethodShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
using System.Threading;
Expand Down Expand Up @@ -111,7 +148,7 @@ private static async ValueTask<int> Handle(

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

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public async Task InvalidParameterTypeShouldError(string method) =>
public async Task InvalidParameterTypeShouldWarn(string method) =>
await AnalyzerTestHelpers.CreateAnalyzerTest<CustomizeEndpointUsageAnalyzer>(
$$"""
using System.Threading;
Expand Down
35 changes: 35 additions & 0 deletions tests/Immediate.Apis.Tests/GeneratorTests/InvalidCodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,39 @@ private static ValueTask<int> HandleAsync(
Assert.Empty(result.Diagnostics);
Assert.Empty(result.GeneratedTrees);
}

[Theory]
[MemberData(nameof(Utility.Methods), MemberType = typeof(Utility))]
public void AuthorizePolicyHasInvalidType(string method)
{
var driver = GeneratorTestHelper.GetDriver(
$$"""
using System.Threading.Tasks;
using Immediate.Apis.Shared;
using Immediate.Handlers.Shared;
using Microsoft.AspNetCore.Authorization;
namespace Dummy;
[Handler]
[Map{{method}}("/test")]
[Authorize(Policy = 3)]
public static class GetUsersQuery
{
public record Query;
private static ValueTask<int> HandleAsync(
Query _,
CancellationToken token)
{
return 0;
}
}
""");

var result = driver.GetRunResult();

Assert.Empty(result.Diagnostics);
Assert.Empty(result.GeneratedTrees);
}
}

0 comments on commit 8cffd05

Please sign in to comment.