Skip to content

Commit d09bb26

Browse files
committed
added test for #514
1 parent 04a374f commit d09bb26

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace DryIoc.IssuesTests
5+
{
6+
[TestFixture]
7+
public class GHIssue514_Avoid_using_the_Func_arguments_for_recursive_service_first_found_outside_the_Func : ITest
8+
{
9+
public int Run()
10+
{
11+
Test();
12+
return 1;
13+
}
14+
15+
[Test]
16+
public void Test()
17+
{
18+
var container = new Container();
19+
20+
container.RegisterInstance(0);
21+
container.Register<DependencyA>();
22+
container.Register<DependencyB>();
23+
container.Register<Parent>();
24+
25+
var parent = container.Resolve<Parent>();
26+
27+
Assert.AreEqual(0, parent.Dependency.Value); // 0
28+
Assert.AreEqual(23, parent.Dependency.Dependency.Value); // 23
29+
Assert.AreEqual(23, parent.Dependency.Dependency.Parent.Dependency.Value); // 23 --> BUT EXPECTING 0
30+
}
31+
32+
internal class DependencyA
33+
{
34+
public int Value { get; }
35+
public DependencyB Dependency { get; }
36+
37+
public DependencyA(
38+
int value,
39+
Func<int, DependencyB> dependencyB)
40+
{
41+
Value = value;
42+
Dependency = dependencyB(23);
43+
}
44+
}
45+
46+
47+
internal class DependencyB
48+
{
49+
private readonly Lazy<Parent> _parentFactory;
50+
public int Value { get; }
51+
public Parent Parent => _parentFactory.Value;
52+
53+
public DependencyB(
54+
int value,
55+
Lazy<Parent> parentFactory)
56+
{
57+
_parentFactory = parentFactory;
58+
Value = value;
59+
}
60+
}
61+
62+
internal class Parent
63+
{
64+
public DependencyA Dependency { get; }
65+
66+
public Parent(
67+
DependencyA dependencyA) =>
68+
Dependency = dependencyA;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)