Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: test/typed_wrapper/stream_test.dart

Issue 1870543004: Add typed wrapper functions to delegate classes. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'dart:async';
6
7 import "package:async/async.dart";
8 import "package:async/src/typed/stream.dart";
9 import "package:test/test.dart";
10
11 import '../utils.dart';
12
13 void main() {
14 group("with valid types, forwards", () {
15 var controller;
16 var wrapper;
17 var emptyWrapper;
18 var singleWrapper;
19 var errorWrapper;
20 setUp(() {
21 controller = new StreamController<Object>()
22 ..add(1)..add(2)..add(3)..add(4)..add(5)..close();
23
24 // TODO(nweiz): Use public methods when test#414 is fixed and we can run
25 // this on DDC.
26 wrapper = new TypeSafeStream<int>(controller.stream);
27 emptyWrapper = new TypeSafeStream<int>(new Stream<Object>.empty());
28 singleWrapper = new TypeSafeStream<int>(
29 new Stream<Object>.fromIterable([1]));
30 errorWrapper = new TypeSafeStream<int>(
31 new Stream<Object>.fromFuture(new Future.error("oh no")));
32 });
33
34 test("first", () {
35 expect(wrapper.first, completion(equals(1)));
36 expect(emptyWrapper.first, throwsStateError);
37 });
38
39 test("last", () {
40 expect(wrapper.last, completion(equals(5)));
41 expect(emptyWrapper.last, throwsStateError);
42 });
43
44 test("single", () {
45 expect(wrapper.single, throwsStateError);
46 expect(singleWrapper.single, completion(equals(1)));
47 });
48
49 test("isBroadcast", () {
50 expect(wrapper.isBroadcast, isFalse);
51 var broadcastWrapper = new TypeSafeStream<int>(
52 new Stream<Object>.empty().asBroadcastStream());
53 expect(broadcastWrapper.isBroadcast, isTrue);
54 });
55
56 test("isEmpty", () {
57 expect(wrapper.isEmpty, completion(isFalse));
58 expect(emptyWrapper.isEmpty, completion(isTrue));
59 });
60
61 test("length", () {
62 expect(wrapper.length, completion(equals(5)));
63 expect(emptyWrapper.length, completion(equals(0)));
64 });
65
66 group("asBroadcastStream()", () {
67 test("with no parameters", () {
68 var broadcast = wrapper.asBroadcastStream();
69 expect(broadcast.toList(), completion(equals([1, 2, 3, 4, 5])));
70 expect(broadcast.toList(), completion(equals([1, 2, 3, 4, 5])));
71 });
72
73 test("with onListen", () {
74 var broadcast = wrapper.asBroadcastStream(
75 onListen: expectAsync((subscription) {
76 expect(subscription, new isInstanceOf<StreamSubscription<int>>());
77 subscription.pause();
78 }));
79
80 broadcast.listen(null);
81 expect(controller.isPaused, isTrue);
82 });
83
84 test("with onCancel", () {
85 var broadcast = wrapper.asBroadcastStream(
86 onCancel: expectAsync((subscription) {
87 expect(subscription, new isInstanceOf<StreamSubscription<int>>());
88 subscription.pause();
89 }));
90
91 broadcast.listen(null).cancel();
92 expect(controller.isPaused, isTrue);
93 });
94 });
95
96 test("asyncExpand()", () {
97 expect(
98 wrapper.asyncExpand((i) => new Stream.fromIterable([i, i])).toList(),
99 completion(equals([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])));
100 });
101
102 test("asyncMap()", () {
103 expect(wrapper.asyncMap((i) => new Future.value(i * 2)).toList(),
104 completion(equals([2, 4, 6, 8, 10])));
105 });
106
107 group("distinct()", () {
108 test("without equals", () {
109 expect(wrapper.distinct().toList(),
110 completion(equals([1, 2, 3, 4, 5])));
111
112 expect(
113 new TypeSafeStream<int>(
114 new Stream<Object>.fromIterable([1, 1, 2, 2, 3, 3]))
115 .distinct().toList(),
116 completion(equals([1, 2, 3])));
117 });
118
119 test("with equals", () {
120 expect(wrapper.distinct((i1, i2) => (i1 ~/ 2 == i2 ~/ 2)).toList(),
121 completion(equals([1, 2, 4])));
122 });
123 });
124
125 group("drain()", () {
126 test("without a value", () {
127 expect(wrapper.drain(), completes);
128 expect(() => wrapper.drain(), throwsStateError);
129 });
130
131 test("with a value", () {
132 expect(wrapper.drain(12), completion(equals(12)));
133 });
134 });
135
136 test("expand()", () {
137 expect(
138 wrapper.expand((i) => [i, i]).toList(),
139 completion(equals([1, 1, 2, 2, 3, 3, 4, 4, 5, 5])));
140 });
141
142 group("firstWhere()", () {
143 test("finding a value", () {
144 expect(wrapper.firstWhere((i) => i > 3), completion(equals(4)));
145 });
146
147 test("finding no value", () {
148 expect(wrapper.firstWhere((i) => i > 5), throwsStateError);
149 });
150
151 test("with a default value", () {
152 expect(wrapper.firstWhere((i) => i > 5, defaultValue: () => "value"),
153 completion(equals("value")));
154 });
155 });
156
157 group("lastWhere()", () {
158 test("finding a value", () {
159 expect(wrapper.lastWhere((i) => i < 3), completion(equals(2)));
160 });
161
162 test("finding no value", () {
163 expect(wrapper.lastWhere((i) => i > 5), throwsStateError);
164 });
165
166 test("with a default value", () {
167 expect(wrapper.lastWhere((i) => i > 5, defaultValue: () => "value"),
168 completion(equals("value")));
169 });
170 });
171
172 group("singleWhere()", () {
173 test("finding a single value", () {
174 expect(wrapper.singleWhere((i) => i == 3), completion(equals(3)));
175 });
176
177 test("finding no value", () {
178 expect(wrapper.singleWhere((i) => i == 6), throwsStateError);
179 });
180
181 test("finding multiple values", () {
182 expect(wrapper.singleWhere((i) => i.isOdd), throwsStateError);
183 });
184 });
185
186 test("fold()", () {
187 expect(wrapper.fold("foo", (previous, i) => previous + i.toString()),
188 completion(equals("foo12345")));
189 });
190
191 test("forEach()", () async {
192 emptyWrapper.forEach(expectAsync((_) {}, count: 0));
193
194 var results = [];
195 await wrapper.forEach(results.add);
196 expect(results, equals([1, 2, 3, 4, 5]));
197 });
198
199 group("handleError()", () {
200 test("without a test", () {
201 expect(errorWrapper.handleError(expectAsync((error) {
202 expect(error, equals("oh no"));
203 })).toList(), completion(isEmpty));
204 });
205
206 test("with a matching test", () {
207 expect(errorWrapper.handleError(expectAsync((error) {
208 expect(error, equals("oh no"));
209 }), test: expectAsync((error) {
210 expect(error, equals("oh no"));
211 return true;
212 })).toList(), completion(isEmpty));
213 });
214
215 test("with a matching test", () {
216 expect(errorWrapper.handleError(expectAsync((_) {}, count: 0),
217 test: expectAsync((error) {
218 expect(error, equals("oh no"));
219 return false;
220 })).toList(), throwsA("oh no"));
221 });
222 });
223
224 group("listen()", () {
225 test("with a callback", () {
226 var subscription;
227 subscription = wrapper.listen(expectAsync((data) {
228 expect(data, equals(1));
229
230 subscription.onData(expectAsync((data) {
231 expect(data, equals(2));
232 subscription.cancel();
233 }));
234 }));
235 });
236
237 test("with a null callback", () {
238 expect(wrapper.listen(null).asFuture(), completes);
239 });
240 });
241
242 test("map()", () {
243 expect(wrapper.map((i) => i * 2).toList(),
244 completion(equals([2, 4, 6, 8, 10])));
245 });
246
247 test("pipe()", () {
248 var consumer = new StreamController<T>();
249 expect(wrapper.pipe(consumer), completes);
250 expect(consumer.stream.toList(), completion(equals([1, 2, 3, 4, 5])));
251 });
252
253 test("reduce()", () {
254 expect(wrapper.reduce((value, i) => value + i), completion(equals(15)));
255 expect(emptyWrapper.reduce((value, i) => value + i), throwsStateError);
256 });
257
258 test("skipWhile()", () {
259 expect(wrapper.skipWhile((i) => i < 3).toList(),
260 completion(equals([3, 4, 5])));
261 });
262
263 test("takeWhile()", () {
264 expect(wrapper.takeWhile((i) => i < 3).toList(),
265 completion(equals([1, 2])));
266 });
267
268 test("toSet()", () {
269 expect(wrapper.toSet(), completion(unorderedEquals([1, 2, 3, 4, 5])));
270 expect(
271 new TypeSafeStream<int>(
272 new Stream<Object>.fromIterable([1, 1, 2, 2, 3, 3]))
273 .toSet(),
274 completion(unorderedEquals([1, 2, 3])));
275 });
276
277 test("transform()", () {
278 var transformer = new StreamTransformer<int, String>.fromHandlers(
279 handleData: (data, sink) {
280 sink.add(data.toString());
281 });
282
283 expect(wrapper.transform(transformer).toList(),
284 completion(equals(["1", "2", "3", "4", "5"])));
285 });
286
287 test("where()", () {
288 expect(wrapper.where((i) => i.isOdd).toList(),
289 completion(equals([1, 3, 5])));
290 });
291
292 group("any()", () {
293 test("with matches", () {
294 expect(wrapper.any((i) => i > 3), completion(isTrue));
295 });
296
297 test("without matches", () {
298 expect(wrapper.any((i) => i > 5), completion(isFalse));
299 });
300 });
301
302 group("every()", () {
303 test("with all matches", () {
304 expect(wrapper.every((i) => i < 6), completion(isTrue));
305 });
306
307 test("with some non-matches", () {
308 expect(wrapper.every((i) => i > 3), completion(isFalse));
309 });
310 });
311
312 group("skip()", () {
313 test("with a valid index", () {
314 expect(wrapper.skip(3).toList(), completion(equals([4, 5])));
315 });
316
317 test("with a longer index than length", () {
318 expect(wrapper.skip(6).toList(), completion(isEmpty));
319 });
320
321 test("with a negative index", () {
322 expect(() => wrapper.skip(-1), throwsArgumentError);
323 });
324 });
325
326 group("take()", () {
327 test("with a valid index", () {
328 expect(wrapper.take(3).toList(), completion(equals([1, 2, 3])));
329 });
330
331 test("with a longer index than length", () {
332 expect(wrapper.take(6).toList(), completion(equals([1, 2, 3, 4, 5])));
333 });
334
335 test("with a negative index", () {
336 expect(wrapper.take(-1).toList(), completion(isEmpty));
337 });
338 });
339
340 group("elementAt()", () {
341 test("with a valid index", () {
342 expect(wrapper.elementAt(3), completion(equals(4)));
343 });
344
345 test("with too high an index", () {
346 expect(wrapper.elementAt(6), throwsRangeError);
347 });
348
349 test("with a negative index", () {
350 expect(wrapper.elementAt(-1), throwsArgumentError);
351 });
352 });
353
354 group("contains()", () {
355 test("with an element", () {
356 expect(wrapper.contains(2), completion(isTrue));
357 });
358
359 test("with a non-element", () {
360 expect(wrapper.contains(6), completion(isFalse));
361 });
362
363 test("with a non-element of a different type", () {
364 expect(wrapper.contains("foo"), completion(isFalse));
365 });
366 });
367
368 group("join()", () {
369 test("without a separator", () {
370 expect(wrapper.join(), completion(equals("12345")));
371 });
372
373 test("with a separator", () {
374 expect(wrapper.join(" "), completion(equals("1 2 3 4 5")));
375 });
376 });
377
378 test("toString()", () {
379 expect(wrapper.toString(), contains("Stream"));
380 });
381 });
382
383 group("with invalid types", () {
384 var wrapper;
385 var singleWrapper;
386 setUp(() {
387 wrapper = new TypeSafeStream<int>(
388 new Stream<Object>.fromIterable(["foo", "bar", "baz"]));
389 singleWrapper = new TypeSafeStream<int>(
390 new Stream<Object>.fromIterable(["foo"]));
391 });
392
393 group("throws a CastError for", () {
394 test("first", () {
395 expect(wrapper.first, throwsCastError);
396 });
397
398 test("last", () {
399 expect(wrapper.last, throwsCastError);
400 });
401
402 test("single", () {
403 expect(singleWrapper.single, throwsCastError);
404 });
405
406 test("asBroadcastStream()", () {
407 var broadcast = wrapper.asBroadcastStream();
408 expect(broadcast.first, throwsCastError);
409 });
410
411 test("asyncExpand()", () {
412 expect(wrapper.asyncExpand(expectAsync((_) {}, count: 0)).first,
413 throwsCastError);
414 });
415
416 test("asyncMap()", () {
417 expect(wrapper.asyncMap(expectAsync((_) {}, count: 0)).first,
418 throwsCastError);
419 });
420
421 group("distinct()", () {
422 test("without equals", () {
423 expect(wrapper.distinct().first, throwsCastError);
424 });
425
426 test("with equals", () {
427 expect(wrapper.distinct(expectAsync((_, __) {}, count: 0)).first,
428 throwsCastError);
429 });
430 });
431
432 test("expand()", () {
433 expect(wrapper.expand(expectAsync((_) {}, count: 0)).first,
434 throwsCastError);
435 });
436
437 test("firstWhere()", () {
438 expect(wrapper.firstWhere(expectAsync((_) {}, count: 0)),
439 throwsCastError);
440 });
441
442 test("lastWhere()", () {
443 expect(wrapper.lastWhere(expectAsync((_) {}, count: 0)),
444 throwsCastError);
445 });
446
447 test("singleWhere()", () {
448 expect(wrapper.singleWhere(expectAsync((_) {}, count: 0)),
449 throwsCastError);
450 });
451
452 test("fold()", () {
453 expect(wrapper.fold("foo", expectAsync((_, __) {}, count: 0)),
454 throwsCastError);
455 });
456
457 test("forEach()", () async {
458 expect(wrapper.forEach(expectAsync((_) {}, count: 0)), throwsCastError);
459 });
460
461 test("handleError()", () {
462 expect(wrapper.handleError(expectAsync((_) {}, count: 0)).first,
463 throwsCastError);
464 });
465
466 test("listen()", () {
467 expect(() => wrapper.take(1).listen(expectAsync((_) {}, count: 0)),
468 throwsZonedCastError);
469 });
470
471 test("map()", () {
472 expect(wrapper.map(expectAsync((_) {}, count: 0)).first,
473 throwsCastError);
474 });
475
476 test("reduce()", () {
477 expect(wrapper.reduce(expectAsync((_, __) {}, count: 0)),
478 throwsCastError);
479 });
480
481 test("skipWhile()", () {
482 expect(wrapper.skipWhile(expectAsync((_) {}, count: 0)).first,
483 throwsCastError);
484 });
485
486 test("takeWhile()", () {
487 expect(wrapper.takeWhile(expectAsync((_) {}, count: 0)).first,
488 throwsCastError);
489 });
490
491 test("toList()", () async {
492 var list = await wrapper.toList();
493 expect(() => list.first, throwsCastError);
494 }, skip: "Re-enable this when test can run DDC (test#414).");
495
496 test("toSet()", () async {
497 var asSet = await wrapper.toSet();
498 expect(() => asSet.first, throwsCastError);
499 }, skip: "Re-enable this when test can run DDC (test#414).");
500
501 test("where()", () {
502 expect(wrapper.where(expectAsync((_) {}, count: 0)).first,
503 throwsCastError);
504 });
505
506 test("any()", () {
507 expect(wrapper.any(expectAsync((_) {}, count: 0)), throwsCastError);
508 });
509
510 test("every()", () {
511 expect(wrapper.every(expectAsync((_) {}, count: 0)), throwsCastError);
512 });
513
514 test("skip()", () {
515 expect(wrapper.skip(1).first, throwsCastError);
516 });
517
518 test("take()", () {
519 expect(wrapper.take(1).first, throwsCastError);
520 });
521
522 test("elementAt()", () {
523 expect(wrapper.elementAt(1), throwsCastError);
524 });
525 });
526
527 group("doesn't throw a CastError for", () {
528 test("single", () {
529 expect(wrapper.single, throwsStateError);
530 });
531
532 test("length", () {
533 expect(wrapper.length, completion(equals(3)));
534 });
535
536 test("isBroadcast", () {
537 expect(wrapper.isBroadcast, isFalse);
538 });
539
540 test("isEmpty", () {
541 expect(wrapper.isEmpty, completion(isFalse));
542 });
543
544 group("drain()", () {
545 test("without a value", () {
546 expect(wrapper.drain(), completes);
547 expect(() => wrapper.drain(), throwsStateError);
548 });
549
550 test("with a value", () {
551 expect(wrapper.drain(12), completion(equals(12)));
552 });
553 });
554
555 test("skip()", () {
556 expect(() => wrapper.skip(-1), throwsArgumentError);
557 });
558
559 group("elementAt()", () {
560 test("with too high an index", () {
561 expect(wrapper.elementAt(6), throwsRangeError);
562 });
563
564 test("with a negative index", () {
565 expect(wrapper.elementAt(-1), throwsArgumentError);
566 });
567 });
568
569 group("contains()", () {
570 test("with an element", () {
571 expect(wrapper.contains("foo"), completion(isTrue));
572 });
573
574 test("with a non-element", () {
575 expect(wrapper.contains("qux"), completion(isFalse));
576 });
577
578 test("with a non-element of a different type", () {
579 expect(wrapper.contains(1), completion(isFalse));
580 });
581 });
582
583 group("join()", () {
584 test("without a separator", () {
585 expect(wrapper.join(), completion(equals("foobarbaz")));
586 });
587
588 test("with a separator", () {
589 expect(wrapper.join(" "), completion(equals("foo bar baz")));
590 });
591 });
592
593 test("toString()", () {
594 expect(wrapper.toString(), contains("Stream"));
595 });
596 });
597 });
598 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698