A lightweight IOC Container that is powerful enough to do all the things you need it to do.
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.

119 lines
3.5 KiB

// Author: Simon Gockner
// Created: 2020-09-18
// Copyright(c) 2020 SimonG. All Rights Reserved.
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using LightweightIocContainer;
using LightweightIocContainer.Exceptions;
using NUnit.Framework;
namespace Test.LightweightIocContainer;
[TestFixture]
public class OpenGenericRegistrationTest
{
private IocContainer _iocContainer;
[UsedImplicitly]
public interface IConstraint
{
}
[UsedImplicitly]
public class Constraint : IConstraint
{
}
[UsedImplicitly]
[SuppressMessage("ReSharper", "UnusedTypeParameter")]
public interface ITest<T> where T : IConstraint, new()
{
}
[UsedImplicitly]
public class Test<T> : ITest<T> where T : IConstraint, new()
{
}
[UsedImplicitly]
public class CtorTest<T> : ITest<T> where T : IConstraint, new()
{
public CtorTest(T item)
{
}
}
[UsedImplicitly]
public interface ITestFactory
{
ITest<T> Create<T>() where T : IConstraint, new();
}
[UsedImplicitly]
public interface ICtorTestFactory
{
ITest<T> Create<T>(T item) where T : IConstraint, new();
}
[SetUp]
public void SetUp() => _iocContainer = new IocContainer();
[TearDown]
public void TearDown() => _iocContainer.Dispose();
[Test]
public void TestRegisterOpenGenericType()
{
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>)));
ITest<Constraint> test = _iocContainer.Resolve<ITest<Constraint>>();
Assert.NotNull(test);
}
[Test]
public void TestRegisterOpenGenericTypeAsSingleton()
{
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Singleton));
ITest<Constraint> test = _iocContainer.Resolve<ITest<Constraint>>();
Assert.NotNull(test);
ITest<Constraint> secondTest = _iocContainer.Resolve<ITest<Constraint>>();
Assert.NotNull(secondTest);
Assert.AreEqual(test, secondTest);
Assert.AreSame(test, secondTest);
}
[Test]
public void TestRegisterOpenGenericTypeAsMultitonThrowsException() =>
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Multiton)));
[Test]
public void TestRegisterNonOpenGenericTypeWithOpenGenericsFunctionThrowsException() =>
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register(r => r.AddOpenGenerics(typeof(int), typeof(int))));
[Test]
public void TestRegisterFactoryOfOpenGenericType()
{
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>)).WithFactory<ITestFactory>());
ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>();
ITest<Constraint> test = testFactory.Create<Constraint>();
Assert.IsInstanceOf<Test<Constraint>>(test);
}
[Test]
public void TestRegisterFactoryOfOpenGenericTypeWithCtorParameter()
{
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(CtorTest<>)).WithFactory<ICtorTestFactory>());
ICtorTestFactory testFactory = _iocContainer.Resolve<ICtorTestFactory>();
ITest<Constraint> test = testFactory.Create(new Constraint());
Assert.IsInstanceOf<CtorTest<Constraint>>(test);
}
}