|
1 | 1 | library test_lib.test_lib;
|
2 | 2 |
|
3 | 3 | import 'package:guinness/guinness.dart' as gns;
|
4 |
| -export 'package:guinness/guinness.dart' hide Expect, expect, NotExpect, beforeEach, it, iit; |
| 4 | +export 'package:guinness/guinness.dart' hide Expect, expect, NotExpect, beforeEach, it, iit, xit; |
5 | 5 | import 'package:unittest/unittest.dart' hide expect;
|
6 | 6 | import 'dart:mirrors';
|
7 | 7 | import 'dart:async';
|
8 |
| -import 'package:angular2/src/reflection/reflection.dart'; |
9 |
| -import 'package:angular2/src/reflection/reflection_capabilities.dart'; |
| 8 | + |
10 | 9 | import 'package:collection/equality.dart';
|
11 | 10 | import 'package:angular2/src/dom/dom_adapter.dart' show DOM;
|
12 | 11 |
|
| 12 | +import 'package:angular2/src/reflection/reflection.dart'; |
| 13 | +import 'package:angular2/src/reflection/reflection_capabilities.dart'; |
| 14 | + |
| 15 | +import 'package:angular2/src/di/binding.dart' show bind; |
| 16 | +import 'package:angular2/src/di/injector.dart' show Injector; |
| 17 | + |
| 18 | +import './test_injector.dart'; |
| 19 | +export './test_injector.dart' show inject; |
| 20 | + |
13 | 21 | bool IS_DARTIUM = true;
|
14 | 22 | bool IS_NODEJS = false;
|
15 | 23 |
|
| 24 | +List _testBindings = []; |
| 25 | +Injector _injector; |
| 26 | +bool _isCurrentTestAsync; |
| 27 | +bool _inIt = false; |
| 28 | + |
| 29 | +class AsyncTestCompleter { |
| 30 | + Completer _completer; |
| 31 | + |
| 32 | + AsyncTestCompleter() { |
| 33 | + _completer = new Completer(); |
| 34 | + } |
| 35 | + |
| 36 | + done() { |
| 37 | + _completer.complete(); |
| 38 | + } |
| 39 | + |
| 40 | + get future => _completer.future; |
| 41 | +} |
| 42 | + |
| 43 | +testSetup() { |
| 44 | + reflector.reflectionCapabilities = new ReflectionCapabilities(); |
| 45 | + // beforeEach configuration: |
| 46 | + // - Priority 3: clear the bindings before each test, |
| 47 | + // - Priority 2: collect the bindings before each test, see beforeEachBindings(), |
| 48 | + // - Priority 1: create the test injector to be used in beforeEach() and it() |
| 49 | + |
| 50 | + gns.beforeEach( |
| 51 | + () { |
| 52 | + _testBindings.clear(); |
| 53 | + }, |
| 54 | + priority: 3 |
| 55 | + ); |
| 56 | + |
| 57 | + var completerBinding = bind(AsyncTestCompleter).toFactory(() { |
| 58 | + // Mark the test as async when an AsyncTestCompleter is injected in an it(), |
| 59 | + if (!_inIt) throw 'AsyncTestCompleter can only be injected in an "it()"'; |
| 60 | + _isCurrentTestAsync = true; |
| 61 | + return new AsyncTestCompleter(); |
| 62 | + }); |
| 63 | + |
| 64 | + gns.beforeEach( |
| 65 | + () { |
| 66 | + _isCurrentTestAsync = false; |
| 67 | + _testBindings.add(completerBinding); |
| 68 | + _injector = createTestInjector(_testBindings); |
| 69 | + }, |
| 70 | + priority: 1 |
| 71 | + ); |
| 72 | +} |
| 73 | + |
16 | 74 | Expect expect(actual, [matcher]) {
|
17 | 75 | final expect = new Expect(actual);
|
18 | 76 | if (matcher != null) expect.to(matcher);
|
@@ -46,38 +104,55 @@ class NotExpect extends gns.NotExpect {
|
46 | 104 | }
|
47 | 105 |
|
48 | 106 | beforeEach(fn) {
|
49 |
| - gns.beforeEach(_enableReflection(fn)); |
| 107 | + if (fn is! FunctionWithParamTokens) fn = new FunctionWithParamTokens([], fn); |
| 108 | + gns.beforeEach(() { |
| 109 | + fn.execute(_injector); |
| 110 | + }); |
50 | 111 | }
|
51 | 112 |
|
52 |
| -it(name, fn) { |
53 |
| - gns.it(name, _enableReflection(_handleAsync(fn))); |
| 113 | +/** |
| 114 | + * Allows overriding default bindings defined in test_injector.js. |
| 115 | + * |
| 116 | + * The given function must return a list of DI bindings. |
| 117 | + * |
| 118 | + * Example: |
| 119 | + * |
| 120 | + * beforeEachBindings(() => [ |
| 121 | + * bind(Compiler).toClass(MockCompiler), |
| 122 | + * bind(SomeToken).toValue(myValue), |
| 123 | + * ]); |
| 124 | + */ |
| 125 | +beforeEachBindings(fn) { |
| 126 | + gns.beforeEach( |
| 127 | + () { |
| 128 | + var bindings = fn(); |
| 129 | + if (bindings != null) _testBindings.addAll(bindings); |
| 130 | + }, |
| 131 | + priority: 2 |
| 132 | + ); |
54 | 133 | }
|
55 | 134 |
|
56 |
| -iit(name, fn) { |
57 |
| - gns.iit(name, _enableReflection(_handleAsync(fn))); |
| 135 | +_it(gnsFn, name, fn) { |
| 136 | + if (fn is! FunctionWithParamTokens) fn = new FunctionWithParamTokens([], fn); |
| 137 | + gnsFn(name, () { |
| 138 | + _inIt = true; |
| 139 | + fn.execute(_injector); |
| 140 | + _inIt = false; |
| 141 | + if (_isCurrentTestAsync) return _injector.get(AsyncTestCompleter).future; |
| 142 | + }); |
58 | 143 | }
|
59 | 144 |
|
60 |
| -_enableReflection(fn) { |
61 |
| - return () { |
62 |
| - reflector.reflectionCapabilities = new ReflectionCapabilities(); |
63 |
| - return fn(); |
64 |
| - }; |
65 |
| -} |
66 | 145 |
|
67 |
| -_handleAsync(fn) { |
68 |
| - ClosureMirror cm = reflect(fn); |
69 |
| - MethodMirror mm = cm.function; |
70 |
| - |
71 |
| - var completer = new Completer(); |
| 146 | +it(name, fn) { |
| 147 | + _it(gns.it, name, fn); |
| 148 | +} |
72 | 149 |
|
73 |
| - if (mm.parameters.length == 1) { |
74 |
| - return () { |
75 |
| - cm.apply([completer.complete]); |
76 |
| - return completer.future; |
77 |
| - }; |
78 |
| - } |
| 150 | +iit(name, fn) { |
| 151 | + _it(gns.iit, name, fn); |
| 152 | +} |
79 | 153 |
|
80 |
| - return fn; |
| 154 | +xit(name, fn) { |
| 155 | + _it(gns.xit, name, fn); |
81 | 156 | }
|
82 | 157 |
|
83 | 158 | // TODO(tbosch): remove when https://github.com/vsavkin/guinness/issues/41
|
|
0 commit comments