Exemplo n.º 1
0
        public void TestInNamespace()
        {
            Container.Bind <IFoo>()
            .To(x => x.AllTypes().DerivingFrom <IFoo>().InNamespace("UniDi.Tests.Convention.NamespaceTest")).AsTransient();

            Assert.IsEqual(Container.ResolveAll <IFoo>().Count(), 1);
        }
Exemplo n.º 2
0
        public void TestAttributeWhereFilter()
        {
            Container.Bind <IFoo>()
            .To(x => x.AllTypes().WithAttributeWhere <ConventionTestAttribute>(a => a.Num == 1)).AsTransient();

            Assert.IsEqual(Container.ResolveAll <IFoo>().Count(), 1);
        }
Exemplo n.º 3
0
        public void TestDerivingFromFail()
        {
            Container.Bind <IFoo>()
            .To(x => x.AllTypes().DerivingFrom <IFoo>().FromAssemblyContaining <Vector3>()).AsTransient();

            Assert.That(Container.ResolveAll <IFoo>().IsEmpty());
        }
Exemplo n.º 4
0
        public void TestMatchAll()
        {
            // Should automatically filter by contract types
            Container.Bind <IFoo>().To(x => x.AllNonAbstractClasses()).AsTransient();

            Assert.IsEqual(Container.ResolveAll <IFoo>().Count(), 4);
        }
Exemplo n.º 5
0
        public void TestDerivingFrom2()
        {
            Container.Bind <IFoo>()
            .To(x => x.AllTypes().DerivingFrom <IFoo>()).AsTransient();

            Assert.IsEqual(Container.ResolveAll <IFoo>().Count(), 4);
        }
Exemplo n.º 6
0
        static List <Type> GetDependencies(
            DiContainer container, Type type)
        {
            var dependencies = new List <Type>();

            foreach (var contractType in container.GetDependencyContracts(type))
            {
                List <Type> dependTypes;

                if (contractType.FullName.StartsWith("System.Collections.Generic.List"))
                {
                    var subTypes = contractType.GenericArguments();
                    Assert.IsEqual(subTypes.Length, 1);

                    var subType = subTypes[0];
                    dependTypes = container.ResolveTypeAll(subType);
                }
                else
                {
                    dependTypes = container.ResolveTypeAll(contractType);
                    Assert.That(dependTypes.Count <= 1);
                }

                foreach (var dependType in dependTypes)
                {
                    dependencies.Add(dependType);
                }
            }

            return(dependencies);
        }
Exemplo n.º 7
0
 protected IEnumerator DestroyEverything()
 {
     Assert.That(_hasStartedInstall,
                 "Called DestroyAll but did not call PreInstall (or SkipInstall) in test '{0}'!", TestContext.CurrentContext.Test.Name);
     DestroyEverythingInternal(false);
     // Wait one frame for GC to really destroy everything
     yield return(null);
 }
Exemplo n.º 8
0
        public void Setup()
        {
            Assert.That(Application.isPlaying,
                        "UniDiIntegrationTestFixture is meant to be used for play mode tests only.  Please ensure your test file '{0}' is outside of the editor folder and try again.", GetType());

            UniDiTestUtil.DestroyEverythingExceptTestRunner(true);
            StaticContext.Clear();
        }
Exemplo n.º 9
0
        public void Test3()
        {
            FooFactory.InstanceCount = 0;

            Container.Bind <Foo>().FromIFactory(b => b.To <FooFactory>().AsTransient()).AsCached();

            Container.Resolve <Foo>();
            Container.Resolve <Foo>();
            Container.Resolve <Foo>();

            Assert.IsEqual(FooFactory.InstanceCount, 1);
        }
Exemplo n.º 10
0
        public void Test1()
        {
            FooFactory.InstanceCount = 0;

            Container.Bind <Foo>().FromIFactory(b => b.To <FooFactory>().AsCached());

            Assert.IsEqual(Container.Resolve <Foo>(), StaticFoo);

            Container.Resolve <Foo>();
            Container.Resolve <Foo>();
            Container.Resolve <Foo>();

            Assert.IsEqual(FooFactory.InstanceCount, 1);
        }
Exemplo n.º 11
0
        public void TestOldVersion()
        {
            FooFactory.InstanceCount = 0;

            Container.Bind <Foo>().FromFactory <FooFactory>();

            Assert.IsEqual(Container.Resolve <Foo>(), StaticFoo);

            Container.Resolve <Foo>();
            Container.Resolve <Foo>();
            Container.Resolve <Foo>();

            Assert.IsEqual(FooFactory.InstanceCount, 1);
        }
Exemplo n.º 12
0
        public void TestConcreteSingle()
        {
            FooFactory.InstanceCount = 0;

            Container.Bind <IFoo>().To <Foo>().FromIFactory(b => b.To <FooFactory>().AsCached()).AsSingle().NonLazy();

            Assert.IsEqual(Container.Resolve <IFoo>(), StaticFoo);

            Container.Resolve <IFoo>();
            Container.Resolve <IFoo>();
            Container.Resolve <IFoo>();

            Assert.IsEqual(FooFactory.InstanceCount, 1);
        }
