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