|
|
|
@ -14,19 +14,36 @@ namespace Test.LightweightIocContainer; |
|
|
|
public class OpenGenericRegistrationTest |
|
|
|
public class OpenGenericRegistrationTest |
|
|
|
{ |
|
|
|
{ |
|
|
|
private IocContainer _iocContainer; |
|
|
|
private IocContainer _iocContainer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
public interface IConstraint |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
public class Constraint : IConstraint |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
[UsedImplicitly] |
|
|
|
[SuppressMessage("ReSharper", "UnusedTypeParameter")] |
|
|
|
[SuppressMessage("ReSharper", "UnusedTypeParameter")] |
|
|
|
public interface ITest<T> |
|
|
|
public interface ITest<T> where T : IConstraint, new() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
[UsedImplicitly] |
|
|
|
public class Test<T> : ITest<T> |
|
|
|
public class Test<T> : ITest<T> where T : IConstraint, new() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface ITestFactory |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
ITest<T> Create<T>() where T : IConstraint, new(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[SetUp] |
|
|
|
[SetUp] |
|
|
|
public void SetUp() => _iocContainer = new IocContainer(); |
|
|
|
public void SetUp() => _iocContainer = new IocContainer(); |
|
|
|
@ -39,7 +56,7 @@ public class OpenGenericRegistrationTest |
|
|
|
{ |
|
|
|
{ |
|
|
|
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>))); |
|
|
|
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>))); |
|
|
|
|
|
|
|
|
|
|
|
ITest<int> test = _iocContainer.Resolve<ITest<int>>(); |
|
|
|
ITest<Constraint> test = _iocContainer.Resolve<ITest<Constraint>>(); |
|
|
|
Assert.NotNull(test); |
|
|
|
Assert.NotNull(test); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -48,10 +65,10 @@ public class OpenGenericRegistrationTest |
|
|
|
{ |
|
|
|
{ |
|
|
|
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Singleton)); |
|
|
|
_iocContainer.Register(r => r.AddOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Singleton)); |
|
|
|
|
|
|
|
|
|
|
|
ITest<int> test = _iocContainer.Resolve<ITest<int>>(); |
|
|
|
ITest<Constraint> test = _iocContainer.Resolve<ITest<Constraint>>(); |
|
|
|
Assert.NotNull(test); |
|
|
|
Assert.NotNull(test); |
|
|
|
|
|
|
|
|
|
|
|
ITest<int> secondTest = _iocContainer.Resolve<ITest<int>>(); |
|
|
|
ITest<Constraint> secondTest = _iocContainer.Resolve<ITest<Constraint>>(); |
|
|
|
Assert.NotNull(secondTest); |
|
|
|
Assert.NotNull(secondTest); |
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(test, secondTest); |
|
|
|
Assert.AreEqual(test, secondTest); |
|
|
|
@ -65,4 +82,13 @@ public class OpenGenericRegistrationTest |
|
|
|
[Test] |
|
|
|
[Test] |
|
|
|
public void TestRegisterNonOpenGenericTypeWithOpenGenericsFunctionThrowsException() => |
|
|
|
public void TestRegisterNonOpenGenericTypeWithOpenGenericsFunctionThrowsException() => |
|
|
|
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register(r => r.AddOpenGenerics(typeof(int), typeof(int)))); |
|
|
|
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<ITest<Constraint>>(test); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |