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