You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
3.9 KiB
157 lines
3.9 KiB
// Author: Gockner, Simon
|
|
// Created: 2019-11-05
|
|
// Copyright(c) 2019 SimonG. All Rights Reserved.
|
|
|
|
using JetBrains.Annotations;
|
|
using LightweightIocContainer;
|
|
using LightweightIocContainer.Exceptions;
|
|
using LightweightIocContainer.Interfaces;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
|
|
namespace Test.LightweightIocContainer
|
|
{
|
|
[TestFixture]
|
|
public class IocContainerRecursionTest
|
|
{
|
|
#region TestClassesCircularDependency
|
|
|
|
[UsedImplicitly]
|
|
public interface IFoo
|
|
{
|
|
|
|
}
|
|
|
|
[UsedImplicitly]
|
|
public interface IBar
|
|
{
|
|
|
|
}
|
|
|
|
[UsedImplicitly]
|
|
private class Foo : IFoo
|
|
{
|
|
public Foo(IBar bar)
|
|
{
|
|
}
|
|
}
|
|
|
|
[UsedImplicitly]
|
|
private class Bar : IBar
|
|
{
|
|
public Bar(IFoo foo)
|
|
{
|
|
}
|
|
}
|
|
|
|
#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
|
|
|
|
|
|
|
|
private IIocContainer _iocContainer;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_iocContainer = new IocContainer();
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_iocContainer.Dispose();
|
|
}
|
|
|
|
[Test]
|
|
public void TestCircularDependencies()
|
|
{
|
|
_iocContainer.Register<IFoo, Foo>();
|
|
_iocContainer.Register<IBar, Bar>();
|
|
|
|
CircularDependencyException exception = Assert.Throws<CircularDependencyException>(() => _iocContainer.Resolve<IFoo>());
|
|
Assert.AreEqual(typeof(IFoo), exception.ResolvingType);
|
|
Assert.AreEqual(2, exception.ResolveStack.Count);
|
|
|
|
string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n" +
|
|
"Resolve stack that resulted in the circular dependency:\n" +
|
|
$"\t`{typeof(IFoo)}` resolved as dependency of\n" +
|
|
$"\t`{typeof(IBar)}` resolved as dependency of\n" +
|
|
$"\t`{typeof(IFoo)}` which is the root type being resolved.";
|
|
|
|
Assert.AreEqual(message, exception.Message);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCircularDependencyExceptionNoStack()
|
|
{
|
|
CircularDependencyException circularDependencyException = new CircularDependencyException(typeof(IFoo), null);
|
|
string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n";
|
|
Assert.AreEqual(message, circularDependencyException.Message);
|
|
}
|
|
|
|
[Test]
|
|
public void TestNonCircularDependencies()
|
|
{
|
|
_iocContainer.Register<IA, A>();
|
|
_iocContainer.Register<IB, B>();
|
|
_iocContainer.Register<IC, C>();
|
|
|
|
IA a = _iocContainer.Resolve<IA>();
|
|
Assert.IsNotNull(a);
|
|
}
|
|
|
|
[Test]
|
|
public void TestRecursionWithParam()
|
|
{
|
|
_iocContainer.Register<IFoo, Foo>();
|
|
_iocContainer.Register<IBar, Bar>();
|
|
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IFoo>(new Mock<IBar>().Object));
|
|
Assert.DoesNotThrow(() => _iocContainer.Resolve<IBar>(new Mock<IFoo>().Object));
|
|
}
|
|
}
|
|
} |