|
|
|
@ -3,7 +3,9 @@ |
|
|
|
// Copyright(c) 2019 SimonG. All Rights Reserved. |
|
|
|
// Copyright(c) 2019 SimonG. All Rights Reserved. |
|
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System; |
|
|
|
|
|
|
|
using JetBrains.Annotations; |
|
|
|
using LightweightIocContainer; |
|
|
|
using LightweightIocContainer; |
|
|
|
|
|
|
|
using LightweightIocContainer.Exceptions; |
|
|
|
using LightweightIocContainer.Interfaces; |
|
|
|
using LightweightIocContainer.Interfaces; |
|
|
|
using LightweightIocContainer.Interfaces.Registrations; |
|
|
|
using LightweightIocContainer.Interfaces.Registrations; |
|
|
|
using LightweightIocContainer.Registrations; |
|
|
|
using LightweightIocContainer.Registrations; |
|
|
|
@ -22,6 +24,16 @@ namespace Test.LightweightIocContainer |
|
|
|
void DoSomething(); |
|
|
|
void DoSomething(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private interface IFoo |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private interface IBar |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class Test : ITest |
|
|
|
private class Test : ITest |
|
|
|
{ |
|
|
|
{ |
|
|
|
public void DoSomething() |
|
|
|
public void DoSomething() |
|
|
|
@ -30,6 +42,20 @@ namespace Test.LightweightIocContainer |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly] |
|
|
|
|
|
|
|
private class Foo : IFoo |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public Foo(IBar bar, ITest test) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class Bar : IBar |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion |
|
|
|
#endregion |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,5 +69,51 @@ namespace Test.LightweightIocContainer |
|
|
|
|
|
|
|
|
|
|
|
Assert.Throws<Exception>(() => testRegistration.OnCreateAction(test)); |
|
|
|
Assert.Throws<Exception>(() => testRegistration.OnCreateAction(test)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
|
|
|
public void TestWithParameters() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IIocContainer>().Object); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IBar bar = new Bar(); |
|
|
|
|
|
|
|
ITest test = new Test(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IRegistrationBase<IFoo> testRegistration = registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(bar, test); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(bar, testRegistration.Parameters[0]); |
|
|
|
|
|
|
|
Assert.AreEqual(test, testRegistration.Parameters[1]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
|
|
|
public void TestWithParametersDifferentOrder() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IIocContainer>().Object); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IBar bar = new Bar(); |
|
|
|
|
|
|
|
ITest test = new Test(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IRegistrationBase<IFoo> testRegistration = registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((0, bar), (3, test), (2, "SomeString")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(bar, testRegistration.Parameters[0]); |
|
|
|
|
|
|
|
Assert.IsInstanceOf<InternalResolvePlaceholder>(testRegistration.Parameters[1]); |
|
|
|
|
|
|
|
Assert.AreEqual("SomeString", testRegistration.Parameters[2]); |
|
|
|
|
|
|
|
Assert.AreEqual(test, testRegistration.Parameters[3]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
|
|
|
public void TestWithParametersCalledTwice() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IIocContainer>().Object); |
|
|
|
|
|
|
|
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test())); |
|
|
|
|
|
|
|
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((0, new Bar())).WithParameters((1, new Test()))); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
|
|
|
public void TestWithParametersNoParametersGiven() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IIocContainer>().Object); |
|
|
|
|
|
|
|
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((object[])null)); |
|
|
|
|
|
|
|
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |