Skip to content

Commit

Permalink
Improve FilterCreationTests to catch changes better
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Sep 27, 2024
1 parent 81270ba commit 35befaa
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions tests/Tingle.AspNetCore.Swagger.Tests/FilterCreationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,45 @@ namespace Tingle.AspNetCore.Swagger.Tests;
public class FilterCreationTests
{
[Fact]
public void InheritDocSchemaFilter_IsAdded()
public void InheritDocSchemaFilter_IsAdded_And_Created()
{
var services = new ServiceCollection().AddLogging()
.AddSwaggerGen(o => o.IncludeXmlCommentsFromInheritDocs())
.BuildServiceProvider();
var services = CreateServiceProvider();

var options = services.GetRequiredService<IOptions<SwaggerGenOptions>>().Value;
var descriptors = options.SchemaFilterDescriptors.Where(sfd => sfd.Type == typeof(InheritDocSchemaFilter));
var options1 = services.GetRequiredService<IOptions<SwaggerGenOptions>>().Value;
var descriptors = options1.SchemaFilterDescriptors.Where(sfd => sfd.Type == typeof(InheritDocSchemaFilter));
var descriptor = Assert.Single(descriptors);
var arguments = descriptor.Arguments;
Assert.Equal(2, arguments.Length);
Assert.IsType<SwaggerGenOptions>(arguments[0]);
Assert.IsType<Type[]>(arguments[1]);
}

[Fact]
public void InheritDocSchemaFilter_CanBe_Created()
{
var services = new ServiceCollection().AddLogging()
.AddSwaggerGen(o => o.IncludeXmlCommentsFromInheritDocs())
.BuildServiceProvider();

var options = services.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
Assert.Single(options.SchemaFilters.OfType<InheritDocSchemaFilter>());
var options2 = services.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
Assert.Single(options2.SchemaFilters.OfType<InheritDocSchemaFilter>());
}

[Fact]
public void EnumDescriptionsFilters_AreAdded()
public void EnumDescriptionsFilters_AreAdded_And_Created()
{
var env = new FakeWebHostEnvironment { ApplicationName = "Test", ContentRootPath = Environment.CurrentDirectory, };
var services = new ServiceCollection().AddLogging()
.AddSwaggerGen(o => o.IncludeXmlComments<EnumDescriptionsSchemaFilter>(true))
.AddSwaggerEnumDescriptions()
.BuildServiceProvider();

var options = services.GetRequiredService<IOptions<SwaggerGenOptions>>().Value;
var services = CreateServiceProvider();

Assert.Single(options.SchemaFilterDescriptors, d => d.Type == typeof(EnumDescriptionsSchemaFilter));
}

[Fact]
public void EnumDescriptionsFilter_CanBe_Created()
{
var env = new FakeWebHostEnvironment { ApplicationName = "Test", ContentRootPath = Environment.CurrentDirectory, };
var services = new ServiceCollection().AddLogging()
.AddSwaggerGen(o => o.IncludeXmlComments<EnumDescriptionsSchemaFilter>(true))
.AddSwaggerEnumDescriptions()
.BuildServiceProvider();
var options1 = services.GetRequiredService<IOptions<SwaggerGenOptions>>().Value;
Assert.Single(options1.SchemaFilterDescriptors, d => d.Type == typeof(EnumDescriptionsSchemaFilter));

var options = services.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
Assert.Single(options.SchemaFilters.OfType<EnumDescriptionsSchemaFilter>());
var options2 = services.GetRequiredService<IOptions<SchemaGeneratorOptions>>().Value;
Assert.Single(options2.SchemaFilters.OfType<EnumDescriptionsSchemaFilter>());
}

private class FakeWebHostEnvironment : IWebHostEnvironment
private static IServiceProvider CreateServiceProvider(Action<IServiceCollection>? configure = null)
{
public string WebRootPath { get; set; } = default!;
public IFileProvider WebRootFileProvider { get; set; } = default!;
public string ApplicationName { get; set; } = default!;
public IFileProvider ContentRootFileProvider { get; set; } = default!;
public string ContentRootPath { get; set; } = default!;
public string EnvironmentName { get; set; } = default!;
var services = new ServiceCollection();
services.AddLogging();
services.AddSwaggerGen(options =>
{
options.IncludeXmlComments<EnumDescriptionsSchemaFilter>(true);
options.IncludeXmlCommentsFromInheritDocs();
});
services.AddSwaggerEnumDescriptions();
configure?.Invoke(services);
return services.BuildServiceProvider();
}
}

0 comments on commit 35befaa

Please sign in to comment.