Initial Commit

This commit is contained in:
2026-03-16 14:38:46 +02:00
commit b8f7327a21
2327 changed files with 253610 additions and 0 deletions
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: af8b779557d36924d820d239ab6ee944
folderAsset: yes
timeCreated: 1476627232
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,162 @@
using System.Collections;
using ModestTree;
using UnityEngine;
using UnityEngine.TestTools;
using Zenject.Tests.Factories.PrefabFactory;
namespace Zenject.Tests.Factories
{
public class TestPrefabFactory : ZenjectIntegrationTestFixture
{
string FooPrefabResourcePath
{
get { return "TestPrefabFactory/Foo"; }
}
GameObject FooPrefab
{
get { return FixtureUtil.GetPrefab(FooPrefabResourcePath); }
}
string Foo2PrefabResourcePath
{
get { return "TestPrefabFactory/Foo2"; }
}
GameObject Foo2Prefab
{
get { return FixtureUtil.GetPrefab(Foo2PrefabResourcePath); }
}
[UnityTest]
public IEnumerator Test1()
{
PreInstall();
Container.BindFactory<Object, Foo, Foo.Factory>().FromFactory<PrefabFactory<Foo>>();
Container.Bind<IInitializable>().To<Runner>().AsSingle().WithArguments(FooPrefab);
PostInstall();
yield break;
}
[UnityTest]
public IEnumerator Test2()
{
PreInstall();
Container.BindFactory<Object, string, Foo2, Foo2.Factory>().FromFactory<PrefabFactory<string, Foo2>>();
Container.Bind<IInitializable>().To<Runner2>().AsSingle().WithArguments(Foo2Prefab);
PostInstall();
yield break;
}
[UnityTest]
public IEnumerator TestResource1()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory2>().FromFactory<PrefabResourceFactory<Foo>>();
Container.Bind<IInitializable>().To<Runner3>().AsSingle().WithArguments(FooPrefabResourcePath);
PostInstall();
yield break;
}
[UnityTest]
public IEnumerator TestResource2()
{
PreInstall();
Container.BindFactory<string, string, Foo2, Foo2.Factory2>().FromFactory<PrefabResourceFactory<string, Foo2>>();
Container.Bind<IInitializable>().To<Runner4>().AsSingle().WithArguments(Foo2PrefabResourcePath);
PostInstall();
yield break;
}
public class Runner : IInitializable
{
readonly GameObject _prefab;
readonly Foo.Factory _fooFactory;
public Runner(
Foo.Factory fooFactory,
GameObject prefab)
{
_prefab = prefab;
_fooFactory = fooFactory;
}
public void Initialize()
{
var foo = _fooFactory.Create(_prefab);
Assert.That(foo.WasInitialized);
}
}
public class Runner2 : IInitializable
{
readonly GameObject _prefab;
readonly Foo2.Factory _fooFactory;
public Runner2(
Foo2.Factory fooFactory,
GameObject prefab)
{
_prefab = prefab;
_fooFactory = fooFactory;
}
public void Initialize()
{
var foo = _fooFactory.Create(_prefab, "asdf");
Assert.IsEqual(foo.Value, "asdf");
}
}
public class Runner3 : IInitializable
{
readonly string _prefabPath;
readonly Foo.Factory2 _fooFactory;
public Runner3(
Foo.Factory2 fooFactory,
string prefabPath)
{
_prefabPath = prefabPath;
_fooFactory = fooFactory;
}
public void Initialize()
{
var foo = _fooFactory.Create(_prefabPath);
Assert.That(foo.WasInitialized);
}
}
public class Runner4 : IInitializable
{
readonly string _prefabPath;
readonly Foo2.Factory2 _fooFactory;
public Runner4(
Foo2.Factory2 fooFactory,
string prefabPath)
{
_prefabPath = prefabPath;
_fooFactory = fooFactory;
}
public void Initialize()
{
var foo = _fooFactory.Create(_prefabPath, "asdf");
Assert.IsEqual(foo.Value, "asdf");
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d68637dd8a89bba40b7d4506d09c1bad
timeCreated: 1476628483
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: c021ba356ec385d469d715722ac7c4a7
folderAsset: yes
timeCreated: 1476627237
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,498 @@
using System.Collections;
using ModestTree;
using UnityEngine;
using UnityEngine.TestTools;
using Zenject.Tests.Factories.BindFactory;
namespace Zenject.Tests.Factories
{
public class TestBindFactory : ZenjectIntegrationTestFixture
{
GameObject FooPrefab
{
get { return FixtureUtil.GetPrefab("TestBindFactory/Foo"); }
}
GameObject CameraPrefab
{
get { return FixtureUtil.GetPrefab("TestBindFactory/Camera"); }
}
GameObject FooSubContainerPrefab
{
get { return FixtureUtil.GetPrefab("TestBindFactory/FooSubContainer"); }
}
[UnityTest]
public IEnumerator TestFromNewScriptableObjectResource()
{
PreInstall();
Container.BindFactory<Bar, Bar.Factory>()
.FromNewScriptableObjectResource("TestBindFactory/Bar");
PostInstall();
var factory = Container.Resolve<Bar.Factory>();
var bar = factory.Create();
Assert.IsNotNull(bar);
Assert.IsNotEqual(bar, factory.Create());
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInHierarchy()
{
PreInstall();
var foo = new GameObject().AddComponent<Foo>();
Container.BindFactory<Foo, Foo.Factory>().FromComponentInHierarchy();
PostInstall();
var factory = Container.Resolve<Foo.Factory>();
var foo2 = factory.Create();
Assert.IsNotNull(foo2);
Assert.IsEqual(foo, foo2);
Assert.IsEqual(foo, factory.Create());
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInHierarchyErrors()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().FromComponentInHierarchy();
PostInstall();
var factory = Container.Resolve<Foo.Factory>();
// zero matches
Assert.Throws(() => factory.Create());
new GameObject().AddComponent<Foo>();
factory.Create();
new GameObject().AddComponent<Foo>();
// Multiple is ok too to mirror unity's GetComponentsInChildren behaviour
factory.Create();
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOn()
{
PreInstall();
var go = new GameObject();
Container.BindFactory<Foo, Foo.Factory>().FromNewComponentOn(go);
PostInstall();
var factory = Container.Resolve<Foo.Factory>();
Assert.IsNull(go.GetComponent<Foo>());
var foo = factory.Create();
Assert.IsNotNull(go.GetComponent<Foo>());
Assert.IsEqual(go.GetComponent<Foo>(), foo);
var foo2 = factory.Create();
Assert.IsNotEqual(foo2, foo);
var allFoos = go.GetComponents<Foo>();
Assert.IsEqual(allFoos.Length, 2);
Assert.IsEqual(allFoos[0], foo);
Assert.IsEqual(allFoos[1], foo2);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObject()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().FromNewComponentOnNewGameObject();
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectComponent()
{
PreInstall();
Container.BindFactory<Camera, CameraFactory>().FromNewComponentOnNewGameObject();
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Camera, CameraFactory>(Container);
FixtureUtil.AssertComponentCount<Camera>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectComponentFailure()
{
PreInstall();
Container.BindFactory<string, Camera, CameraFactory2>().FromNewComponentOnNewGameObject();
PostInstall();
Assert.Throws(() => Container.Resolve<CameraFactory2>().Create("asdf"));
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectWithParamsSuccess()
{
PreInstall();
Container.BindFactory<int, Foo2, Foo2.Factory2>().FromNewComponentOnNewGameObject();
PostInstall();
Container.Resolve<Foo2.Factory2>().Create(5);
FixtureUtil.AssertComponentCount<Foo2>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectWithParamsFailure()
{
PreInstall();
Container.BindFactory<Foo2, Foo2.Factory>().FromNewComponentOnNewGameObject();
PostInstall();
Assert.Throws(() => Container.Resolve<Foo2.Factory>().Create());
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectConcrete()
{
PreInstall();
Container.BindFactory<IFoo, IFooFactory>().To<Foo>().FromNewComponentOnNewGameObject();
PostInstall();
FixtureUtil.CallFactoryCreateMethod<IFoo, IFooFactory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnSelf()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<Foo, Foo.Factory>().FromNewComponentOn(gameObject);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnSelfFail()
{
PreInstall();
Assert.Throws(() => Container.BindFactory<Foo2, Foo2.Factory>().FromNewComponentOn((GameObject)null));
PostInstall();
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnConcrete()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<IFoo, IFooFactory>().To<Foo>().FromNewComponentOn(gameObject);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<IFoo, IFooFactory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInNewPrefab()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInPrefabComponent()
{
PreInstall();
Container.BindFactory<Camera, CameraFactory>().FromComponentInNewPrefab(CameraPrefab).WithGameObjectName("asdf");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Camera, CameraFactory>(Container);
FixtureUtil.AssertComponentCount<Camera>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabSelfFail()
{
PreInstall();
// Foo3 is not on the prefab
Container.BindFactory<Foo3, Foo3.Factory>().FromComponentInNewPrefab(FooPrefab);
PostInstall();
Assert.Throws(() => FixtureUtil.CallFactoryCreateMethod<Foo3, Foo3.Factory>(Container));
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabConcrete()
{
PreInstall();
Container.BindFactory<IFoo, IFooFactory>().To<Foo>().FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<IFoo, IFooFactory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToResourceSelf()
{
PreInstall();
Container.BindFactory<Texture, PlaceholderFactory<Texture>>()
.FromResource("TestBindFactory/TestTexture").NonLazy();
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Texture, PlaceholderFactory<Texture>>(Container);
yield break;
}
[UnityTest]
public IEnumerator TestToResource()
{
PreInstall();
Container.BindFactory<Object, PlaceholderFactory<Object>>()
.To<Texture>().FromResource("TestBindFactory/TestTexture").NonLazy();
PostInstall();
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().FromComponentInNewPrefabResource("TestBindFactory/Foo").WithGameObjectName("asdf");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().To<Foo>().FromComponentInNewPrefabResource("TestBindFactory/Foo").WithGameObjectName("asdf");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabSelf()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>().FromSubContainerResolve().ByNewContextPrefab(FooSubContainerPrefab);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabConcrete()
{
PreInstall();
Container.BindFactory<IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefab(FooSubContainerPrefab);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<IFoo, IFooFactory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>()
.FromSubContainerResolve().ByNewContextPrefabResource("TestBindFactory/FooSubContainer");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefabResource("TestBindFactory/FooSubContainer");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<IFoo, IFooFactory>(Container);
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestUnderTransformGroup()
{
PreInstall();
Container.BindFactory<Foo, Foo.Factory>()
.FromNewComponentOnNewGameObject().UnderTransformGroup("Foos");
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
var child = GameObject.Find("Foos").transform.GetChild(0);
Assert.IsNotNull(child.GetComponent<Foo>());
yield break;
}
[UnityTest]
public IEnumerator TestUnderTransform()
{
PreInstall();
var tempGameObject = new GameObject("Foo");
Container.BindFactory<Foo, Foo.Factory>().FromNewComponentOnNewGameObject().
UnderTransform(tempGameObject.transform);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
Assert.IsNotNull(tempGameObject.transform.GetChild(0).GetComponent<Foo>());
yield break;
}
[UnityTest]
public IEnumerator TestUnderTransformGetter()
{
PreInstall();
var tempGameObject = new GameObject("Foo");
Container.BindFactory<Foo, Foo.Factory>().FromNewComponentOnNewGameObject()
.UnderTransform(context => tempGameObject.transform);
PostInstall();
FixtureUtil.CallFactoryCreateMethod<Foo, Foo.Factory>(Container);
Assert.IsNotNull(tempGameObject.transform.GetChild(0).GetComponent<Foo>());
yield break;
}
public class CameraFactory2 : PlaceholderFactory<string, Camera>
{
}
public class CameraFactory : PlaceholderFactory<Camera>
{
}
public class Foo3 : MonoBehaviour
{
public class Factory : PlaceholderFactory<Foo3>
{
}
}
public class Foo2 : MonoBehaviour
{
[Inject]
public int Value
{
get; private set;
}
public class Factory : PlaceholderFactory<Foo2>
{
}
public class Factory2 : PlaceholderFactory<int, Foo2>
{
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 27adf673c597fc3409c6f978c63ab887
timeCreated: 1476628483
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 99205e310884d9d4b9039013bf5eaaee
folderAsset: yes
timeCreated: 1476627243
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,250 @@
using System.Collections;
using ModestTree;
using UnityEngine;
using UnityEngine.TestTools;
using Zenject.Tests.Factories.BindFactoryFive;
namespace Zenject.Tests.Factories
{
public class TestBindFactoryFive : ZenjectIntegrationTestFixture
{
GameObject FooPrefab
{
get
{
return FixtureUtil.GetPrefab("TestBindFactoryFive/Foo");
}
}
GameObject FooSubContainerPrefab
{
get
{
return FixtureUtil.GetPrefab("TestBindFactoryFive/FooSubContainer");
}
}
[UnityTest]
public IEnumerator TestToGameObjectSelf()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>().FromNewComponentOnNewGameObject();
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToGameObjectConcrete()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>().To<Foo>().FromNewComponentOnNewGameObject();
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToMonoBehaviourSelf()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>().FromNewComponentOn(gameObject);
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToMonoBehaviourConcrete()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>().To<Foo>().FromNewComponentOn(gameObject);
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabSelf()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>().FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabConcrete()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>().To<Foo>().FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>().FromComponentInNewPrefabResource("TestBindFactoryFive/Foo").WithGameObjectName("asdf");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>()
.To<Foo>().FromComponentInNewPrefabResource("TestBindFactoryFive/Foo").WithGameObjectName("asdf");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabSelf()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>()
.FromSubContainerResolve().ByNewContextPrefab<FooInstaller>(FooSubContainerPrefab);
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabConcrete()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefab<FooInstaller>(FooSubContainerPrefab);
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, Foo, Foo.Factory>().FromSubContainerResolve().ByNewContextPrefabResource<FooInstaller>("TestBindFactoryFive/FooSubContainer");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestToSubContainerPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<double, int, float, string, char, IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefabResource<FooInstaller>("TestBindFactoryFive/FooSubContainer");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
void AddFactoryUser<TValue, TFactory>()
where TValue : IFoo
where TFactory : PlaceholderFactory<double, int, float, string, char, TValue>
{
Container.Bind<IInitializable>()
.To<FooFactoryTester<TValue, TFactory>>().AsSingle();
Container.BindExecutionOrder<FooFactoryTester<TValue, TFactory>>(-100);
}
public class FooFactoryTester<TValue, TFactory> : IInitializable
where TFactory : PlaceholderFactory<double, int, float, string, char, TValue>
where TValue : IFoo
{
readonly TFactory _factory;
public FooFactoryTester(TFactory factory)
{
_factory = factory;
}
public void Initialize()
{
Assert.IsEqual(_factory.Create(0.15, 0, 2.4f, "zxcv", 'z').Value, "zxcv");
Log.Info("Factory created foo successfully");
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 572457f9104bd1e4b9b08350f69f5041
timeCreated: 1476628483
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 26299dac98af5b84483bf41c4b2897ca
folderAsset: yes
timeCreated: 1476627250
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,298 @@
using System.Collections;
using ModestTree;
using UnityEngine;
using UnityEngine.TestTools;
using Zenject.Tests.Factories.BindFactoryOne;
namespace Zenject.Tests.Factories
{
public class TestBindFactoryOne : ZenjectIntegrationTestFixture
{
GameObject FooPrefab
{
get
{
return FixtureUtil.GetPrefab("TestBindFactoryOne/Foo");
}
}
GameObject FooSubContainerPrefab
{
get
{
return FixtureUtil.GetPrefab("TestBindFactoryOne/FooSubContainer");
}
}
[UnityTest]
public IEnumerator TestFromNewComponentOn()
{
PreInstall();
var go = new GameObject();
Container.BindFactory<string, Foo, Foo.Factory>().FromNewComponentOn(go);
PostInstall();
var factory = Container.Resolve<Foo.Factory>();
Assert.IsNull(go.GetComponent<Foo>());
var foo = factory.Create("asdf");
Assert.IsEqual(foo.Value, "asdf");
Assert.IsNotNull(go.GetComponent<Foo>());
Assert.IsEqual(go.GetComponent<Foo>(), foo);
var foo2 = factory.Create("zxcv");
Assert.IsNotEqual(foo2, foo);
var allFoos = go.GetComponents<Foo>();
Assert.IsEqual(allFoos.Length, 2);
Assert.IsEqual(allFoos[0], foo);
Assert.IsEqual(allFoos[1], foo2);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewScriptableObjectResource()
{
PreInstall();
Container.BindFactory<string, Bar, Bar.Factory>()
.FromNewScriptableObjectResource("TestBindFactoryOne/Bar");
PostInstall();
var factory = Container.Resolve<Bar.Factory>();
var bar = factory.Create("asdf");
Assert.IsNotNull(bar);
Assert.IsEqual(bar.Value, "asdf");
Assert.IsNotEqual(bar, factory.Create("zxcv"));
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectSelf()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory>().FromNewComponentOnNewGameObject();
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectConcrete()
{
PreInstall();
Container.BindFactory<string, IFoo, IFooFactory>().To<Foo>().FromNewComponentOnNewGameObject();
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnSelf()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<string, Foo, Foo.Factory>().FromNewComponentOn(gameObject);
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromNewComponentOnConcrete()
{
PreInstall();
var gameObject = Container.CreateEmptyGameObject("foo");
Container.BindFactory<string, IFoo, IFooFactory>().To<Foo>().FromNewComponentOn(gameObject);
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInNewPrefabSelf()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory>().FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInNewPrefabConcrete()
{
PreInstall();
Container.BindFactory<string, IFoo, IFooFactory>().To<Foo>()
.FromComponentInNewPrefab(FooPrefab).WithGameObjectName("asdf");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInNewPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory>().FromComponentInNewPrefabResource("TestBindFactoryOne/Foo").WithGameObjectName("asdf");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestFromComponentInNewPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<string, IFoo, IFooFactory>().To<Foo>()
.FromComponentInNewPrefabResource("TestBindFactoryOne/Foo").WithGameObjectName("asdf");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}
[UnityTest]
public IEnumerator TestFromSubContainerResolveByNewPrefabSelf()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory>()
.FromSubContainerResolve().ByNewContextPrefab<FooInstaller>(FooSubContainerPrefab);
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromSubContainerResolveByNewPrefabConcrete()
{
PreInstall();
Container.BindFactory<string, IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefab<FooInstaller>(FooSubContainerPrefab);
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromSubContainerResolveByNewPrefabResourceSelf()
{
PreInstall();
Container.BindFactory<string, Foo, Foo.Factory>()
.FromSubContainerResolve().ByNewContextPrefabResource<FooInstaller>("TestBindFactoryOne/FooSubContainer");
AddFactoryUser<Foo, Foo.Factory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
[UnityTest]
public IEnumerator TestFromSubContainerResolveByNewPrefabResourceConcrete()
{
PreInstall();
Container.BindFactory<string, IFoo, IFooFactory>()
.To<Foo>().FromSubContainerResolve().ByNewContextPrefabResource<FooInstaller>("TestBindFactoryOne/FooSubContainer");
AddFactoryUser<IFoo, IFooFactory>();
PostInstall();
FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}
void AddFactoryUser<TValue, TFactory>()
where TValue : IFoo
where TFactory : PlaceholderFactory<string, TValue>
{
Container.Bind<IInitializable>()
.To<FooFactoryTester<TValue, TFactory>>().AsSingle();
Container.BindExecutionOrder<FooFactoryTester<TValue, TFactory>>(-100);
}
public class FooFactoryTester<TValue, TFactory> : IInitializable
where TFactory : PlaceholderFactory<string, TValue>
where TValue : IFoo
{
readonly TFactory _factory;
public FooFactoryTester(TFactory factory)
{
_factory = factory;
}
public void Initialize()
{
Assert.IsEqual(_factory.Create("asdf").Value, "asdf");
Log.Info("Factory created foo successfully");
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 07b8699c6cfb35041acbf91f227fe7d4
timeCreated: 1476628483
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 0cf5d221a15233c4a96f91df1d41379e
folderAsset: yes
timeCreated: 1506454125
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,145 @@
using System.Collections;
using ModestTree;
using UnityEngine.TestTools;
#pragma warning disable 219
namespace Zenject.Tests.Bindings
{
public class TestMemoryPool1 : ZenjectIntegrationTestFixture
{
[UnityTest]
public IEnumerator TestFactoryProperties()
{
PreInstall();
Container.BindMemoryPool<Foo, Foo.Pool>();
PostInstall();
var pool = Container.Resolve<Foo.Pool>();
Assert.IsEqual(pool.NumActive, 0);
Assert.IsEqual(pool.NumTotal, 0);
Assert.IsEqual(pool.NumInactive, 0);
var foo = pool.Spawn("asdf");
Assert.IsEqual(pool.NumActive, 1);
Assert.IsEqual(pool.NumTotal, 1);
Assert.IsEqual(pool.NumInactive, 0);
Assert.IsEqual(foo.ResetCount, 1);
Assert.IsEqual(foo.Value, "asdf");
pool.Despawn(foo);
Assert.IsEqual(pool.NumActive, 0);
Assert.IsEqual(pool.NumTotal, 1);
Assert.IsEqual(pool.NumInactive, 1);
Assert.IsEqual(foo.ResetCount, 1);
foo = pool.Spawn("zxcv");
Assert.IsEqual(pool.NumActive, 1);
Assert.IsEqual(pool.NumTotal, 1);
Assert.IsEqual(pool.NumInactive, 0);
Assert.IsEqual(foo.ResetCount, 2);
Assert.IsEqual(foo.Value, "zxcv");
var foo2 = pool.Spawn("qwer");
Assert.IsEqual(pool.NumActive, 2);
Assert.IsEqual(pool.NumTotal, 2);
Assert.IsEqual(pool.NumInactive, 0);
Assert.IsEqual(foo2.ResetCount, 1);
Assert.IsEqual(foo2.Value, "qwer");
pool.Despawn(foo);
Assert.IsEqual(pool.NumActive, 1);
Assert.IsEqual(pool.NumTotal, 2);
Assert.IsEqual(pool.NumInactive, 1);
Assert.IsEqual(foo.ResetCount, 2);
pool.Despawn(foo2);
Assert.IsEqual(pool.NumActive, 0);
Assert.IsEqual(pool.NumTotal, 2);
Assert.IsEqual(pool.NumInactive, 2);
pool.Spawn("zxcv");
pool.Spawn("bxzc");
pool.Spawn("bxzc");
Assert.IsEqual(pool.NumActive, 3);
Assert.IsEqual(pool.NumTotal, 3);
Assert.IsEqual(pool.NumInactive, 0);
yield break;
}
class Foo
{
public string Value
{
get;
private set;
}
public int ResetCount
{
get; private set;
}
public class Pool : MemoryPool<string, Foo>
{
protected override void Reinitialize(string value, Foo foo)
{
foo.Value = value;
foo.ResetCount++;
}
}
}
[UnityTest]
public IEnumerator TestAbstractMemoryPoolValidate()
{
TestAbstractMemoryPoolInternal();
yield break;
}
[UnityTest]
public IEnumerator TestAbstractMemoryPool()
{
TestAbstractMemoryPoolInternal();
var pool = Container.Resolve<BarPool>();
var foo = pool.Spawn(5);
Assert.IsEqual(foo.GetType(), typeof(Bar));
yield break;
}
void TestAbstractMemoryPoolInternal()
{
PreInstall();
Container.BindMemoryPool<IBar, BarPool>()
.WithInitialSize(3).To<Bar>().NonLazy();
PostInstall();
}
public interface IBar
{
}
public class Bar : IBar
{
}
public class BarPool : MemoryPool<int, IBar>
{
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f50a5f22f2c3e034d9e90b4244a3e412
timeCreated: 1506454125
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: