コード例 #1
0
        public void TestDecoratorMethod()
        {
            SaveHandler.NumInstances = 0;
            SaveDecorator1.CallCount = 0;

            bool wasCalled = false;

            Container.Bind <ISaveHandler>().To <SaveHandler>().AsSingle();
            Container.Decorate <ISaveHandler>()
            .With <SaveDecorator1>().FromMethod((x, h) =>
            {
                wasCalled = true;
                return(new SaveDecorator1(h));
            });

            CallCounter = 1;
            Assert.That(!wasCalled);
            Assert.IsEqual(SaveHandler.NumInstances, 0);
            Assert.IsEqual(SaveDecorator1.CallCount, 0);

            Container.Resolve <ISaveHandler>().Save();

            Assert.That(wasCalled);
            Assert.IsEqual(SaveHandler.NumInstances, 1);
            Assert.IsEqual(SaveDecorator1.CallCount, 1);
        }
コード例 #2
0
ファイル: TestSubContainers.cs プロジェクト: UniDi/UniDi
        public void TestCase2()
        {
            Test0 test0;
            Test1 test1;

            var subContainer = Container.CreateSubContainer();
            var test0Local   = new Test0();

            subContainer.Bind <Test0>().FromInstance(test0Local);
            subContainer.Bind <Test1>().AsSingle();

            test0 = subContainer.Resolve <Test0>();
            Assert.IsEqual(test0Local, test0);

            test1 = subContainer.Resolve <Test1>();

            Assert.Throws(
                delegate { Container.Resolve <Test0>(); });

            Assert.Throws(
                delegate { Container.Resolve <Test1>(); });

            Container.Bind <Test0>().AsSingle();
            Container.Bind <Test1>().AsSingle();

            Assert.That(Container.Resolve <Test0>() != test0);

            Assert.That(Container.Resolve <Test1>() != test1);
        }
コード例 #3
0
        public void Test1()
        {
            NumInstalls        = 0;
            InitTest.WasRun    = false;
            TickTest.WasRun    = false;
            DisposeTest.WasRun = false;

            var container = new DiContainer();

            container.Bind(typeof(TickableManager), typeof(InitializableManager), typeof(DisposableManager))
            .ToSelf().AsSingle().CopyIntoAllSubContainers();

            // This is how you add ITickables / etc. within sub containers
            container.BindInterfacesAndSelfTo <FooKernel>()
            .FromSubContainerResolve().ByMethod(InstallFoo).AsSingle();

            var tickManager    = container.Resolve <TickableManager>();
            var initManager    = container.Resolve <InitializableManager>();
            var disposeManager = container.Resolve <DisposableManager>();

            Assert.That(!InitTest.WasRun);
            Assert.That(!TickTest.WasRun);
            Assert.That(!DisposeTest.WasRun);

            initManager.Initialize();
            tickManager.Update();
            disposeManager.Dispose();

            Assert.That(InitTest.WasRun);
            Assert.That(TickTest.WasRun);
            Assert.That(DisposeTest.WasRun);
        }
コード例 #4
0
ファイル: TestFrom.cs プロジェクト: UniDi/UniDi
        public void TestMultipleBindingsSingle()
        {
            Container.Bind(typeof(IFoo), typeof(IBar)).To <Foo>().AsSingle().NonLazy();

            Assert.IsEqual(Container.Resolve <IFoo>(), Container.Resolve <IBar>());
            Assert.That(Container.Resolve <IFoo>() is Foo);
        }
コード例 #5
0
ファイル: TestTestUtil.cs プロジェクト: UniDi/UniDi
        public void TestTrue()
        {
            Assert.That(TestListComparer.ContainSameElements(
                            new List <int> {
                1
            },
                            new List <int> {
                1
            }));

            Assert.That(TestListComparer.ContainSameElements(
                            new List <int> {
                1, 2
            },
                            new List <int> {
                2, 1
            }));

            Assert.That(TestListComparer.ContainSameElements(
                            new List <int> {
                1, 2, 3
            },
                            new List <int> {
                3, 2, 1
            }));

            Assert.That(TestListComparer.ContainSameElements(
                            new List <int>(),
                            new List <int>()));
        }
コード例 #6
0
        public void TestTargetConditionSuccess()
        {
            Container.Bind <Test2>().AsSingle().NonLazy();

            var test2 = Container.Resolve <Test2>();

            Assert.That(test2 != null);
        }
コード例 #7
0
ファイル: TestFactoryFrom0.cs プロジェクト: UniDi/UniDi
        public void TestConcreteUntyped()
        {
            Container.BindFactory <IFoo, IFooFactory>().To(typeof(Foo)).NonLazy();

            Assert.IsNotNull(Container.Resolve <IFooFactory>().Create());

            Assert.That(Container.Resolve <IFooFactory>().Create() is Foo);
        }
