// Author: Simon.Gockner // Created: 2025-12-03 // Copyright(c) 2025 SimonG. All Rights Reserved. using LightweightIocContainer; using LightweightIocContainer.FactoryGenerator; using Test.LightweightIocContainer.FactoryGenerator.TestClasses; using Test.LightweightIocContainer.FactoryGenerator.TestClasses.Factories; using Test.LightweightIocContainer.FactoryGenerator.TestClasses.Interfaces; namespace Test.LightweightIocContainer.FactoryGenerator; [TestFixture] public class AsyncFactoryTest { [Test] public async Task TestAsyncFactoryResolve() { IocContainer container = new(); container.Register(r => r.Add().WithGeneratedFactory()); IAsyncTestFactory testFactory = container.Resolve(); IAsyncTest test = await testFactory.Create(); Assert.That(test, Is.InstanceOf()); } [Test] public async Task TestAsyncFactoryResolveOnCreateCalled() { IocContainer container = new(); container.Register(r => r.Add().OnCreateAsync(t => t.Initialize()).WithGeneratedFactory()); IAsyncTestFactory testFactory = container.Resolve(); IAsyncTest test = await testFactory.Create(); Assert.That(test, Is.InstanceOf()); Assert.That(test.IsInitialized, Is.True); } [Test] public async Task TestAsyncMultitonFactoryResolveOnCreateCalledCorrectly() { IocContainer container = new(); container.Register(r => r.AddMultiton().OnCreateAsync(t => t.Initialize()).WithGeneratedFactory()); IAsyncMultitonTestFactory testFactory = container.Resolve(); IAsyncTest test1 = await testFactory.Create(1); IAsyncTest test2 = await testFactory.Create(2); IAsyncTest anotherTest1 = await testFactory.Create(1); Assert.That(test1, Is.InstanceOf()); Assert.That(test1.IsInitialized, Is.True); Assert.That(test2, Is.InstanceOf()); Assert.That(test2.IsInitialized, Is.True); Assert.That(test1, Is.SameAs(anotherTest1)); } }