| OLD | NEW |
| 1 @Injectables(const [ | 1 @Injectables(const [ |
| 2 ClassOne, | 2 ClassOne, |
| 3 CircularA, | 3 CircularA, |
| 4 CircularB, | 4 CircularB, |
| 5 MultipleConstructors, | 5 MultipleConstructors, |
| 6 NumDependency, | 6 NumDependency, |
| 7 IntDependency, | 7 IntDependency, |
| 8 DoubleDependency, | 8 DoubleDependency, |
| 9 BoolDependency, | 9 BoolDependency, |
| 10 StringDependency | 10 StringDependency |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 * generated. For testing purposes not all classes are marked with this | 25 * generated. For testing purposes not all classes are marked with this |
| 26 * annotation, some classes are included in @Injectables at the top. | 26 * annotation, some classes are included in @Injectables at the top. |
| 27 */ | 27 */ |
| 28 class Injectable { | 28 class Injectable { |
| 29 const Injectable(); | 29 const Injectable(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // just some classes for testing | 32 // just some classes for testing |
| 33 @Injectable() | 33 @Injectable() |
| 34 class Engine { | 34 class Engine { |
| 35 final String id = 'v8-id'; | 35 String id = 'v8-id'; |
| 36 } | 36 } |
| 37 | 37 |
| 38 @Injectable() | 38 @Injectable() |
| 39 class MockEngine implements Engine { | 39 class MockEngine implements Engine { |
| 40 final String id = 'mock-id'; | 40 String id = 'mock-id'; |
| 41 } | 41 } |
| 42 | 42 |
| 43 @Injectable() | 43 @Injectable() |
| 44 class MockEngine2 implements Engine { | 44 class MockEngine2 implements Engine { |
| 45 String id = 'mock-id-2'; | 45 String id = 'mock-id-2'; |
| 46 } | 46 } |
| 47 | 47 |
| 48 class HiddenConstructor { | |
| 49 HiddenConstructor._(); | |
| 50 } | |
| 51 | |
| 52 @Injectable() | 48 @Injectable() |
| 53 class Car { | 49 class Car { |
| 54 Engine engine; | 50 Engine engine; |
| 55 Injector injector; | 51 Injector injector; |
| 56 | 52 |
| 57 Car(this.engine, this.injector); | 53 Car(this.engine, this.injector); |
| 58 } | 54 } |
| 59 | 55 |
| 60 class Lemon { | |
| 61 final engine; | |
| 62 final Injector injector; | |
| 63 | |
| 64 Lemon(this.engine, this.injector); | |
| 65 } | |
| 66 | |
| 67 class NumDependency { | 56 class NumDependency { |
| 68 NumDependency(num value) {} | 57 NumDependency(num value) {} |
| 69 } | 58 } |
| 70 | 59 |
| 71 class IntDependency { | 60 class IntDependency { |
| 72 IntDependency(int value) {} | 61 IntDependency(int value) {} |
| 73 } | 62 } |
| 74 | 63 |
| 75 class DoubleDependency { | 64 class DoubleDependency { |
| 76 DoubleDependency(double value) {} | 65 DoubleDependency(double value) {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 93 CircularB(CircularA a) {} | 82 CircularB(CircularA a) {} |
| 94 } | 83 } |
| 95 | 84 |
| 96 typedef int CompareInt(int a, int b); | 85 typedef int CompareInt(int a, int b); |
| 97 | 86 |
| 98 int compareIntAsc(int a, int b) => b.compareTo(a); | 87 int compareIntAsc(int a, int b) => b.compareTo(a); |
| 99 | 88 |
| 100 class WithTypeDefDependency { | 89 class WithTypeDefDependency { |
| 101 CompareInt compare; | 90 CompareInt compare; |
| 102 | 91 |
| 103 WithTypeDefDependency(this.compare); | 92 WithTypeDefDependency(CompareInt c) { |
| 93 compare = c; |
| 94 } |
| 104 } | 95 } |
| 105 | 96 |
| 106 class MultipleConstructors { | 97 class MultipleConstructors { |
| 107 String instantiatedVia; | 98 String instantiatedVia; |
| 108 MultipleConstructors() : instantiatedVia = 'default'; | 99 MultipleConstructors() : instantiatedVia = 'default'; |
| 109 MultipleConstructors.named() : instantiatedVia = 'named'; | 100 MultipleConstructors.named() : instantiatedVia = 'named'; |
| 110 } | 101 } |
| 111 | 102 |
| 112 class InterfaceOne { | 103 class InterfaceOne { |
| 113 } | 104 } |
| 114 | 105 |
| 115 class ClassOne implements InterfaceOne { | 106 class ClassOne implements InterfaceOne { |
| 116 ClassOne(Log log) { | 107 ClassOne(Log log) { |
| 117 log.add('ClassOne'); | 108 log.add('ClassOne'); |
| 118 } | 109 } |
| 119 } | 110 } |
| 120 | 111 |
| 121 @Injectable() | 112 @Injectable() |
| 122 class ParameterizedType<T1, T2> { | |
| 123 ParameterizedType(); | |
| 124 } | |
| 125 | |
| 126 @Injectable() | |
| 127 class ParameterizedDependency { | |
| 128 final ParameterizedType<bool, int> _p; | |
| 129 ParameterizedDependency(this._p); | |
| 130 } | |
| 131 | |
| 132 @Injectable() | |
| 133 class GenericParameterizedDependency { | |
| 134 final ParameterizedType _p; | |
| 135 GenericParameterizedDependency(this._p); | |
| 136 } | |
| 137 | |
| 138 @Injectable() | |
| 139 class Log { | 113 class Log { |
| 140 var log = []; | 114 var log = []; |
| 141 | 115 |
| 142 add(String message) => log.add(message); | 116 add(String message) => log.add(message); |
| 143 } | 117 } |
| 144 | 118 |
| 145 class EmulatedMockEngineFactory { | 119 class EmulatedMockEngineFactory { |
| 146 call(Injector i) => new MockEngine(); | 120 call(Injector i) => new MockEngine(); |
| 147 } | 121 } |
| 148 | 122 |
| 149 void main() { | 123 void main() { |
| 150 createInjectorSpec('DynamicInjector', | 124 createInjectorSpec('DynamicInjector', |
| 151 (modules, [name]) => new DynamicInjector(modules: modules, name: name)); | 125 (modules, [name]) => new DynamicInjector(modules: modules, name: name)); |
| 152 | 126 |
| 127 // Initialize generated type factories. |
| 128 type_factories_gen.main(); |
| 129 |
| 153 createInjectorSpec('StaticInjector', | 130 createInjectorSpec('StaticInjector', |
| 154 (modules, [name]) => new StaticInjector(modules: modules, name: name, | 131 (modules, [name]) => new StaticInjector(modules: modules, name: name, |
| 155 typeFactories: type_factories_gen.typeFactories)); | 132 typeFactories: type_factories_gen.typeFactories)); |
| 156 | |
| 157 dynamicInjectorTest(); | |
| 158 staticInjectorTest(); | |
| 159 } | 133 } |
| 160 | 134 |
| 161 typedef Injector InjectorFactory(List<Module> modules, [String name]); | 135 typedef Injector InjectorFactory(List<Module> modules, [String name]); |
| 162 | 136 |
| 163 createInjectorSpec(String injectorName, InjectorFactory injectorFactory) { | 137 createInjectorSpec(String injectorName, InjectorFactory injectorFactory) { |
| 164 | 138 |
| 165 describe(injectorName, () { | 139 describe(injectorName, () { |
| 166 | 140 |
| 167 it('should instantiate a type', () { | 141 it('should instantiate a type', () { |
| 168 var injector = injectorFactory([new Module()..type(Engine)]); | 142 var injector = injectorFactory([new Module()..type(Engine)]); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 183 | 157 |
| 184 it('should resolve basic dependencies', () { | 158 it('should resolve basic dependencies', () { |
| 185 var injector = injectorFactory([new Module()..type(Car)..type(Engine)]); | 159 var injector = injectorFactory([new Module()..type(Car)..type(Engine)]); |
| 186 var instance = injector.get(Car); | 160 var instance = injector.get(Car); |
| 187 | 161 |
| 188 expect(instance, instanceOf(Car)); | 162 expect(instance, instanceOf(Car)); |
| 189 expect(instance.engine.id, toEqual('v8-id')); | 163 expect(instance.engine.id, toEqual('v8-id')); |
| 190 }); | 164 }); |
| 191 | 165 |
| 192 | 166 |
| 193 it('should inject generic parameterized types', () { | |
| 194 var injector = injectorFactory([new Module() | |
| 195 ..type(ParameterizedType) | |
| 196 ..type(GenericParameterizedDependency) | |
| 197 ]); | |
| 198 expect(injector.get(GenericParameterizedDependency), | |
| 199 new isInstanceOf<GenericParameterizedDependency>()); | |
| 200 }); | |
| 201 | |
| 202 | |
| 203 xit('should error while resolving parameterized types', () { | |
| 204 var injector = injectorFactory([new Module() | |
| 205 ..type(ParameterizedType) | |
| 206 ..type(ParameterizedDependency) | |
| 207 ]); | |
| 208 expect(() => injector.get(ParameterizedDependency), throws); | |
| 209 }); | |
| 210 | |
| 211 | |
| 212 it('should allow modules and overriding providers', () { | 167 it('should allow modules and overriding providers', () { |
| 213 var module = new Module()..type(Engine, implementedBy: MockEngine); | 168 var module = new Module()..type(Engine, implementedBy: MockEngine); |
| 214 | 169 |
| 215 // injector is immutable | 170 // injector is immutable |
| 216 // you can't load more modules once it's instantiated | 171 // you can't load more modules once it's instantiated |
| 217 // (you can create a child injector) | 172 // (you can create a child injector) |
| 218 var injector = injectorFactory([module]); | 173 var injector = injectorFactory([module]); |
| 219 var instance = injector.get(Engine); | 174 var instance = injector.get(Engine); |
| 220 | 175 |
| 221 expect(instance.id, toEqual('mock-id')); | 176 expect(instance.id, toEqual('mock-id')); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 }); | 286 }); |
| 332 | 287 |
| 333 | 288 |
| 334 it('should provide the injector as Injector', () { | 289 it('should provide the injector as Injector', () { |
| 335 var injector = injectorFactory([]); | 290 var injector = injectorFactory([]); |
| 336 | 291 |
| 337 expect(injector.get(Injector), toBe(injector)); | 292 expect(injector.get(Injector), toBe(injector)); |
| 338 }); | 293 }); |
| 339 | 294 |
| 340 | 295 |
| 341 it('should inject a typedef', () { | 296 // Typedef injection is not supported in dart2js: http://dartbug.com/11612 |
| 297 xit('should inject a typedef', () { |
| 342 var module = new Module()..value(CompareInt, compareIntAsc); | 298 var module = new Module()..value(CompareInt, compareIntAsc); |
| 343 | 299 |
| 344 var injector = injectorFactory([module]); | 300 var injector = injectorFactory([module]); |
| 345 var compare = injector.get(CompareInt); | 301 var compare = injector.get(CompareInt); |
| 346 | 302 |
| 347 expect(compare(1, 2), toBe(1)); | 303 expect(compare(1, 2), toBe(1)); |
| 348 expect(compare(5, 2), toBe(-1)); | 304 expect(compare(5, 2), toBe(-1)); |
| 349 }); | 305 }); |
| 350 | 306 |
| 351 | 307 |
| 352 it('should throw an exception when injecting typedef without providing it',
() { | 308 // Typedef injection is not supported in dart2js: http://dartbug.com/11612 |
| 309 xit('should throw an exception when injecting typedef without providing it',
() { |
| 353 var injector = injectorFactory([new Module()..type(WithTypeDefDependency)]
); | 310 var injector = injectorFactory([new Module()..type(WithTypeDefDependency)]
); |
| 354 | 311 |
| 355 expect(() { | 312 expect(() { |
| 356 injector.get(WithTypeDefDependency); | 313 injector.get(WithTypeDefDependency); |
| 357 }, throws); | 314 }, toThrow(NoProviderError, 'No provider found for CompareInt! ' |
| 315 '(resolving WithTypeDefDependency -> CompareInt)')); |
| 358 }); | 316 }); |
| 359 | 317 |
| 360 | 318 |
| 361 it('should instantiate via the default/unnamed constructor', () { | 319 it('should instantiate via the default/unnamed constructor', () { |
| 362 var injector = injectorFactory([new Module()..type(MultipleConstructors)])
; | 320 var injector = injectorFactory([new Module()..type(MultipleConstructors)])
; |
| 363 MultipleConstructors instance = injector.get(MultipleConstructors); | 321 MultipleConstructors instance = injector.get(MultipleConstructors); |
| 364 expect(instance.instantiatedVia, 'default'); | 322 expect(instance.instantiatedVia, 'default'); |
| 365 }); | 323 }); |
| 366 | 324 |
| 367 // CHILD INJECTORS | 325 // CHILD INJECTORS |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 NoProviderError, | 510 NoProviderError, |
| 553 'No provider found for Car! (resolving Car)' | 511 'No provider found for Car! (resolving Car)' |
| 554 )); | 512 )); |
| 555 }); | 513 }); |
| 556 | 514 |
| 557 }); | 515 }); |
| 558 | 516 |
| 559 }); | 517 }); |
| 560 | 518 |
| 561 } | 519 } |
| 562 | |
| 563 void dynamicInjectorTest() { | |
| 564 describe('DynamicInjector', () { | |
| 565 | |
| 566 it('should throw a comprehensible error message on untyped argument', () { | |
| 567 var module = new Module()..type(Lemon)..type(Engine); | |
| 568 var injector = new DynamicInjector(modules : [module]); | |
| 569 | |
| 570 expect(() { | |
| 571 injector.get(Lemon); | |
| 572 }, toThrow(NoProviderError, "The 'engine' parameter must be typed " | |
| 573 "(resolving Lemon)")); | |
| 574 }); | |
| 575 | |
| 576 it('should throw a comprehensible error message when no default constructor
found', () { | |
| 577 var module = new Module()..type(HiddenConstructor); | |
| 578 var injector = new DynamicInjector(modules: [module]); | |
| 579 | |
| 580 expect(() { | |
| 581 injector.get(HiddenConstructor); | |
| 582 }, toThrow(NoProviderError, startsWith('Unable to find default ' | |
| 583 'constructor for HiddenConstructor. Make sure class has a ' | |
| 584 'default constructor.'))); | |
| 585 }); | |
| 586 | |
| 587 }); | |
| 588 } | |
| 589 | |
| 590 void staticInjectorTest() { | |
| 591 describe('StaticInjector', () { | |
| 592 | |
| 593 it('should use type factories passed in the constructor', () { | |
| 594 var module = new Module() | |
| 595 ..type(Engine); | |
| 596 var injector = new StaticInjector(modules: [module], typeFactories: { | |
| 597 Engine: (f) => new Engine() | |
| 598 }); | |
| 599 | |
| 600 var engine; | |
| 601 expect(() { | |
| 602 engine = injector.get(Engine); | |
| 603 }, isNot(throws)); | |
| 604 expect(engine, new isInstanceOf<Engine>()); | |
| 605 }); | |
| 606 | |
| 607 it('should use type factories passes in one module', () { | |
| 608 var module = new Module() | |
| 609 ..type(Engine) | |
| 610 ..typeFactories = { | |
| 611 Engine: (f) => new Engine() | |
| 612 }; | |
| 613 var injector = new StaticInjector(modules: [module]); | |
| 614 | |
| 615 var engine; | |
| 616 expect(() { | |
| 617 engine = injector.get(Engine); | |
| 618 }, isNot(throws)); | |
| 619 expect(engine, new isInstanceOf<Engine>()); | |
| 620 }); | |
| 621 | |
| 622 it('should use type factories passes in many modules', () { | |
| 623 var module1 = new Module() | |
| 624 ..type(Engine) | |
| 625 ..typeFactories = { | |
| 626 Engine: (f) => new Engine() | |
| 627 }; | |
| 628 var module2 = new Module() | |
| 629 ..type(Car) | |
| 630 ..typeFactories = { | |
| 631 Car: (f) => new Car(f(Engine), f(Injector)) | |
| 632 }; | |
| 633 | |
| 634 var injector = new StaticInjector(modules: [module1, module2]); | |
| 635 | |
| 636 var engine; | |
| 637 expect(() { | |
| 638 engine = injector.get(Car); | |
| 639 }, isNot(throws)); | |
| 640 expect(engine, new isInstanceOf<Car>()); | |
| 641 }); | |
| 642 | |
| 643 it('should use type factories passes in hierarchical module', () { | |
| 644 var module = new Module() | |
| 645 ..type(Engine) | |
| 646 ..typeFactories = { | |
| 647 Engine: (f) => new Engine() | |
| 648 }; | |
| 649 | |
| 650 module.install(new Module() | |
| 651 ..type(Car) | |
| 652 ..typeFactories = { | |
| 653 Car: (f) => new Car(f(Engine), f(Injector)) | |
| 654 }); | |
| 655 | |
| 656 var injector = new StaticInjector(modules: [module]); | |
| 657 | |
| 658 var engine; | |
| 659 expect(() { | |
| 660 engine = injector.get(Car); | |
| 661 }, isNot(throws)); | |
| 662 expect(engine, new isInstanceOf<Car>()); | |
| 663 }); | |
| 664 | |
| 665 it('should find type factories from parent injector', () { | |
| 666 var module1 = new Module() | |
| 667 ..type(Engine) | |
| 668 ..typeFactories = { | |
| 669 Engine: (f) => new Engine() | |
| 670 }; | |
| 671 var module2 = new Module() | |
| 672 ..type(Car) | |
| 673 ..typeFactories = { | |
| 674 Car: (f) => new Car(f(Engine), f(Injector)) | |
| 675 }; | |
| 676 | |
| 677 var rootInjector = new StaticInjector(modules: [module1]); | |
| 678 var childInjector = rootInjector.createChild([module2]); | |
| 679 | |
| 680 expect(() { | |
| 681 rootInjector.get(Car); | |
| 682 }, throws); | |
| 683 | |
| 684 var engine; | |
| 685 expect(() { | |
| 686 engine = childInjector.get(Car); | |
| 687 }, isNot(throws)); | |
| 688 expect(engine, new isInstanceOf<Car>()); | |
| 689 }); | |
| 690 | |
| 691 }); | |
| 692 } | |
| OLD | NEW |