コード例 #8
0
        public void TestCase1()
        {
            Container.Bind <Test2>().AsSingle().NonLazy();

            var test1 = Container.Resolve <Test2>();

            Assert.That(test1.val == null);
        }
コード例 #9
0
        public void TestConstructByFactory()
        {
            Container.Bind <Test2>().AsSingle();

            var test1 = Container.Instantiate <Test2>();

            Assert.That(test1.val == null);
        }
コード例 #10
0
ファイル: TestTestOptional.cs プロジェクト: UniDi/UniDi
        public void TestFieldOptional()
        {
            Container.Bind <Test3>().AsSingle().NonLazy();

            var test = Container.Resolve <Test3>();

            Assert.That(test.val1 == null);
        }
コード例 #11
0
ファイル: TestTestOptional.cs プロジェクト: UniDi/UniDi
        public void TestParameterOptional()
        {
            Container.Bind <Test5>().AsSingle().NonLazy();

            var test = Container.Resolve <Test5>();

            Assert.That(test.Val1 == null);
        }
コード例 #12
0
ファイル: TestPostInjectCall.cs プロジェクト: UniDi/UniDi
        public void TestPrivateBaseClassPostInject()
        {
            Container.Bind <SimpleBase>().To <SimpleDerived>().AsSingle().NonLazy();

            var simple = Container.Resolve <SimpleBase>();

            Assert.That(simple.WasCalled);
        }
コード例 #13
0
ファイル: TestPostInjectCall.cs プロジェクト: UniDi/UniDi
 public void Init()
 {
     Assert.That(!HasInitialized);
     Assert.IsNotNull(test1);
     Assert.IsNotNull(test0);
     Assert.IsNotNull(_test2);
     HasInitialized = true;
 }
コード例 #14
0
        public void TestNameConditionSuccess()
        {
            Container.Bind <Test1>().AsSingle().NonLazy();

            var test1 = Container.Resolve <Test1>();

            Assert.That(test1 != null);
        }
コード例 #15
0
        public void TestCase6()
        {
            Container.Bind <ITest1>().To <Test2>().AsSingle().NonLazy();
            Container.Bind <Test0>().AsSingle().When(c => c.AllObjectTypes.Where(x => typeof(ITest1).IsAssignableFrom(x)).Any());

            var test1 = Container.Resolve <ITest1>();

            Assert.That(test1 != null);
        }
コード例 #16
0
        public void TestCaseBaseClassPropertyInjection()
        {
            Container.Bind <Test0>().AsSingle().NonLazy();
            Container.Bind <Test2>().AsSingle().NonLazy();

            var test1 = Container.Resolve <Test2>();

            Assert.That(test1.GetVal() != null);
        }
コード例 #17
0
        public void TestCase2()
        {
            Container.Bind <Test1>().AsSingle().NonLazy();
            Container.Bind <Test0>().AsSingle().When(c => c.AllObjectTypes.Contains(typeof(Test1)));

            var test1 = Container.Resolve <Test1>();

            Assert.That(test1 != null);
        }
コード例 #18
0
ファイル: TestResolveMany.cs プロジェクト: UniDi/UniDi
        public void TestCase1()
        {
            Container.Bind <Test0>().To <Test1>().AsSingle();
            Container.Bind <Test0>().To <Test2>().AsSingle();

            List <Test0> many = Container.ResolveAll <Test0>();

            Assert.That(many.Count == 2);
        }
コード例 #19
0
        public void TestBindAllInterfacesSimple()
        {
            var container = new DiContainer();

            container.Bind(x => x.AllInterfaces()).To <Foo>().AsTransient();

            Assert.That(container.Resolve <IFoo>() is Foo);
            Assert.That(container.Resolve <IBar>() is Foo);
        }
コード例 #20
0
        public void TestMultiBindListInjection()
        {
            Container.Bind <Test1>().To <Test2>().AsSingle().NonLazy();
            Container.Bind <Test1>().To <Test3>().AsSingle().NonLazy();
            Container.Bind <TestImpl2>().AsSingle().NonLazy();

            var test = Container.Resolve <TestImpl2>();

            Assert.That(test.tests.Count == 2);
        }
コード例 #21
0
ファイル: TestFromResolve.cs プロジェクト: UniDi/UniDi
        public void TestResolveManyCached()
        {
            Container.Bind <Foo>().AsTransient();
            Container.Bind <Foo>().AsTransient();

            Container.Bind <IFoo>().To <Foo>().FromResolveAll().AsCached();

            Assert.IsEqual(Container.ResolveAll <IFoo>().Count, 2);
            Assert.That(Enumerable.SequenceEqual(Container.ResolveAll <IFoo>(), Container.ResolveAll <IFoo>()));
        }
