| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Test the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
| 6 library stream_controller_async_test; | 6 library stream_controller_async_test; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 import '../../../pkg/unittest/lib/unittest.dart'; | 10 import '../../../pkg/unittest/lib/unittest.dart'; |
| 11 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
| 12 | 12 |
| 13 testController() { | 13 testController() { |
| 14 // Test reduce | 14 // Test fold |
| 15 test("StreamController.reduce", () { | 15 test("StreamController.fold", () { |
| 16 StreamController c = new StreamController.broadcast(); | 16 StreamController c = new StreamController.broadcast(); |
| 17 Stream stream = c.stream; | 17 Stream stream = c.stream; |
| 18 stream.reduce(0, (a,b) => a + b) | 18 stream.fold(0, (a,b) => a + b) |
| 19 .then(expectAsync1((int v) { | 19 .then(expectAsync1((int v) { |
| 20 Expect.equals(42, v); | 20 Expect.equals(42, v); |
| 21 })); | 21 })); |
| 22 c.add(10); | 22 c.add(10); |
| 23 c.add(32); | 23 c.add(32); |
| 24 c.close(); | 24 c.close(); |
| 25 }); | 25 }); |
| 26 | 26 |
| 27 test("StreamController.reduce throws", () { | 27 test("StreamController.fold throws", () { |
| 28 StreamController c = new StreamController.broadcast(); | 28 StreamController c = new StreamController.broadcast(); |
| 29 Stream stream = c.stream; | 29 Stream stream = c.stream; |
| 30 stream.reduce(0, (a,b) { throw "Fnyf!"; }) | 30 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
| 31 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 31 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); |
| 32 c.add(42); | 32 c.add(42); |
| 33 }); | 33 }); |
| 34 | 34 |
| 35 test("StreamController.pipeInto", () { | 35 test("StreamController.pipeInto", () { |
| 36 StreamController c = new StreamController.broadcast(); | 36 StreamController c = new StreamController.broadcast(); |
| 37 var list = <int>[]; | 37 var list = <int>[]; |
| 38 Stream stream = c.stream; | 38 Stream stream = c.stream; |
| 39 stream.pipeInto(new CollectionSink<int>(list)) | 39 stream.pipeInto(new CollectionSink<int>(list)) |
| 40 .whenComplete(expectAsync0(() { | 40 .whenComplete(expectAsync0(() { |
| 41 Expect.listEquals(<int>[1,2,9,3,9], list); | 41 Expect.listEquals(<int>[1,2,9,3,9], list); |
| 42 })); | 42 })); |
| 43 c.add(1); | 43 c.add(1); |
| 44 c.add(2); | 44 c.add(2); |
| 45 c.add(9); | 45 c.add(9); |
| 46 c.add(3); | 46 c.add(3); |
| 47 c.add(9); | 47 c.add(9); |
| 48 c.close(); | 48 c.close(); |
| 49 }); | 49 }); |
| 50 } | 50 } |
| 51 | 51 |
| 52 testSingleController() { | 52 testSingleController() { |
| 53 test("Single-subscription StreamController.reduce", () { | 53 test("Single-subscription StreamController.fold", () { |
| 54 StreamController c = new StreamController(); | 54 StreamController c = new StreamController(); |
| 55 Stream stream = c.stream; | 55 Stream stream = c.stream; |
| 56 stream.reduce(0, (a,b) => a + b) | 56 stream.fold(0, (a,b) => a + b) |
| 57 .then(expectAsync1((int v) { Expect.equals(42, v); })); | 57 .then(expectAsync1((int v) { Expect.equals(42, v); })); |
| 58 c.add(10); | 58 c.add(10); |
| 59 c.add(32); | 59 c.add(32); |
| 60 c.close(); | 60 c.close(); |
| 61 }); | 61 }); |
| 62 | 62 |
| 63 test("Single-subscription StreamController.reduce throws", () { | 63 test("Single-subscription StreamController.fold throws", () { |
| 64 StreamController c = new StreamController(); | 64 StreamController c = new StreamController(); |
| 65 Stream stream = c.stream; | 65 Stream stream = c.stream; |
| 66 stream.reduce(0, (a,b) { throw "Fnyf!"; }) | 66 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
| 67 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 67 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); |
| 68 c.add(42); | 68 c.add(42); |
| 69 }); | 69 }); |
| 70 | 70 |
| 71 test("Single-subscription StreamController.pipeInto", () { | 71 test("Single-subscription StreamController.pipeInto", () { |
| 72 StreamController c = new StreamController(); | 72 StreamController c = new StreamController(); |
| 73 var list = <int>[]; | 73 var list = <int>[]; |
| 74 Stream stream = c.stream; | 74 Stream stream = c.stream; |
| 75 stream.pipeInto(new CollectionSink<int>(list)) | 75 stream.pipeInto(new CollectionSink<int>(list)) |
| 76 .whenComplete(expectAsync0(() { | 76 .whenComplete(expectAsync0(() { |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 testStream("map", (s, act) => s.map(act)); | 442 testStream("map", (s, act) => s.map(act)); |
| 443 testStream("expand", (s, act) => s.expand(act)); | 443 testStream("expand", (s, act) => s.expand(act)); |
| 444 testStream("where", (s, act) => s.where(act)); | 444 testStream("where", (s, act) => s.where(act)); |
| 445 testStreamError("handleError", (s, act) => s.handleError(act)); | 445 testStreamError("handleError", (s, act) => s.handleError(act)); |
| 446 testStreamError("handleTest", (s, act) => s.handleError((v) {}, test: act)); | 446 testStreamError("handleTest", (s, act) => s.handleError((v) {}, test: act)); |
| 447 testFuture("every", (s, act) => s.every(act)); | 447 testFuture("every", (s, act) => s.every(act)); |
| 448 testFuture("any", (s, act) => s.any(act)); | 448 testFuture("any", (s, act) => s.any(act)); |
| 449 testFuture("min", (s, act) => s.min((a, b) => act(b))); | 449 testFuture("min", (s, act) => s.min((a, b) => act(b))); |
| 450 testFuture("max", (s, act) => s.max((a, b) => act(b))); | 450 testFuture("max", (s, act) => s.max((a, b) => act(b))); |
| 451 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); | 451 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); |
| 452 testFuture("fold", (s, act) => s.fold(0, (a,b) => act(b))); |
| 452 } | 453 } |
| 453 | 454 |
| 454 main() { | 455 main() { |
| 455 testController(); | 456 testController(); |
| 456 testSingleController(); | 457 testSingleController(); |
| 457 testExtraMethods(); | 458 testExtraMethods(); |
| 458 testPause(); | 459 testPause(); |
| 459 testRethrow(); | 460 testRethrow(); |
| 460 } | 461 } |
| OLD | NEW |