Skip to content

Commit

Permalink
Apply suggestions from review
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed Feb 5, 2025
1 parent 4aaecc9 commit 3eeb5b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 248 deletions.
238 changes: 28 additions & 210 deletions src/Jab.FunctionalTests.Common/ContainerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#nullable enable
using Jab;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

using Jab;

namespace JabTests
{
public partial class ContainerTests
Expand Down Expand Up @@ -548,9 +547,9 @@ public void DisposingScopeDisposesServices()
internal partial class DisposingScopeDisposesServicesContainer { }

[Fact]
public void DisposingScopeDisposesFactoryGeneratedServicesWhenInstructedTo()
public void DisposingScopeDisposesFactoryGeneratedServices()
{
DisposingScopeDisposesFactoryGeneratedServicesWhenInstructedToServicesContainer c = new();
DisposingScopeDisposesFactoryGeneratedServicesContainer c = new();
var scope = c.CreateScope();
var service = Assert.IsType<DisposableServiceImplementation>(scope.GetService<IService>());

Expand All @@ -559,28 +558,9 @@ public void DisposingScopeDisposesFactoryGeneratedServicesWhenInstructedTo()
Assert.Equal(1, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateDisposableService), AutoDispose = true)]
internal partial class DisposingScopeDisposesFactoryGeneratedServicesWhenInstructedToServicesContainer
{
private static IService CreateDisposableService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingScopeDoesNotDisposeFactoryGeneratedServicesWhenNotInstructedTo()
{
DisposingScopeDoesNotDisposeFactoryGeneratedServicesWhenNotInstructedToServicesContainer c = new();
var scope = c.CreateScope();
var service = Assert.IsType<DisposableServiceImplementation>(scope.GetService<IService>());

scope.Dispose();

Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateDisposableService))]
internal partial class DisposingScopeDoesNotDisposeFactoryGeneratedServicesWhenNotInstructedToServicesContainer
internal partial class DisposingScopeDisposesFactoryGeneratedServicesContainer
{
private static IService CreateDisposableService() => new DisposableServiceImplementation();
}
Expand Down Expand Up @@ -612,9 +592,9 @@ public async Task DisposingScopeDisposesAsyncServices()
internal partial class DisposingScopeDisposesAsyncServicesContainer { }

