Beispiel #1
0
        public void TestBaseType()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterType <MyBaseClass, MyClass>(CreateMyClass);

            var a = container.Resolve <MyBaseClass>();

            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(1, ((MyClass)a).InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyBaseClass>();
            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(2, ((MyClass)a).InstanceId);
            Assert.AreEqual(2, _counter);

            a = container.Resolve <MyBaseClass>();
            Assert.IsNotNull(a);
            Assert.IsInstanceOf(typeof(MyClass), a);
            Assert.AreEqual(3, ((MyClass)a).InstanceId);
            Assert.AreEqual(3, _counter);
        }
Beispiel #2
0
        public void TestSingleton()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MainClass>();
            container.RegisterAlias <IFirstInterface, MainClass>();
            container.RegisterAlias <ISecondInterface, MainClass>();

            var firstInterface  = container.Resolve <IFirstInterface>();
            var secondInterface = container.Resolve <ISecondInterface>();
            var mainClass       = container.Resolve <MainClass>();

            Assert.AreSame(mainClass, firstInterface);
            Assert.AreSame(mainClass, secondInterface);
        }
Beispiel #3
0
        public void TestInstance()
        {
            var instance = new MainClass();

            var container = new CryoContainer();

            container.RegisterInstance(instance);

            container.RegisterAlias <IFirstInterface, MainClass>();
            container.RegisterAlias <ISecondInterface, MainClass>();

            var firstInterface  = container.Resolve <IFirstInterface>();
            var secondInterface = container.Resolve <ISecondInterface>();

            Assert.AreSame(instance, firstInterface);
            Assert.AreSame(instance, secondInterface);
        }
    public void OnSetup()
    {
        var container = new CryoContainer();

        container.RegisterInstance(new Mock <IErrorMessageController>().Object);
        container.RegisterSingleton <IPathfindingAlgorithm, AstarAlgorithm>();

        _pathfindingAlgorithm = container.Resolve <IPathfindingAlgorithm>();
    }
        public void SelfReference()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassC>();
            container.Resolve <ClassC>();
        }
Beispiel #6
0
        public void SceneInExternal()
        {
            var container = new CryoContainer {
                OnLifetimeError = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.Scene);
            container.RegisterType <MyInstance>(LifeTime.External);
            container.Resolve <MyInstance>();
        }
Beispiel #7
0
        public void ExternalInGlobal()
        {
            var container = new CryoContainer {
                LifetimeErrorReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.External);
            container.RegisterType <MyInstance>(LifeTime.Global);
            container.Resolve <MyInstance>();
        }
        public void LongChain()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <Class1>();
            container.RegisterSingleton <Class2>();
            container.RegisterSingleton <Class3>();
            container.RegisterSingleton <Class4>();
            container.Resolve <Class2>();
        }
Beispiel #9
0
        public void TestType()
        {
            var container = new CryoContainer();

            container.RegisterType <MyClazz>();

            Assert.IsNull(container.TryResolve <MyClazz>());
            var clazz = container.Resolve <MyClazz>();

            Assert.IsNotNull(clazz);
            Assert.IsNull(container.TryResolve <MyClazz>());
        }
Beispiel #10
0
        public void TestAiasWithName()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MainClass>();
            container.RegisterAlias <IFirstInterface, MainClass>("AliasWithName");

            var firstInterface = container.ResolveByName <IFirstInterface>("AliasWithName");
            var mainClass      = container.Resolve <MainClass>();

            Assert.AreSame(mainClass, firstInterface);

            try
            {
                container.Resolve <IFirstInterface>();
                Assert.Fail("Expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.IFirstInterface");
            }
            catch (ContainerException ex)
            {
                // expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.MainClass
            }
        }
Beispiel #11
0
        public void TestResolveSimilarParams()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithSimilarParams>();

            var val = container.Resolve <ClassWithSimilarParams>(new Param {
                Name = "Two", Value = 2
            }, "Hello, world!", new Param("One", 1));

            Assert.AreEqual(1, val.One);
            Assert.AreEqual(2, val.Two);
            Assert.AreEqual("Hello, world!", val.Str);
        }
        public void TestSimplePropertiesResolve()
        {
            var container = new CryoContainer();

            container.RegisterInstance("private base", "PrivateBase");
            container.RegisterInstance("protected base", "ProtectedBase");
            container.RegisterInstance("public base", "PublicBase");
            container.RegisterType <ClassBase>();


            var a = container.Resolve <ClassBase>();

            a.Check();
        }
Beispiel #13
0
        public void TestSingleton()
        {
            _counter = 0;
            var container = new CryoContainer();

            container.RegisterSingleton <MyClass>(CreateMyClass);

            var a = container.Resolve <MyClass>();

            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyClass>();
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);

            a = container.Resolve <MyClass>();
            Assert.IsNotNull(a);
            Assert.AreEqual(1, a.InstanceId);
            Assert.AreEqual(1, _counter);
        }
