diff --git a/VContainer.StandaloneTests/FactoryTest.cs b/VContainer.StandaloneTests/FactoryTest.cs deleted file mode 100644 index 241c3a16..00000000 --- a/VContainer.StandaloneTests/FactoryTest.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using NUnit.Framework; - -namespace VContainer.Tests -{ - class Foo - { - public int Param1 { get; set; } - public int Param2 { get; set; } - public int Param3 { get; set; } - public int Param4 { get; set; } - public I2 Service2 { get; set; } - public I3 Service3 { get; set; } - } - - [TestFixture] - public class FactoryTest - { - [Test] - public void RegisterFactoryWithParams() - { - var builder = new ContainerBuilder(); - - builder.RegisterFactory(() => new Foo()); - builder.RegisterFactory(param1 => new Foo { Param1 = param1}); - builder.RegisterFactory((param1, param2) => new Foo - { - Param1 = param1, - Param2 = param2 - }); - builder.RegisterFactory((param1, param2, param3) => new Foo - { - Param1 = param1, - Param2 = param2, - Param3 = param3, - }); - builder.RegisterFactory((param1, param2, param3, param4) => new Foo - { - Param1 = param1, - Param2 = param2, - Param3 = param3, - Param4 = param4, - }); - - var container = builder.Build(); - - var func0 = container.Resolve>(); - Assert.That(func0(), Is.TypeOf()); - - var func1 = container.Resolve>(); - var foo1 = func1(100); - Assert.That(func1, Is.TypeOf>()); - Assert.That(foo1.Param1, Is.EqualTo(100)); - - var func2 = container.Resolve>(); - var foo2 = func2(100, 200); - Assert.That(func2, Is.TypeOf>()); - Assert.That(foo2.Param1, Is.EqualTo(100)); - Assert.That(foo2.Param2, Is.EqualTo(200)); - - var func3 = container.Resolve>(); - var foo3 = func3(100, 200, 300); - Assert.That(func3, Is.TypeOf>()); - Assert.That(foo3.Param1, Is.EqualTo(100)); - Assert.That(foo3.Param2, Is.EqualTo(200)); - Assert.That(foo3.Param3, Is.EqualTo(300)); - - var func4 = container.Resolve>(); - var foo4 = func4(100, 200, 300, 400); - Assert.That(func4, Is.TypeOf>()); - Assert.That(foo4.Param1, Is.EqualTo(100)); - Assert.That(foo4.Param2, Is.EqualTo(200)); - Assert.That(foo4.Param3, Is.EqualTo(300)); - Assert.That(foo4.Param4, Is.EqualTo(400)); - } - - [Test] - public void RegisterFactoryWithContainerTransient() - { - var builder = new ContainerBuilder(); - - builder.Register(Lifetime.Transient); - - builder.RegisterFactory(container => - { - return () => new Foo { Service2 = container.Resolve() }; - }, Lifetime.Transient); - - var container = builder.Build(); - var func0 = container.Resolve>(); - var foo0 = func0(); - Assert.That(func0, Is.TypeOf>()); - Assert.That(foo0, Is.TypeOf()); - Assert.That(foo0.Service2, Is.InstanceOf<>()); - } - } -} \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Runtime/Internal/TypeAnalyzer.cs b/VContainer/Assets/VContainer/Runtime/Internal/TypeAnalyzer.cs index cbef73e2..a507e1fa 100644 --- a/VContainer/Assets/VContainer/Runtime/Internal/TypeAnalyzer.cs +++ b/VContainer/Assets/VContainer/Runtime/Internal/TypeAnalyzer.cs @@ -99,7 +99,8 @@ public static InjectTypeInfo Analyze(Type type) // Constructor, single [Inject] constructor -> single most parameters constuctor var injectConstructorCount = 0; var maxParameters = -1; - foreach (var constructorInfo in typeInfo.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { + foreach (var constructorInfo in typeInfo.GetConstructors(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) + { if (constructorInfo.IsDefined(typeof(InjectAttribute), true)) { if (++injectConstructorCount > 1) diff --git a/VContainer/Assets/VContainer/Tests/FactoryTest.cs b/VContainer/Assets/VContainer/Tests/FactoryTest.cs new file mode 100644 index 00000000..28394394 --- /dev/null +++ b/VContainer/Assets/VContainer/Tests/FactoryTest.cs @@ -0,0 +1,328 @@ +using System; +using NUnit.Framework; + +namespace VContainer.Tests +{ + class Foo + { + public int Param1 { get; set; } + public int Param2 { get; set; } + public int Param3 { get; set; } + public int Param4 { get; set; } + public I2 Service2 { get; set; } + public I3 Service3 { get; set; } + } + + [TestFixture] + public class FactoryTest + { + [Test] + public void RegisterFactoryWithParams() + { + var builder = new ContainerBuilder(); + + builder.RegisterFactory(() => new Foo()); + builder.RegisterFactory(param1 => new Foo { Param1 = param1}); + builder.RegisterFactory((param1, param2) => new Foo + { + Param1 = param1, + Param2 = param2 + }); + builder.RegisterFactory((param1, param2, param3) => new Foo + { + Param1 = param1, + Param2 = param2, + Param3 = param3, + }); + builder.RegisterFactory((param1, param2, param3, param4) => new Foo + { + Param1 = param1, + Param2 = param2, + Param3 = param3, + Param4 = param4, + }); + + var container = builder.Build(); + + var func0 = container.Resolve>(); + Assert.That(func0(), Is.TypeOf()); + + var func1 = container.Resolve>(); + var foo1 = func1(100); + Assert.That(func1, Is.TypeOf>()); + Assert.That(foo1.Param1, Is.EqualTo(100)); + + var func2 = container.Resolve>(); + var foo2 = func2(100, 200); + Assert.That(func2, Is.TypeOf>()); + Assert.That(foo2.Param1, Is.EqualTo(100)); + Assert.That(foo2.Param2, Is.EqualTo(200)); + + var func3 = container.Resolve>(); + var foo3 = func3(100, 200, 300); + Assert.That(func3, Is.TypeOf>()); + Assert.That(foo3.Param1, Is.EqualTo(100)); + Assert.That(foo3.Param2, Is.EqualTo(200)); + Assert.That(foo3.Param3, Is.EqualTo(300)); + + var func4 = container.Resolve>(); + var foo4 = func4(100, 200, 300, 400); + Assert.That(func4, Is.TypeOf>()); + Assert.That(foo4.Param1, Is.EqualTo(100)); + Assert.That(foo4.Param2, Is.EqualTo(200)); + Assert.That(foo4.Param3, Is.EqualTo(300)); + Assert.That(foo4.Param4, Is.EqualTo(400)); + } + + [Test] + public void RegisterFactoryWithContainerTransient() + { + var builder = new ContainerBuilder(); + + builder.Register(Lifetime.Transient); + + builder.RegisterFactory(c0 => + { + var dependency = c0.Resolve(); + return () => new Foo { Service2 = dependency }; + }, Lifetime.Transient); + + builder.RegisterFactory(c1 => + { + var dependency = c1.Resolve(); + return param1 => new Foo + { + Param1 = param1 , + Service2 = dependency + }; + }, Lifetime.Transient); + + builder.RegisterFactory(c2 => + { + var dependency = c2.Resolve(); + return (param1, param2) => new Foo + { + Param1 = param1, + Param2 = param2, + Service2 = dependency + }; + }, Lifetime.Transient); + + builder.RegisterFactory(c3 => + { + var dependency = c3.Resolve(); + return (param1, param2, param3) => new Foo + { + Param1 = param1, + Param2 = param2, + Param3 = param3, + Service2 = dependency + }; + }, Lifetime.Transient); + + builder.RegisterFactory(c4 => + { + var dependency = c4.Resolve(); + return (param1, param2, param3, param4) => new Foo + { + Param1 = param1, + Param2 = param2, + Param3 = param3, + Param4 = param4, + Service2 = dependency + }; + }, Lifetime.Transient); + + var container = builder.Build(); + + var func0A = container.Resolve>(); + var func0B = container.Resolve>(); + var foo0A = func0A(); + var foo0B = func0B(); + + Assert.That(func0A, Is.TypeOf>()); + Assert.That(foo0A, Is.TypeOf()); + Assert.That(foo0A.Service2, Is.InstanceOf()); + Assert.That(foo0A.Service2, Is.Not.EqualTo(foo0B.Service2)); + + var func1A = container.Resolve>(); + var func1B = container.Resolve>(); + var foo1A = func1A(100); + var foo1B = func1B(100); + + Assert.That(func1A, Is.TypeOf>()); + Assert.That(foo1A, Is.TypeOf()); + Assert.That(foo1A.Service2, Is.InstanceOf()); + Assert.That(foo1A.Service2, Is.Not.EqualTo(foo1B.Service2)); + Assert.That(foo1A.Param1, Is.EqualTo(100)); + + var func2A = container.Resolve>(); + var func2B = container.Resolve>(); + var foo2A = func2A(100, 200); + var foo2B = func2B(100, 200); + + Assert.That(func2A, Is.TypeOf>()); + Assert.That(foo2A, Is.TypeOf()); + Assert.That(foo2A.Service2, Is.InstanceOf()); + Assert.That(foo2A.Service2, Is.Not.EqualTo(foo2B.Service2)); + Assert.That(foo2A.Param1, Is.EqualTo(100)); + Assert.That(foo2A.Param2, Is.EqualTo(200)); + + var func3A = container.Resolve>(); + var func3B = container.Resolve>(); + var foo3A = func3A(100, 200, 300); + var foo3B = func3B(100, 200, 300); + + Assert.That(func3A, Is.TypeOf>()); + Assert.That(foo3A, Is.TypeOf()); + Assert.That(foo3A.Service2, Is.InstanceOf()); + Assert.That(foo3A.Service2, Is.Not.EqualTo(foo3B.Service2)); + Assert.That(foo3A.Param1, Is.EqualTo(100)); + Assert.That(foo3A.Param2, Is.EqualTo(200)); + Assert.That(foo3A.Param3, Is.EqualTo(300)); + + var func4A = container.Resolve>(); + var func4B = container.Resolve>(); + var foo4A = func4A(100, 200, 300, 400); + var foo4B = func4B(100, 200, 300, 400); + + Assert.That(func4A, Is.TypeOf>()); + Assert.That(foo4A, Is.TypeOf()); + Assert.That(foo4A.Service2, Is.InstanceOf()); + Assert.That(foo4A.Service2, Is.Not.EqualTo(foo4B.Service2)); + Assert.That(foo4A.Param1, Is.EqualTo(100)); + Assert.That(foo4A.Param2, Is.EqualTo(200)); + Assert.That(foo4A.Param3, Is.EqualTo(300)); + Assert.That(foo4A.Param4, Is.EqualTo(400)); + } + + [Test] + public void RegisterFactoryWithContainerScoped() + { + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + + builder.RegisterFactory(c0 => + { + var dependency = c0.Resolve(); + return () => new Foo { Service2 = dependency }; + }, Lifetime.Scoped); + + builder.RegisterFactory(c1 => + { + var dependency = c1.Resolve(); + return param1 => new Foo { Service2 = dependency, Param1 = param1 }; + }, Lifetime.Scoped); + + builder.RegisterFactory(c2 => + { + var dependency = c2.Resolve(); + return (param1, param2) => new Foo + { + Service2 = dependency, + Param1 = param1, + Param2 = param2 + }; + }, Lifetime.Scoped); + + builder.RegisterFactory(c3 => + { + var dependency = c3.Resolve(); + return (param1, param2) => new Foo + { + Service2 = dependency, + Param1 = param1, + Param2 = param2 + }; + }, Lifetime.Scoped); + + builder.RegisterFactory(c4 => + { + var dependency = c4.Resolve(); + return (param1, param2, param3) => new Foo + { + Service2 = dependency, + Param1 = param1, + Param2 = param2, + Param3 = param3 + }; + }, Lifetime.Scoped); + + builder.RegisterFactory(c5 => + { + var dependency = c5.Resolve(); + return (param1, param2, param3, param4) => new Foo + { + Service2 = dependency, + Param1 = param1, + Param2 = param2, + Param3 = param3, + Param4 = param4 + }; + }, Lifetime.Scoped); + + var container = builder.Build(); + + var func0A = container.Resolve>(); + var func0B = container.Resolve>(); + var foo0A = func0A(); + var foo0B = func0B(); + + Assert.That(func0A, Is.TypeOf>()); + Assert.That(foo0A, Is.TypeOf()); + Assert.That(foo0A.Service2, Is.InstanceOf()); + Assert.That(foo0A.Service2, Is.EqualTo(foo0B.Service2)); + + var func1A = container.Resolve>(); + var func1B = container.Resolve>(); + var foo1A = func1A(100); + var foo1B = func1B(100); + + Assert.That(func1A, Is.TypeOf>()); + Assert.That(foo1A, Is.TypeOf()); + Assert.That(foo1A.Service2, Is.InstanceOf()); + Assert.That(foo1A.Service2, Is.EqualTo(foo1B.Service2)); + Assert.That(foo1A.Param1, Is.EqualTo(100)); + + var func2A = container.Resolve>(); + var func2B = container.Resolve>(); + var foo2A = func2A(100, 200); + var foo2B = func2B(100, 200); + + Assert.That(func2A, Is.TypeOf>()); + Assert.That(foo2A, Is.TypeOf()); + Assert.That(foo2A.Service2, Is.InstanceOf()); + Assert.That(foo2A.Service2, Is.EqualTo(foo2B.Service2)); + Assert.That(foo2A.Param1, Is.EqualTo(100)); + Assert.That(foo2A.Param2, Is.EqualTo(200)); + + var func3A = container.Resolve>(); + var func3B = container.Resolve>(); + var foo3A = func3A(100, 200, 300); + var foo3B = func3B(100, 200, 300); + + Assert.That(func3A, Is.TypeOf>()); + Assert.That(foo3A, Is.TypeOf()); + Assert.That(foo3A.Service2, Is.InstanceOf()); + Assert.That(foo3A.Service2, Is.EqualTo(foo3B.Service2)); + Assert.That(foo3A.Param1, Is.EqualTo(100)); + Assert.That(foo3A.Param2, Is.EqualTo(200)); + Assert.That(foo3A.Param3, Is.EqualTo(300)); + + var func4A = container.Resolve>(); + var func4B = container.Resolve>(); + var foo4A = func4A(100, 200, 300, 400); + var foo4B = func4B(100, 200, 300, 400); + + Assert.That(func4A, Is.TypeOf>()); + Assert.That(foo4A, Is.TypeOf()); + Assert.That(foo4A.Service2, Is.InstanceOf()); + Assert.That(foo4A.Service2, Is.EqualTo(foo4B.Service2)); + Assert.That(foo4A.Param1, Is.EqualTo(100)); + Assert.That(foo4A.Param2, Is.EqualTo(200)); + Assert.That(foo4A.Param3, Is.EqualTo(300)); + Assert.That(foo4A.Param4, Is.EqualTo(400)); + + } + } +} \ No newline at end of file diff --git a/VContainer/Assets/VContainer/Tests/FactoryTest.cs.meta b/VContainer/Assets/VContainer/Tests/FactoryTest.cs.meta new file mode 100644 index 00000000..7ba90dad --- /dev/null +++ b/VContainer/Assets/VContainer/Tests/FactoryTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a1ee44a97e1ac4d81a5d844696f701ac +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VContainer/VContainer.csproj b/VContainer/VContainer.csproj index b94429e6..c07f62b1 100644 --- a/VContainer/VContainer.csproj +++ b/VContainer/VContainer.csproj @@ -64,12 +64,12 @@ - - + + @@ -78,8 +78,8 @@ + -