Fix StyletIoC bug w/ singleton unbound generics, and improve coverage further

This commit is contained in:
Antony Male 2014-05-07 16:11:15 +01:00
parent 43fe090fa9
commit 6d3a222249
4 changed files with 20 additions and 10 deletions

View File

@ -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()));

View File

@ -22,6 +22,7 @@ namespace StyletIoC
{
this.Type = type;
this.container = container;
this.IsSingleton = isSingleton;
}
public IRegistration CreateRegistrationForType(Type boundType)

View File

@ -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()
{

View File

@ -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"));
}
}
}