[Fact]
public async Task DisposingScopeDisposesFactoryGeneratedAsyncServicesWhenInstructedTo()
public async Task DisposingScopeDisposesFactoryGeneratedAsyncServices()
{
DisposingScopeDisposesFactoryGeneratedAsyncServicesWhenInstructedToServicesContainer c = new();
DisposingScopeDisposesFactoryGeneratedAsyncServicesContainer c = new();
var scope = c.CreateScope();
var service = Assert.IsType<AsyncDisposableServiceImplementation>(scope.GetService<IService>());

Expand All @@ -632,37 +612,9 @@ public async Task DisposingScopeDisposesFactoryGeneratedAsyncServicesWhenInstruc
Assert.Equal(1, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingScopeDisposesFactoryGeneratedAsyncServicesWhenInstructedToServicesContainer
{
private static IService CreateService() => new AsyncDisposableServiceImplementation();
}

[Fact]
public async Task DisposingScopeDoesNotDisposeFactoryGeneratedAsyncServicesWhenNotInstructedTo()
{
DisposingScopeDoesNotDisposeFactoryGeneratedAsyncServicesWhenNotInstructedToServicesContainer c = new();
var scope = c.CreateScope();
var service = Assert.IsType<AsyncDisposableServiceImplementation>(scope.GetService<IService>());

await scope.DisposeAsync();

Assert.Equal(0, service.AsyncDisposalCount);
Assert.Equal(0, service.DisposalCount);

scope = c.CreateScope();
service = Assert.IsType<AsyncDisposableServiceImplementation>(scope.GetService<IService>());

scope.Dispose();

Assert.Equal(0, service.AsyncDisposalCount);
Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService))]
internal partial class DisposingScopeDoesNotDisposeFactoryGeneratedAsyncServicesWhenNotInstructedToServicesContainer
internal partial class DisposingScopeDisposesFactoryGeneratedAsyncServicesContainer
{
private static IService CreateService() => new AsyncDisposableServiceImplementation();
}
Expand All @@ -686,35 +638,17 @@ internal partial class DisposingProviderDisposesRootScopedServicesContainer { }
[Fact]
public void DisposingProviderDisposesFactoryGeneratedRootScopedServicesIfInstructedTo()
{
DisposingProviderDisposesFactoryGeneratedRootScopedServicesIfInstructedToServicesContainer c = new();
DisposingProviderDisposesFactoryGeneratedRootScopedServicesContainer c = new();
var service = Assert.IsType<DisposableServiceImplementation>(c.GetService<IService>());

c.Dispose();

Assert.Equal(1, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(GenerateService), AutoDispose = true)]
internal partial class DisposingProviderDisposesFactoryGeneratedRootScopedServicesIfInstructedToServicesContainer
{
private static IService GenerateService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingProviderDoesNotDisposeFactoryGeneratedRootScopedServicesIfNotInstructedTo()
{
DisposingProviderDoesNotDisposeFactoryGeneratedRootScopedServicesIfNotInstructedToServicesContainer c = new();
var service = Assert.IsType<DisposableServiceImplementation>(c.GetService<IService>());

c.Dispose();

Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(GenerateService))]
internal partial class DisposingProviderDoesNotDisposeFactoryGeneratedRootScopedServicesIfNotInstructedToServicesContainer
internal partial class DisposingProviderDisposesFactoryGeneratedRootScopedServicesContainer
{
private static IService GenerateService() => new DisposableServiceImplementation();
}
Expand All @@ -735,37 +669,19 @@ public void DisposingProviderDisposesRootSingletonServices()
internal partial class DisposingProviderDisposesRootSingletonServicesContainer { }

[Fact]
public void DisposingProviderDisposesFactoryGeneratedRootSingletonServicesWhenInstructedTo()
public void DisposingProviderDisposesFactoryGeneratedRootSingletonServices()
{
DisposingProviderDisposesFactoryGeneratedRootSingletonServicesWhenInstructedToServicesContainer c = new();
DisposingProviderDisposesFactoryGeneratedRootSingletonServicesContainer c = new();
var service = Assert.IsType<DisposableServiceImplementation>(c.GetService<IService>());

c.Dispose();

Assert.Equal(1, service.DisposalCount);
}