Beispiel #14
0
        public void InitializeCalled()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            var a = container.Resolve <ClassA>();

            Assert.IsTrue(a.InitializeCalled);
            Assert.IsTrue(a.ClassB.InitializeCalled);
        }
Beispiel #15
0
        public void TestResolveSimilarParamsFailed2()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithSimilarParams>();

            try
            {
                container.Resolve <ClassWithSimilarParams>(1, "Hello, world!", 2);
                Assert.Fail("Expected exception not happened");
            }
            catch (ContainerException)
            {
            }
        }
Beispiel #16
0
        public void TestResolveFailed()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithParams>();

            try
            {
                container.Resolve <ClassWithParams>();
                Assert.Fail("Expected exception not happened");
            }
            catch (ContainerException)
            {
            }
        }
Beispiel #17
0
        public void RefResolved()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            var a = container.Resolve <ClassA>();

            Assert.IsNotNull(a);
            Assert.IsNotNull(a.ClassB);
            Assert.IsNotNull(a.ClassB.ClassA);
            Assert.AreSame(a, a.ClassB.ClassA);
        }
Beispiel #18
0
        public void TestResolve()
        {
            var container = new CryoContainer();

            container.RegisterType <ClassWithParams>();

            var now    = DateTime.Now;
            var stream = new MemoryStream();

            var val = container.Resolve <ClassWithParams>(stream, now, 3, 1.12f);

            Assert.AreEqual(now, val.DateTimeParam);
            Assert.AreEqual(stream, val.DisposableParam);
            Assert.AreEqual(3, val.IntParam);
            Assert.AreEqual(1.12f, val.FloatParam, 0.000001);
        }
Beispiel #19
0
        public void TestSingleton()
        {
            var container = new CryoContainer();

            container.RegisterSingleton <MyClazz>();

            Assert.IsNull(container.TryResolve <MyClazz>());
            var clazz = container.Resolve <MyClazz>();

            Assert.IsNotNull(clazz);

            var clazz2 = container.TryResolve <MyClazz>();

            Assert.IsNotNull(clazz2);
            Assert.AreSame(clazz, clazz2);
        }
        public void TestDerivedPropertiesResolve()
        {
            var container = new CryoContainer();

            container.RegisterInstance("private base", "PrivateBase");
            container.RegisterInstance("protected base", "ProtectedBase");
            container.RegisterInstance("public base", "PublicBase");

            container.RegisterInstance("private derived", "PrivateDerived");
            container.RegisterInstance("protected derived", "ProtectedDerived");
            container.RegisterInstance("public derived", "PublicDerived");
            container.RegisterType <ClassDerived>();

            var a = container.Resolve <ClassDerived>();

            a.Check();
        }
Beispiel #21
0
        public void SelfReferenceThrow()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <ClassC>();

            try
            {
                container.Resolve <ClassC>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }
Beispiel #22
0
        public void RefSet()
        {
            var container = new CryoContainer {
                OnCircularDependency = Reaction.LogWarning
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            var a = container.Resolve <ClassA>();

            Assert.IsNotNull(a.BackRef);
            Assert.AreSame(a, a.BackRef);

            var b = a.ClassB;

            Assert.IsNotNull(b.BackRef);
            Assert.AreSame(b, b.BackRef);
        }
Beispiel #23
0
        public void SceneInGlobal()
        {
            var container = new CryoContainer {
                OnLifetimeError = Reaction.ThrowException
            };

            container.RegisterSingleton <MySingleton>(LifeTime.Scene);
            container.RegisterType <MyInstance>(LifeTime.Global);

            try
            {
                container.Resolve <MyInstance>();
                Assert.Fail("Exception expected");
            }
            catch (WrongLifetimeException)
            {
                // expected
            }
        }
Beispiel #24
0
        public void ExceptionThrown()
        {
            var container = new CryoContainer {
                OnCircularDependency = Reaction.ThrowException
            };

            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            try
            {
                container.Resolve <ClassA>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }
Beispiel #25
0
        public void LongChainThrow()
        {
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };

            container.RegisterSingleton <Class1>();
            container.RegisterSingleton <Class2>();
            container.RegisterSingleton <Class3>();
            container.RegisterSingleton <Class4>();

            try
            {
                container.Resolve <Class3>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
            }
        }
Beispiel #26
0
        public void ExceptionThrown()
        {
#if UNITY_EDITOR
            LogAssert.ignoreFailingMessages = true;
#endif
            var container = new CryoContainer {
                CircularDependencyReaction = Reaction.ThrowException
            };
            container.RegisterSingleton <ClassA>();
            container.RegisterSingleton <ClassB>();

            try
            {
                container.Resolve <ClassA>();
                Assert.Fail("Exception expected");
            }
            catch (CircularDependencyException)
            {
                // expected
            }
        }