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

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

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

Powered by Google App Engine
This is Rietveld 408576698