[ServiceProvider]
[Singleton(typeof(IService), Factory = nameof(GetService), AutoDispose = true)]
internal partial class DisposingProviderDisposesFactoryGeneratedRootSingletonServicesWhenInstructedToServicesContainer
{
private static IService GetService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingProviderDoesNotDisposeFactoryGeneratedRootSingletonServicesWhenNotInstructedTo()
{
DisposingProviderDoesNotDisposeFactoryGeneratedRootSingletonServicesWhenNotInstructedToServicesContainer c = new();
var service = Assert.IsType<DisposableServiceImplementation>(c.GetService<IService>());

c.Dispose();

Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Singleton(typeof(IService), Factory = nameof(GetService))]
internal partial class DisposingProviderDoesNotDisposeFactoryGeneratedRootSingletonServicesWhenNotInstructedToServicesContainer
internal partial class DisposingProviderDisposesFactoryGeneratedRootSingletonServicesContainer
{
private static IService GetService() => new DisposableServiceImplementation();
}
Expand All @@ -788,9 +704,9 @@ public async Task DisposingProviderDisposesRootSingAsyncServices()
internal partial class DisposingProviderDisposesRootSingAsyncServicesContainer { }

[Fact]
public async Task DisposingProviderDisposesFactoryGeneratedRootSingAsyncServicesWhenInstructedTo()
public async Task DisposingProviderDisposesFactoryGeneratedRootSingAsyncServices()
{
DisposingProviderDisposesFactoryGeneratedRootSingAsyncServicesWhenInstructedToServicesContainer c = new();
DisposingProviderDisposesFactoryGeneratedRootSingAsyncServicesContainer c = new();
var service = Assert.IsType<AsyncDisposableServiceImplementation>(c.GetService<IService>());

await c.DisposeAsync();
Expand All @@ -799,28 +715,9 @@ public async Task DisposingProviderDisposesFactoryGeneratedRootSingAsyncServices
Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingProviderDisposesFactoryGeneratedRootSingAsyncServicesWhenInstructedToServicesContainer
{
private static IService CreateService() => new AsyncDisposableServiceImplementation();
}

[Fact]
public async Task DisposingProviderDoesNotDisposeFactoryGeneratedRootSingAsyncServicesWhenNotInstructedTo()
{
DisposingProviderDoesNotDisposeFactoryGeneratedRootSingAsyncServicesWhenNotInstructedToServicesContainer c = new();
var service = Assert.IsType<AsyncDisposableServiceImplementation>(c.GetService<IService>());

await c.DisposeAsync();

Assert.Equal(0, service.AsyncDisposalCount);
Assert.Equal(0, service.DisposalCount);
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService))]
internal partial class DisposingProviderDoesNotDisposeFactoryGeneratedRootSingAsyncServicesWhenNotInstructedToServicesContainer
internal partial class DisposingProviderDisposesFactoryGeneratedRootSingAsyncServicesContainer
{
private static IService CreateService() => new AsyncDisposableServiceImplementation();
}
Expand Down Expand Up @@ -851,7 +748,7 @@ internal partial class DisposingProviderDisposesAllSingletonEnumerableServicesCo
[Fact]
public async Task DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerableServicesWhenInstructedTo()
{
DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerableServicesWhenInstructedToServicesContainer c = new();
DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerableServicesContainer c = new();
var services = Assert.IsType<IService[]>(c.GetService<IEnumerable<IService>>());

await c.DisposeAsync();
Expand All @@ -863,36 +760,11 @@ public async Task DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerabl
}
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerableServicesWhenInstructedToServicesContainer
{
private static IService CreateService() => new DisposableServiceImplementation();
}

[Fact]
public async Task DisposingProviderDisposesOnlyFactoryGeneratedSingletonEnumerableServicesThatAreRegisteredToDispose()
{
DisposingProviderDisposesOnlyFactoryGeneratedSingletonEnumerableServicesThatAreRegisteredToDisposeServicesContainer c = new();
var services = Assert.IsType<IService[]>(c.GetService<IEnumerable<IService>>());

await c.DisposeAsync();

for (int i = 0; i < services.Length; i++)
{
var disposableService = Assert.IsType<DisposableServiceImplementation>(services[i]);
Assert.Equal(i % 2, disposableService.DisposalCount);
}
}

