public void TestImplicitToSingleton() { //This succeeds if no error IInjectionBinding binding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().ToSingleton(); factory.Get(binding); //Succeeds if throws error IInjectionBinding binding2 = new InjectionBinding(resolver).Bind <ISimpleInterface> ().ToSingleton(); TestDelegate testDelegate = delegate() { factory.Get(binding2); }; InjectionException ex = Assert.Throws <InjectionException>(testDelegate); Assert.That(ex.type == InjectionExceptionType.NOT_INSTANTIABLE); //Succeeds if throws error IInjectionBinding binding3 = new InjectionBinding(resolver).Bind <AbstractClass> ().ToSingleton(); TestDelegate testDelegate2 = delegate() { factory.Get(binding3); }; InjectionException ex2 = Assert.Throws <InjectionException>(testDelegate2); Assert.That(ex2.type == InjectionExceptionType.NOT_INSTANTIABLE); }
public void TestSingletonToValueBinding() { InjectableSuperClass instance = new InjectableSuperClass(); InjectionBinding binding = binder.Bind <InjectableSuperClass> ().ToSingleton().ToValue(instance) as InjectionBinding; Assert.AreEqual(InjectionBindingType.VALUE, binding.type); }
public void TestGetSupply() { Type[] supplied = new Type[3]; supplied [0] = typeof(HasANamedInjection); supplied [1] = typeof(HasANamedInjection2); supplied [2] = typeof(InjectsClassToBeInjected); int iterator = 0; Binder.BindingResolver resolver = delegate(IBinding bound) { object[] value = (bound as IInjectionBinding).GetSupply(); Assert.AreEqual(value[value.Length - 1], supplied[iterator]); }; InjectionBinding binding = new InjectionBinding(resolver); while (iterator < 3) { binding.SupplyTo(supplied[iterator]); iterator++; } object[] supply = binding.GetSupply(); Assert.AreEqual(3, supply.Length); for (var a = 0; a < supply.Length; a++) { Assert.AreEqual(supply[a], supplied[a]); } }
public void TestGetFromPool() { IPool <ClassToBeInjected> pool = new Pool <ClassToBeInjected> (); // Format the pool pool.size = 4; pool.instanceProvider = new TestInstanceProvider(); IInjectionBinding binding = new InjectionBinding(resolver); binding.Bind <IPool <ClassToBeInjected> > ().To <Pool <ClassToBeInjected> > ().ToValue(pool); IPool <ClassToBeInjected> myPool = factory.Get(binding) as Pool <ClassToBeInjected>; Assert.NotNull(myPool); ClassToBeInjected instance1 = myPool.GetInstance() as ClassToBeInjected; Assert.NotNull(instance1); ClassToBeInjected instance2 = myPool.GetInstance() as ClassToBeInjected; Assert.NotNull(instance2); Assert.AreNotSame(instance1, instance2); }
public void TestInstantiation() { IInjectionBinding defaultBinding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> (); InjectableDerivedClass testResult = factory.Get(defaultBinding) as InjectableDerivedClass; Assert.IsNotNull(testResult); }
public void TestSupplyOne() { Binder.BindingResolver resolver = delegate(IBinding bound) { object[] value = (bound as IInjectionBinding).GetSupply(); Assert.AreEqual(value[0], typeof(HasANamedInjection)); }; InjectionBinding binding = new InjectionBinding(resolver); binding.SupplyTo <HasANamedInjection> (); }
public void TestInstantiateSingleton() { IInjectionBinding defaultBinding = new InjectionBinding (resolver).Key<InjectableSuperClass> ().To <InjectableDerivedClass> ().ToSingleton(); InjectableDerivedClass testResult = factory.Get (defaultBinding) as InjectableDerivedClass; Assert.IsNotNull (testResult); //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's the same instance InjectableDerivedClass testResult2 = factory.Get (defaultBinding) as InjectableDerivedClass; Assert.That (testResult2.intValue == 42); }
public void TestValueMap() { InjectableDerivedClass testvalue = new InjectableDerivedClass(); testvalue.intValue = 42; IInjectionBinding binding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ().ToValue(testvalue); InjectableDerivedClass testResult = factory.Get(binding) as InjectableDerivedClass; Assert.IsNotNull(testResult); Assert.That(testResult.intValue == testvalue.intValue); Assert.That(testResult.intValue == 42); }
public void TestSingletonType() { const string TEST_KEY = "TEST_KEY"; Binder.BindingResolver resolver = delegate (IBinding binding) { (binding as IInjectionBinding).type = InjectionBindingType.SINGLETON; Assert.That (TEST_KEY == binding.value as string); Assert.That ((binding as InjectionBinding).type == InjectionBindingType.SINGLETON); }; InjectionBinding defaultBinding = new InjectionBinding (resolver); defaultBinding.To (TEST_KEY); }
public void TestInstantiationFactory () { IInjectionBinding defaultBinding = new InjectionBinding (resolver).Bind<InjectableSuperClass> ().To <InjectableDerivedClass> (); InjectableDerivedClass testResult = factory.Get (defaultBinding) as InjectableDerivedClass; Assert.IsNotNull (testResult); int defaultValue = testResult.intValue; //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's a different instance InjectableDerivedClass testResult2 = factory.Get (defaultBinding) as InjectableDerivedClass; Assert.That (testResult2.intValue == defaultValue); }
public void TestInstantiateSingleton() { IInjectionBinding defaultBinding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ().ToSingleton(); InjectableDerivedClass testResult = factory.Get(defaultBinding) as InjectableDerivedClass; Assert.IsNotNull(testResult); //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's the same instance InjectableDerivedClass testResult2 = factory.Get(defaultBinding) as InjectableDerivedClass; Assert.That(testResult2.intValue == 42); }
public void TestUnsupply() { Binder.BindingResolver resolver = delegate(IBinding bound) { }; InjectionBinding binding = new InjectionBinding(resolver); binding.To <ClassToBeInjected>().SupplyTo <HasANamedInjection> (); Assert.AreEqual(typeof(HasANamedInjection), binding.GetSupply()[0]); Assert.AreEqual(typeof(ClassToBeInjected), binding.value); binding.Unsupply <HasANamedInjection> (); Assert.IsNull(binding.GetSupply()); }
public void TestValueType() { const string TEST_KEY = "TEST_KEY"; Binder.BindingResolver resolver = delegate(IBinding binding) { (binding as IInjectionBinding).type = InjectionBindingType.VALUE; Assert.That(TEST_KEY == binding.value as string); Assert.That((binding as InjectionBinding).type == InjectionBindingType.VALUE); }; InjectionBinding defaultBinding = new InjectionBinding(resolver); defaultBinding.To(TEST_KEY); }
public void TestInstantiationFactory() { IInjectionBinding defaultBinding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> (); InjectableDerivedClass testResult = factory.Get(defaultBinding) as InjectableDerivedClass; Assert.IsNotNull(testResult); int defaultValue = testResult.intValue; //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's a different instance InjectableDerivedClass testResult2 = factory.Get(defaultBinding) as InjectableDerivedClass; Assert.That(testResult2.intValue == defaultValue); }
public void TestNamedSingletons () { //Create two named singletons IInjectionBinding defaultBinding = new InjectionBinding (resolver).Bind<InjectableSuperClass> ().To <InjectableDerivedClass> ().ToName (SomeEnum.ONE).ToSingleton(); IInjectionBinding defaultBinding2 = new InjectionBinding (resolver).Bind<InjectableSuperClass> ().To <InjectableDerivedClass> ().ToName (SomeEnum.TWO).ToSingleton(); InjectableDerivedClass testResult = factory.Get (defaultBinding) as InjectableDerivedClass; int defaultValue = testResult.intValue; Assert.IsNotNull (testResult); //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's a different instance InjectableDerivedClass testResult2 = factory.Get (defaultBinding2) as InjectableDerivedClass; Assert.IsNotNull (testResult2); Assert.That (testResult2.intValue == defaultValue); }
public void TestNamedSingletons() { //Create two named singletons IInjectionBinding defaultBinding = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ().ToName(SomeEnum.ONE).ToSingleton(); IInjectionBinding defaultBinding2 = new InjectionBinding(resolver).Bind <InjectableSuperClass> ().To <InjectableDerivedClass> ().ToName(SomeEnum.TWO).ToSingleton(); InjectableDerivedClass testResult = factory.Get(defaultBinding) as InjectableDerivedClass; int defaultValue = testResult.intValue; Assert.IsNotNull(testResult); //Set a value testResult.intValue = 42; //Now get an instance again and ensure it's a different instance InjectableDerivedClass testResult2 = factory.Get(defaultBinding2) as InjectableDerivedClass; Assert.IsNotNull(testResult2); Assert.That(testResult2.intValue == defaultValue); }
// METHODS #region IInjector implementations public IInstanceProviderSetter <T> AddBinding <T>() { Type bindingType = typeof(T); IInjectionBinding binding = null; if (!_isBindingCompleted) { // Check is there is an existing binding with given type if (_bindings.TryGetValue(bindingType, out binding)) { // Handler error InjectionError error = CreateError(InjectionErrorType.AlreadyAddedBindingForType, bindingType, null, "", 1); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } } else { // Add binding binding = new InjectionBinding <T>(this); _bindings.Add(bindingType, binding); } } else { // Handler error InjectionError error = CreateError(InjectionErrorType.BindingAfterInjection, bindingType, null, "", 1); if (_shouldThrowException) { throw new InjectionException(error.error, error.message); } } return((InjectionBinding <T>)binding); }
public void TestImplicitToSingleton() { //This succeeds if no error IInjectionBinding binding = new InjectionBinding(resolver).Bind<InjectableSuperClass> ().ToSingleton (); factory.Get (binding); //Succeeds if throws error IInjectionBinding binding2 = new InjectionBinding(resolver).Bind<ISimpleInterface> ().ToSingleton (); TestDelegate testDelegate = delegate() { factory.Get (binding2); }; InjectionException ex = Assert.Throws<InjectionException>(testDelegate); Assert.That (ex.type == InjectionExceptionType.NOT_INSTANTIABLE); //Succeeds if throws error IInjectionBinding binding3 = new InjectionBinding(resolver).Bind<AbstractClass> ().ToSingleton (); TestDelegate testDelegate2 = delegate() { factory.Get(binding3); }; InjectionException ex2 = Assert.Throws<InjectionException>(testDelegate2); Assert.That (ex2.type == InjectionExceptionType.NOT_INSTANTIABLE); }
public void TestGetSupply() { Type[] supplied = new Type[3]; supplied [0] = typeof (HasANamedInjection); supplied [1] = typeof (HasANamedInjection2); supplied [2] = typeof (InjectsClassToBeInjected); int iterator = 0; Binder.BindingResolver resolver = delegate (IBinding bound) { object[] value = (bound as IInjectionBinding).GetSupply(); Assert.AreEqual(value[value.Length-1], supplied[iterator]); }; InjectionBinding binding = new InjectionBinding (resolver); while (iterator < 3) { binding.SupplyTo (supplied[iterator]); iterator++; } object[] supply = binding.GetSupply (); Assert.AreEqual (3, supply.Length); for (var a = 0; a < supply.Length; a++) { Assert.AreEqual (supply[a], supplied[a]); } }
public void TestGetFromPool() { IPool<ClassToBeInjected> pool = new Pool<ClassToBeInjected> (); // Format the pool pool.size = 4; pool.instanceProvider = new TestInstanceProvider (); IInjectionBinding binding = new InjectionBinding (resolver); binding.Bind<IPool<ClassToBeInjected>> ().To <Pool<ClassToBeInjected>> ().ToValue(pool); IPool<ClassToBeInjected> myPool = factory.Get (binding) as Pool<ClassToBeInjected>; Assert.NotNull (myPool); ClassToBeInjected instance1 = myPool.GetInstance () as ClassToBeInjected; Assert.NotNull (instance1); ClassToBeInjected instance2 = myPool.GetInstance () as ClassToBeInjected; Assert.NotNull (instance2); Assert.AreNotSame (instance1, instance2); }
public void TestUnsupply() { Binder.BindingResolver resolver = delegate (IBinding bound) { }; InjectionBinding binding = new InjectionBinding (resolver); binding.To<ClassToBeInjected>().SupplyTo<HasANamedInjection> (); Assert.AreEqual (typeof(HasANamedInjection), binding.GetSupply()[0]); Assert.AreEqual (typeof(ClassToBeInjected), binding.value); binding.Unsupply<HasANamedInjection> (); Assert.IsNull (binding.GetSupply()); }
public void TestInstantiation() { IInjectionBinding defaultBinding = new InjectionBinding (resolver).Key<InjectableSuperClass> ().To <InjectableDerivedClass> (); InjectableDerivedClass testResult = factory.Get (defaultBinding) as InjectableDerivedClass; Assert.IsNotNull (testResult); }
public void TestValueMap() { InjectableDerivedClass testvalue = new InjectableDerivedClass (); testvalue.intValue = 42; IInjectionBinding binding = new InjectionBinding (resolver).Key<InjectableSuperClass> ().To <InjectableDerivedClass> ().ToValue (testvalue); InjectableDerivedClass testResult = factory.Get (binding) as InjectableDerivedClass; Assert.IsNotNull (testResult); Assert.That (testResult.intValue == testvalue.intValue); Assert.That (testResult.intValue == 42); }
public void TestSupplyOne() { Binder.BindingResolver resolver = delegate (IBinding bound) { object[] value = (bound as IInjectionBinding).GetSupply(); Assert.AreEqual(value[0], typeof(HasANamedInjection)); }; InjectionBinding binding = new InjectionBinding (resolver); binding.SupplyTo<HasANamedInjection> (); }