Esempio n. 1
0
        public void Covariant_Enumerable_ShouldContainOneMatchBecauseOptionDisablesEnumerableCovarianceForThatEnumerable()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.SetOption <Options.EnableEnumerableCovariance, BaseClass>(false);
            var baseTarget             = Target.ForType <BaseClass>();
            var childTarget            = Target.ForType <BaseClassChild>();
            var grandChildTarget       = Target.ForType <BaseClassGrandchild>();
            var nestedbaseTarget       = Target.ForType <Covariant <BaseClass> >();
            var nestedchildTarget      = Target.ForType <Covariant <BaseClassChild> >();
            var nestedgrandChildTarget = Target.ForType <Covariant <BaseClassGrandchild> >();

            targets.Register(baseTarget);
            targets.Register(childTarget);
            targets.Register(grandChildTarget);
            targets.Register(nestedbaseTarget, typeof(ICovariant <BaseClass>));
            targets.Register(nestedchildTarget, typeof(ICovariant <BaseClassChild>));
            targets.Register(nestedgrandChildTarget, typeof(ICovariant <BaseClassGrandchild>));

            // Act
            var enumerableTarget       = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <BaseClass>)));
            var nestedEnumerableTarget = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <ICovariant <BaseClass> >)));

            // Assert
            Assert.Equal(new[] { baseTarget.Id }, enumerableTarget.Targets.Select(t => t.Id));
            Assert.Equal(new[] { nestedbaseTarget.Id, nestedchildTarget.Id, nestedgrandChildTarget.Id }, nestedEnumerableTarget.Targets.Select(t => t.Id));
        }
        public void ShouldNotAddRegistrationsIfDisabledByOption()
        {
            var configs = new CombinedTargetContainerConfig
            {
                Configuration.InjectLists.Instance,
                Configuration.Configure.Option <Options.EnableListInjection>(false)
            };

            var targets = new TargetContainer(configs);

            Assert.Null(targets.Fetch(typeof(List <string>)));
            Assert.Null(targets.Fetch(typeof(IList <string>)));
        }
Esempio n. 3
0
        public void ShouldSupportTwoRegistrations()
        {
            ITargetContainer targets = new TargetContainer();
            var simpleType           = new NoCtor();

            ITarget target1 = Target.ForObject("hello world");
            ITarget target2 = Target.ForObject(new NoCtor());

            targets.Register(target1);
            targets.Register(target2);
            Assert.Same(target1, targets.Fetch(typeof(string)));
            Assert.Same(target2, targets.Fetch(typeof(NoCtor)));
        }
        public void ShouldSupportRegisteringOpenGenericAndFetchingAsClosed()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = Target.ForType(typeof(Generic <>));

            targets.Register(target, typeof(IGeneric <>));

            // Act
            var fetched       = targets.Fetch(typeof(IGeneric <>));
            var fetchedClosed = targets.Fetch(typeof(IGeneric <int>));

            // Assert
            Assert.Same(target, fetched);
            Assert.Same(target, fetchedClosed);
        }
        public void ShouldRegisterAlias()
        {
            TargetContainer targets = new TargetContainer();

            targets.RegisterAlias <object, string>();
            Assert.NotNull(targets.Fetch(typeof(object)));
        }
Esempio n. 6
0
        public void Projection_ShouldAutoProject_SpecificImplementationType()
        {
            // Projecting a different implementation type from the type that we're projecting to

            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, ITo, To>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 7
0
        public void Projection_ShouldProject_FromRegistration()
        {
            // This time, projecting to an interface for which we have one registration
            // Demonstrates that the container will use a specific registration instead
            // of auto-binding the implementation type.

            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            var expectedTarget = Target.ForType <To>();

            targets.Register(expectedTarget, typeof(ITo));
            targets.RegisterProjection <From, ITo>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Same(expectedTarget, projTarget.OutputTarget);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Same(expectedTarget, projTarget.OutputTarget);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 8
0
        public void Projection_ShouldProject_FromTypeSelector()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, ITo <From> >((r, t) => typeof(To <>).MakeGenericType(t.DeclaredType));

            // Act
            var result = targets.Fetch(typeof(IEnumerable <ITo <From> >));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To <From1>), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(To <From2>), projTarget.OutputTarget.DeclaredType);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
