Skip to content

Commit

Permalink
adding the tests and make all passing for #565
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Apr 21, 2023
1 parent ff330a0 commit a3bdb06
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace DryIoc.IssuesTests
Expand All @@ -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<ICar, Car>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient);
container.Register<IBar, Bar>(reuse: Reuse.Scoped);
container.Register<IFoo, Foo>(Reuse.Singleton);
container.Resolve<ICar>().GetId();
var car = container.Resolve<ICar>();

// 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<ICar, Car>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient);
container.Register<IBar, Bar>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Scoped);
container.Register<IBar, Bar>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.ScopedTo<ICar>());
container.Register<IFoo, Foo>(Reuse.Singleton);
container.Resolve<ICar>().GetId();
var car = container.Resolve<ICar>();
// 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<ICar, Car>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient);
container.Register<Bar>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient);
container.Register<IBar>(made: Made.Of(r => ServiceInfo.Of<WrapScopeInternally<Bar>>(), f => f.SubObject), reuse: Reuse.Transient);

container.Register<IBar>(made: Made.Of(r => ServiceInfo.Of<WrapScopeInternally<Bar>>(), f => f.Object()), reuse: Reuse.Transient);
container.Register<IFoo, Foo>(Reuse.Singleton);
container.Resolve<ICar>().GetId();
}

public class WrapScopeInternally<TSubObject>
{
public readonly TSubObject SubObject;
public WrapScopeInternally(TSubObject subObject) => SubObject = subObject;
var car = container.Resolve<ICar>();

// 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<out TSubObject>
{
public TSubObject Object();
}

public interface IBar
{
public Guid GetId();
}
public class WrapScopeInternally<TSubObject>
{
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<IBar>().GetId()} IN Car (resolved object)");
Console.WriteLine($"Bar ID 3 :{container.Resolve<IBar>().GetId()} IN Car (resolved object)");

using (var scope = container.OpenScope())
{
Console.WriteLine($"Bar ID 4 :{scope.Resolve<IBar>().GetId()} IN Car New Scope");
Console.WriteLine($"Bar ID 5 :{scope.Resolve<IBar>().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<IBar>().GetId());
_guidArray[5] = (nameof(IFoo), container.Resolve<IBar>().GetFooId());
using (var scope = container.OpenScope(ResolutionScopeName.Of<ICar>()))
{
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<IBar>().GetId());
_guidArray[8] = (nameof(IFoo), scope.Resolve<IBar>().GetFooId());
/// 4
_guidArray[9] = (nameof(ICar), _id);
_guidArray[10] = (nameof(IBar), scope.Resolve<IBar>().GetId());
_guidArray[11] = (nameof(IFoo), scope.Resolve<IBar>().GetFooId());
}
/// 5 - IBar should be original instance scoped to ICar.
_guidArray[12] = (nameof(ICar), _id);
_guidArray[13] = (nameof(IBar), container.Resolve<IBar>().GetId());
_guidArray[14] = (nameof(IFoo), container.Resolve<IBar>().GetFooId());
}
public Guid GetId() => _id;
public Guid GetBarId() => _bar.GetId();
public Guid GetFooId() => _bar.GetFooId();
public (string, Guid)[] GetIdArray() => _guidArray;
}
}
5 changes: 2 additions & 3 deletions test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -81,6 +79,7 @@ void Run(Func<int> 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(),
};
Expand Down

0 comments on commit a3bdb06

Please sign in to comment.