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