Esempio n. 9
0
        public void Projection_ShouldAutoProject()
        {
            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <From1>();
            targets.RegisterType <From2>();
            targets.RegisterProjection <From, To>();

            // Act
            var result = targets.Fetch(typeof(IEnumerable <To>));

            // Assert
            var enumTarget = Assert.IsType <EnumerableTarget>(result);

            Assert.Collection(enumTarget, new Action <ITarget>[] {
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Equal(typeof(To), projTarget.DeclaredType);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(From1), projTarget.InputTarget.DeclaredType);
                },
                t => {
                    var projTarget = Assert.IsType <ProjectionTarget>(t);
                    Assert.Equal(typeof(To), projTarget.DeclaredType);
                    Assert.IsType <ConstructorTarget>(projTarget.OutputTarget);
                    Assert.Equal(typeof(From2), projTarget.InputTarget.DeclaredType);
                }
            });
        }
        public void ShouldAddRegistrations()
        {
            var configs = new CombinedTargetContainerConfig
            {
                Configuration.InjectLists.Instance
            };

            var targets = new TargetContainer(configs);

            var concreteTarget  = targets.Fetch(typeof(List <string>));
            var interfaceTarget = targets.Fetch(typeof(IList <string>));

            Assert.NotNull(concreteTarget);
            Assert.False(concreteTarget.UseFallback);
            Assert.NotNull(interfaceTarget);
            Assert.False(interfaceTarget.UseFallback);
        }
Esempio n. 11
0
        public void ShouldNotAddRegistrationsIfDisabledByOption()
        {
            // note input order reversed from the preferred: tests that InjectCollections config
            // expresses dependency on a configuration which configures the option
            CombinedTargetContainerConfig config = new CombinedTargetContainerConfig()
            {
                InjectCollections.Instance,
                Configure.Option <EnableCollectionInjection>(false)
            };

            var targets = new TargetContainer(config);

            Assert.Null(targets.Fetch(typeof(Collection <string>)));
            Assert.Null(targets.Fetch(typeof(ICollection <string>)));
            Assert.Null(targets.Fetch(typeof(ReadOnlyCollection <string>)));
            Assert.Null(targets.Fetch(typeof(IReadOnlyCollection <string>)));
        }
Esempio n. 12
0
        public void ShouldRegisterForImplicitType()
        {
            ITarget          t = new ObjectTarget("hello word");
            ITargetContainer rezolverBuilder = new TargetContainer();

            rezolverBuilder.Register(t);
            var t2 = rezolverBuilder.Fetch(typeof(string));

            Assert.Same(t, t2);
        }
        public void SimpleLookup()
        {
            // <example1>
            var targets = new TargetContainer();

            targets.RegisterObject("hello world");

            var target = targets.Fetch(typeof(string));

            Assert.IsType <ObjectTarget>(target);
            // </example1>
        }
        public void ShouldFavourGenericSpecialisationOfGeneric()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = Target.ForType(typeof(Generic <>));

            //note here - using MakeGenericType is the only way to get a reference to a type like IFoo<IFoo<>> because
            //supply an open generic as a type parameter to a generic is not valid.
            var target2 = Target.ForType(typeof(NestedGenericA <>));

            targets.Register(target, typeof(IGeneric <>));
            targets.Register(target2, typeof(IGeneric <>).MakeGenericType(typeof(IEnumerable <>)));

            // Act
            var fetched  = targets.Fetch(typeof(IGeneric <IEnumerable <int> >));
            var fetched2 = targets.Fetch(typeof(IGeneric <int>));

            // Assert
            Assert.Same(target2, fetched);
            Assert.Same(target, fetched2);
        }
        public void LookupByBase()
        {
            // <example2>
            var targets = new TargetContainer();

            targets.Register(Target.ForType <MyService>(), typeof(IMyService));

            var target = targets.Fetch(typeof(IMyService));

            Assert.IsType <ConstructorTarget>(target);
            // </example2>
        }
