forked from ssannandeji/Zenject-2019
-
Notifications
You must be signed in to change notification settings - Fork 273
/
TestInjectSources.cs
149 lines (116 loc) · 3.43 KB
/
TestInjectSources.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Injection
{
[TestFixture]
public class TestInjectSources
{
class Test0
{
}
class Test1
{
public Test0 val;
public Test1(
[InjectLocal]
Test0 val)
{
this.val = val;
}
}
class Test2
{
public Test0 val;
public Test2(
[Inject(Source = InjectSources.Parent)]
Test0 val)
{
this.val = val;
}
}
class Test3
{
public Test0 val;
public Test3(
[Inject(Source = InjectSources.AnyParent)]
Test0 val)
{
this.val = val;
}
}
class Test4
{
public Test0 val;
public Test4(
[Inject(Source = InjectSources.Any)]
Test0 val)
{
this.val = val;
}
}
[Test]
public void TestAny()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
rootContainer.Bind<Test0>().AsSingle();
sub1.Bind<Test4>().AsSingle();
Assert.IsNotNull(sub1.Resolve<Test4>());
}
[Test]
public void TestLocal1()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
rootContainer.Bind<Test0>().AsSingle();
sub1.Bind<Test1>().AsSingle();
Assert.Throws(() => sub1.Resolve<Test1>());
}
[Test]
public void TestLocal2()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
sub1.Bind<Test0>().AsSingle();
sub1.Bind<Test1>().AsSingle();
Assert.IsNotNull(sub1.Resolve<Test1>());
}
[Test]
public void TestParent1()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
rootContainer.Bind<Test0>().AsSingle();
sub1.Bind<Test2>().AsSingle();
Assert.IsNotNull(sub1.Resolve<Test2>());
}
[Test]
public void TestParent2()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
var sub2 = sub1.CreateSubContainer();
rootContainer.Bind<Test0>().AsSingle();
sub2.Bind<Test2>().AsSingle();
Assert.Throws(() => sub2.Resolve<Test2>());
}
[Test]
public void TestParent3()
{
var rootContainer = new DiContainer();
rootContainer.Bind<Test0>().AsSingle();
rootContainer.Bind<Test2>().AsSingle();
Assert.Throws(() => rootContainer.Resolve<Test2>());
}
[Test]
public void TestParentAny1()
{
var rootContainer = new DiContainer();
var sub1 = rootContainer.CreateSubContainer();
var sub2 = sub1.CreateSubContainer();
rootContainer.Bind<Test0>().AsSingle();
sub2.Bind<Test3>().AsSingle();
Assert.IsNotNull(sub2.Resolve<Test3>());
}
}
}