Ejemplo n.º 1
0
        public void Type_List_Delegates_And_Lazy_Type_Combination()
        {
            Func <IEnumerable <Type> > types = () => new[] { typeof(TransientObject3) };

            var resolver = new LazyResolver(types);

            resolver.AddType(new Lazy <Type>(() => typeof(TransientObject2)));
            resolver.AddType <TransientObject1>();

            Resolution.Freeze();

            var instances1 = resolver.Objects;

            Assert.AreEqual(3, instances1.Count());
            Assert.IsTrue(instances1.Select(x => x.GetType()).ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
        }
        public void LazyResolverResolvesBothWays()
        {
            Func <IEnumerable <Type> > types = () => new[] { typeof(TransientObject3) };

            var resolver = new LazyResolver(types);

            resolver.AddType(new Lazy <Type>(() => typeof(TransientObject2)));
            resolver.AddType <TransientObject1>();

            Resolution.Freeze();

            var values = resolver.Objects;

            Assert.AreEqual(3, values.Count());
            Assert.IsTrue(values.Select(x => x.GetType())
                          .ContainsAll(new[] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
        }
Ejemplo n.º 3
0
        public void Ensure_Lazy_Type_Resolution()
        {
            var resolver = new LazyResolver(new[] { new Lazy <Type>(() => typeof(TransientObject3)) });

            resolver.AddType <TransientObject1>();
            resolver.AddType(new Lazy <Type>(() => typeof(TransientObject2)));

            Resolution.Freeze();

            Assert.IsFalse(resolver.HasResolvedTypes);

            var instances1 = resolver.Objects;

            Assert.IsTrue(resolver.HasResolvedTypes);

            Assert.AreEqual(3, instances1.Count());
            Assert.IsTrue(instances1.Select(x => x.GetType()).ContainsAll(new [] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
        }
        public void LazyResolverResolvesLazyTypes()
        {
            var resolver = new LazyResolver(new[] { new Lazy <Type>(() => typeof(TransientObject3)) });

            resolver.AddType <TransientObject1>();
            resolver.AddType(new Lazy <Type>(() => typeof(TransientObject2)));

            Resolution.Freeze();

            Assert.IsFalse(resolver.HasResolvedTypes);

            var values = resolver.Objects;

            Assert.IsTrue(resolver.HasResolvedTypes);

            Assert.AreEqual(3, values.Count());
            Assert.IsTrue(values.Select(x => x.GetType())
                          .ContainsAll(new [] { typeof(TransientObject1), typeof(TransientObject2), typeof(TransientObject3) }));
        }
        public void LazyResolverThrowsOnDuplicate()
        {
            Func <IEnumerable <Type> > types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) };

            var resolver = new LazyResolver(types);

            // duplicate, but will not throw here
            resolver.AddType <TransientObject1>();

            Resolution.Freeze();

            Assert.Throws <InvalidOperationException>(() =>
            {
                var values = resolver.Objects;
            });
        }
Ejemplo n.º 6
0
        public void LazyResolverThrowsOnInvalidType()
        {
            Func <IEnumerable <Type> > types = () => new[] { typeof(TransientObject3), typeof(TransientObject2), typeof(TransientObject1) };

            var resolver = new LazyResolver(
                new ActivatorServiceProvider(),
                Mock.Of <ILogger>(),
                types);

            // invalid, but will not throw here
            resolver.AddType(new Lazy <Type>(() => typeof(TransientObject4)));

            Resolution.Freeze();

            Assert.Throws <InvalidOperationException>(() =>
            {
                var values = resolver.Objects;
            });
        }