Esempio n. 16
0
        public void ShouldRegisterNullObjectTarget()
        {
            // <example1>
            ITarget          t = new ObjectTarget(null);
            ITargetContainer r = new TargetContainer();

            r.Register(t, serviceType: typeof(object));
            var t2 = r.Fetch(typeof(object));

            Assert.Same(t, t2);
            // </example1>
        }
        public void ShouldSupportRegisteringSpecialisationOfGeneric()
        {
            // Assert
            ITargetContainer targets = new TargetContainer();

            targets.RegisterType(typeof(Generic <>), typeof(IGeneric <>));

            // Act
            var fetched = targets.Fetch(typeof(IGeneric <int>));

            // Assert
            Assert.NotNull(fetched);
            Assert.False(fetched.UseFallback);
        }
        public void MixedVariance_ShouldFetchCompatibleFunc()
        {
            // Arrange
            var targets  = new TargetContainer();
            var expected = Target.ForObject(new Func <object, string>(o => o.ToString()));

            targets.Register(expected);

            // Act
            var result = targets.Fetch(typeof(Func <string, object>));

            // Assert
            Assert.Equal(expected.Id, result.Id);
        }
        public void ShouldSupportRegisteringAndRetrievingGenericWithAsymmetricGenericBase()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = Target.ForType(typeof(NestedGenericA <>));

            targets.Register(target, typeof(IGeneric <>).MakeGenericType(typeof(IEnumerable <>)));

            // Act
            var fetched = targets.Fetch(typeof(IGeneric <IEnumerable <int> >));

            // Assert
            Assert.Same(target, fetched);
        }
        public void ShouldSupportRegisteringAndRetrievingGenericWithGenericParameter()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = Target.ForType(typeof(Generic <>));

            targets.Register(target, typeof(IGeneric <>));

            // Act
            var fetched = targets.Fetch(typeof(IGeneric <IGeneric <int> >));

            // Assert
            Assert.Same(target, fetched);
        }
Esempio n. 21
0
        public void Covariant_ShouldFetch(string name, Type tTarget, Type toFetch)
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = new TestTarget(tTarget, false, true, ScopeBehaviour.None);

            targets.Register(target);

            // Act
            var fetched = targets.Fetch(toFetch);

            // Assert
            Assert.Equal(target.Id, fetched.Id);
        }
Esempio n. 22
0
        public void ShouldAddRegistrations()
        {
            CombinedTargetContainerConfig config = new CombinedTargetContainerConfig
            {
                InjectCollections.Instance
            };

            var targets = new TargetContainer(config);

            var rwConcreteTarget  = targets.Fetch(typeof(Collection <string>));
            var rwInterfaceTarget = targets.Fetch(typeof(ICollection <string>));
            var roConcreteTarget  = targets.Fetch(typeof(ReadOnlyCollection <string>));
            var roInterfaceTarget = targets.Fetch(typeof(IReadOnlyCollection <string>));

            Assert.NotNull(rwConcreteTarget);
            Assert.NotNull(rwInterfaceTarget);
            Assert.NotNull(roConcreteTarget);
            Assert.NotNull(roInterfaceTarget);

            Assert.False(rwConcreteTarget.UseFallback);
            Assert.False(rwInterfaceTarget.UseFallback);
            Assert.False(roConcreteTarget.UseFallback);
            Assert.False(roInterfaceTarget.UseFallback);
        }
        public void ShouldConfigureAfterOptionSet()
        {
            CombinedTargetContainerConfig coll = new CombinedTargetContainerConfig
            {
                // add the dependant first
                new ConfiguredByTestOptionConfig(t => Assert.Equal("configured!", t.GetOption <TestOption>()), true)
            };

            // then add the config that configures the option :)
            coll.ConfigureOption <TestOption>("configured!");

            var targets = new TargetContainer(coll);

            // this just ensures that the configure method was actually called
            Assert.IsType <Rezolver.Targets.ObjectTarget>(targets.Fetch(typeof(ConfiguredByTestOptionConfig)));
        }
        public void ShouldFavourSpecialisationOfGenericInt()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var notExpected          = Target.ForType(typeof(Generic <>));
            var expected             = Target.ForType(typeof(AltGeneric <int>));

            targets.Register(notExpected, typeof(IGeneric <>));
            targets.Register(expected, typeof(IGeneric <int>));

            // Act
            var fetched = targets.Fetch(typeof(IGeneric <int>));

            // Assert
            Assert.NotSame(notExpected, fetched);
            Assert.Same(expected, fetched);
        }
