using System.Collections; using ModestTree; using UnityEngine.TestTools; using Zenject.Tests.Bindings.FromPrefabResource; namespace Zenject.Tests.Bindings { public class TestFromPrefabResource : ZenjectIntegrationTestFixture { const string PathPrefix = "TestFromPrefabResource/"; [UnityTest] public IEnumerator TestTransientError() { PreInstall(); // Validation should detect that it doesn't exist Container.Bind().FromComponentInNewPrefabResource(PathPrefix + "asdfasdfas").AsTransient().NonLazy(); Assert.Throws(() => PostInstall()); yield break; } [UnityTest] public IEnumerator TestTransient() { PreInstall(); Container.Bind().FromComponentInNewPrefabResource(PathPrefix + "Foo").AsTransient().NonLazy(); Container.Bind().FromComponentInNewPrefabResource(PathPrefix + "Foo").AsTransient().NonLazy(); PostInstall(); FixtureUtil.AssertComponentCount(2); yield break; } [UnityTest] public IEnumerator TestSingle() { PreInstall(); Container.Bind(typeof(Foo), typeof(IFoo)).To().FromComponentInNewPrefabResource(PathPrefix + "Foo").AsSingle().NonLazy(); PostInstall(); FixtureUtil.AssertComponentCount(1); yield break; } [UnityTest] public IEnumerator TestCached1() { PreInstall(); Container.Bind(typeof(Foo), typeof(Bar)).FromComponentInNewPrefabResource(PathPrefix + "Foo") .WithGameObjectName("Foo").AsSingle().NonLazy(); PostInstall(); FixtureUtil.AssertNumGameObjects(1); FixtureUtil.AssertComponentCount(1); FixtureUtil.AssertComponentCount(1); FixtureUtil.AssertNumGameObjectsWithName("Foo", 1); yield break; } [UnityTest] public IEnumerator TestWithArgumentsFail() { PreInstall(); // They have required arguments Container.Bind(typeof(Gorp), typeof(Qux)).FromComponentInNewPrefabResource(PathPrefix + "GorpAndQux").AsSingle().NonLazy(); Assert.Throws(() => PostInstall()); yield break; } [UnityTest] public IEnumerator TestWithArguments() { PreInstall(); Container.Bind(typeof(Gorp)) .FromComponentInNewPrefabResource(PathPrefix + "Gorp").WithGameObjectName("Gorp").AsSingle() .WithArguments("test1").NonLazy(); PostInstall(); FixtureUtil.AssertNumGameObjects(1); FixtureUtil.AssertComponentCount(1); FixtureUtil.AssertNumGameObjectsWithName("Gorp", 1); yield break; } [UnityTest] public IEnumerator TestWithAbstractSearchSingleMatch() { PreInstall(); // There are three components that implement INorf on this prefab Container.Bind().FromComponentInNewPrefabResource(PathPrefix + "Norf").AsCached().NonLazy(); PostInstall(); FixtureUtil.AssertNumGameObjects(1); FixtureUtil.AssertComponentCount(3); FixtureUtil.AssertResolveCount(Container, 1); yield break; } [UnityTest] public IEnumerator TestWithAbstractSearchMultipleMatch() { PreInstall(); // There are three components that implement INorf on this prefab Container.Bind().FromComponentsInNewPrefabResource(PathPrefix + "Norf").AsCached().NonLazy(); PostInstall(); FixtureUtil.AssertNumGameObjects(1); FixtureUtil.AssertComponentCount(3); FixtureUtil.AssertResolveCount(Container, 3); yield break; } [UnityTest] public IEnumerator TestAbstractBindingConcreteSearch() { PreInstall(); // Should ignore the Norf2 component on it Container.Bind().To().FromComponentsInNewPrefabResource(PathPrefix + "Norf").AsCached().NonLazy(); PostInstall(); FixtureUtil.AssertNumGameObjects(1); FixtureUtil.AssertResolveCount(Container, 2); yield break; } [UnityTest] public IEnumerator TestMultipleMatchFailure() { PreInstall(); Container.Bind().FromComponentsInNewPrefabResource(PathPrefix + "Foo").AsSingle().NonLazy(); Assert.Throws(() => PostInstall()); yield break; } [UnityTest] public IEnumerator TestCircularDependencies() { PreInstall(); // Jim and Bob both depend on each other Container.Bind(typeof(Jim), typeof(Bob)).FromComponentInNewPrefabResource(PathPrefix + "JimAndBob").AsSingle().NonLazy(); Container.BindInterfacesTo().AsSingle().NonLazy(); PostInstall(); yield break; } public class JimAndBobRunner : IInitializable { readonly Bob _bob; readonly Jim _jim; public JimAndBobRunner(Jim jim, Bob bob) { _bob = bob; _jim = jim; } public void Initialize() { Assert.IsNotNull(_jim.Bob); Assert.IsNotNull(_bob.Jim); Log.Info("Jim and bob successfully got the other reference"); } } } }