Esempio n. 1
0
        public void InjectionConstructorInsertsChooserForConstructorWithParameters()
        {
            string expectedString = "Hello";
            int    expectedInt    = 12;

            var ctor    = new InjectionConstructor(expectedString, expectedInt);
            var context = new MockBuilderContext
            {
                BuildKey = new NamedTypeBuildKey(typeof(GuineaPig))
            };
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get <IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);

            string[] keys = selected.GetParameterKeys();

            Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(int))), selected.Constructor);
            Assert.AreEqual(2, keys.Length);

            Assert.AreEqual(expectedString, (string)ResolveValue(policies, keys[0]));
            Assert.AreEqual(expectedInt, (int)ResolveValue(policies, keys[1]));
        }
Esempio n. 2
0
        public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer()
        {
            InjectionConstructor  ctor    = new InjectionConstructor("Logger", typeof(ILogger));
            TestingBuilderContext context = new TestingBuilderContext();

            context.BuildKey = typeof(GuineaPig);
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context);

            string[] keys = selected.GetParameterKeys();

            Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor);
            Assert.AreEqual(2, keys.Length);

            IDependencyResolverPolicy policy =
                context.Policies.Get <IDependencyResolverPolicy>(keys[1]);

            Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy);
        }
Esempio n. 3
0
        public void InjectionConstructorThrowsIfNoMatchingConstructor()
        {
            InjectionConstructor ctor = new InjectionConstructor(typeof(double));
            var context = new MockBuilderContext();

            ctor.AddPolicies(typeof(GuineaPig), context.PersistentPolicies);
        }
        public void InjectionConstructorInsertsChooserForConstructorWithParameters()
        {
            string expectedString = "Hello";
            int expectedInt = 12;

            var ctor = new InjectionConstructor(expectedString, expectedInt);
            var context = new MockBuilderContext
                {
                    BuildKey = new NamedTypeBuildKey(typeof (GuineaPig))
                };
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get<IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);
            string[] keys = selected.GetParameterKeys();

            Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(int))), selected.Constructor);
            Assert.AreEqual(2, keys.Length);

            Assert.AreEqual(expectedString, (string)ResolveValue(policies, keys[0]));
            Assert.AreEqual(expectedInt, (int)ResolveValue(policies, keys[1]));
        }
        public void InjectionConstructorThrowsIfNoMatchingConstructor()
        {
            InjectionConstructor ctor = new InjectionConstructor(typeof(double));
            var context = new MockBuilderContext();

            AssertExtensions.AssertException <InvalidOperationException>(
                () => ctor.AddPolicies(typeof(GuineaPig), context.PersistentPolicies));
        }
        public void InjectionConstructorInsertsChooserForDefaultConstructor()
        {
            InjectionConstructor  ctor     = new InjectionConstructor();
            TestingBuilderContext context  = new TestingBuilderContext();
            IPolicyList           policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context);

            Assert.AreEqual(typeof(GuineaPig).GetConstructor(new Type[0]), selected.Constructor);
            Assert.AreEqual(0, selected.GetParameterKeys().Length);
        }
        public void InjectionConstructorInsertsChooserForDefaultConstructor()
        {
            var ctor = new InjectionConstructor();
            var context = new MockBuilderContext
                {
                    BuildKey = new NamedTypeBuildKey(typeof (GuineaPig))
                };
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get<IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);
            Assert.AreEqual(typeof(GuineaPig).GetConstructor(new Type[0]), selected.Constructor);
            Assert.AreEqual(0, selected.GetParameterKeys().Length);
        }
        public void InjectionConstructorInsertsChooserForDefaultConstructor()
        {
            var ctor    = new InjectionConstructor();
            var context = new MockBuilderContext
            {
                BuildKey = new NamedTypeBuildKey(typeof(GuineaPig))
            };
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get <IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);

            Assert.Equal(typeof(GuineaPig).GetMatchingConstructor(new Type[0]), selected.Constructor);
            Assert.Equal(0, selected.GetParameterResolvers().Length);
        }
        public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer()
        {
            var ctor = new InjectionConstructor("Logger", typeof(ILogger));
            var context = new MockBuilderContext();
            context.BuildKey = new NamedTypeBuildKey(typeof(GuineaPig));
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get<IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);
            string[] keys = selected.GetParameterKeys();

            Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor);
            Assert.AreEqual(2, keys.Length);

            var policy = context.Policies.Get<IDependencyResolverPolicy>(keys[1]);
            Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy);
        }
Esempio n. 10
0
        public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer()
        {
            var ctor    = new InjectionConstructor("Logger", typeof(ILogger));
            var context = new MockBuilderContext();

            context.BuildKey = new NamedTypeBuildKey(typeof(GuineaPig));
            IPolicyList policies = context.PersistentPolicies;

            ctor.AddPolicies(typeof(GuineaPig), policies);

            var selector = policies.Get <IConstructorSelectorPolicy>(
                new NamedTypeBuildKey(typeof(GuineaPig)));

            SelectedConstructor selected = selector.SelectConstructor(context, policies);
            var resolvers = selected.GetParameterResolvers();

            Assert.Equal(typeof(GuineaPig).GetMatchingConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor);
            Assert.Equal(2, resolvers.Length);

            var policy = resolvers[1];

            Assert.True(policy is NamedTypeDependencyResolverPolicy);
        }
        public void InjectionConstructorThrowsIfNoMatchingConstructor()
        {
            InjectionConstructor ctor = new InjectionConstructor(typeof(double));
            var context = new MockBuilderContext();

            ctor.AddPolicies(typeof(GuineaPig), context.PersistentPolicies);
        }
Esempio n. 12
0
        public void InjectionConstructorThrowsIfNoMatchingConstructor()
        {
            InjectionConstructor ctor = new InjectionConstructor(typeof(double));
            var context = new MockBuilderContext();

            AssertExtensions.AssertException<InvalidOperationException>(
                () => ctor.AddPolicies(typeof(GuineaPig), context.PersistentPolicies));
        }