Esempio n. 25
0
        public void Covariant_ShouldNotRetrieveConstrained()
        {
            // Arrange
            var targets     = new TargetContainer();
            var expected    = Target.ForType(typeof(Covariant <>));
            var notExpected = Target.ForType(typeof(ConstrainedCovariant <>));

            targets.Register(expected, typeof(ICovariant <>));
            targets.Register(notExpected, typeof(ICovariant <>));

            // Act
            var single = targets.Fetch(typeof(ICovariant <string>));
            var all    = targets.FetchAll(typeof(ICovariant <string>));

            // Assert
            Assert.Same(expected, single);
            Assert.Single(all, expected);
        }
Esempio n. 26
0
        public void Covariant_Enumerable_ShouldContainAllMatches_NestedCovariant()
        {
            // Arrange
            var targets          = new TargetContainer();
            var baseTarget       = Target.ForType <Covariant <BaseClass> >();
            var childTarget      = Target.ForType <Covariant <BaseClassChild> >();
            var grandChildTarget = Target.ForType <Covariant <BaseClassGrandchild> >();

            targets.Register(baseTarget, typeof(ICovariant <BaseClass>));
            targets.Register(childTarget, typeof(ICovariant <BaseClassChild>));
            targets.Register(grandChildTarget, typeof(ICovariant <BaseClassGrandchild>));

            // Act
            var enumerableTarget = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <ICovariant <BaseClass> >)));

            // Assert
            Assert.Equal(new[] { baseTarget.Id, childTarget.Id, grandChildTarget.Id }, enumerableTarget.Targets.Select(t => t.Id));
        }
        public void ShouldNotFetchConstrainedGenericForIncompatibleType()
        {
            // Arrange
            ITargetContainer targets = new TargetContainer();
            var expected             = Target.ForType(typeof(Generic <>));
            var notexpected          = Target.ForType(typeof(ConstrainedGeneric <>));

            targets.Register(expected, typeof(IGeneric <>));
            targets.Register(notexpected, typeof(IGeneric <>));

            // Act
            var single = targets.Fetch(typeof(IGeneric <string>));
            var all    = targets.FetchAll(typeof(IGeneric <string>));

            // Assert
            Assert.Same(expected, single);
            Assert.Single(all, expected);
        }
Esempio n. 28
0
        public void Covariant_ShouldRegister_TypeWhichReferencesItselfInImplementedCovariant_Complex()
        {
            // This is inspired by an error that I'm getting in Xamarin when I attempt to register
            // pages in the container - it immediately goes into an endless loop as it attempts
            // to produce the known list of covariant types

            // Arrange
            var targets = new TargetContainer();

            targets.RegisterType <GenericArg>();

            // Act
            var fetched = targets.Fetch(typeof(GenericArg));

            // Assert
            Assert.NotNull(fetched);
            Assert.False(fetched.UseFallback);
        }
Esempio n. 29
0
        public void Covariant_Enumerable_ShouldContainAllMatches()
        {
            // Arrange
            var targets          = new TargetContainer();
            var baseTarget       = Target.ForType <BaseClass>();
            var childTarget      = Target.ForType <BaseClassChild>();
            var grandChildTarget = Target.ForType <BaseClassGrandchild>();

            targets.Register(baseTarget);
            targets.Register(childTarget);
            targets.Register(grandChildTarget);

            // Act
            var enumerableTarget = Assert.IsType <EnumerableTarget>(targets.Fetch(typeof(IEnumerable <BaseClass>)));

            // Assert
            // (can't compare targets because
            Assert.Equal(new[] { typeof(BaseClass), typeof(BaseClassChild), typeof(BaseClassGrandchild) }, enumerableTarget.Targets.Select(t => t.DeclaredType));
        }
Esempio n. 30
0
        public void ShouldFetchContravariant(Type tTarget, Type toFetch)
        {
            // this theory specifically tests that if we register a target for a generic which
            // has contravariant type parameters, then it will be found automatically.

            // the actual handling of creating an instance is tested in the compiler spec tests
            // covering the ConstructorTarget

            // Arrange
            ITargetContainer targets = new TargetContainer();
            var target = new TestTarget(tTarget, false, true, ScopeBehaviour.None);

            targets.Register(target);

            // Act
            var fetched = targets.Fetch(toFetch);

            // Assert
            Assert.Equal(target.Id, fetched.Id);
        }