Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: third_party/pkg/angular/lib/mock/test_injection.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 part of angular.mock;
2
3 _SpecInjector _currentSpecInjector = null;
4
5 class _SpecInjector {
6 DynamicInjector moduleInjector;
7 DynamicInjector injector;
8 List<Module> modules = [];
9 List<Function> initFns = [];
10
11 _SpecInjector() {
12 var moduleModule = new Module()
13 ..factory(Module, (Injector injector) => addModule(new Module()));
14 moduleInjector = new DynamicInjector(modules: [moduleModule]);
15 }
16
17 addModule(module) {
18 if (injector != null) {
19 throw ["Injector already crated, can not add more modules."];
20 }
21 modules.add(module);
22 return module;
23 }
24
25 module(fnOrModule, [declarationStack]) {
26 try {
27 if (fnOrModule is Function) {
28 var initFn = moduleInjector.invoke(fnOrModule);
29 if (initFn is Function) {
30 initFns.add(initFn);
31 }
32 } else if (fnOrModule is Module) {
33 addModule(fnOrModule);
34 } else {
35 throw 'Unsupported type: $fnOrModule';
36 }
37 } catch (e, s) {
38 throw "$e\n$s\nDECLARED AT:$declarationStack";
39 }
40 }
41
42 inject(Function fn, [declarationStack]) {
43 try {
44 if (injector == null) {
45 injector = new DynamicInjector(modules: modules); // Implicit injection is disabled.
46 initFns.forEach((fn) {
47 injector.invoke(fn);
48 });
49 }
50 injector.invoke(fn);
51 } catch (e, s) {
52 throw "$e\n$s\nDECLARED AT:$declarationStack";
53 }
54 }
55
56 reset() {
57 injector = null;
58 }
59 }
60
61 /**
62 * Allows the injection of instances into a test. See [module] on how to install new
63 * types into injector.
64 *
65 * NOTE: Calling inject creates an injector, which prevents any more calls to [m odule].
66 *
67 * Typical usage:
68 *
69 * test('wrap whole test', inject((TestBed tb) {
70 * tb.compile(...);
71 * });
72 *
73 * test('wrap part of a test', () {
74 * module((Module module) {
75 * module.type(Foo);
76 * });
77 * inject((TestBed tb) {
78 * tb.compile(...);
79 * });
80 * });
81 *
82 */
83 inject(Function fn) {
84 try { throw ''; } catch (e, stack) {
85 if (_currentSpecInjector == null ) {
86 return () => _currentSpecInjector.inject(fn, stack);
87 } else {
88 return _currentSpecInjector.inject(fn, stack);
89 }
90 }
91 }
92
93 /**
94 * Allows the installation of new types/modules into the current test injector.
95 *
96 * This method can be called in declaration or inline in test. The method can be called
97 * repeatedly, as long as [inject] is not called. Invocation of [inject] creates the injector and
98 * hence no more calls to [module] can be made.
99 *
100 * setUp(module((Module model) {
101 * module.type(Foo);
102 * });
103 *
104 * test('foo', () {
105 * module((Module module) {
106 * module.type(Foo);
107 * });
108 * });
109 */
110 module(fnOrModule) {
111 try { throw ''; } catch(e, stack) {
112 if (_currentSpecInjector == null ) {
113 return () => _currentSpecInjector.module(fnOrModule, stack);
114 } else {
115 return _currentSpecInjector.module(fnOrModule, stack);
116 }
117 }
118 }
119
120 /**
121 * Call this method in your test harness [setUp] method to setup the injector.
122 */
123 setUpInjector() {
124 _currentSpecInjector = new _SpecInjector();
125 _currentSpecInjector.module((Module m) {
126 m.install(new AngularModule());
127 m.install(new AngularMockModule());
128 });
129 }
130
131 /**
132 * Call this method in your test harness [tearDown] method to cleanup the inject or.
133 */
134 tearDownInjector() => _currentSpecInjector = null;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698