using NUnit.Framework; using StyletIoC; using System; namespace StyletUnitTests.StyletIoC { [TestFixture] public class StyletIoCBindingChecksTests { interface I1 { } class C1 : I1 { } class C2 { } interface I3 : I1 { } abstract class C4 : I1 { } class C5 : I1 { } interface I6 { } class C6 : I6 { } interface I7 { } class C7 { } class C8 : I6 { } [Test] public void ThrowsIfTypeDoesNotImplementService() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind().To()); } [Test] public void ThrowsIfImplementationIsNotConcrete() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind().To()); Assert.Throws(() => builder.Bind().To()); } [Test] public void ThrowsIfUnboundGenericServiceBoundToNormalImplementation() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind(typeof(I6<>)).To>()); } [Test] public void ThrowsINonGenericServiceBoundToNormalImplementation() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind(typeof(I6<>)).To()); } [Test] public void ThrowsIfNormalServiceBoundToUnboundGenericService() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind>().To(typeof(C6<>))); } [Test] public void ThrowsIfUnboundTypesHaveDifferentNumbersOfTypeParameters() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind(typeof(I6<>)).To(typeof(C7<,>))); Assert.Throws(() => builder.Bind(typeof(I7<,>)).To(typeof(C6<>))); } [Test] public void ThrowsIfFactoryBoundToUnboundGeneric() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind(typeof(I6<>)).ToFactory(x => new C6())); } [Test] public void ThrowsIfRegistrationFactoryIsNUll() { var builder = new StyletIoCBuilder(); Assert.Throws(() => builder.Bind().ToSelf().WithRegistrationFactory(null)); } [Test] public void AllowsFactoryToProvideInterfaceType() { var builder = new StyletIoCBuilder(); Assert.DoesNotThrow(() => builder.Bind().ToFactory(c =>new C1())); } [Test] public void AllowsInstanceToBeInterfaceType() { var builder = new StyletIoCBuilder(); I1 i1 = new C1(); Assert.DoesNotThrow(() => builder.Bind().ToInstance(i1)); } [Test] public void ThrowsIfMissingBuilderBinding() { var builder = new StyletIoCBuilder(); builder.Bind(); Assert.Throws(() => builder.BuildContainer()); } } }