b81f12a549
Change-Id: I0fa2ee2561dd0bc399d88868d790d56e6df3c94d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134940 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
101 lines
1.9 KiB
Dart
101 lines
1.9 KiB
Dart
// Copyright (c) 2020, 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 'package:async_helper/async_minitest.dart';
|
|
|
|
import 'utils.dart';
|
|
|
|
main() {
|
|
test('simple yield', () {
|
|
f() async* {
|
|
for (int i = 0; i < 3; i++) {
|
|
yield i;
|
|
}
|
|
}
|
|
|
|
return expectList(f(), [0, 1, 2]);
|
|
});
|
|
|
|
test('yield in double loop', () {
|
|
f() async* {
|
|
for (int i = 0; i < 3; i++) {
|
|
for (int j = 0; j < 2; j++) {
|
|
yield i * 2 + j;
|
|
}
|
|
}
|
|
}
|
|
|
|
return expectList(f(), [0, 1, 2, 3, 4, 5]);
|
|
});
|
|
|
|
test('yield in try body', () {
|
|
var list = [];
|
|
f() async* {
|
|
for (int i = 0; i < 3; i++) {
|
|
try {
|
|
yield i;
|
|
} finally {
|
|
list.add('$i');
|
|
}
|
|
}
|
|
}
|
|
|
|
return expectList(f(), [0, 1, 2]).whenComplete(() {
|
|
expect(list, equals(['0', '1', '2']));
|
|
});
|
|
});
|
|
|
|
test('yield in catch', () {
|
|
var list = [];
|
|
f() async* {
|
|
for (int i = 0; i < 3; i++) {
|
|
try {
|
|
throw i;
|
|
} catch (e) {
|
|
yield e;
|
|
} finally {
|
|
list.add('$i');
|
|
}
|
|
}
|
|
}
|
|
|
|
return expectList(f(), [0, 1, 2]).whenComplete(() {
|
|
expect(list, equals(['0', '1', '2']));
|
|
});
|
|
});
|
|
|
|
test('yield in finally', () {
|
|
var list = [];
|
|
f() async* {
|
|
for (int i = 0; i < 3; i++) {
|
|
try {
|
|
throw i;
|
|
} finally {
|
|
yield i;
|
|
list.add('$i');
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return expectList(f(), [0, 1, 2]).whenComplete(() {
|
|
expect(list, equals(['0', '1', '2']));
|
|
});
|
|
});
|
|
|
|
test('keep yielding after cancel', () {
|
|
f() async* {
|
|
for (int i = 0; i < 10; i++) {
|
|
try {
|
|
yield i;
|
|
} finally {
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
return expectList(f().take(3), [0, 1, 2]);
|
|
});
|
|
}
|