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); }
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); }
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); }
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); }
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>())); }
public void TestTargetConditionSuccess() { Container.Bind <Test2>().AsSingle().NonLazy(); var test2 = Container.Resolve <Test2>(); Assert.That(test2 != null); }
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); }
public void TestCase1() { Container.Bind <Test2>().AsSingle().NonLazy(); var test1 = Container.Resolve <Test2>(); Assert.That(test1.val == null); }
public void TestConstructByFactory() { Container.Bind <Test2>().AsSingle(); var test1 = Container.Instantiate <Test2>(); Assert.That(test1.val == null); }
public void TestFieldOptional() { Container.Bind <Test3>().AsSingle().NonLazy(); var test = Container.Resolve <Test3>(); Assert.That(test.val1 == null); }
public void TestParameterOptional() { Container.Bind <Test5>().AsSingle().NonLazy(); var test = Container.Resolve <Test5>(); Assert.That(test.Val1 == null); }
public void TestPrivateBaseClassPostInject() { Container.Bind <SimpleBase>().To <SimpleDerived>().AsSingle().NonLazy(); var simple = Container.Resolve <SimpleBase>(); Assert.That(simple.WasCalled); }
public void Init() { Assert.That(!HasInitialized); Assert.IsNotNull(test1); Assert.IsNotNull(test0); Assert.IsNotNull(_test2); HasInitialized = true; }
public void TestNameConditionSuccess() { Container.Bind <Test1>().AsSingle().NonLazy(); var test1 = Container.Resolve <Test1>(); Assert.That(test1 != null); }
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); }
public void TestCaseBaseClassPropertyInjection() { Container.Bind <Test0>().AsSingle().NonLazy(); Container.Bind <Test2>().AsSingle().NonLazy(); var test1 = Container.Resolve <Test2>(); Assert.That(test1.GetVal() != null); }
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); }
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); }
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); }
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); }
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>())); }
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); }
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)); }
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)); }
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); }
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>())); }
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); }
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>(); }); }
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); }
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; }