diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index b27627a..7abeb2a 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -140,6 +140,7 @@ namespace LightweightIocContainer /// The registered /// An instance of the given registered , an if parameters need to be resolved or an if a factory method is used to create an instance /// The given is not registered + /// A direct resolve with a registered factory is not allowed /// An interface was registered without an implementation or factory method /// Tried resolving a multiton without scope argument /// No matching constructor for the given found @@ -201,6 +202,7 @@ namespace LightweightIocContainer /// /// An instance of the given registered , an if parameters need to be resolved or an if a factory method is used to create an instance /// The given is not registered + /// A direct resolve with a registered factory is not allowed /// An interface was registered without an implementation or factory method /// Tried resolving a multiton without scope argument /// No matching constructor for the given found @@ -464,8 +466,7 @@ namespace LightweightIocContainer return (true, parameters, null); noMatchingConstructorFoundException ??= new NoMatchingConstructorFoundException(type); - exceptions?.ForEach(e => - noMatchingConstructorFoundException.AddInnerException(new ConstructorNotMatchingException(constructor, e))); + exceptions?.ForEach(e => noMatchingConstructorFoundException.AddInnerException(e)); } return (false, null, noMatchingConstructorFoundException); diff --git a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs index 851388d..54d2bf8 100644 --- a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs +++ b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs @@ -113,9 +113,18 @@ namespace Test.LightweightIocContainer ITestFactory factory = _iocContainer.Resolve(); ITest test = factory.Create(); + ITest test2 = factory.CreateTest(); Assert.IsInstanceOf(factory); Assert.IsInstanceOf(test); + Assert.IsInstanceOf(test2); + } + + [Test] + public void TestFluentFactoryRegistrationResolveWithoutFactoryFails() + { + _iocContainer.Register(r => r.Add().WithFactory()); + Assert.Throws(()=>_iocContainer.Resolve()); } [Test] @@ -130,6 +139,14 @@ namespace Test.LightweightIocContainer Assert.IsInstanceOf(test); } + [Test] + public void TestFluentFactoryRegistration_WithoutFactoryFails() + { + _iocContainer.Register(r => r.Add().WithFactory()); + Assert.Throws(()=>_iocContainer.Resolve()); + } + + [Test] public void TestRegisterFactoryWithoutCreate() => Assert.Throws(() => _iocContainer.Register(r => r.Add().WithFactory())); diff --git a/Test.LightweightIocContainer/IocContainerRecursionTest.cs b/Test.LightweightIocContainer/IocContainerRecursionTest.cs index b8f8952..2c0d960 100644 --- a/Test.LightweightIocContainer/IocContainerRecursionTest.cs +++ b/Test.LightweightIocContainer/IocContainerRecursionTest.cs @@ -115,12 +115,10 @@ namespace Test.LightweightIocContainer NoMatchingConstructorFoundException noMatchingConstructorFoundException = Assert.Throws(() => _iocContainer.Resolve()); ConstructorNotMatchingException fooConstructorNotMatchingException = (ConstructorNotMatchingException) noMatchingConstructorFoundException?.InnerExceptions[0]; - ConstructorNotMatchingException barConstructorNotMatchingException = (ConstructorNotMatchingException) fooConstructorNotMatchingException?.InnerExceptions[0]; - NoMatchingConstructorFoundException noMatchingBarConstructorFoundException = (NoMatchingConstructorFoundException) barConstructorNotMatchingException?.InnerExceptions[0]; - ConstructorNotMatchingException secondFooConstructorNotMatchingException = (ConstructorNotMatchingException) noMatchingBarConstructorFoundException?.InnerExceptions[0]; - ConstructorNotMatchingException secondBarConstructorNotMatchingException = (ConstructorNotMatchingException) secondFooConstructorNotMatchingException?.InnerExceptions[0]; + NoMatchingConstructorFoundException noMatchingBarConstructorFoundException = (NoMatchingConstructorFoundException) fooConstructorNotMatchingException?.InnerExceptions[0]; + ConstructorNotMatchingException barConstructorNotMatchingException = (ConstructorNotMatchingException) noMatchingBarConstructorFoundException?.InnerExceptions[0]; - CircularDependencyException exception = (CircularDependencyException) secondBarConstructorNotMatchingException?.InnerExceptions[0]; + CircularDependencyException exception = (CircularDependencyException) barConstructorNotMatchingException?.InnerExceptions[0]; Assert.AreEqual(typeof(IFoo), exception?.ResolvingType); Assert.AreEqual(2, exception.ResolveStack.Count); diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index 12f5724..d968a21 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -22,6 +22,11 @@ namespace Test.LightweightIocContainer } + public interface ITest2 + { + + } + [UsedImplicitly] public interface IParameter { @@ -33,12 +38,30 @@ namespace Test.LightweightIocContainer public Test(IParameter parameter) => parameter.Method(); } + + [UsedImplicitly] public interface ITestFactory { ITest Create(IParameter parameter); } + [UsedImplicitly] + public interface ITest2Factory + { + ITest2 InvalidCreate(); + ITest2 Create(ITest test); + } + + + private class Test2 : ITest2 + { + public Test2(ITest parameter) + { + + } + } + [UsedImplicitly] public interface IInvalidFactory { @@ -60,6 +83,15 @@ namespace Test.LightweightIocContainer public void Install(IRegistrationCollector registration) => registration.Add().WithFactory(); } + private class InvalidTestClassInstaller : IIocInstaller + { + public void Install(IRegistrationCollector registration) + { + registration.Add().WithFactory(); + registration.Add().WithFactory(); + } + } + [Test] public void TestValidateWithoutFactory() { @@ -102,6 +134,35 @@ namespace Test.LightweightIocContainer parameterMock.Verify(p => p.Method(), Times.Never); } + + [Test] + public void TestValidateWithInvalidParameterWithFactory() + { + IocContainer iocContainer = new(); + iocContainer.Install(new InvalidTestClassInstaller()); + + IocValidator validator = new(iocContainer); + + Mock parameterMock = new(); + validator.AddParameter(parameterMock.Object); + + AggregateException aggregateException = Assert.Throws(() => validator.Validate()); + + if (aggregateException?.InnerExceptions[0] is not NoMatchingConstructorFoundException noMatchingConstructorFoundException) + { + Assert.Fail(); + return; + } + + if (noMatchingConstructorFoundException.InnerExceptions[0] is not ConstructorNotMatchingException iTest2CtorNotMatchingException) + { + Assert.Fail(); + return; + } + + Assert.IsInstanceOf(iTest2CtorNotMatchingException.InnerExceptions[0]); + } + [Test] public void TestValidateInvalidFactory() {