Exemplo n.º 13
0
        public void Test1()
        {
            ReflectionTypeAnalyzer.AddCustomInjectAttribute(typeof(InjectCustomAttribute));

            Container.Bind <Bar>().AsSingle();
            Container.Bind <Foo>().AsSingle();

            var foo = Container.Resolve <Foo>();
            var bar = Container.Resolve <Bar>();

            Assert.IsEqual(foo.BarProperty, bar);
            Assert.IsEqual(foo.BarField, bar);
            Assert.IsEqual(foo.BarMethod, bar);
            Assert.IsEqual(foo.BarParam, bar);
        }
Exemplo n.º 14
0
        public IEnumerator LoadScenes(params string[] sceneNames)
        {
            Assert.That(!_hasLoadedScene, "Attempted to load scene twice!");
            _hasLoadedScene = true;

            // Clean up any leftovers from previous test
            UniDiTestUtil.DestroyEverythingExceptTestRunner(false);

            Assert.That(SceneContainers.IsEmpty());

            for (int i = 0; i < sceneNames.Length; i++)
            {
                var sceneName = sceneNames[i];

                Assert.That(Application.CanStreamedLevelBeLoaded(sceneName),
                            "Cannot load scene '{0}' for test '{1}'.  The scenes used by SceneTestFixture derived classes must be added to the build settings for the test to work",
                            sceneName, GetType());

                Log.Info("Loading scene '{0}' for testing", sceneName);

                var loader = SceneManager.LoadSceneAsync(sceneName, i == 0 ? LoadSceneMode.Single : LoadSceneMode.Additive);

                while (!loader.isDone)
                {
                    yield return(null);
                }

                SceneContext sceneContext = null;

                if (ProjectContext.HasInstance)
                // ProjectContext might be null if scene does not have a scene context
                {
                    var scene = SceneManager.GetSceneByName(sceneName);

                    sceneContext = ProjectContext.Instance.Container.Resolve <SceneContextRegistry>()
                                   .TryGetSceneContextForScene(scene);
                }

                _sceneContainers.Add(sceneContext == null ? null : sceneContext.Container);
            }

            _sceneContainer = _sceneContainers.Where(x => x != null).LastOrDefault();

            if (_sceneContainer != null)
            {
                _sceneContainer.Inject(this);
            }
        }
Exemplo n.º 15
0
        public void TearDown()
        {
            if (TestContext.CurrentContext.Result.Outcome == ResultState.Success)
            {
                Assert.That(_hasStartedInstall,
                            "PreInstall (or SkipInstall) was not called in test '{0}'!", TestContext.CurrentContext.Test.Name);

                Assert.That(_hasEndedInstall,
                            "PostInstall was not called in test '{0}'!", TestContext.CurrentContext.Test.Name);
            }

            DestroyEverythingInternal(true);

            _hasStartedInstall = false;
            _hasEndedInstall   = false;
        }
Exemplo n.º 16
0
        protected void PostInstall()
        {
            Assert.That(_hasStartedInstall,
                        "Called PostInstall but did not call PreInstall in test '{0}'!", TestContext.CurrentContext.Test.Name);

            Assert.That(!_hasEndedInstall, "Called PostInstall twice in test '{0}'!", TestContext.CurrentContext.Test.Name);

            _hasEndedInstall = true;
            _sceneContext.Resolve();

            Container.Inject(this);

            if (!Container.IsValidating)
            {
                // We don't have to do this here but it's kind of convenient
                // We could also remove it and just require that users add a yield after calling
                // and it would have the same effect
                Container.Resolve <MonoKernel>().Initialize();
            }
        }
Exemplo n.º 17
0
        protected void PreInstall()
        {
            Assert.That(!_hasStartedInstall, "Called PreInstall twice in test '{0}'!", TestContext.CurrentContext.Test.Name);
            _hasStartedInstall = true;

            Assert.That(!ProjectContext.HasInstance);

            var shouldValidate = CurrentTestHasAttribute <ValidateOnlyAttribute>();

            ProjectContext.ValidateOnNextRun = shouldValidate;

            Assert.That(_sceneContext == null);

            _sceneContext = SceneContext.Create();
            _sceneContext.Install();

            Assert.That(ProjectContext.HasInstance);

            Assert.IsEqual(shouldValidate, ProjectContext.Instance.Container.IsValidating);
            Assert.IsEqual(shouldValidate, _sceneContext.Container.IsValidating);
        }
Exemplo n.º 18
0
        public void TestMoveIntoSubcontainers()
        {
            FooFactory.InstanceCount = 0;

            Container.Bind <Foo>().FromIFactory(b => b.To <FooFactory>().AsCached()).MoveIntoDirectSubContainers();

            Assert.That(Container.AllContracts.Where(x => x.Type == typeof(IFactory <Foo>)).IsEmpty());

            Assert.IsNull(Container.TryResolve <Foo>());

            var subContainer = Container.CreateSubContainer();

            Assert.IsEqual(subContainer.Resolve <Foo>(), StaticFoo);

            Assert.That(subContainer.AllContracts.Where(x => x.Type == typeof(IFactory <Foo>)).Count() == 1);

            subContainer.Resolve <Foo>();
            subContainer.Resolve <Foo>();
            subContainer.Resolve <Foo>();

            Assert.IsEqual(FooFactory.InstanceCount, 1);
        }