Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WithArguments when used with IFactory bindings including Components #197

Merged
merged 2 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Foo>()
.FromNewComponentOnNewGameObject()
.WithArguments(ArgumentValue);

AddFactoryUser<Foo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}

[UnityTest]
public IEnumerator TestFromNewComponentOnNewGameObjectConcrete()
{
PreInstall();
Container.BindIFactory<IFoo>()
.To<Foo>()
.FromNewComponentOnNewGameObject()
.WithArguments(ArgumentValue);

AddFactoryUser<IFoo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
yield break;
}

[UnityTest]
public IEnumerator TestFromComponentInNewPrefabSelf()
{
PreInstall();
Container.BindIFactory<Foo>()
.FromComponentInNewPrefab(FooPrefab)
.WithGameObjectName("asdf")
.WithArguments(ArgumentValue);

AddFactoryUser<Foo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}

[UnityTest]
public IEnumerator TestFromComponentInNewPrefabConcrete()
{
PreInstall();
Container.BindIFactory<IFoo>()
.To<Foo>()
.FromComponentInNewPrefab(FooPrefab)
.WithGameObjectName("asdf")
.WithArguments(ArgumentValue);

AddFactoryUser<IFoo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}

[UnityTest]
public IEnumerator TestFromComponentInNewPrefabResourceSelf()
{
PreInstall();
Container.BindIFactory<Foo>()
.FromComponentInNewPrefabResource("TestBindFactoryOne/Foo")
.WithGameObjectName("asdf")
.WithArguments(ArgumentValue);

AddFactoryUser<Foo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(1);
FixtureUtil.AssertNumGameObjects(1);
FixtureUtil.AssertNumGameObjectsWithName("asdf", 1);
yield break;
}

[UnityTest]
public IEnumerator TestFromComponentInNewPrefabResourceConcrete()
{
PreInstall();
Container.BindIFactory<IFoo>().To<Foo>()
.FromComponentInNewPrefabResource("TestBindFactoryOne/Foo")
.WithGameObjectName("asdf")
.WithArguments(ArgumentValue);

AddFactoryUser<IFoo>();

PostInstall();

FixtureUtil.AssertComponentCount<Foo>(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<TValue>()
where TValue : IFoo
{
Container.Bind<IInitializable>()
.To<FooFactoryTester<TValue>>().AsSingle();

Container.BindExecutionOrder<FooFactoryTester<TValue>>(-100);
}

public class FooFactoryTester<TValue> : IInitializable
where TValue : IFoo
{
readonly IFactory<TValue> _factory;

public FooFactoryTester(IFactory<TValue> factory)
{
_factory = factory;
}

public void Initialize()
{
Assert.IsEqual(_factory.Create().Value, ArgumentValue);

Log.Info("Factory created foo successfully");
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public ConditionCopyNonLazyBinder FromNewComponentOn(GameObject gameObject)
ProviderFunc =
(container) => new AddToExistingGameObjectComponentProvider(
gameObject, container, ContractType,
new List<TypeValuePair>(), BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);
BindInfo.Arguments, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);

return this;
}
Expand All @@ -161,7 +161,7 @@ public ConditionCopyNonLazyBinder FromNewComponentOn(
ProviderFunc =
(container) => new AddToExistingGameObjectComponentProviderGetter(
gameObjectGetter, container, ContractType,
new List<TypeValuePair>(), BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);
BindInfo.Arguments, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);

return this;
}
Expand All @@ -176,7 +176,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen
ProviderFunc =
(container) => new AddToNewGameObjectComponentProvider(
container, ContractType,
new List<TypeValuePair>(), gameObjectInfo, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);
BindInfo.Arguments, gameObjectInfo, BindInfo.ConcreteIdentifier, BindInfo.InstantiatedCallback);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
}
Expand All @@ -194,7 +194,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen
ContractType,
new PrefabInstantiator(
container, gameObjectInfo,
ContractType, new [] { ContractType }, new List<TypeValuePair>(),
ContractType, new [] { ContractType }, BindInfo.Arguments,
new PrefabProvider(prefab), BindInfo.InstantiatedCallback));

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
Expand All @@ -212,7 +212,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentIn
ContractType,
new PrefabInstantiator(
container, gameObjectInfo,
ContractType, new [] { ContractType }, new List<TypeValuePair>(),
ContractType, new [] { ContractType }, BindInfo.Arguments,
new PrefabProvider(prefab),
BindInfo.InstantiatedCallback), true);

Expand All @@ -231,7 +231,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromComponentIn
ContractType,
new PrefabInstantiator(
container, gameObjectInfo,
ContractType, new [] { ContractType }, new List<TypeValuePair>(),
ContractType, new [] { ContractType }, BindInfo.Arguments,
new PrefabProviderResource(resourcePath), BindInfo.InstantiatedCallback), true);

return new NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder(BindInfo, gameObjectInfo);
Expand All @@ -250,7 +250,7 @@ public NameTransformScopeConcreteIdArgConditionCopyNonLazyBinder FromNewComponen
ContractType,
new PrefabInstantiator(
container, gameObjectInfo,
ContractType, new [] { ContractType }, new List<TypeValuePair>(),
ContractType, new [] { ContractType }, BindInfo.Arguments,
new PrefabProviderResource(resourcePath),
BindInfo.InstantiatedCallback));

Expand All @@ -264,7 +264,7 @@ public ConditionCopyNonLazyBinder FromNewScriptableObjectResource(string resourc

ProviderFunc =
(container) => new ScriptableObjectResourceProvider(
resourcePath, ContractType, container, new List<TypeValuePair>(),
resourcePath, ContractType, container, BindInfo.Arguments,
true, null, BindInfo.InstantiatedCallback);

return this;
Expand All @@ -277,7 +277,7 @@ public ConditionCopyNonLazyBinder FromScriptableObjectResource(string resourcePa

ProviderFunc =
(container) => new ScriptableObjectResourceProvider(
resourcePath, ContractType, container, new List<TypeValuePair>(),
resourcePath, ContractType, container, BindInfo.Arguments,
false, null, BindInfo.InstantiatedCallback);

return this;
Expand Down