public void CreateObjectByArgumentsTest()
        {
            ServiceDescriptor  descA = ServiceDescriptor.Singleton <Int>(service => new Int(10));
            ServiceDescriptor  descB = ServiceDescriptor.Singleton <IFake, FakeA>();
            IServiceCollection pool  = Implementation.CreateServiceCollection();

            pool.Add(descA);
            pool.Add(descB);
            IServiceScopeFactory factory  = Implementation.CreateServiceScopeFactory(pool);
            IServiceProvider     services = factory.CreateScope().ServiceProvider;
            TakeArguments        args     = services.CreateInstance <TakeArguments>("match", 4, 0);

            Assert.AreEqual(1, args.FakeValue);
            Assert.AreEqual(10, args.IntValue);
            Assert.AreEqual("match", args.Match);
            Assert.AreEqual(4, args.TestInt);
            Assert.AreEqual(0, args.TestB);

            args = services.CreateInstance <TakeArguments>("match", 4);
            Assert.AreEqual(1, args.FakeValue);
            Assert.AreEqual(10, args.IntValue);
            Assert.AreEqual("match", args.Match);
            Assert.AreEqual(4, args.TestInt);
            Assert.AreEqual(1, args.TestB);

            args = services.CreateInstance <TakeArguments>(new Dictionary <string, object>()
            {
                ["match"] = "test_match"
            }, 4, 0);
            Assert.AreEqual(1, args.FakeValue);
            Assert.AreEqual(10, args.IntValue);
            Assert.AreEqual("test_match", args.Match);
            Assert.AreEqual(4, args.TestInt);
            Assert.AreEqual(0, args.TestB);

            args = services.CreateInstance <TakeArguments>(new Dictionary <string, object>()
            {
                ["match"] = "test_match",
                ["testb"] = 10
            });
            Assert.AreEqual(1, args.FakeValue);
            Assert.AreEqual(10, args.IntValue);
            Assert.AreEqual("test_match", args.Match);
            Assert.AreEqual(0, args.TestInt);
            Assert.AreEqual(10, args.TestB);

            args = services.CreateInstance <TakeArguments>(new Dictionary <string, object>()
            {
                ["match"] = "test_match",
                ["testb"] = 10
            }, "match", 4, 5);
            Assert.AreEqual(1, args.FakeValue);
            Assert.AreEqual(10, args.IntValue);
            Assert.AreEqual("test_match", args.Match);
            Assert.AreEqual(4, args.TestInt);
            Assert.AreEqual(10, args.TestB);
        }
Beispiel #2
0
        public void AddRemoveTest()
        {
            ServiceDescriptor  descA = ServiceDescriptor.Transient <Int>(service => new Int(4));
            ServiceDescriptor  descB = ServiceDescriptor.Singleton <Int>(service => new Int(4));
            ServiceDescriptor  descC = ServiceDescriptor.Singleton <FakeA>();
            ServiceDescriptor  descD = ServiceDescriptor.Singleton <IFake, FakeA>();
            ServiceDescriptor  descE = ServiceDescriptor.Singleton <IFake, FakeB>();
            IServiceCollection pool  = Implementation.CreateServiceCollection();

            Assert.AreEqual(true, pool.Add(descA));
            Assert.AreEqual(false, pool.Add(descA));
            Assert.AreEqual(false, pool.Add(descB));
            Assert.AreEqual(true, pool.Add(descC));
            Assert.AreEqual(true, pool.Add(descD));
            Assert.AreEqual(false, pool.Add(descE));
            Assert.AreEqual(3, pool.GetDescriptors().Count());

            Assert.AreEqual(false, pool.Remove(descB));
            Assert.AreEqual(true, pool.Remove(descA));
        }
        public void CreateObjectsTest()
        {
            IServiceCollection   pool     = Implementation.CreateServiceCollection();
            IServiceScopeFactory factory  = Implementation.CreateServiceScopeFactory(pool);
            IServiceProvider     services = factory.CreateScope().ServiceProvider;

            try
            {
                object staticobj = services.CreateInstance(typeof(StaticObject), 10) as StaticObject;
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                IFake fakeobj = services.CreateInstance <IFake>();
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                AbstractObject absobj = services.CreateInstance <AbstractObject>();
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                PrivateObject privateobj = services.CreateInstance <PrivateObject>(5);
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                EnumTest enumobj = services.CreateInstance <EnumTest>();
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            try
            {
                DelegateTest delegateobj = services.CreateInstance <DelegateTest>();
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            GenericObject <int> intgeneric = services.CreateInstance <GenericObject <int> >(10);

            Assert.AreEqual(10, intgeneric.Value);

            GenericObject <double> doublegeneric = services.CreateInstance <GenericObject <double> >();

            Assert.AreEqual(0, doublegeneric.Value);

            try
            {
                int[] intarray = services.CreateInstance <int[]>(10);
                Assert.Fail();
            }
            catch (InvalidOperationException)
            {
            }

            int inttest = services.CreateInstance <int>(5);

            Assert.AreEqual(5, inttest);

            StructObject structobj = services.CreateInstance <StructObject>(5);

            Assert.AreEqual(5, structobj.Value);
        }