using System; using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Installers; using Moq; using NUnit.Framework; namespace Test.LightweightIocContainer { [TestFixture] public class IocContainerTest { #region TestClasses //some of the test classes have to be public to allow the implementation of the factory public interface ITest { } [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 interface ITestFactoryNoCreate { } private interface ITestFactoryNonGenericClear { ITest Create(); void ClearMultitonInstance(); } private interface IFoo { } private class Test : ITest { } [UsedImplicitly] private class TestConstructor : ITest { public TestConstructor(string name, Test test) { } public TestConstructor(Test test, string name = null) { } public TestConstructor(IFoo foo, string name) { } } [UsedImplicitly] private class TestPrivateConstructor : ITest { private TestPrivateConstructor() { } } [UsedImplicitly] private class TestByte : ITest { [UsedImplicitly] private readonly byte _id; public TestByte(byte id) { _id = id; } } [UsedImplicitly] private class Foo : IFoo { } public class MultitonScope { } #endregion TestClasses private IIocContainer _iocContainer; [SetUp] public void SetUp() { _iocContainer = new IocContainer(); } [TearDown] public void TearDown() { _iocContainer.Dispose(); } [Test] public void TestInstall() { Mock installerMock = new Mock(); IIocContainer returnedContainer = _iocContainer.Install(installerMock.Object); installerMock.Verify(m => m.Install(It.IsAny()), Times.Once); Assert.AreEqual(_iocContainer, returnedContainer); } [Test] public void TestInstallMultiple() { Mock installer1Mock = new Mock(); Mock installer2Mock = new Mock(); Mock installer3Mock = new Mock(); IIocContainer returnedContainer = _iocContainer.Install(installer1Mock.Object, installer2Mock.Object, installer3Mock.Object); installer1Mock.Verify(m => m.Install(It.IsAny()), Times.Once); installer2Mock.Verify(m => m.Install(It.IsAny()), Times.Once); installer3Mock.Verify(m => m.Install(It.IsAny()), Times.Once); Assert.AreEqual(_iocContainer, returnedContainer); } [Test] public void TestRegister() { Assert.DoesNotThrow(() => _iocContainer.Register()); } [Test] public void TestRegisterTypeWithoutInterface() { Assert.DoesNotThrow(() => _iocContainer.Register()); } [Test] public void TestRegisterMultiton() { Assert.DoesNotThrow(() => _iocContainer.Register()); } [Test] public void TestRegisterFactory() { Assert.DoesNotThrow(() => _iocContainer.RegisterFactory()); } [Test] public void TestRegisterMultiple() { _iocContainer.Register(); MultipleRegistrationException exception = Assert.Throws(() => _iocContainer.Register()); Assert.AreEqual(typeof(ITest), exception.Type); } [Test] public void TestRegisterFactoryWithoutCreate() { Assert.Throws(() => _iocContainer.RegisterFactory()); } [Test] public void TestRegisterFactoryClearMultitonsNonGeneric() { Assert.Throws(() => _iocContainer.RegisterFactory()); } [Test] [Obsolete("RegisterUnitTestCallback is deprecated, use `WithFactoryMethod()` from ISingleTypeRegistration instead.")] public void TestRegisterUnitTestCallback() { Assert.DoesNotThrow(() => _iocContainer.RegisterUnitTestCallback(delegate {return new Test(); })); } [Test] public void TestResolveNotRegistered() { TypeNotRegisteredException exception = Assert.Throws(() => _iocContainer.Resolve()); Assert.AreEqual(typeof(ITest), exception.Type); } [Test] public void TestResolve() { _iocContainer.Register(); ITest resolvedTest = _iocContainer.Resolve(); Assert.IsInstanceOf(resolvedTest); } [Test] public void TestResolveWithoutInterface() { _iocContainer.Register(); Test resolvedTest = _iocContainer.Resolve(); Assert.IsInstanceOf(resolvedTest); } [Test] public void TestResolveInterfaceWithoutImplementation() { _iocContainer.Register(); Assert.Throws(() => _iocContainer.Resolve()); } [Test] public void TestResolveWithParams() { _iocContainer.Register(); ITest resolvedTest = _iocContainer.Resolve("Test", new Test()); Assert.IsInstanceOf(resolvedTest); } [Test] public void TestResolveWithMissingParam() { _iocContainer.Register(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests ITest resolvedTest = _iocContainer.Resolve("Test"); Assert.IsInstanceOf(resolvedTest); } [Test] public void TestResolveSingleton() { _iocContainer.Register(Lifestyle.Singleton); ITest resolvedTest = _iocContainer.Resolve(); ITest secondResolvedTest = _iocContainer.Resolve(); Assert.AreEqual(resolvedTest, secondResolvedTest); } [Test] public void TestResolveMultiton() { _iocContainer.Register(); MultitonScope scope1 = new MultitonScope(); MultitonScope scope2 = new MultitonScope(); ITest resolvedTest1 = _iocContainer.Resolve(scope1); ITest resolvedTest2 = _iocContainer.Resolve(scope1); ITest resolvedTest3 = _iocContainer.Resolve(scope2); Assert.AreSame(resolvedTest1, resolvedTest2); Assert.AreNotSame(resolvedTest1, resolvedTest3); Assert.AreNotSame(resolvedTest2, resolvedTest3); } [Test] public void TestResolveMultitonNoArgs() { _iocContainer.Register(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve()); Assert.AreEqual(typeof(ITest), exception.Type); } [Test] public void TestResolveMultitonWrongArgs() { _iocContainer.Register(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve(new object())); Assert.AreEqual(typeof(ITest), exception.Type); } [Test] public void TestResolveTransient() { _iocContainer.Register(); ITest resolvedTest = _iocContainer.Resolve(); ITest secondResolvedTest = _iocContainer.Resolve(); Assert.AreNotEqual(resolvedTest, secondResolvedTest); } [Test] public void TestResolveNoMatchingConstructor() { _iocContainer.Register(); NoMatchingConstructorFoundException exception = Assert.Throws(() => _iocContainer.Resolve()); Assert.AreEqual(typeof(TestConstructor), exception.Type); } [Test] public void TestResolvePrivateConstructor() { _iocContainer.Register(); NoPublicConstructorFoundException exception = Assert.Throws(() => _iocContainer.Resolve()); Assert.AreEqual(typeof(TestPrivateConstructor), exception.Type); } [Test] public void TestResolveFactory() { _iocContainer.Register(); _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); Assert.IsInstanceOf(testFactory); } [Test] public void TestResolveFromFactory() { _iocContainer.Register(); _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithParams() { _iocContainer.Register(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create("Test"); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithDefaultParamCreate() { _iocContainer.Register(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.CreateTest(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithDefaultParamCtor() { _iocContainer.Register(); _iocContainer.Register(); //this registration is abnormal and should only be used in unit tests _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveFromFactoryWithByte() { _iocContainer.Register(); _iocContainer.RegisterFactory(); ITestFactory testFactory = _iocContainer.Resolve(); ITest createdTest = testFactory.Create(1); Assert.IsInstanceOf(createdTest); } [Test] public void TestResolveMultitonFromFactory() { _iocContainer.Register(); _iocContainer.RegisterFactory(); MultitonScope scope1 = new MultitonScope(); MultitonScope scope2 = new MultitonScope(); 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.Register(); _iocContainer.RegisterFactory(); MultitonScope scope1 = new MultitonScope(); MultitonScope scope2 = new MultitonScope(); 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); } [Test] [Obsolete("RegisterUnitTestCallback is deprecated, use `WithFactoryMethod()` from ISingleTypeRegistration instead.")] public void TestResolveUnitTestCallbackRegistration() { ITest callbackTest = new Test(); _iocContainer.RegisterUnitTestCallback(delegate { return callbackTest; }); ITest test = _iocContainer.Resolve(); Assert.AreEqual(callbackTest, test); } [Test] public void TestResolveSingleTypeRegistrationWithFactoryMethod() { _iocContainer.Register(); _iocContainer.Register().WithFactoryMethod(c => new TestConstructor(c.Resolve(), "someName")); ITest test = _iocContainer.Resolve(); Assert.NotNull(test); } [Test] public void TestIsTypeRegistered() { Assert.False(_iocContainer.IsTypeRegistered()); _iocContainer.Register(); Assert.True(_iocContainer.IsTypeRegistered()); _iocContainer.Register(); Assert.True(_iocContainer.IsTypeRegistered()); } } }