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

Unified Diff: test/lazy_stream_test.dart

Issue 1491923005: Add a LazyStream class. (Closed) Base URL: git@github.com:dart-lang/async.git@master
Patch Set: Code review changes Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/lazy_stream_test.dart
diff --git a/test/lazy_stream_test.dart b/test/lazy_stream_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..63f2a197b1495ee1edb396ab49bbfb264d081f27
--- /dev/null
+++ b/test/lazy_stream_test.dart
@@ -0,0 +1,106 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async";
+
+import "package:async/async.dart";
+import "package:test/test.dart";
+
+import "utils.dart";
+
+main() {
+ test("disallows a null callback", () {
+ expect(() => new LazyStream(null), throwsArgumentError);
+ });
+
+ test("calls the callback when the stream is listened", () async {
+ var callbackCalled = false;
+ var stream = new LazyStream(expectAsync(() {
+ callbackCalled = true;
+ return new Stream.empty();
+ }));
+
+ await flushMicrotasks();
+ expect(callbackCalled, isFalse);
+
+ stream.listen(null);
+ expect(callbackCalled, isTrue);
+ });
+
+ test("calls the callback when the stream is listened", () async {
+ var callbackCalled = false;
+ var stream = new LazyStream(expectAsync(() {
+ callbackCalled = true;
+ return new Stream.empty();
+ }));
+
+ await flushMicrotasks();
+ expect(callbackCalled, isFalse);
+
+ stream.listen(null);
+ expect(callbackCalled, isTrue);
+ });
+
+ test("forwards to a synchronously-provided stream", () async {
+ var controller = new StreamController<int>();
+ var stream = new LazyStream(expectAsync(() => controller.stream));
+
+ var events = [];
+ stream.listen(events.add);
+
+ controller.add(1);
+ await flushMicrotasks();
+ expect(events, equals([1]));
+
+ controller.add(2);
+ await flushMicrotasks();
+ expect(events, equals([1, 2]));
+
+ controller.add(3);
+ await flushMicrotasks();
+ expect(events, equals([1, 2, 3]));
+
+ controller.close();
+ });
+
+ test("forwards to an asynchronously-provided stream", () async {
+ var controller = new StreamController<int>();
+ var stream = new LazyStream(expectAsync(() async => controller.stream));
+
+ var events = [];
+ stream.listen(events.add);
+
+ controller.add(1);
+ await flushMicrotasks();
+ expect(events, equals([1]));
+
+ controller.add(2);
+ await flushMicrotasks();
+ expect(events, equals([1, 2]));
+
+ controller.add(3);
+ await flushMicrotasks();
+ expect(events, equals([1, 2, 3]));
+
+ controller.close();
+ });
+
+ test("a lazy stream can't be listened to multiple times", () {
+ var stream = new LazyStream(expectAsync(() => new Stream.empty()));
+ expect(stream.isBroadcast, isFalse);
+
+ stream.listen(null);
+ expect(() => stream.listen(null), throwsStateError);
+ expect(() => stream.listen(null), throwsStateError);
+ });
+
+ test("a lazy stream can't be listened to from within its callback", () {
+ var stream;
+ stream = new LazyStream(expectAsync(() {
+ expect(() => stream.listen(null), throwsStateError);
+ return new Stream.empty();
+ }));
+ stream.listen(null);
+ });
+}
« no previous file with comments | « pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698