コード例 #22
0
ファイル: TestRebind.cs プロジェクト: UniDi/UniDi
        public void Run()
        {
            Container.Bind <ITest>().To <Test2>().AsSingle();

            Assert.That(Container.Resolve <ITest>() is Test2);

            Container.Rebind <ITest>().To <Test3>().AsSingle();

            Assert.That(Container.Resolve <ITest>() is Test3);
        }
コード例 #23
0
        public void TestCopyDirect1()
        {
            Container.Bind <Foo>().AsSingle().CopyIntoDirectSubContainers();

            var sub1 = Container.CreateSubContainer();
            var sub2 = sub1.CreateSubContainer();

            Assert.That(Container.HasBindingId(typeof(Foo), null, InjectSources.Local));
            Assert.That(sub1.HasBindingId(typeof(Foo), null, InjectSources.Local));
            Assert.That(!sub2.HasBindingId(typeof(Foo), null, InjectSources.Local));
        }
コード例 #24
0
        public void TestMoveAll()
        {
            Container.Bind <Foo>().AsSingle().MoveIntoAllSubContainers();

            var sub1 = Container.CreateSubContainer();
            var sub2 = sub1.CreateSubContainer();

            Assert.That(!Container.HasBindingId(typeof(Foo), null, InjectSources.Local));
            Assert.That(sub1.HasBindingId(typeof(Foo), null, InjectSources.Local));
            Assert.That(sub2.HasBindingId(typeof(Foo), null, InjectSources.Local));
        }
コード例 #25
0
ファイル: TestPostInjectCall.cs プロジェクト: UniDi/UniDi
        public void TestInheritance()
        {
            Container.Bind <IFoo>().To <FooDerived>().AsSingle().NonLazy();

            var foo = Container.Resolve <IFoo>();

            Assert.That(((FooDerived)foo).WasDerivedCalled);
            Assert.That(((FooBase)foo).WasBaseCalled);
            Assert.That(((FooDerived)foo).WasDerivedCalled2);
            Assert.That(((FooBase)foo).WasBaseCalled2);
        }
コード例 #26
0
ファイル: TestFromResolve.cs プロジェクト: UniDi/UniDi
        public void TestResolveManyCached2()
        {
            Container.Bind <Foo>().AsTransient();
            Container.Bind <Foo>().AsTransient();

            Container.Bind(typeof(IFoo), typeof(IBar)).To <Foo>().FromResolveAll().AsCached();

            Assert.IsEqual(Container.ResolveAll <IBar>().Count, 2);
            Assert.IsEqual(Container.ResolveAll <IFoo>().Count, 2);
            Assert.That(Enumerable.SequenceEqual(Container.ResolveAll <IFoo>().Cast <object>(), Container.ResolveAll <IBar>().Cast <object>()));
        }
コード例 #27
0
ファイル: TestParameters.cs プロジェクト: UniDi/UniDi
        public void TestExtraParametersSameType()
        {
            var test1 = Container.Instantiate <Test1>(new object[] { 5, 10 });

            Assert.That(test1 != null);
            Assert.That(test1.f1 == 5 && test1.f2 == 10);

            var test2 = Container.Instantiate <Test1>(new object[] { 10, 5 });

            Assert.That(test2 != null);
            Assert.That(test2.f1 == 10 && test2.f2 == 5);
        }
コード例 #28
0
ファイル: TestSubContainers.cs プロジェクト: UniDi/UniDi
        public void TestIsRemoved()
        {
            var subContainer = Container.CreateSubContainer();
            var test1        = new Test0();

            subContainer.Bind <Test0>().FromInstance(test1);

            Assert.That(ReferenceEquals(test1, subContainer.Resolve <Test0>()));

            Assert.Throws(
                delegate { Container.Resolve <Test0>(); });
        }
コード例 #29
0
        public void Test()
        {
            Container.Bind <Test1>().AsSingle().NonLazy();
            Container.Bind <Test3>().AsSingle().NonLazy();

            Container.Bind <Test0>().AsSingle().NonLazy();

            var test3 = Container.Resolve <Test3>();

            Assert.That(test3.HasInitialized);
            Assert.IsNotNull(test3.test0);
        }
コード例 #30
0
 public void Init(
     Test0 test0,
     [InjectOptional]
     Test2 test2)
 {
     Assert.That(!HasInitialized);
     Assert.IsNotNull(test1);
     Assert.IsNull(test2);
     Assert.IsNull(this.test0);
     this.test0     = test0;
     HasInitialized = true;
 }