public void Test_Error_AlreadyAddedValueProviderWithDifferentProvider()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding to a value
            injector.AddBinding <ISimpleInterfaceAA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding with same provider type
            injector.AddBinding <ISimpleInterfaceA>().ToValue(new SimpleClassA());

            //  Check bindings and providers
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedTypeWithDifferentProvider, injector.GetError(0).error);
        }
Ejemplo n.º 2
0
        public void Test_Error_BindingAfterInjectInto()
        {
            InjectorContext injector = new InjectorContext();

            //  Add a bindings
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Validate bindings
            Assert.Equal(1, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Get instance
            var value = new ClassThatUses_SimpleClassA();

            injector.InjectInto(value);

            //  Try adding a new binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.BindingAfterInjection, injector.GetError(0).error);
        }
Ejemplo n.º 3
0
        public void Test_UnifyTypedProvidersWithSameType()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <ISimpleInterfaceA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding with same provider type
            injector.AddBinding <ISimpleInterfaceAA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Add third binding with same provider type
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Check bindings and providers
            Assert.Equal(3, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check errors
            Assert.Equal(0, injector.ErrorCount);
        }
Ejemplo n.º 4
0
        public void Test_AddingBindings()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <SimpleClassA>();

            //  Validate binding
            Assert.Equal(1, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding
            injector.AddBinding <SimpleClassB>();

            //  Validate binding
            Assert.Equal(2, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));
            Assert.True(injector.HasBindingForType(typeof(SimpleClassB)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Ejemplo n.º 5
0
        public void Test_SettingValueProvider()
        {
            InjectorContext injector = new InjectorContext();

            //  Add bindings
            injector.AddBinding <CrossReferenceClassA>().ToType <CrossReferenceClassA>();
            injector.AddBinding <CrossReferenceClassB>().ToType <CrossReferenceClassB>();

            //  Create injection targets
            var target = new CrossReferenceContainer();

            //  Check before injection
            Assert.Null(target.valueA);
            Assert.Null(target.valueB);

            //  Inject
            injector.InjectInto(target);

            //  Check after injection
            Assert.NotNull(target.valueA);
            Assert.NotNull(target.valueB);
            Assert.NotNull(target.valueA.value);
            Assert.NotNull(target.valueB.value);
            Assert.Equal(target.valueA.value, target.valueB);
            Assert.Equal(target.valueB.value, target.valueA);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Ejemplo n.º 6
0
        public void Test_Error_ReAddingExistingBindings()
        {
            InjectorContext injector = new InjectorContext();

            //  Add two bindings
            injector.AddBinding <SimpleClassA>();
            injector.AddBinding <SimpleClassB>();

            //  Validate bindings
            Assert.Equal(2, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Try re-adding first binding
            injector.AddBinding <SimpleClassA>();

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(0).error);

            //  Try re-adding second binding
            injector.AddBinding <SimpleClassB>();

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.AlreadyAddedBindingForType, injector.GetError(1).error);
        }
Ejemplo n.º 7
0
        public void Test_GetAssignableInstances()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();
            injector.AddBinding <SimpleClassAA>().ToType <SimpleClassAA>();
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();
            injector.AddBinding <SimpleClassC>().ToValue(new SimpleClassC());

            //  Validate binding
            Assert.Equal(4, injector.BindingCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check instance types and count
            IEnumerator <ISimpleInterfaceAA> instances = injector.GetAssignableInstances <ISimpleInterfaceAA>();
            int instanceCounter = 0;

            while (instances.MoveNext())
            {
                Assert.IsAssignableFrom <ISimpleInterfaceAA>(instances.Current);
                instanceCounter++;
            }
            Assert.Equal(2, instanceCounter);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
    private void AddBindings()
    {
        //  Create your object and initialize
        Game3Settings settings = new Game3Settings();

        settings.Volume         = 0.5f;
        settings.IsMusicPlaying = true;

        //  Bind those types to instance
        _injector.AddBinding <Game3Settings>().ToValue(settings);
        _injector.AddBinding <IGame3Settings>().ToValue(settings);
    }
Ejemplo n.º 9
0
        public void Test_Error_TypedInjectionToWrongType()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();

            //  Create injection target
            var target = new ClassThatUses_SimpleClassA();

            //  Check before injection
            Assert.Null(target.value2);
            Assert.Null(target.value1);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(2, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(0).error);
            Assert.Equal(InjectionErrorType.CanNotFindBindingForType, injector.GetError(1).error);

            //  Check after injection
            Assert.Null(target.value2);
            Assert.Null(target.value1);
        }
        public void Test_NamespaceRestriction()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>().RestrictToNamespace("Com.Bit34Games.Injector.Test.Containers");

            //  Validate binding
            Assert.Equal(1, injector.BindingCount);
            Assert.True(injector.HasBindingForType(typeof(SimpleClassA)));

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Inject
            var instanceA = new ClassThatUses_SimpleClassA();

            injector.InjectInto(instanceA);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Inject
            var instanceB = new ClassThatUses_SimpleClassA_InAnotherNamespace();

            injector.InjectInto(instanceB);

            //  Check error
            Assert.Equal(1, injector.ErrorCount);
            Assert.Equal(InjectionErrorType.InjectionRestricted, injector.GetError(0).error);
        }
        public void Test_ValueInjectionToNestedMembers()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            var value = new SimpleClassA();

            injector.AddBinding <SimpleClassA>().ToValue(value);

            //  Create injection target
            var target = new ExtendedClassThatUses_SimpleClassA();

            //  Check before injection
            Assert.Null(target.value2);
            Assert.Null(target.value1);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check after injection
            Assert.NotNull(target.value2);
            Assert.NotNull(target.value1);
            Assert.Same(value, target.value2);
            Assert.Same(value, target.value1);
        }
        public void Test_AddingTypedProvider()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding and a typed provider
            injector.AddBinding <SimpleClassA>().ToType <SimpleClassA>();

            //  Validate
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding and set typed provider
            injector.AddBinding <SimpleClassB>().ToType <SimpleClassB>();

            //  Validate
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(2, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
        public void Test_AddingValueProviderToAssignableType()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding and a typed provider
            injector.AddBinding <ISimpleInterfaceA>().ToValue(new SimpleClassA());

            //  Validate
            Assert.Equal(1, injector.BindingCount);
            Assert.Equal(1, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Add second binding and set typed provider
            injector.AddBinding <ISimpleInterfaceB>().ToValue(new SimpleClassB());

            //  Validate
            Assert.Equal(2, injector.BindingCount);
            Assert.Equal(2, injector.ProviderCount);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);
        }
Ejemplo n.º 14
0
        public void Test_TypedInjectionToAssignableType()
        {
            InjectorContext injector = new InjectorContext();

            //  Add first binding
            injector.AddBinding <ISimpleInterfaceA>().ToType <SimpleClassA>();

            //  Create injection target
            var target = new ClassThatUses_SimpleInterfaceA();

            //  Check before injection
            Assert.Null(target.value);

            //  Inject
            injector.InjectInto(target);

            //  Check error
            Assert.Equal(0, injector.ErrorCount);

            //  Check after injection
            Assert.NotNull(target.value);
        }