mirror of https://github.com/AMT-Cheif/Stylet.git
Fix StyletIoC bug w/ singleton unbound generics, and improve coverage further
This commit is contained in:
parent
43fe090fa9
commit
6d3a222249
|
@ -161,9 +161,6 @@ namespace StyletIoC
|
|||
// Test this first, as it's a bit clearer than hitting 'type doesn't implement service'
|
||||
if (implementationType.IsGenericTypeDefinition)
|
||||
{
|
||||
if (this.isSingleton)
|
||||
throw new StyletIoCRegistrationException(String.Format("You cannot create singleton registration for unbound generic type {0}", implementationType.Description()));
|
||||
|
||||
if (!serviceType.IsGenericTypeDefinition)
|
||||
throw new StyletIoCRegistrationException(String.Format("You can't use an unbound generic type to implement anything that isn't an unbound generic service. Service: {0}, Type: {1}", serviceType.Description(), implementationType.Description()));
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ namespace StyletIoC
|
|||
{
|
||||
this.Type = type;
|
||||
this.container = container;
|
||||
this.IsSingleton = isSingleton;
|
||||
}
|
||||
|
||||
public IRegistration CreateRegistrationForType(Type boundType)
|
||||
|
|
|
@ -21,6 +21,7 @@ namespace StyletUnitTests
|
|||
class C6<T> : I6<T> { }
|
||||
interface I7<T, U> { }
|
||||
class C7<T, U> { }
|
||||
class C8 : I6<int> { }
|
||||
|
||||
[Test]
|
||||
public void ThrowsIfTypeDoesNotImplementService()
|
||||
|
@ -37,13 +38,6 @@ namespace StyletUnitTests
|
|||
Assert.Throws<StyletIoCRegistrationException>(() => builder.Bind<I1>().To<C4>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowsIfImplementationIsSingletonUnboundGeneric()
|
||||
{
|
||||
var builder = new StyletIoCBuilder();
|
||||
Assert.Throws<StyletIoCRegistrationException>(() => builder.Bind<I1>().To(typeof(C5<>)).InSingletonScope());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowsIfUnboundGenericServiceBoundToNormalImplementation()
|
||||
{
|
||||
|
@ -51,6 +45,13 @@ namespace StyletUnitTests
|
|||
Assert.Throws<StyletIoCRegistrationException>(() => builder.Bind(typeof(I6<>)).To<C6<int>>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowsINonGenericServiceBoundToNormalImplementation()
|
||||
{
|
||||
var builder = new StyletIoCBuilder();
|
||||
Assert.Throws<StyletIoCRegistrationException>(() => builder.Bind(typeof(I6<>)).To<C8>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ThrowsIfNormalServiceBoundToUnboundGenericService()
|
||||
{
|
||||
|
|
|
@ -161,5 +161,16 @@ namespace StyletUnitTests
|
|||
builder.Bind<IFactoryWithVoidMethod>().ToAbstractFactory();
|
||||
Assert.Throws<StyletIoCCreateFactoryException>(() => builder.BuildContainer());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BindsWithKey()
|
||||
{
|
||||
var builder = new StyletIoCBuilder();
|
||||
builder.Bind<I1Factory>().ToAbstractFactory().WithKey("hello");
|
||||
var ioc = builder.BuildContainer();
|
||||
|
||||
Assert.Throws<StyletIoCRegistrationException>(() => ioc.Get<I1Factory>());
|
||||
Assert.NotNull(ioc.Get<I1Factory>("hello"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue