From 80ec70fec534878b239e400bef1961b6483586d6 Mon Sep 17 00:00:00 2001 From: Sakari Bergen Date: Fri, 18 Oct 2019 15:22:29 +0300 Subject: [PATCH 1/2] Fixes WithArguments when used with factories that bind to Unity resources --- .../Binders/Factory/FactoryFromBinderBase.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/UnityProject/Assets/Plugins/Zenject/Source/Binding/Binders/Factory/FactoryFromBinderBase.cs b/UnityProject/Assets/Plugins/Zenject/Source/Binding/Binders/Factory/FactoryFromBinderBase.cs index 6a99753cf..cbad75ed8 100644 --- a/UnityProject/Assets/Plugins/Zenject/Source/Binding/Binders/Factory/FactoryFromBinderBase.cs +++ b/UnityProject/Assets/Plugins/Zenject/Source/Binding/Binders/Factory/FactoryFromBinderBase.cs @@ -147,7 +147,7 @@ public ConditionCopyNonLazyBinder FromNewComponentOn(GameObject gameObject) ProviderFunc = (container) => new AddToExistingGameObjectComponentProvider( gameObject, container, ContractType, - new List(), BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); + BindInfo.Arguments, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); return this; } @@ -161,7 +161,7 @@ public ConditionCopyNonLazyBinder FromNewComponentOn( ProviderFunc = (container) => new AddToExistingGameObjectComponentProviderGetter( gameObjectGetter, container, ContractType, - new List(), BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); + BindInfo.Arguments, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); return this; } @@ -176,7 +176,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen ProviderFunc = (container) => new AddToNewGameObjectComponentProvider( container, ContractType, - new List(), gameObjectInfo, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); + BindInfo.Arguments, gameObjectInfo, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback); return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo); } @@ -194,7 +194,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen ContractType, new PrefabInstantiator( container, gameObjectInfo, - ContractType, new [] { ContractType }, new List(), + ContractType, new [] { ContractType }, BindInfo.Arguments, new PrefabProvider(prefab), BindInfo.InstantiatedCallback)); return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo); @@ -212,7 +212,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentIn ContractType, new PrefabInstantiator( container, gameObjectInfo, - ContractType, new [] { ContractType }, new List(), + ContractType, new [] { ContractType }, BindInfo.Arguments, new PrefabProvider(prefab), BindInfo.InstantiatedCallback), true); @@ -231,7 +231,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentIn ContractType, new PrefabInstantiator( container, gameObjectInfo, - ContractType, new [] { ContractType }, new List(), + ContractType, new [] { ContractType }, BindInfo.Arguments, new PrefabProviderResource(resourcePath), BindInfo.InstantiatedCallback), true); return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo); @@ -250,7 +250,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen ContractType, new PrefabInstantiator( container, gameObjectInfo, - ContractType, new [] { ContractType }, new List(), + ContractType, new [] { ContractType }, BindInfo.Arguments, new PrefabProviderResource(resourcePath), BindInfo.InstantiatedCallback)); @@ -264,7 +264,7 @@ public ConditionCopyNonLazyBinder FromNewScriptableObjectResource(string resourc ProviderFunc = (container) => new ScriptableObjectResourceProvider( - resourcePath, ContractType, container, new List(), + resourcePath, ContractType, container, BindInfo.Arguments, true, null, BindInfo.InstantiatedCallback); return this; @@ -277,7 +277,7 @@ public ConditionCopyNonLazyBinder FromScriptableObjectResource(string resourcePa ProviderFunc = (container) => new ScriptableObjectResourceProvider( - resourcePath, ContractType, container, new List(), + resourcePath, ContractType, container, BindInfo.Arguments, false, null, BindInfo.InstantiatedCallback); return this; From 3f93e3ad662fb0ab792cb667f76b4aed370e2c55 Mon Sep 17 00:00:00 2001 From: Sakari Bergen Date: Fri, 6 Nov 2020 13:31:31 +0200 Subject: [PATCH 2/2] Add tests for failing WithArguments GameObject bindings Cases copied from TestBindFactoryOne, dropped ones that were not applicable --- .../TestBindFactoryOneWithArguments.cs | 163 ++++++++++++++++++ .../TestBindFactoryOneWithArguments.cs.meta | 3 + 2 files changed, 166 insertions(+) create mode 100644 UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs create mode 100644 UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs.meta diff --git a/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs b/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs new file mode 100644 index 000000000..5ba7a6fd2 --- /dev/null +++ b/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs @@ -0,0 +1,163 @@ +using System.Collections; +using ModestTree; +using UnityEngine; +using UnityEngine.TestTools; +using Zenject.Tests.Factories.BindFactoryOne; + +namespace Zenject.Tests.Factories +{ + public class TestBindFactoryOneWithArguments : ZenjectIntegrationTestFixture + { + private const string ArgumentValue = "asdf"; + + GameObject FooPrefab + { + get + { + return FixtureUtil.GetPrefab("TestBindFactoryOne/Foo"); + } + } + + [UnityTest] + public IEnumerator TestFromNewComponentOnNewGameObjectSelf() + { + PreInstall(); + Container.BindIFactory() + .FromNewComponentOnNewGameObject() + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + yield break; + } + + [UnityTest] + public IEnumerator TestFromNewComponentOnNewGameObjectConcrete() + { + PreInstall(); + Container.BindIFactory() + .To() + .FromNewComponentOnNewGameObject() + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + yield break; + } + + [UnityTest] + public IEnumerator TestFromComponentInNewPrefabSelf() + { + PreInstall(); + Container.BindIFactory() + .FromComponentInNewPrefab(FooPrefab) + .WithGameObjectName("asdf") + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + FixtureUtil.AssertNumGameObjectsWithName("asdf", 1); + yield break; + } + + [UnityTest] + public IEnumerator TestFromComponentInNewPrefabConcrete() + { + PreInstall(); + Container.BindIFactory() + .To() + .FromComponentInNewPrefab(FooPrefab) + .WithGameObjectName("asdf") + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + FixtureUtil.AssertNumGameObjectsWithName("asdf", 1); + yield break; + } + + [UnityTest] + public IEnumerator TestFromComponentInNewPrefabResourceSelf() + { + PreInstall(); + Container.BindIFactory() + .FromComponentInNewPrefabResource("TestBindFactoryOne/Foo") + .WithGameObjectName("asdf") + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + FixtureUtil.AssertNumGameObjectsWithName("asdf", 1); + yield break; + } + + [UnityTest] + public IEnumerator TestFromComponentInNewPrefabResourceConcrete() + { + PreInstall(); + Container.BindIFactory().To() + .FromComponentInNewPrefabResource("TestBindFactoryOne/Foo") + .WithGameObjectName("asdf") + .WithArguments(ArgumentValue); + + AddFactoryUser(); + + PostInstall(); + + FixtureUtil.AssertComponentCount(1); + FixtureUtil.AssertNumGameObjects(1); + FixtureUtil.AssertNumGameObjectsWithName("asdf", 1); + yield break; + } + + // Note that unlike the TestBindFactory tests, WithArguments still doesn't work nicely with subcontainers... + + void AddFactoryUser() + where TValue : IFoo + { + Container.Bind() + .To>().AsSingle(); + + Container.BindExecutionOrder>(-100); + } + + public class FooFactoryTester : IInitializable + where TValue : IFoo + { + readonly IFactory _factory; + + public FooFactoryTester(IFactory factory) + { + _factory = factory; + } + + public void Initialize() + { + Assert.IsEqual(_factory.Create().Value, ArgumentValue); + + Log.Info("Factory created foo successfully"); + } + } + } +} + diff --git a/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs.meta b/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs.meta new file mode 100644 index 000000000..3ad755a56 --- /dev/null +++ b/UnityProject/Assets/Plugins/Zenject/OptionalExtras/IntegrationTests/Tests/Factories/TestBindFactoryOne/TestBindFactoryOneWithArguments.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 94b7f15ee7704375a72da83287e0a7b1 +timeCreated: 1604660417 \ No newline at end of file