OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library stream_state_helper; | 5 library stream_state_helper; |
6 | 6 |
7 import "../../../pkg/unittest/lib/unittest.dart"; | 7 import "../../../pkg/unittest/lib/unittest.dart"; |
8 import "dart:async"; | 8 import "dart:async"; |
9 import "dart:collection"; | 9 import "dart:collection"; |
10 | 10 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 _fail("Expected: $expect\n" | 98 _fail("Expected: $expect\n" |
99 "Found : [Paused:${_controller.isPaused}]"); | 99 "Found : [Paused:${_controller.isPaused}]"); |
100 } | 100 } |
101 }); | 101 }); |
102 } | 102 } |
103 | 103 |
104 void _onSubcription() { | 104 void _onSubcription() { |
105 _withNextExpectation((Event expect) { | 105 _withNextExpectation((Event expect) { |
106 if (!expect.matchSubscriptionChange(_controller)) { | 106 if (!expect.matchSubscriptionChange(_controller)) { |
107 _fail("Expected: $expect\n" | 107 _fail("Expected: $expect\n" |
108 "Found: [Subscribed:${_controller.hasSubscribers}, " | 108 "Found: [Has listener:${_controller.hasListener}, " |
109 "Paused:${_controller.isPaused}]"); | 109 "Paused:${_controller.isPaused}]"); |
110 } | 110 } |
111 }); | 111 }); |
112 } | 112 } |
113 | 113 |
114 void _withNextExpectation(void action(Event expect)) { | 114 void _withNextExpectation(void action(Event expect)) { |
115 if (_nextExpectationIndex == _expectations.length) { | 115 if (_nextExpectationIndex == _expectations.length) { |
116 action(new MismatchEvent()); | 116 action(new MismatchEvent()); |
117 } else { | 117 } else { |
118 Event next = _expectations[_nextExpectationIndex++]; | 118 Event next = _expectations[_nextExpectationIndex++]; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 _fail("Adding expectation after completing"); | 152 _fail("Adding expectation after completing"); |
153 } | 153 } |
154 _expectations.add(new DoneEvent(action)); | 154 _expectations.add(new DoneEvent(action)); |
155 } | 155 } |
156 void expectPause(bool isPaused, [void action()]) { | 156 void expectPause(bool isPaused, [void action()]) { |
157 if (_onComplete == null) { | 157 if (_onComplete == null) { |
158 _fail("Adding expectation after completing"); | 158 _fail("Adding expectation after completing"); |
159 } | 159 } |
160 _expectations.add(new PauseCallbackEvent(isPaused, action)); | 160 _expectations.add(new PauseCallbackEvent(isPaused, action)); |
161 } | 161 } |
162 void expectSubscription(bool hasSubscribers, bool isPaused, [void action()]) { | 162 void expectSubscription(bool hasListener, bool isPaused, [void action()]) { |
163 if (_onComplete == null) { | 163 if (_onComplete == null) { |
164 _fail("Adding expectation after completing"); | 164 _fail("Adding expectation after completing"); |
165 } | 165 } |
166 _expectations.add( | 166 _expectations.add( |
167 new SubscriptionCallbackEvent(hasSubscribers, isPaused, action)); | 167 new SubscriptionCallbackEvent(hasListener, isPaused, action)); |
168 } | 168 } |
169 | 169 |
170 void _fail(String message) { | 170 void _fail(String message) { |
171 if (_nextExpectationIndex == 0) { | 171 if (_nextExpectationIndex == 0) { |
172 throw "Unexpected event:\n$message\nNo earlier events matched."; | 172 throw "Unexpected event:\n$message\nNo earlier events matched."; |
173 } | 173 } |
174 throw "Unexpected event:\n$message\nMatched so far:\n" | 174 throw "Unexpected event:\n$message\nMatched so far:\n" |
175 " ${_expectations.take(_nextExpectationIndex).join("\n ")}"; | 175 " ${_expectations.take(_nextExpectationIndex).join("\n ")}"; |
176 } | 176 } |
177 } | 177 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 245 |
246 class PauseCallbackEvent extends Event { | 246 class PauseCallbackEvent extends Event { |
247 final bool isPaused; | 247 final bool isPaused; |
248 PauseCallbackEvent(this.isPaused, void action()) | 248 PauseCallbackEvent(this.isPaused, void action()) |
249 : super(action); | 249 : super(action); |
250 bool _testPause(StreamController c) => isPaused == c.isPaused; | 250 bool _testPause(StreamController c) => isPaused == c.isPaused; |
251 String toString() => "[Paused:$isPaused]"; | 251 String toString() => "[Paused:$isPaused]"; |
252 } | 252 } |
253 | 253 |
254 class SubscriptionCallbackEvent extends Event { | 254 class SubscriptionCallbackEvent extends Event { |
255 final bool hasSubscribers; | 255 final bool hasListener; |
256 final bool isPaused; | 256 final bool isPaused; |
257 SubscriptionCallbackEvent(this.hasSubscribers, this.isPaused, void action()) | 257 SubscriptionCallbackEvent(this.hasListener, this.isPaused, void action()) |
258 : super(action); | 258 : super(action); |
259 bool _testSubscribe(StreamController c) { | 259 bool _testSubscribe(StreamController c) { |
260 return hasSubscribers == c.hasSubscribers && isPaused == c.isPaused; | 260 return hasListener == c.hasListener && isPaused == c.isPaused; |
261 } | 261 } |
262 String toString() => "[Subscribers:$hasSubscribers, Paused:$isPaused]"; | 262 String toString() => "[Has listener:$hasListener, Paused:$isPaused]"; |
263 } | 263 } |
264 | 264 |
265 | 265 |
266 class LogAnyEvent extends Event { | 266 class LogAnyEvent extends Event { |
267 String _actual = "*Not matched yet*"; | 267 String _actual = "*Not matched yet*"; |
268 LogAnyEvent(void action()) : super(action); | 268 LogAnyEvent(void action()) : super(action); |
269 bool _testData(var data) { | 269 bool _testData(var data) { |
270 _actual = "*[Data $data]"; | 270 _actual = "*[Data $data]"; |
271 return true; | 271 return true; |
272 } | 272 } |
273 bool _testError(AsyncError error) { | 273 bool _testError(AsyncError error) { |
274 _actual = "*[Error ${error.error}]"; | 274 _actual = "*[Error ${error.error}]"; |
275 return true; | 275 return true; |
276 } | 276 } |
277 bool _testDone() { | 277 bool _testDone() { |
278 _actual = "*[Done]"; | 278 _actual = "*[Done]"; |
279 return true; | 279 return true; |
280 } | 280 } |
281 bool _testPause(StreamController c) { | 281 bool _testPause(StreamController c) { |
282 _actual = "*[Paused:${c.isPaused}]"; | 282 _actual = "*[Paused:${c.isPaused}]"; |
283 return true; | 283 return true; |
284 } | 284 } |
285 bool _testSubcribe(StreamController c) { | 285 bool _testSubcribe(StreamController c) { |
286 _actual = "*[Subscribers:${c.hasSubscribers}, Paused:${c.isPaused}]"; | 286 _actual = "*[Has listener:${c.hasListener}, Paused:${c.isPaused}]"; |
287 return true; | 287 return true; |
288 } | 288 } |
289 | 289 |
290 String toString() => _actual; | 290 String toString() => _actual; |
291 } | 291 } |
OLD | NEW |