[ServiceProvider]
[Scoped(typeof(IService), Factory = nameof(CreateService))]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
[Scoped(typeof(IService), Factory = nameof(CreateService))]
[Scoped(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingProviderDisposesOnlyFactoryGeneratedSingletonEnumerableServicesThatAreRegisteredToDisposeServicesContainer
[Scoped(typeof(IService), Factory = nameof(CreateService))]
internal partial class DisposingProviderDisposesAllFactoryGeneratedSingletonEnumerableServicesContainer
{
private static IService CreateService() => new DisposableServiceImplementation();
}
Expand Down Expand Up @@ -922,9 +794,9 @@ public void DisposingProviderDisposesTransients()
internal partial class DisposingProviderDisposesTransientsContainer { }

[Fact]
public void DisposingProviderDisposesFactoryGeneratedTransientsIfInstructedTo()
public void DisposingProviderDisposesFactoryGeneratedTransients()
{
DisposingProviderDisposesFactoryGeneratedTransientsIfInstructedToContainer c = new();
DisposingProviderDisposesFactoryGeneratedTransientsContainer c = new();
List<IService> services = new();
for (int i = 0; i < 5; i++)
{
Expand All @@ -940,35 +812,9 @@ public void DisposingProviderDisposesFactoryGeneratedTransientsIfInstructedTo()
}
}

[ServiceProvider]
[Transient(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingProviderDisposesFactoryGeneratedTransientsIfInstructedToContainer
{
private static IService CreateService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingProviderDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedTo()
{
DisposingProviderDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedToContainer c = new();
List<IService> services = new();
for (int i = 0; i < 5; i++)
{
services.Add(c.GetService<IService>());
}

c.Dispose();

foreach (var service in services)
{
var disposableService = Assert.IsType<DisposableServiceImplementation>(service);
Assert.Equal(0, disposableService.DisposalCount);
}
}

[ServiceProvider]
[Transient(typeof(IService), Factory = nameof(CreateService))]
internal partial class DisposingProviderDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedToContainer
internal partial class DisposingProviderDisposesFactoryGeneratedTransientsContainer
{
private static IService CreateService() => new DisposableServiceImplementation();
}
Expand Down Expand Up @@ -999,9 +845,9 @@ public void DisposingScopeDisposesTransients()
internal partial class DisposingScopeDisposesTransientsContainer { }

[Fact]
public void DisposingScopeDisposesFactoryGeneratedTransientsIfInstructedTo()
public void DisposingScopeDisposesFactoryGeneratedTransients()
{
DisposingScopeDisposesFactoryGeneratedTransientsIfInstructedToContainer c = new();
DisposingScopeDisposesFactoryGeneratedTransientsContainer c = new();
var scope = c.CreateScope();

List<IService> services = new();
Expand All @@ -1019,45 +865,17 @@ public void DisposingScopeDisposesFactoryGeneratedTransientsIfInstructedTo()
}
}

[ServiceProvider]
[Transient(typeof(IService), Factory = nameof(CreateService), AutoDispose = true)]
internal partial class DisposingScopeDisposesFactoryGeneratedTransientsIfInstructedToContainer
{
private IService CreateService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingScopeDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedTo()
{
DisposingScopeDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedToContainer c = new();
var scope = c.CreateScope();

List<IService> services = new();
for (int i = 0; i < 5; i++)
{
services.Add(scope.GetService<IService>());
}

scope.Dispose();

foreach (var service in services)
{
var disposableService = Assert.IsType<DisposableServiceImplementation>(service);
Assert.Equal(0, disposableService.DisposalCount);
}
}

[ServiceProvider]
[Transient(typeof(IService), Factory = nameof(CreateService))]
internal partial class DisposingScopeDoesNotDisposeFactoryGeneratedTransientsIfNotInstructedToContainer
internal partial class DisposingScopeDisposesFactoryGeneratedTransientsContainer
{
private IService CreateService() => new DisposableServiceImplementation();
}

[Fact]
public void DisposingProviderDoesNotDisposeRootSingletonInstanceServicesEvenWithAutoDisposeFlagSet()
public void DisposingProviderDoesNotDisposeRootSingletonInstanceServices()
{
DisposingProviderDoesNotDisposeRootSingletonInstanceServicesEvenWithAutoDisposeFlagSetContainer c = new();
DisposingProviderDoesNotDisposeRootSingletonInstanceServicesContainer c = new();
var service = Assert.IsType<DisposableServiceImplementation>(c.GetService<IService>());

c.Dispose();
Expand All @@ -1067,8 +885,8 @@ public void DisposingProviderDoesNotDisposeRootSingletonInstanceServicesEvenWith
}

[ServiceProvider]
[Singleton(typeof(IService), Instance = nameof(DisposableServiceImplementation), AutoDispose = true)]
internal partial class DisposingProviderDoesNotDisposeRootSingletonInstanceServicesEvenWithAutoDisposeFlagSetContainer
[Singleton(typeof(IService), Instance = nameof(DisposableServiceImplementation))]
internal partial class DisposingProviderDoesNotDisposeRootSingletonInstanceServicesContainer
{
internal DisposableServiceImplementation DisposableServiceImplementation { get; } = new DisposableServiceImplementation();
}
Expand Down
Loading

0 comments on commit 3eeb5b3

Please sign in to comment.