From b9f29cdb01c5c9ea2412ad167e765fc2a35d00af Mon Sep 17 00:00:00 2001 From: Simon G Date: Wed, 8 Dec 2021 15:09:18 +0100 Subject: [PATCH] #51: fix unit test, add circular cross dependency test --- .../IocContainerRecursionTest.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Test.LightweightIocContainer/IocContainerRecursionTest.cs b/Test.LightweightIocContainer/IocContainerRecursionTest.cs index 0f10744..eddf3d5 100644 --- a/Test.LightweightIocContainer/IocContainerRecursionTest.cs +++ b/Test.LightweightIocContainer/IocContainerRecursionTest.cs @@ -2,6 +2,7 @@ // Created: 2019-11-05 // Copyright(c) 2019 SimonG. All Rights Reserved. +using System; using JetBrains.Annotations; using LightweightIocContainer; using LightweightIocContainer.Exceptions; @@ -82,6 +83,20 @@ namespace Test.LightweightIocContainer { } + + [UsedImplicitly] + private class ATwoCtor : IA + { + public ATwoCtor(IB b) => Console.WriteLine("A with args"); + public ATwoCtor() => Console.WriteLine("A without args"); + } + + [UsedImplicitly] + private class BTwoCtor : IB + { + public BTwoCtor(IA a) => Console.WriteLine("B with args"); + public BTwoCtor() => Console.WriteLine("B without args"); + } private IocContainer _iocContainer; @@ -98,7 +113,12 @@ namespace Test.LightweightIocContainer _iocContainer.Register(); _iocContainer.Register(); - CircularDependencyException exception = Assert.Throws(() => _iocContainer.Resolve()); + NoMatchingConstructorFoundException noMatchingConstructorFoundException = Assert.Throws(() => _iocContainer.Resolve()); + ConstructorNotMatchingException fooConstructorNotMatchingException = (ConstructorNotMatchingException) noMatchingConstructorFoundException?.InnerExceptions[0]; + ConstructorNotMatchingException barConstructorNotMatchingException = (ConstructorNotMatchingException) fooConstructorNotMatchingException?.InnerExceptions[0]; + + CircularDependencyException exception = (CircularDependencyException) barConstructorNotMatchingException?.InnerExceptions[0]; + Assert.AreEqual(typeof(IFoo), exception?.ResolvingType); Assert.AreEqual(2, exception.ResolveStack.Count); @@ -139,5 +159,15 @@ namespace Test.LightweightIocContainer Assert.DoesNotThrow(() => _iocContainer.Resolve(new Mock().Object)); Assert.DoesNotThrow(() => _iocContainer.Resolve(new Mock().Object)); } + + [Test] + public void TestNonCircularCrossDependencies() + { + _iocContainer.Register(); + _iocContainer.Register(); + + Assert.DoesNotThrow(() => _iocContainer.Resolve()); + Assert.DoesNotThrow(() => _iocContainer.Resolve()); + } } } \ No newline at end of file