Files
sdk/tests/lib/async/stream_take_test.dart
T
Jacob Richman 2dcd56ef43 Format all tests.
There are far too many files here to review everyone carefully.
Spot checking most of the diffs look good as test code is generally written
with less care than application code so lots of ugly formatting get through.
If people notice files where the automated formatting bothers them feel free
to comment indicating file names and I'll move spaces within comments to make
the formatting cleaner and use comments to force block formatting as I have
done for other case where formatting looked bad.

BUG=
R=efortuna@google.com

Review-Url: https://codereview.chromium.org/2771453003 .
2017-04-17 14:53:02 -07:00

71 lines
1.7 KiB
Dart

// Copyright (c) 2016, 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:expect/expect.dart";
import "package:async_helper/async_helper.dart";
class A {
const A();
}
class B extends A {
const B();
}
/// Stream which emits an error if it's not canceled at the correct time.
///
/// Must be canceled after at most [maxEvents] events.
Stream makeStream(int maxEvents) {
var c;
int event = 0;
bool canceled = false;
c = new StreamController(onListen: () {
new Timer.periodic(const Duration(milliseconds: 10), (t) {
if (canceled) {
t.cancel();
return;
}
if (event == maxEvents) {
c.addError("NOT CANCELED IN TIME: $maxEvents");
c.close();
t.cancel();
} else {
c.add(event++);
}
});
}, onCancel: () {
canceled = true;
});
return c.stream;
}
main() {
asyncStart();
tests().then((_) {
asyncEnd();
});
}
tests() async {
await expectThrowsAsync(makeStream(4).take(5).toList(), "5/4");
await expectThrowsAsync(makeStream(0).take(1).toList(), "1/0");
Expect.listEquals([0, 1, 2, 3, 4], await makeStream(5).take(5).toList());
Expect.listEquals([0, 1, 2, 3], await makeStream(5).take(4).toList());
Expect.listEquals([0], await makeStream(5).take(1).toList());
Expect.listEquals([], await makeStream(5).take(0).toList());
Expect.listEquals([], await makeStream(0).take(0).toList());
}
Future expectThrowsAsync(Future computation, String name) {
return computation.then((_) {
Expect.fail("$name: Did not throw");
}, onError: (e, s) {});
}