From a3bdb067f689da0324e9d76be754236d25b88a80 Mon Sep 17 00:00:00 2001 From: dadhi Date: Fri, 21 Apr 2023 22:04:13 +0200 Subject: [PATCH] adding the tests and make all passing for #565 --- ...ur_when_also_set_to_openResolutionScope.cs | 292 +++++++++++++----- test/DryIoc.TestRunner/Program.cs | 5 +- 2 files changed, 214 insertions(+), 83 deletions(-) diff --git a/test/DryIoc.IssuesTests/GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope.cs b/test/DryIoc.IssuesTests/GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope.cs index e5c212c7..24bab22a 100644 --- a/test/DryIoc.IssuesTests/GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope.cs +++ b/test/DryIoc.IssuesTests/GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using NUnit.Framework; namespace DryIoc.IssuesTests @@ -10,124 +8,258 @@ public class GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_ { public int Run() { - TestScope(); - TestScope2(); - TestScope4(); - return 2; + TestScope_Zero(); + TestScope_One(); + TestScope_Two(); + return 3; } [Test] - public void TestScope() + public void TestScope_Zero() { + /// This Test works and displays normal behaviour var container = new Container(); container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient); container.Register(reuse: Reuse.Scoped); container.Register(Reuse.Singleton); - container.Resolve().GetId(); + var car = container.Resolve(); + + // Get guid id for each class and guid store array + var iCarId = (nameof(ICar), car.GetId()); + var iBarId = (nameof(IBar), car.GetBarId()); + var iFooId = (nameof(IFoo), car.GetFooId()); + var IdArray = car.GetIdArray(); + Assert.Multiple(() => + { + // Assert that each name id pair matches with that stored + // in the first three elements of the array + Assert.That(IdArray[0], Is.EqualTo(iCarId)); + Assert.That(IdArray[1], Is.EqualTo(iBarId)); + Assert.That(IdArray[2], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar and IFoo have been resolved from container in ICar class. + Assert.That(IdArray[3], Is.EqualTo(iCarId)); + Assert.That(IdArray[4], Is.EqualTo(iBarId)); + Assert.That(IdArray[5], Is.EqualTo(iFooId)); + // New Scope is opened in ICar. Check if ICar and IFoo is the Same + // but IBar should be new ID. + Assert.That(IdArray[6], Is.EqualTo(iCarId)); + Assert.That(IdArray[7], !Is.EqualTo(iBarId)); + Assert.That(IdArray[8], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar should still be new ID. but should resolve twice + // and be the same as the id from Array index 7 + Assert.That(IdArray[9], Is.EqualTo(iCarId)); + Assert.That(IdArray[10], !Is.EqualTo(iBarId)); + Assert.That(IdArray[10], Is.EqualTo(IdArray[7])); + Assert.That(IdArray[11], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar Should match the original Ibar Id. + Assert.That(IdArray[12], Is.EqualTo(iCarId)); + Assert.That(IdArray[13], Is.EqualTo(iBarId)); + Assert.That(IdArray[14], Is.EqualTo(iFooId)); + }); } [Test] - public void TestScope2() + public void TestScope_One() { + /// This Test Fails when IBar is registered with openResolutionScope: true. + /// When iBar is Resolved it is resolved with a new instance each time. + /// My expectation is that is is still scoped to which ever scope it was resolved to + /// even though it has opened a new scope internally. var container = new Container(); container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient); - container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Scoped); + container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.ScopedTo()); container.Register(Reuse.Singleton); container.Resolve().GetId(); + var car = container.Resolve(); + // Get guid id for each class and guid store array + var iCarId = (nameof(ICar), car.GetId()); + var iBarId = (nameof(IBar), car.GetBarId()); + var iFooId = (nameof(IFoo), car.GetFooId()); + var IdArray = car.GetIdArray(); + Assert.Multiple(() => + { + // Assert that each name id pair matches with that stored + // in the first three elements of the array + Assert.That(IdArray[0], Is.EqualTo(iCarId)); + Assert.That(IdArray[1], Is.EqualTo(iBarId)); + Assert.That(IdArray[2], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar and IFoo have been resolved from container in ICar class. + // IBar should be the same: Fail!! + Assert.That(IdArray[3], Is.EqualTo(iCarId)); + Assert.That(IdArray[4], Is.EqualTo(iBarId)); + Assert.That(IdArray[5], Is.EqualTo(iFooId)); + // New Scope is opened in ICar. Check if ICar and IFoo is the Same + // but IBar should be new ID. + Assert.That(IdArray[6], Is.EqualTo(iCarId)); + Assert.That(IdArray[7], !Is.EqualTo(iBarId)); + Assert.That(IdArray[8], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar should still be new ID. but should resolve twice + // and be the same as the id from Array index 7 : Fail!! + Assert.That(IdArray[9], Is.EqualTo(iCarId)); + Assert.That(IdArray[10], !Is.EqualTo(iBarId)); + Assert.That(IdArray[10], Is.EqualTo(IdArray[7])); + Assert.That(IdArray[11], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar Should match the original Ibar Id. Fail!! + Assert.That(IdArray[12], Is.EqualTo(iCarId)); + Assert.That(IdArray[13], Is.EqualTo(iBarId)); + Assert.That(IdArray[14], Is.EqualTo(iFooId)); + }); } [Test] - public void TestScope4() + public void TestScope_Two() { + ///This Test Passes when we use wrapper mod to get same behaviour as TestScope_Zero but with openResolutionScope: true. var container = new Container(); + container.Register(typeof(WrapScopeInternally<>), reuse: Reuse.Scoped); container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient); container.Register(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient); - container.Register(made: Made.Of(r => ServiceInfo.Of>(), f => f.SubObject), reuse: Reuse.Transient); + + container.Register(made: Made.Of(r => ServiceInfo.Of>(), f => f.Object()), reuse: Reuse.Transient); container.Register(Reuse.Singleton); - container.Resolve().GetId(); - } - public class WrapScopeInternally - { - public readonly TSubObject SubObject; - public WrapScopeInternally(TSubObject subObject) => SubObject = subObject; + var car = container.Resolve(); + + // Get guid id for each class and guid store array + var iCarId = (nameof(ICar), car.GetId()); + var iBarId = (nameof(IBar), car.GetBarId()); + var iFooId = (nameof(IFoo), car.GetFooId()); + var IdArray = car.GetIdArray(); + Assert.Multiple(() => + { + // Assert that each name id pair matches with that stored + // in the first three elements of the array + Assert.That(IdArray[0], Is.EqualTo(iCarId)); + Assert.That(IdArray[1], Is.EqualTo(iBarId)); + Assert.That(IdArray[2], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar and IFoo have been resolved from container in ICar class. + // IBar should be the same : Success + Assert.That(IdArray[3], Is.EqualTo(iCarId)); + Assert.That(IdArray[4], Is.EqualTo(iBarId)); + Assert.That(IdArray[5], Is.EqualTo(iFooId)); + // New Scope is opened in ICar. Check if ICar and IFoo is the Same + // but IBar should be new ID. :Success + Assert.That(IdArray[6], Is.EqualTo(iCarId)); + Assert.That(IdArray[7], !Is.EqualTo(iBarId)); + Assert.That(IdArray[8], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar should still be new ID. but should resolve twice + // and be the same as the id from Array index 7 : Success + Assert.That(IdArray[9], Is.EqualTo(iCarId)); + Assert.That(IdArray[10], !Is.EqualTo(iBarId)); + Assert.That(IdArray[10], Is.EqualTo(IdArray[7])); + Assert.That(IdArray[11], Is.EqualTo(iFooId)); + // Assert that each name id pair matches with each again. + // IBar Should match the original Ibar Id. : Success + Assert.That(IdArray[12], Is.EqualTo(iCarId)); + Assert.That(IdArray[13], Is.EqualTo(iBarId)); + Assert.That(IdArray[14], Is.EqualTo(iFooId)); + }); } + } - public interface IFoo - { - public Guid GetId(); - } + public interface IInternalWrapScope + { + public TSubObject Object(); + } - public interface IBar - { - public Guid GetId(); - } + public class WrapScopeInternally + { + private readonly TSubObject _subObject; + public WrapScopeInternally(TSubObject subObject) => + _subObject = subObject; + public TSubObject Object() => _subObject; + } - public interface ICar - { - public Guid GetId(); - } + public interface IFoo + { + public Guid GetId(); + } + public interface IBar + { + public Guid GetId(); + public Guid GetFooId(); + } - public class Foo : IFoo - { - private Guid _id; - public Foo(IResolverContext container) - { - _id = Guid.NewGuid(); - } + public interface ICar + { + public Guid GetId(); + public Guid GetBarId(); + public Guid GetFooId(); + public (string, Guid)[] GetIdArray(); + } - public Guid GetId() - { - return _id; - } - } - public class Bar : IBar - { - private readonly IFoo _foo; - private Guid _id; - public Bar(IResolverContext container, IFoo foo) - { - _foo = foo; - _id = Guid.NewGuid(); - Console.WriteLine($"Bar ID :{_id} IN Bar Constructor"); - } + public class Foo : IFoo + { + private readonly Guid _id; + public Foo() => _id = Guid.NewGuid(); + public Guid GetId() => _id; + } - public Guid GetId() - { - Console.WriteLine($"Foo ID :{_foo.GetId()} IN Bar GetId()"); - return _id; - } + public class Bar : IBar + { + private readonly IFoo _foo; + private readonly Guid _id; + public Bar(IFoo foo) + { + _foo = foo; + _id = Guid.NewGuid(); } + public Guid GetId() => _id; + public Guid GetFooId() => _foo.GetId(); + } - public class Car : ICar + public class Car : ICar + { + private readonly Guid _id; + private readonly (string, Guid)[] _guidArray; + private readonly IBar _bar; + public Car(IResolverContext container, IBar bar) { - private readonly IBar _bar; - private Guid _id; - public Car(IResolverContext container, IBar bar) - { - _bar = bar; - _id = Guid.NewGuid(); - - Console.WriteLine($"Car ID 1 :{_id} IN Car (constructor)"); - Console.WriteLine($"Bar ID 1 :{_bar.GetId()} IN Car (constructor object)"); - Console.WriteLine($"Bar ID 2 :{container.Resolve().GetId()} IN Car (resolved object)"); - Console.WriteLine($"Bar ID 3 :{container.Resolve().GetId()} IN Car (resolved object)"); - - using (var scope = container.OpenScope()) - { - Console.WriteLine($"Bar ID 4 :{scope.Resolve().GetId()} IN Car New Scope"); - Console.WriteLine($"Bar ID 5 :{scope.Resolve().GetId()} IN Car New Scope"); - } - } - - public Guid GetId() + _id = Guid.NewGuid(); + _guidArray = new (string, Guid)[15]; + _bar = bar; + /// Store id for each class five times + /// ICar is this class and iFoo is singleton + /// so both should always be the same id + /// IBar is scoped so my expectiation is that is should produce 2 ids + /// One in the main scope of ICar and the second one in the below new scope. + _guidArray[0] = (nameof(ICar), _id); + _guidArray[1] = (nameof(IBar), _bar.GetId()); + _guidArray[2] = (nameof(IFoo), _bar.GetFooId()); + /// 2 + _guidArray[3] = (nameof(ICar), _id); + _guidArray[4] = (nameof(IBar), container.Resolve().GetId()); + _guidArray[5] = (nameof(IFoo), container.Resolve().GetFooId()); + using (var scope = container.OpenScope(ResolutionScopeName.Of())) { - Console.WriteLine($"Car ID 2 :{_id} IN Car GetId()"); - return _id; + //// 3 in new scope. IBar should have new id and be the same instance for each resolution. + _guidArray[6] = (nameof(ICar), _id); + _guidArray[7] = (nameof(IBar), scope.Resolve().GetId()); + _guidArray[8] = (nameof(IFoo), scope.Resolve().GetFooId()); + /// 4 + _guidArray[9] = (nameof(ICar), _id); + _guidArray[10] = (nameof(IBar), scope.Resolve().GetId()); + _guidArray[11] = (nameof(IFoo), scope.Resolve().GetFooId()); } + /// 5 - IBar should be original instance scoped to ICar. + _guidArray[12] = (nameof(ICar), _id); + _guidArray[13] = (nameof(IBar), container.Resolve().GetId()); + _guidArray[14] = (nameof(IFoo), container.Resolve().GetFooId()); } + public Guid GetId() => _id; + public Guid GetBarId() => _bar.GetId(); + public Guid GetFooId() => _bar.GetFooId(); + public (string, Guid)[] GetIdArray() => _guidArray; } } diff --git a/test/DryIoc.TestRunner/Program.cs b/test/DryIoc.TestRunner/Program.cs index 411dd6a0..e575d752 100644 --- a/test/DryIoc.TestRunner/Program.cs +++ b/test/DryIoc.TestRunner/Program.cs @@ -10,15 +10,13 @@ public static void Main() { RunAllTests(); - // todo: @wip - // new GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope().Run(); - // new RequiredPropertiesTests().Run(); // new PropertyResolutionTests().Run(); // new IssuesTests.Samples.DefaultReuseTest().Run(); // new GHIssue391_Deadlock_during_Resolve().Run(); // new GHIssue559_Possible_inconsistent_behaviour().Run(); // new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed().Run(); + // new GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope().Run(); // new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules().Run(); // new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object().Run(); // new GHIssue536_DryIoc_Exception_in_a_Constructor_of_a_Dependency_does_tunnel_through_Resolve_call().Run(); @@ -81,6 +79,7 @@ void Run(Func run, string name = null) new GHIssue546_Generic_type_constraint_resolution_doesnt_see_arrays_as_IEnumerable(), new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object(), new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules(), + new GHIssue565_Is_ignoring_ReuseScoped_setting_expected_behaviour_when_also_set_to_openResolutionScope(), new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed(), new GHIssue559_Possible_inconsistent_behaviour(), };