From c0bcc1b8f56acf3c7055bf26ef71b72ab95229c9 Mon Sep 17 00:00:00 2001 From: Alon Talmi <130899578+AlonTalmi@users.noreply.github.com> Date: Thu, 30 Nov 2023 16:42:29 +0100 Subject: [PATCH] Added Unit tests --- VContainer/Assets/Tests/ContainerTest.cs | 67 ++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/VContainer/Assets/Tests/ContainerTest.cs b/VContainer/Assets/Tests/ContainerTest.cs index ad8247d5..a9320011 100644 --- a/VContainer/Assets/Tests/ContainerTest.cs +++ b/VContainer/Assets/Tests/ContainerTest.cs @@ -328,7 +328,7 @@ public void RegisterFromFunc() Assert.That(resolved, Is.InstanceOf()); Assert.That(resolved.Service2, Is.InstanceOf()); } - + [Test] public void RegisterValueTypeFromFunc() { @@ -442,7 +442,7 @@ public void RegisterWithParameter() var resolved = container.Resolve(); Assert.That(resolved.Service2, Is.EqualTo(paramValue)); } - + { var builder = new ContainerBuilder(); builder.Register(Lifetime.Scoped); @@ -601,8 +601,10 @@ public void OnContainerDisposeCallback() builder.Register(Lifetime.Scoped); builder.Register(Lifetime.Scoped); - builder.RegisterDisposeCallback(resolver => resolvedJustBeforeDispose = resolver.Resolve()); - builder.RegisterDisposeCallback(resolver => resolvedJustBeforeDispose2 = resolver.Resolve()); + builder.RegisterDisposeCallback(resolver => + resolvedJustBeforeDispose = resolver.Resolve()); + builder.RegisterDisposeCallback(resolver => + resolvedJustBeforeDispose2 = resolver.Resolve()); var container = builder.Build(); @@ -614,5 +616,62 @@ public void OnContainerDisposeCallback() Assert.That(resolvedJustBeforeDispose, Is.Not.Null); Assert.That(resolvedJustBeforeDispose2, Is.Not.Null); } + + [Test] + public void TryResolveTransient() + { + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Transient); + + var container = builder.Build(); + + Assert.That(container.TryResolve(out var obj1), Is.True); + Assert.That(container.TryResolve(out var obj2), Is.True); + Assert.That(container.TryResolve(out var obj3), Is.False); + + Assert.That(obj1, Is.TypeOf()); + Assert.That(obj2, Is.TypeOf()); + Assert.That(obj1, Is.Not.EqualTo(obj2)); + Assert.That(obj3, Is.Null); + } + + [Test] + public void TryResolveSingleton() + { + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Singleton); + + var container = builder.Build(); + Assert.That(container.TryResolve(out var obj1), Is.True); + Assert.That(container.TryResolve(out var obj2), Is.True); + Assert.That(container.TryResolve(out var obj3), Is.False); + + Assert.That(obj1, Is.TypeOf()); + Assert.That(obj2, Is.TypeOf()); + Assert.That(obj1, Is.EqualTo(obj2)); + Assert.That(obj3, Is.Null); + } + + [Test] + public void TryResolveScoped() + { + var builder = new ContainerBuilder(); + builder.Register(Lifetime.Scoped); + + var container = builder.Build(); + Assert.That(container.TryResolve(out var obj1), Is.True); + Assert.That(container.TryResolve(out var obj2), Is.True); + Assert.That(container.TryResolve(out var obj3), Is.False); + + Assert.That(obj1, Is.TypeOf()); + Assert.That(obj2, Is.TypeOf()); + Assert.That(obj1, Is.EqualTo(obj2)); + Assert.That(obj3, Is.Null); + + container.Dispose(); + + Assert.That(obj1.Disposed, Is.True); +>>>>>>> 1d6258b (Added Unit tests):VContainer/Assets/VContainer/Tests/ContainerTest.cs + } } }