##// END OF EJS Templates
Added test: Optional dependency with child container...
cin -
r158:7e27596a76a8 default
parent child
Show More
@@ -123,7 +123,7 export class ActivationContext<S extends
123 123
124 124 activate<T>(d: Descriptor<S, T>, name: string) {
125 125 if (trace.isLogEnabled())
126 trace.log(`enter ${name} ${d}`);
126 trace.log("enter {0} {1}", name, d);
127 127
128 128 const ctx = this.enter(d, name);
129 129 const v = d.activate(ctx);
@@ -26,6 +26,10 const emptyLifetime: ILifetime = Object.
26 26
27 27 store() {
28 28 // does nothing
29 },
30
31 toString() {
32 return `[object EmptyLifetime]`;
29 33 }
30 34
31 35 });
@@ -42,6 +46,9 const unknownLifetime: ILifetime = Objec
42 46 },
43 47 store() {
44 48 throw new Error("Can't store a value in the unknown lifetime object");
49 },
50 toString() {
51 return `[object UnknownLifetime]`;
45 52 }
46 53 });
47 54
@@ -112,7 +119,7 export class LifetimeManager implements
112 119 return emptyLifetime;
113 120 }
114 121
115 static hierarchyLifetime(): ILifetime {
122 static hierarchyLifetime() {
116 123 let _lifetime = unknownLifetime;
117 124 return {
118 125 initialize(context: ActivationContext<any>) {
@@ -129,11 +136,14 export class LifetimeManager implements
129 136 },
130 137 store(item: any, cleanup?: (item: any) => void) {
131 138 return _lifetime.store(item, cleanup);
139 },
140 toString() {
141 return `[object HierarchyLifetime, has=${this.has()}]`;
132 142 }
133 143 };
134 144 }
135 145
136 static contextLifetime(): ILifetime {
146 static contextLifetime() {
137 147 let _lifetime = unknownLifetime;
138 148 return {
139 149 initialize(context: ActivationContext<any>) {
@@ -149,11 +159,14 export class LifetimeManager implements
149 159 },
150 160 store(item: any) {
151 161 _lifetime.store(item);
162 },
163 toString() {
164 return `[object ContextLifetime, has=${this.has()}]`;
152 165 }
153 166 };
154 167 }
155 168
156 static singletonLifetime(typeId: string): ILifetime {
169 static singletonLifetime(typeId: string) {
157 170 argumentNotEmptyString(typeId, "typeId");
158 171 let pending = false;
159 172 return {
@@ -173,6 +186,9 export class LifetimeManager implements
173 186 store(item: any) {
174 187 singletons[typeId] = item;
175 188 pending = false;
189 },
190 toString() {
191 return `[object SingletonLifetime, has=${this.has()}, typeId=${typeId}]`;
176 192 }
177 193 };
178 194 }
@@ -193,6 +209,9 export class LifetimeManager implements
193 209 },
194 210 store(item: any) {
195 211 _lifetime.store(item);
212 },
213 toString() {
214 return `[object ContainerLifetime, has=${_lifetime.has()}]`
196 215 }
197 216 };
198 217 }
@@ -60,4 +60,7 export class DescriptorImpl<S extends ob
60 60 return instance;
61 61 }
62 62
63 toString() {
64 return `[object DescriptorImpl, lifetime=${this._lifetime}]`;
65 }
63 66 }
@@ -6,6 +6,7 import { ValueDescriptor } from "../di/V
6 6 import { Foo } from "../mock/Foo";
7 7 import { Bar } from "../mock/Bar";
8 8 import { isNull } from "../safe";
9 import { Box } from "../mock/Box";
9 10
10 11 test("Container register/resolve tests", async t => {
11 12 const container = new Container<{
@@ -104,3 +105,21 test("Load configuration from module", a
104 105 t.assert(!isNull(b1._v), "bar.foo should not be null");
105 106
106 107 });
108
109 test("Optional dependency with child container", async t => {
110 const container = new Container<{
111 foo?: Foo;
112 box: Box<Foo>;
113 }>();
114 await container.fluent({
115 box: it => it.factory($ => new Box($("foo")))
116 });
117
118 const child = await container.createChildContainer()
119 .fluent({
120 foo: it => it.factory(() => new Foo())
121 })
122
123 const box = child.resolve("box");
124 t.assert(!isNull(box.getValue()), "'foo' dependency is declared in child container");
125 }); No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now