| 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 String id = 'v8-id'; | 35 final String id = 'v8-id'; |
| 36 } | 36 } |
| 37 | 37 |
| 38 @Injectable() | 38 @Injectable() |
| 39 class MockEngine implements Engine { | 39 class MockEngine implements Engine { |
| 40 String id = 'mock-id'; | 40 final 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 |
| 48 @Injectable() | 52 @Injectable() |
| 49 class Car { | 53 class Car { |
| 50 Engine engine; | 54 Engine engine; |
| 51 Injector injector; | 55 Injector injector; |
| 52 | 56 |
| 53 Car(this.engine, this.injector); | 57 Car(this.engine, this.injector); |
| 54 } | 58 } |
| 55 | 59 |
| 60 class Lemon { |
| 61 final engine; |
| 62 final Injector injector; |
| 63 |
| 64 Lemon(this.engine, this.injector); |
| 65 } |
| 66 |
| 56 class NumDependency { | 67 class NumDependency { |
| 57 NumDependency(num value) {} | 68 NumDependency(num value) {} |
| 58 } | 69 } |
| 59 | 70 |
| 60 class IntDependency { | 71 class IntDependency { |
| 61 IntDependency(int value) {} | 72 IntDependency(int value) {} |
| 62 } | 73 } |
| 63 | 74 |
| 64 class DoubleDependency { | 75 class DoubleDependency { |
| 65 DoubleDependency(double value) {} | 76 DoubleDependency(double value) {} |
| (...skipping 16 matching lines...) Expand all Loading... |
| 82 CircularB(CircularA a) {} | 93 CircularB(CircularA a) {} |
| 83 } | 94 } |
| 84 | 95 |
| 85 typedef int CompareInt(int a, int b); | 96 typedef int CompareInt(int a, int b); |
| 86 | 97 |
| 87 int compareIntAsc(int a, int b) => b.compareTo(a); | 98 int compareIntAsc(int a, int b) => b.compareTo(a); |
| 88 | 99 |
| 89 class WithTypeDefDependency { | 100 class WithTypeDefDependency { |
| 90 CompareInt compare; | 101 CompareInt compare; |
| 91 | 102 |
| 92 WithTypeDefDependency(CompareInt c) { | 103 WithTypeDefDependency(this.compare); |
| 93 compare = c; | |
| 94 } | |
| 95 } | 104 } |
| 96 | 105 |
| 97 class MultipleConstructors { | 106 class MultipleConstructors { |
| 98 String instantiatedVia; | 107 String instantiatedVia; |
| 99 MultipleConstructors() : instantiatedVia = 'default'; | 108 MultipleConstructors() : instantiatedVia = 'default'; |
| 100 MultipleConstructors.named() : instantiatedVia = 'named'; | 109 MultipleConstructors.named() : instantiatedVia = 'named'; |
| 101 } | 110 } |
| 102 | 111 |
| 103 class InterfaceOne { | 112 class InterfaceOne { |
| 104 } | 113 } |
| 105 | 114 |
| 106 class ClassOne implements InterfaceOne { | 115 class ClassOne implements InterfaceOne { |
| 107 ClassOne(Log log) { | 116 ClassOne(Log log) { |
| 108 log.add('ClassOne'); | 117 log.add('ClassOne'); |
| 109 } | 118 } |
| 110 } | 119 } |
| 111 | 120 |
| 112 @Injectable() | 121 @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() |
| 113 class Log { | 139 class Log { |
| 114 var log = []; | 140 var log = []; |
| 115 | 141 |
| 116 add(String message) => log.add(message); | 142 add(String message) => log.add(message); |
| 117 } | 143 } |
| 118 | 144 |
| 119 class EmulatedMockEngineFactory { | 145 class EmulatedMockEngineFactory { |
| 120 call(Injector i) => new MockEngine(); | 146 call(Injector i) => new MockEngine(); |
| 121 } | 147 } |
| 122 | 148 |
| 123 void main() { | 149 void main() { |
| 124 createInjectorSpec('DynamicInjector', | 150 createInjectorSpec('DynamicInjector', |
| 125 (modules, [name]) => new DynamicInjector(modules: modules, name: name)); | 151 (modules, [name]) => new DynamicInjector(modules: modules, name: name)); |
| 126 | 152 |
| 127 // Initialize generated type factories. | |
| 128 type_factories_gen.main(); | |
| 129 | |
| 130 createInjectorSpec('StaticInjector', | 153 createInjectorSpec('StaticInjector', |
| 131 (modules, [name]) => new StaticInjector(modules: modules, name: name, | 154 (modules, [name]) => new StaticInjector(modules: modules, name: name, |
| 132 typeFactories: type_factories_gen.typeFactories)); | 155 typeFactories: type_factories_gen.typeFactories)); |
| 156 |
| 157 dynamicInjectorTest(); |
| 158 staticInjectorTest(); |
| 133 } | 159 } |
| 134 | 160 |
| 135 typedef Injector InjectorFactory(List<Module> modules, [String name]); | 161 typedef Injector InjectorFactory(List<Module> modules, [String name]); |
| 136 | 162 |
| 137 createInjectorSpec(String injectorName, InjectorFactory injectorFactory) { | 163 createInjectorSpec(String injectorName, InjectorFactory injectorFactory) { |
| 138 | 164 |
| 139 describe(injectorName, () { | 165 describe(injectorName, () { |
| 140 | 166 |
| 141 it('should instantiate a type', () { | 167 it('should instantiate a type', () { |
| 142 var injector = injectorFactory([new Module()..type(Engine)]); | 168 var injector = injectorFactory([new Module()..type(Engine)]); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 | 183 |
| 158 it('should resolve basic dependencies', () { | 184 it('should resolve basic dependencies', () { |
| 159 var injector = injectorFactory([new Module()..type(Car)..type(Engine)]); | 185 var injector = injectorFactory([new Module()..type(Car)..type(Engine)]); |
| 160 var instance = injector.get(Car); | 186 var instance = injector.get(Car); |
| 161 | 187 |
| 162 expect(instance, instanceOf(Car)); | 188 expect(instance, instanceOf(Car)); |
| 163 expect(instance.engine.id, toEqual('v8-id')); | 189 expect(instance.engine.id, toEqual('v8-id')); |
| 164 }); | 190 }); |
| 165 | 191 |
| 166 | 192 |
| 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 |
| 167 it('should allow modules and overriding providers', () { | 212 it('should allow modules and overriding providers', () { |
| 168 var module = new Module()..type(Engine, implementedBy: MockEngine); | 213 var module = new Module()..type(Engine, implementedBy: MockEngine); |
| 169 | 214 |
| 170 // injector is immutable | 215 // injector is immutable |
| 171 // you can't load more modules once it's instantiated | 216 // you can't load more modules once it's instantiated |
| 172 // (you can create a child injector) | 217 // (you can create a child injector) |
| 173 var injector = injectorFactory([module]); | 218 var injector = injectorFactory([module]); |
| 174 var instance = injector.get(Engine); | 219 var instance = injector.get(Engine); |
| 175 | 220 |
| 176 expect(instance.id, toEqual('mock-id')); | 221 expect(instance.id, toEqual('mock-id')); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 }); | 331 }); |
| 287 | 332 |
| 288 | 333 |
| 289 it('should provide the injector as Injector', () { | 334 it('should provide the injector as Injector', () { |
| 290 var injector = injectorFactory([]); | 335 var injector = injectorFactory([]); |
| 291 | 336 |
| 292 expect(injector.get(Injector), toBe(injector)); | 337 expect(injector.get(Injector), toBe(injector)); |
| 293 }); | 338 }); |
| 294 | 339 |
| 295 | 340 |
| 296 // Typedef injection is not supported in dart2js: http://dartbug.com/11612 | 341 it('should inject a typedef', () { |
| 297 xit('should inject a typedef', () { | |
| 298 var module = new Module()..value(CompareInt, compareIntAsc); | 342 var module = new Module()..value(CompareInt, compareIntAsc); |
| 299 | 343 |
| 300 var injector = injectorFactory([module]); | 344 var injector = injectorFactory([module]); |
| 301 var compare = injector.get(CompareInt); | 345 var compare = injector.get(CompareInt); |
| 302 | 346 |
| 303 expect(compare(1, 2), toBe(1)); | 347 expect(compare(1, 2), toBe(1)); |
| 304 expect(compare(5, 2), toBe(-1)); | 348 expect(compare(5, 2), toBe(-1)); |
| 305 }); | 349 }); |
| 306 | 350 |
| 307 | 351 |
| 308 // Typedef injection is not supported in dart2js: http://dartbug.com/11612 | 352 it('should throw an exception when injecting typedef without providing it',
() { |
| 309 xit('should throw an exception when injecting typedef without providing it',
() { | |
| 310 var injector = injectorFactory([new Module()..type(WithTypeDefDependency)]
); | 353 var injector = injectorFactory([new Module()..type(WithTypeDefDependency)]
); |
| 311 | 354 |
| 312 expect(() { | 355 expect(() { |
| 313 injector.get(WithTypeDefDependency); | 356 injector.get(WithTypeDefDependency); |
| 314 }, toThrow(NoProviderError, 'No provider found for CompareInt! ' | 357 }, throws); |
| 315 '(resolving WithTypeDefDependency -> CompareInt)')); | |
| 316 }); | 358 }); |
| 317 | 359 |
| 318 | 360 |
| 319 it('should instantiate via the default/unnamed constructor', () { | 361 it('should instantiate via the default/unnamed constructor', () { |
| 320 var injector = injectorFactory([new Module()..type(MultipleConstructors)])
; | 362 var injector = injectorFactory([new Module()..type(MultipleConstructors)])
; |
| 321 MultipleConstructors instance = injector.get(MultipleConstructors); | 363 MultipleConstructors instance = injector.get(MultipleConstructors); |
| 322 expect(instance.instantiatedVia, 'default'); | 364 expect(instance.instantiatedVia, 'default'); |
| 323 }); | 365 }); |
| 324 | 366 |
| 325 // CHILD INJECTORS | 367 // CHILD INJECTORS |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 NoProviderError, | 552 NoProviderError, |
| 511 'No provider found for Car! (resolving Car)' | 553 'No provider found for Car! (resolving Car)' |
| 512 )); | 554 )); |
| 513 }); | 555 }); |
| 514 | 556 |
| 515 }); | 557 }); |
| 516 | 558 |
| 517 }); | 559 }); |
| 518 | 560 |
| 519 } | 561 } |
| 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 |