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

Side by Side Diff: test/debounce_test.dart

Issue 1648963002: Add reactive-inspired stream transformers: Base URL: https://github.com/dart-lang/async@master
Patch Set: Restructure failes and add more tests. Created 4 years, 9 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 // Test stream transformers.
nweiz 2016/03/01 02:10:04 Nit: I don't think this is necessary.
Lasse Reichstein Nielsen 2016/03/01 16:51:17 True, that's a left-over from the original file.
6 import "dart:async";
7
8 import "package:async/async.dart";
9 import "package:test/test.dart";
10 import "package:clock/clock.dart";
11
12 const ms = const Duration(milliseconds: 1);
13
14 void main() {
15 var currentTime;
16 var createStopwatch;
17 var error = new Result.error("BAD", StackTrace.current);
18
19 setUp(() {
20 currentTime = Duration.ZERO;
21 createStopwatch = new Clock(elapsed: () => currentTime).getStopwatch;
22 });
23
24 /// Stream with events at the specified times.
25 Stream createTimerStream(List<int> eventTimes,
26 {List errors: const[]}) async* {
27 for (var time in eventTimes) {
28 currentTime = ms * time;
29 if (errors.contains(time)) {
30 yield* error.asFuture.asStream();
31 } else {
32 yield time;
33 }
34 // Wait for the event to propagate before changing timer again.
35 await new Future.delayed(Duration.ZERO);
36 }
37 }
38
39 test("expected behavior", () async {
40 var stream =
41 createTimerStream([0, 30, 60, 90, 180, 280, 380, 479, 480, 580]);
42 var debounced = stream.transform(
43 new Debounce<int>(ms * 100, createStopwatch: createStopwatch));
44 expect(debounced.toList(), completion([0, 280, 380, 580]));
45 });
46
47 test("empty source", () async {
48 var stream = new Stream<int>.empty();
49 var debounced = stream.transform(
50 new Debounce<int>(ms * 100, createStopwatch: createStopwatch));
51 expect(debounced.toList(), completion([]));
52 });
53
54 test("omit last event", () async {
55 var stream =
56 createTimerStream([0, 30, 60, 90, 180, 270]);
57 var debounced = stream.transform(
58 new Debounce<int>(ms * 100, createStopwatch: createStopwatch));
59 expect(debounced.toList(), completion([0]));
60 });
61
62 test("stops at first error", () async {
63 var stream =
64 createTimerStream([0, 30, 60, 180, 280], errors: [180]);
65 var debounced = stream.transform(
66 new Debounce<int>(ms * 100, createStopwatch: createStopwatch));
67 // Stops at error.
68 Result r(v) => new Result.value(v);
nweiz 2016/03/01 02:10:04 Defining an anonymous function for this only to us
69 expect(Result.captureStream(debounced).toList(), completion([r(0), error]));
70 });
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698