OLD | NEW |
1 part of unittest; | 1 part of unittest; |
2 | 2 |
| 3 const _PLACE_HOLDER = const _ArgPlaceHolder(); |
| 4 |
| 5 /// Used to track unused positional args. |
| 6 class _ArgPlaceHolder { |
| 7 const _ArgPlaceHolder(); |
| 8 } |
| 9 |
3 /** Simulates spread arguments using named arguments. */ | 10 /** Simulates spread arguments using named arguments. */ |
4 // TODO(sigmund): remove this class and simply use a closure with named | 11 // TODO(sigmund): remove this class and simply use a closure with named |
5 // arguments (if still applicable). | 12 // arguments (if still applicable). |
6 class _SpreadArgsHelper { | 13 class _SpreadArgsHelper { |
7 final Function callback; | 14 final Function callback; |
8 final int minExpectedCalls; | 15 final int minExpectedCalls; |
9 final int maxExpectedCalls; | 16 final int maxExpectedCalls; |
10 final Function isDone; | 17 final Function isDone; |
11 final String id; | 18 final String id; |
12 int actualCalls = 0; | 19 int actualCalls = 0; |
13 final TestCase testCase; | 20 final TestCase testCase; |
14 bool complete; | 21 bool complete; |
15 | 22 |
16 _SpreadArgsHelper(Function callback, int minExpected, int maxExpected, | 23 _SpreadArgsHelper(Function callback, int minExpected, int maxExpected, |
17 Function isDone, String id) | 24 String id, {bool isDone()}) |
18 : this.callback = callback, | 25 : this.callback = callback, |
19 minExpectedCalls = minExpected, | 26 minExpectedCalls = minExpected, |
20 maxExpectedCalls = (maxExpected == 0 && minExpected > 0) | 27 maxExpectedCalls = (maxExpected == 0 && minExpected > 0) |
21 ? minExpected | 28 ? minExpected |
22 : maxExpected, | 29 : maxExpected, |
23 this.isDone = isDone, | 30 this.isDone = isDone, |
24 this.testCase = currentTestCase, | 31 this.testCase = currentTestCase, |
25 this.id = _makeCallbackId(id, callback) { | 32 this.id = _makeCallbackId(id, callback) { |
26 ensureInitialized(); | 33 ensureInitialized(); |
27 if (testCase == null) { | 34 if (testCase == null) { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return; | 90 if (minExpectedCalls > 0 && actualCalls < minExpectedCalls) return; |
84 if (isDone != null && !isDone()) return; | 91 if (isDone != null && !isDone()) return; |
85 | 92 |
86 // Mark this callback as complete and remove it from the testcase | 93 // Mark this callback as complete and remove it from the testcase |
87 // oustanding callback count; if that hits zero the testcase is done. | 94 // oustanding callback count; if that hits zero the testcase is done. |
88 complete = true; | 95 complete = true; |
89 testCase._markCallbackComplete(); | 96 testCase._markCallbackComplete(); |
90 } | 97 } |
91 } | 98 } |
92 | 99 |
93 invoke0() { | 100 /// Returns a function that has as many required + positional arguments as |
| 101 /// [callback] (up to a total of 6). |
| 102 /// |
| 103 /// Optional positional arguments are supported by using const place-holders |
| 104 Function get func { |
| 105 if (callback is _Func6) return _max6; |
| 106 if (callback is _Func5) return _max5; |
| 107 if (callback is _Func4) return _max4; |
| 108 if (callback is _Func3) return _max3; |
| 109 if (callback is _Func2) return _max2; |
| 110 if (callback is _Func1) return _max1; |
| 111 if (callback is _Func0) return _max0; |
| 112 |
| 113 throw new ArgumentError( |
| 114 'The callback argument has more than 6 required arguments'); |
| 115 } |
| 116 |
| 117 /// This indirection is critical. It ensures the returned function has an |
| 118 /// argument count of zero. |
| 119 _max0() => _max6(); |
| 120 |
| 121 _max1([a0 = _PLACE_HOLDER]) => _max6(a0); |
| 122 |
| 123 _max2([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER]) => _max6(a0, a1); |
| 124 |
| 125 _max3([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER, a2 = _PLACE_HOLDER]) => |
| 126 _max6(a0, a1, a2); |
| 127 |
| 128 _max4([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER, a2 = _PLACE_HOLDER, |
| 129 a3 = _PLACE_HOLDER]) => _max6(a0, a1, a2, a3); |
| 130 |
| 131 _max5([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER, a2 = _PLACE_HOLDER, |
| 132 a3 = _PLACE_HOLDER, a4 = _PLACE_HOLDER]) => _max6(a0, a1, a2, a3, a4); |
| 133 |
| 134 _max6([a0 = _PLACE_HOLDER, a1 = _PLACE_HOLDER, a2 = _PLACE_HOLDER, |
| 135 a3 = _PLACE_HOLDER, a4 = _PLACE_HOLDER, a5 = _PLACE_HOLDER]) { |
| 136 var args = [a0, a1, a2, a3, a4, a5]; |
| 137 args.removeWhere((a) => a == _PLACE_HOLDER); |
| 138 |
94 return _guardAsync( | 139 return _guardAsync( |
95 () { | 140 () { |
96 if (shouldCallBack()) { | 141 if (shouldCallBack()) { |
97 return callback(); | 142 return Function.apply(callback, args); |
98 } | 143 } |
99 }, | 144 }, |
100 after, testCase); | 145 after, testCase); |
101 } | |
102 | |
103 invoke1(arg1) { | |
104 return _guardAsync( | |
105 () { | |
106 if (shouldCallBack()) { | |
107 return callback(arg1); | |
108 } | |
109 }, | |
110 after, testCase); | |
111 } | |
112 | |
113 invoke2(arg1, arg2) { | |
114 return _guardAsync( | |
115 () { | |
116 if (shouldCallBack()) { | |
117 return callback(arg1, arg2); | |
118 } | |
119 }, | |
120 after, testCase); | |
121 } | 146 } |
122 | 147 |
123 _guardAsync(Function tryBody, Function finallyBody, TestCase testCase) { | 148 _guardAsync(Function tryBody, Function finallyBody, TestCase testCase) { |
124 assert(testCase != null); | 149 assert(testCase != null); |
125 try { | 150 try { |
126 return tryBody(); | 151 return tryBody(); |
127 } catch (e, trace) { | 152 } catch (e, trace) { |
128 _registerException(testCase, e, trace); | 153 _registerException(testCase, e, trace); |
129 } finally { | 154 } finally { |
130 if (finallyBody != null) finallyBody(); | 155 if (finallyBody != null) finallyBody(); |
131 } | 156 } |
132 } | 157 } |
133 } | 158 } |
| 159 |
| 160 typedef _Func0(); |
| 161 typedef _Func1(a); |
| 162 typedef _Func2(a, b); |
| 163 typedef _Func3(a, b, c); |
| 164 typedef _Func4(a, b, c, d); |
| 165 typedef _Func5(a, b, c, d, e); |
| 166 typedef _Func6(a, b, c, d, e, f); |
OLD | NEW |