예제 #1
0
        public void TestRescanDoesNotOverrideCrossContextValue()
        {
            object mockGameObject = new object();
            TestImplicitBindingContext context = new TestImplicitBindingContext(mockGameObject);

            //Get our binding. It should have value of Runtime Type. It hasn't been instantiated yet.
            IInjectionBinding binding = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.IsTrue(binding.value is Type);

            //GetInstance. This should set the value to the instantiated class
            TestImplicitBindingClass instanceValue = context.injectionBinder.GetInstance <TestImplicitBindingClass>();

            Assert.IsNotNull(instanceValue);
            IInjectionBinding bindingAfterGetInstance = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.IsTrue(bindingAfterGetInstance.value is TestImplicitBindingClass);
            Assert.AreSame(bindingAfterGetInstance, binding);

            //Rescan our implicit bindings
            //Our binding value should remain
            context.Scan();
            IInjectionBinding bindingAfterRescan = context.injectionBinder.GetBinding <TestImplicitBindingClass>();

            Assert.AreSame(bindingAfterRescan, binding);             //Should be the same binding, and not override it

            Assert.IsTrue(bindingAfterRescan.value is TestImplicitBindingClass);
            Assert.AreSame(bindingAfterRescan.value, instanceValue);
        }
예제 #2
0
        public void TestMultipleCrossContextImplicitBindings()
        {
            TestImplicitBindingClass.instantiationCount = 0;

            int               contextsBeforeInstancing   = 3;
            int               contextsAfterInstancing    = 4;
            int               contextsToCreate           = contextsBeforeInstancing + contextsAfterInstancing;
            int               getInstanceCallsPerContext = 3;
            int               injectsPerContext          = 3;
            List <Context>    contexts = new List <Context>();
            IInjectionBinding bindingBeforeContextCreation      = null;
            object            bindingValueBeforeContextCreation = null;

            //We create several Contexts.
            //note contextNumber is 1-based
            for (int contextNumber = 1; contextNumber <= contextsToCreate; contextNumber++)
            {
                //The first batch of Contexts don't actually create instances, just the implicit bindings
                bool toInstance = (contextNumber > contextsBeforeInstancing);
                //Specifically call out the Context that is first to create actual instances
                bool isFirstContextToCallGetInstance = (contextNumber == (contextsBeforeInstancing + 1));

                //Create each "ContextView" and its Context
                object mockGameObject = new object();

                TestImplicitBindingContext context = new TestImplicitBindingContext(mockGameObject);
                contexts.Add(context);

                //For each Context, check that the TestImplicitBindingClass BINDING exists (no instance created yet)
                IInjectionBinding bindingAfterContextCreation      = context.injectionBinder.GetBinding <TestImplicitBindingClass> ();
                object            bindingValueAfterContextCreation = bindingAfterContextCreation.value;

                bool bindingChangedDueToContextCreation      = !bindingAfterContextCreation.Equals(bindingBeforeContextCreation);
                bool bindingValueChangedDueToContextCreation = bindingValueAfterContextCreation != bindingValueBeforeContextCreation;


                //due to the weak binding replacement rules, the binding should change every time we scan until we instance
                Assert.IsFalse(bindingChangedDueToContextCreation && toInstance && !isFirstContextToCallGetInstance);

                //after creating a new context, the value of the binding should only change on the first context
                //(it was null before that)
                Assert.IsFalse(bindingValueChangedDueToContextCreation && contextNumber != 1);


                if (toInstance)
                {
                    //For the Contexts that actually create instances...
                    for (int a = 0; a < getInstanceCallsPerContext; a++)
                    {
                        //...create some instances (well, duh) of the TestImplicitBindingClass...
                        TestImplicitBindingClass instance = context.injectionBinder.GetInstance <TestImplicitBindingClass> ();
                        Assert.IsNotNull(instance);
                    }

                    for (int b = 0; b < injectsPerContext; b++)
                    {
                        //...and some instances of the class that gets injected with TestImplicitBindingClass.
                        TestImplicitBindingInjectionReceiver instance = context.injectionBinder.GetInstance <TestImplicitBindingInjectionReceiver> ();
                        Assert.IsNotNull(instance);
                        Assert.IsNotNull(instance.testImplicitBindingClass);
                    }
                }

                //We inspect the binding and its value after all this mapping/instantiation
                IInjectionBinding bindingAfterGetInstanceCalls      = context.injectionBinder.GetBinding <TestImplicitBindingClass> ();
                object            bindingValueAfterGetInstanceCalls = bindingAfterGetInstanceCalls.value;

                bool bindingChangedDueToGetInstanceCalls      = bindingAfterGetInstanceCalls != bindingAfterContextCreation;
                bool bindingValueChangedDueToGetInstanceCalls = bindingValueAfterGetInstanceCalls != bindingValueAfterContextCreation;

                //the binding itself should only change during the scan
                Assert.IsFalse(bindingChangedDueToGetInstanceCalls);

                //if the weak binding replacement rules are working, the only time the value should
                //change is the first time we call GetInstance
                Assert.IsFalse(bindingValueChangedDueToGetInstanceCalls && !isFirstContextToCallGetInstance);

                //reset values for the next pass
                bindingBeforeContextCreation      = bindingAfterGetInstanceCalls;
                bindingValueBeforeContextCreation = bindingValueAfterGetInstanceCalls;
            }

            //This is a Cross-Context Singleton.
            //The primary purpose of this test is to ensure (that under the circumstances of this test),
            //TestImplicitBindingClass should only get instantiated once
            Assert.AreEqual(1, TestImplicitBindingClass.instantiationCount);
        }