From 3c636a786eeac278f413e381a53c4315bb491b4f Mon Sep 17 00:00:00 2001 From: Simon Gockner Date: Fri, 22 Nov 2019 14:27:29 +0100 Subject: [PATCH] #29: add unit test for withParameters method --- .../IocContainerParameterRegistrationTest.cs | 169 ++++++++++++++++++ .../RegistrationBaseTest.cs | 72 ++++++++ 2 files changed, 241 insertions(+) create mode 100644 Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs diff --git a/Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs b/Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs new file mode 100644 index 0000000..be23d2e --- /dev/null +++ b/Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs @@ -0,0 +1,169 @@ +// Author: Gockner, Simon +// Created: 2019-11-22 +// Copyright(c) 2019 SimonG. All Rights Reserved. + +using JetBrains.Annotations; +using LightweightIocContainer; +using LightweightIocContainer.Interfaces; +using NUnit.Framework; + +namespace Test.LightweightIocContainer +{ + [TestFixture] + // ReSharper disable MemberHidesStaticFromOuterClass + public class IocContainerParameterRegistrationTest + { + #region TestClasses + + [UsedImplicitly] + public interface IA + { + IB B { get; } + IC C { get; } + } + + [UsedImplicitly] + public interface IB + { + + } + + [UsedImplicitly] + public interface IC + { + + } + + [UsedImplicitly] + public interface ID + { + IA A { get; } + IA A2 { get; } + IB B { get; } + IC C { get; } + } + + [UsedImplicitly] + private class A : IA + { + public A(IB b, IC c) + { + B = b; + C = c; + } + + public IB B { get; } + public IC C { get; } + } + + [UsedImplicitly] + private class B : IB + { + public B(IC c) + { + } + } + + [UsedImplicitly] + private class C : IC + { + + } + + [UsedImplicitly] + private class D : ID + { + public D(IA a, IA a2, IB b, IC c) + { + A = a; + A2 = a2; + B = b; + C = c; + } + + public IA A { get; } + public IA A2 { get; } + public IB B { get; } + public IC C { get; } + } + + #endregion TestClasses + + private IIocContainer _iocContainer; + + [SetUp] + public void SetUp() + { + _iocContainer = new IocContainer(); + } + + + [TearDown] + public void TearDown() + { + _iocContainer.Dispose(); + } + + + [Test] + public void TestResolveOnlyRegistrationParameters() + { + IC c = new C(); + IB b = new B(c); + + _iocContainer.Register().WithParameters(b, c); + IA a = _iocContainer.Resolve(); + + Assert.AreEqual(b, a.B); + Assert.AreEqual(c, a.C); + } + + [Test] + public void TestResolveRegistrationAndResolveParameters() + { + IC c = new C(); + IB b = new B(c); + + _iocContainer.Register().WithParameters(b); + IA a = _iocContainer.Resolve(c); + + Assert.AreEqual(b, a.B); + Assert.AreEqual(c, a.C); + } + + [Test] + public void TestResolveRegistrationAndResolveParametersMixedOrder() + { + IC c = new C(); + IB b = new B(c); + IA a = new A(b, c); + IA a2 = new A(b, c); + + _iocContainer.Register().WithParameters((0, a), (2, b), (3, c)); + ID d = _iocContainer.Resolve(a2); + + Assert.AreEqual(a, d.A); + Assert.AreEqual(a2, d.A2); + Assert.AreEqual(b, d.B); + Assert.AreEqual(c, d.C); + } + + [Test] + public void TestResolveRegistrationParametersAndResolvedParameters() + { + IC c = new C(); + IB b = new B(c); + IA a = new A(b, c); + IA a2 = new A(b, c); + + _iocContainer.Register().WithParameters(a2); + _iocContainer.Register(); + _iocContainer.Register(); + + ID d = _iocContainer.Resolve(a); + + Assert.AreEqual(a, d.A2); + Assert.AreEqual(a2, d.A); + } + } +} \ No newline at end of file diff --git a/Test.LightweightIocContainer/RegistrationBaseTest.cs b/Test.LightweightIocContainer/RegistrationBaseTest.cs index 8e0cd28..b29afa7 100644 --- a/Test.LightweightIocContainer/RegistrationBaseTest.cs +++ b/Test.LightweightIocContainer/RegistrationBaseTest.cs @@ -3,7 +3,9 @@ // Copyright(c) 2019 SimonG. All Rights Reserved. using System; +using JetBrains.Annotations; using LightweightIocContainer; +using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Registrations; @@ -22,6 +24,16 @@ namespace Test.LightweightIocContainer void DoSomething(); } + private interface IFoo + { + + } + + private interface IBar + { + + } + private class Test : ITest { public void DoSomething() @@ -30,6 +42,20 @@ namespace Test.LightweightIocContainer } } + [UsedImplicitly] + private class Foo : IFoo + { + public Foo(IBar bar, ITest test) + { + + } + } + + private class Bar : IBar + { + + } + #endregion @@ -43,5 +69,51 @@ namespace Test.LightweightIocContainer Assert.Throws(() => testRegistration.OnCreateAction(test)); } + + [Test] + public void TestWithParameters() + { + RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); + + IBar bar = new Bar(); + ITest test = new Test(); + + IRegistrationBase testRegistration = registrationFactory.Register(Lifestyle.Transient).WithParameters(bar, test); + + Assert.AreEqual(bar, testRegistration.Parameters[0]); + Assert.AreEqual(test, testRegistration.Parameters[1]); + } + + [Test] + public void TestWithParametersDifferentOrder() + { + RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); + + IBar bar = new Bar(); + ITest test = new Test(); + + IRegistrationBase testRegistration = registrationFactory.Register(Lifestyle.Transient).WithParameters((0, bar), (3, test), (2, "SomeString")); + + Assert.AreEqual(bar, testRegistration.Parameters[0]); + Assert.IsInstanceOf(testRegistration.Parameters[1]); + Assert.AreEqual("SomeString", testRegistration.Parameters[2]); + Assert.AreEqual(test, testRegistration.Parameters[3]); + } + + [Test] + public void TestWithParametersCalledTwice() + { + RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); + Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test())); + Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters((0, new Bar())).WithParameters((1, new Test()))); + } + + [Test] + public void TestWithParametersNoParametersGiven() + { + RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); + Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters((object[])null)); + Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null)); + } } } \ No newline at end of file