using NUnit.Framework; using StyletIoC; namespace StyletUnitTests.StyletIoC { [TestFixture] public class StyletIoCUnboundGenericTests { interface I1 { } class C1 : I1 { } class C12 { } interface I2 { } class C2 : I2 { } [Test] public void ResolvesSingleGenericType() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(C1<>)).ToSelf(); var ioc = builder.BuildContainer(); Assert.DoesNotThrow(() => ioc.Get>()); } [Test] public void ResolvesGenericTypeFromInterface() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(I1<>)).To(typeof(C1<>)); var ioc = builder.BuildContainer(); var result = ioc.Get>(); Assert.IsInstanceOf>(result); } [Test] public void ResolvesGenericTypeWhenOrderOfTypeParamsChanged() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(I2<,>)).To(typeof(C2<,>)); var ioc = builder.BuildContainer(); var c2 = ioc.Get>(); Assert.IsInstanceOf>(c2); } [Test] public void ResolvesSingletonUnboundGeneric() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(I1<>)).To(typeof(C1<>)).InSingletonScope(); var ioc = builder.BuildContainer(); Assert.AreEqual(ioc.Get>(), ioc.Get>()); } [Test] public void ResolvesUnboundGenericFromKey() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(I1<>)).To(typeof(C1<>)).WithKey("test"); var ioc = builder.BuildContainer(); Assert.NotNull(ioc.Get>("test")); } [Test] public void ThrowsIfMultipleRegistrationsForUnboundGeneric() { var builder = new StyletIoCBuilder(); builder.Bind(typeof(C1<>)).ToSelf(); builder.Bind(typeof(C1<>)).ToSelf(); Assert.Throws(() => builder.BuildContainer()); } [Test] public void ThrowsIfUnboundGenericDoesNotImplementService() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind(typeof(I1<>)).To(typeof(C12<>))); } } }