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);
        }
Esempio n. 2
0
        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> ();
        }
Esempio n. 7
0
 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);
        }
Esempio n. 9
0
 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);
 }
Esempio n. 10
0
		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);
		}
Esempio n. 11
0
        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);
        }
Esempio n. 14
0
        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);
        }
Esempio n. 15
0
		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);
		}
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        //  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);
        }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
		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]);
			}
		}
Esempio n. 20
0
		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);
		}
Esempio n. 21
0
		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());
		}
Esempio n. 22
0
 public void TestInstantiation()
 {
     IInjectionBinding defaultBinding = new InjectionBinding (resolver).Key<InjectableSuperClass> ().To <InjectableDerivedClass> ();
     InjectableDerivedClass testResult = factory.Get (defaultBinding) as InjectableDerivedClass;
     Assert.IsNotNull (testResult);
 }
Esempio n. 23
0
 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);
 }
Esempio n. 24
0
		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> ();
		}