|
|
|
@ -14,7 +14,7 @@ namespace Test.LightweightIocContainer |
|
|
|
[TestFixture] |
|
|
|
[TestFixture] |
|
|
|
public class IocContainerRecursionTest |
|
|
|
public class IocContainerRecursionTest |
|
|
|
{ |
|
|
|
{ |
|
|
|
#region TestClasses |
|
|
|
#region TestClassesCircularDependency |
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
[UsedImplicitly] |
|
|
|
public interface IFoo |
|
|
|
public interface IFoo |
|
|
|
@ -44,7 +44,51 @@ namespace Test.LightweightIocContainer |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion TestClasses |
|
|
|
#endregion TestClassesCircularDependency |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region TestClassesNonCircularDependency |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
public interface IA |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
public interface IB |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
public interface IC |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
private class A : IA |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public A(IB b, IC c) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
private class B : IB |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public B(IC c) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
private class C : IC |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion TestClassesNonCircularDependency |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -54,9 +98,6 @@ namespace Test.LightweightIocContainer |
|
|
|
public void SetUp() |
|
|
|
public void SetUp() |
|
|
|
{ |
|
|
|
{ |
|
|
|
_iocContainer = new IocContainer(); |
|
|
|
_iocContainer = new IocContainer(); |
|
|
|
|
|
|
|
|
|
|
|
_iocContainer.Register<IFoo, Foo>(); |
|
|
|
|
|
|
|
_iocContainer.Register<IBar, Bar>(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[TearDown] |
|
|
|
[TearDown] |
|
|
|
@ -68,6 +109,9 @@ namespace Test.LightweightIocContainer |
|
|
|
[Test] |
|
|
|
[Test] |
|
|
|
public void TestCircularDependencies() |
|
|
|
public void TestCircularDependencies() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
_iocContainer.Register<IFoo, Foo>(); |
|
|
|
|
|
|
|
_iocContainer.Register<IBar, Bar>(); |
|
|
|
|
|
|
|
|
|
|
|
CircularDependencyException exception = Assert.Throws<CircularDependencyException>(() => _iocContainer.Resolve<IFoo>()); |
|
|
|
CircularDependencyException exception = Assert.Throws<CircularDependencyException>(() => _iocContainer.Resolve<IFoo>()); |
|
|
|
Assert.AreEqual(typeof(IFoo), exception.ResolvingType); |
|
|
|
Assert.AreEqual(typeof(IFoo), exception.ResolvingType); |
|
|
|
Assert.AreEqual(2, exception.ResolveStack.Count); |
|
|
|
Assert.AreEqual(2, exception.ResolveStack.Count); |
|
|
|
@ -81,9 +125,22 @@ namespace Test.LightweightIocContainer |
|
|
|
Assert.AreEqual(message, exception.Message); |
|
|
|
Assert.AreEqual(message, exception.Message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
|
|
|
public void TestNonCircularDependencies() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_iocContainer.Register<IA, A>(); |
|
|
|
|
|
|
|
_iocContainer.Register<IB, B>(); |
|
|
|
|
|
|
|
_iocContainer.Register<IC, C>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IA a = _iocContainer.Resolve<IA>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
[Test] |
|
|
|
public void TestRecursionWithParam() |
|
|
|
public void TestRecursionWithParam() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
_iocContainer.Register<IFoo, Foo>(); |
|
|
|
|
|
|
|
_iocContainer.Register<IBar, Bar>(); |
|
|
|
|
|
|
|
|
|
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IFoo>(new Mock<IBar>().Object)); |
|
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IFoo>(new Mock<IBar>().Object)); |
|
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IBar>(new Mock<IFoo>().Object)); |
|
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IBar>(new Mock<IFoo>().Object)); |
|
|
|
} |
|
|
|
} |
|
|
|
|