// Author: Gockner, Simon // Created: 2021-11-29 // Copyright(c) 2021 SimonG. All Rights Reserved. using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using NUnit.Framework; namespace Test.LightweightIocContainer { [TestFixture] public class FluentFactoryRegistrationTest { public interface ITest { } private class Test : ITest { } private class TestByte : ITest { [UsedImplicitly] private readonly byte _id; public TestByte(byte id) => _id = id; } [UsedImplicitly] private class TestConstructor : ITest { public TestConstructor(string name, Test test) { } public TestConstructor(Test test, string name = null) { } } private interface ITestFactoryNoCreate { } private interface ITestFactoryNonGenericClear { ITest Create(); void ClearMultitonInstance(); } [UsedImplicitly] public interface ITestFactory { ITest Create(); ITest Create(string name); ITest Create(MultitonScope scope); ITest CreateTest(string name = null); ITest Create(byte id); void ClearMultitonInstance(); } private class TestFactory : ITestFactory { public ITest Create() => new Test(); public ITest Create(string name) => throw new System.NotImplementedException(); public ITest Create(MultitonScope scope) => throw new System.NotImplementedException(); public ITest CreateTest(string name = null) => throw new System.NotImplementedException(); public ITest Create(byte id) => throw new System.NotImplementedException(); public void ClearMultitonInstance() => throw new System.NotImplementedException(); } public class MultitonScope { } private IIocContainer _iocContainer; [SetUp] public void SetUp() => _iocContainer = new IocContainer(); [TearDown] public void TearDown() => _iocContainer.Dispose(); [Test] public void TestFluentFactoryRegistration() { _iocContainer.Register().WithFactory(); ITestFactory factory = _iocContainer.Resolve(); ITest test = _iocContainer.Resolve(); Assert.IsInstanceOf(factory); Assert.IsInstanceOf(test); } [Test] public void TestFluentFactoryRegistration_CustomFactory() { _iocContainer.Register().WithFactory(); ITestFactory factory = _iocContainer.Resolve(); ITest test = _iocContainer.Resolve(); Assert.IsInstanceOf(factory); Assert.IsInstanceOf(test); } [Test] public void TestRegisterFactoryWithoutCreate() => Assert.Throws(() => _iocContainer.Register().WithFactory()); [Test] public void TestRegisterFactoryClearMultitonsNonGeneric() => Assert.Throws(() => _iocContainer.Register().WithFactory()); [Test] public void TestResolveFromFactory() { _iocContainer.Register().WithFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithParams() { _iocContainer.Register().WithFactory(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create("Test"); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithDefaultParamCreate() { _iocContainer.Register().WithFactory(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.CreateTest(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithDefaultParamCtor() { _iocContainer.Register().WithFactory(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithByte() { _iocContainer.Register().WithFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(1); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveMultitonFromFactory() { _iocContainer.RegisterMultiton().WithFactory(); MultitonScope scope1 = new(); MultitonScope scope2 = new(); ITestFactory testFactory = _iocContainer.Resolve(); ITest resolvedTest1 = testFactory.Create(scope1); ITest resolvedTest2 = testFactory.Create(scope1); ITest resolvedTest3 = testFactory.Create(scope2); Assert.AreSame(resolvedTest1, resolvedTest2); Assert.AreNotSame(resolvedTest1, resolvedTest3); Assert.AreNotSame(resolvedTest2, resolvedTest3); } [Test] public void TestResolveMultitonFromFactoryClearInstances() { _iocContainer.RegisterMultiton().WithFactory(); MultitonScope scope1 = new(); MultitonScope scope2 = new(); ITestFactory testFactory = _iocContainer.Resolve(); ITest resolvedTest1 = testFactory.Create(scope1); ITest resolvedTest2 = testFactory.Create(scope1); ITest resolvedTest3 = testFactory.Create(scope2); Assert.AreSame(resolvedTest1, resolvedTest2); Assert.AreNotSame(resolvedTest1, resolvedTest3); Assert.AreNotSame(resolvedTest2, resolvedTest3); testFactory.ClearMultitonInstance(); ITest resolvedTest4 = testFactory.Create(scope1); ITest resolvedTest5 = testFactory.Create(scope2); Assert.AreNotSame(resolvedTest1, resolvedTest4); Assert.AreNotSame(resolvedTest2, resolvedTest4); Assert.AreNotSame(resolvedTest3, resolvedTest5); } } }