Skip to content

Commit ceeea68

Browse files
author
sgrekhov
committed
dart-lang#1363. Tests for yield updated to the current specification
1 parent f9b3e93 commit ceeea68

8 files changed

+254
-156
lines changed

Language/Statements/Yield_and_Yield_Each/Yield/async_delivery_t01.dart

Lines changed: 0 additions & 54 deletions
This file was deleted.

Language/Statements/Yield_and_Yield_Each/Yield/execution_async_t01.dart renamed to Language/Statements/Yield_and_Yield_Each/Yield/execution_async_A01_t01.dart

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,32 @@ import '../../../../Utils/expect.dart';
3030
List<int> readyToSent = [];
3131
List<int> sent = [];
3232

33-
Stream<int> generator(List log) async* {
33+
Stream<int> generator() async* {
3434
for (int i = 1; i <= 3; i++) {
3535
readyToSent.add(i);
3636
yield i;
3737
sent.add(i);
3838
}
3939
}
4040

41-
test() async {
42-
List received = [];
43-
Stream<int> s = generator(received);
44-
late StreamSubscription<int> ss;
45-
ss = s.listen(
46-
(int i) async {
47-
received.add(i);
48-
if (i == 1) {
49-
ss.pause();
50-
await Future.delayed(Duration(milliseconds: 100));
51-
Expect.listEquals([], sent);
52-
Expect.listEquals([1], readyToSent);
53-
ss.resume();
54-
}
55-
},
56-
onDone:() {
57-
Expect.listEquals([1, 2, 3], received);
58-
Expect.listEquals(sent, received);
59-
Expect.listEquals(readyToSent, received);
60-
asyncEnd();
61-
}
62-
);
63-
}
64-
6541
main() {
6642
asyncStart();
67-
test();
43+
List received = [];
44+
Stream<int> s = generator();
45+
late StreamSubscription<int> ss;
46+
ss = s.listen((int i) async {
47+
received.add(i);
48+
if (i == 1) {
49+
ss.pause();
50+
await Future.delayed(Duration(milliseconds: 100));
51+
Expect.listEquals([], sent);
52+
Expect.listEquals([1], readyToSent);
53+
ss.resume();
54+
}
55+
}, onDone: () {
56+
Expect.listEquals([1, 2, 3], received);
57+
Expect.listEquals(sent, received);
58+
Expect.listEquals(readyToSent, received);
59+
asyncEnd();
60+
});
6861
}

Language/Statements/Yield_and_Yield_Each/Yield/execution_async_t02.dart renamed to Language/Statements/Yield_and_Yield_Each/Yield/execution_async_A01_t02.dart

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,40 @@
2323
///
2424
/// @author [email protected]
2525
/// @author [email protected]
26+
/// @issue 49451
2627
2728
import 'dart:async';
2829
import '../../../../Utils/expect.dart';
2930

3031
List<int> readyToSent = [];
3132
List<int> sent = [];
3233

33-
Stream<int> generator(List log) async* {
34-
for (int i = 1; i <= 3; i++) {
34+
Stream<int> generator() async* {
35+
for (int i = 1; i <= 5; i++) {
3536
readyToSent.add(i);
3637
yield i;
3738
sent.add(i);
3839
}
3940
}
4041

41-
test() async {
42+
main() async {
43+
asyncStart();
4244
List received = [];
43-
Stream<int> s = generator(received);
45+
Stream<int> s = generator();
4446
late StreamSubscription<int> ss;
45-
ss = s.listen(
46-
(int i) async {
47-
received.add(i);
48-
if (i == 1) {
49-
ss.pause();
50-
await Future.delayed(Duration(milliseconds: 100));
51-
Expect.listEquals([1], readyToSent);
52-
ss.cancel();
53-
}
54-
}
55-
);
47+
ss = s.listen((int i) async {
48+
received.add(i);
49+
if (i == 2) {
50+
ss.pause();
51+
await Future.delayed(Duration(milliseconds: 100));
52+
Expect.listEquals([1], sent);
53+
Expect.listEquals([1, 2], readyToSent);
54+
await ss.cancel();
55+
}
56+
});
5657
await Future.delayed(Duration(seconds: 1));
57-
Expect.listEquals([1], received);
58+
Expect.listEquals([1, 2], received);
5859
Expect.listEquals([1], sent);
5960
Expect.listEquals([1, 2], readyToSent);
6061
asyncEnd();
6162
}
62-
63-
main() {
64-
asyncStart();
65-
test();
66-
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2022, 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+
/// @assertion Execution of a statement s of the form yield e; proceeds as
6+
/// follows:
7+
/// First, the expression `e` is evaluated to an object `o`. If the enclosing
8+
/// function `m` is marked `async*` and the stream `u` associated with `m` has
9+
/// been paused, then the nearest enclosing asynchronous `for` loop if any, is
10+
/// paused and execution of `m` is suspended until `u` is resumed or canceled.
11+
/// Next, `o` is added to the iterable or stream associated with the
12+
/// immediately enclosing function.
13+
/// If the enclosing function `m` is marked `async*` and the stream `u`
14+
/// associated with `m` has been canceled, then the `yield` statement returns
15+
/// without an object, otherwise it completes normally.
16+
/// Otherwise, if the enclosing function `m` is marked `async*` then the
17+
/// enclosing function may suspend, in which case the nearest enclosing
18+
/// asynchronous `for` loop, if any, is paused first.
19+
///
20+
/// @description Check that if the enclosing function `m` is marked `async*` and
21+
/// the stream `u` associated with `m` has been paused, then the nearest
22+
/// enclosing asynchronous `for` loop is paused and execution of `m` is
23+
/// suspended until `u` is resumed
24+
///
25+
/// @author [email protected]
26+
27+
import 'dart:async';
28+
import '../../../../Utils/expect.dart';
29+
30+
List<int> readyToSent = [];
31+
List<int> sent = [];
32+
33+
Stream<int> generator() async* {
34+
for (int i = 1; i <= 5; i++) {
35+
readyToSent.add(i);
36+
yield i;
37+
sent.add(i);
38+
}
39+
}
40+
41+
main() async {
42+
List<int> received = [];
43+
Stream<int> stream = generator();
44+
await for (int i in stream) {
45+
received.add(i);
46+
// We have no access to StreamSubscription here to pause the stream and then
47+
// to check that this loop is also pause. So, let's check that the stream is
48+
// paused now
49+
await Future.delayed(Duration(milliseconds: 100));
50+
Expect.listEquals([for (var j = 1; j <= i; j++) j], received);
51+
Expect.listEquals([for (var j = 1; j <= i - 1; j++) j], sent);
52+
Expect.listEquals([for (var j = 1; j <= i; j++) j], readyToSent);
53+
}
54+
Expect.listEquals([1, 2, 3, 4, 5], received);
55+
Expect.listEquals([1, 2, 3, 4, 5], sent);
56+
Expect.listEquals([1, 2, 3, 4, 5], readyToSent);
57+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2022, 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+
/// @assertion Execution of a statement s of the form yield e; proceeds as
6+
/// follows:
7+
/// First, the expression `e` is evaluated to an object `o`. If the enclosing
8+
/// function `m` is marked `async*` and the stream `u` associated with `m` has
9+
/// been paused, then the nearest enclosing asynchronous `for` loop if any, is
10+
/// paused and execution of `m` is suspended until `u` is resumed or canceled.
11+
/// Next, `o` is added to the iterable or stream associated with the
12+
/// immediately enclosing function.
13+
/// If the enclosing function `m` is marked `async*` and the stream `u`
14+
/// associated with `m` has been canceled, then the `yield` statement returns
15+
/// without an object, otherwise it completes normally.
16+
/// Otherwise, if the enclosing function `m` is marked `async*` then the
17+
/// enclosing function may suspend, in which case the nearest enclosing
18+
/// asynchronous `for` loop, if any, is paused first.
19+
///
20+
/// @description Check that if the enclosing function `m` is marked `async*` and
21+
/// the stream `u` associated with `m` has been paused, then the nearest
22+
/// enclosing asynchronous `for` loop is paused and execution of `m` is
23+
/// suspended until `u` is canceled
24+
///
25+
/// @author [email protected]
26+
/// @issue 49451
27+
28+
import 'dart:async';
29+
import '../../../../Utils/expect.dart';
30+
31+
List<int> readyToSent = [];
32+
List<int> sent = [];
33+
34+
Stream<int> generator() async* {
35+
for (int i = 1; i <= 5; i++) {
36+
readyToSent.add(i);
37+
yield i;
38+
sent.add(i);
39+
}
40+
}
41+
42+
main() async {
43+
List<int> received = [];
44+
Stream<int> stream = generator();
45+
await for (int i in stream) {
46+
received.add(i);
47+
if (i == 3) {
48+
await Future.delayed(Duration(milliseconds: 100));
49+
Expect.listEquals([1, 2, 3], received);
50+
Expect.listEquals([1, 2], sent);
51+
Expect.listEquals([1, 2, 3], readyToSent);
52+
// stream is paused now. Let's cancel it
53+
break;
54+
}
55+
}
56+
await Future.delayed(Duration(seconds: 1));
57+
Expect.listEquals([1, 2, 3], received);
58+
Expect.listEquals([1, 2], sent);
59+
Expect.listEquals([1, 2, 3], readyToSent);
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/// @assertion Execution of a statement s of the form yield e; proceeds as
6+
/// follows:
7+
/// First, the expression `e` is evaluated to an object `o`. If the enclosing
8+
/// function `m` is marked `async*` and the stream `u` associated with `m` has
9+
/// been paused, then the nearest enclosing asynchronous `for` loop if any, is
10+
/// paused and execution of `m` is suspended until `u` is resumed or canceled.
11+
/// Next, `o` is added to the iterable or stream associated with the
12+
/// immediately enclosing function.
13+
/// If the enclosing function `m` is marked `async*` and the stream `u`
14+
/// associated with `m` has been canceled, then the `yield` statement returns
15+
/// without an object, otherwise it completes normally.
16+
/// Otherwise, if the enclosing function `m` is marked `async*` then the
17+
/// enclosing function may suspend, in which case the nearest enclosing
18+
/// asynchronous `for` loop, if any, is paused first.
19+
///
20+
/// @description Check that object o is added to stream associated with the
21+
/// immediately enclosing function.
22+
///
23+
/// @author [email protected]
24+
25+
import 'dart:async';
26+
import '../../../../Utils/expect.dart';
27+
28+
Stream<int> generator(Iterable<int> iterable) async* {
29+
for (var o in iterable) {
30+
yield o;
31+
}
32+
}
33+
34+
test() async {
35+
List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
36+
List<int> log = [];
37+
Stream<int> s = generator(data);
38+
s.listen(
39+
(int x) => log.add(x),
40+
onDone:() {
41+
Expect.listEquals(data, log);
42+
asyncEnd();
43+
}
44+
);
45+
}
46+
47+
main() {
48+
asyncStart();
49+
test();
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2022, 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+
/// @assertion Execution of a statement s of the form yield e; proceeds as
6+
/// follows:
7+
/// First, the expression `e` is evaluated to an object `o`. If the enclosing
8+
/// function `m` is marked `async*` and the stream `u` associated with `m` has
9+
/// been paused, then the nearest enclosing asynchronous `for` loop if any, is
10+
/// paused and execution of `m` is suspended until `u` is resumed or canceled.
11+
/// Next, `o` is added to the iterable or stream associated with the
12+
/// immediately enclosing function.
13+
/// If the enclosing function `m` is marked `async*` and the stream `u`
14+
/// associated with `m` has been canceled, then the `yield` statement returns
15+
/// without an object, otherwise it completes normally.
16+
/// Otherwise, if the enclosing function `m` is marked `async*` then the
17+
/// enclosing function may suspend, in which case the nearest enclosing
18+
/// asynchronous `for` loop, if any, is paused first.
19+
///
20+
/// @description Check that if the enclosing function `m` is marked `async*` and
21+
/// the stream `u`associated with `m` has been canceled, then the `yield`
22+
/// statement returns without an object, otherwise it completes normally.
23+
///
24+
/// @author [email protected]
25+
26+
import 'dart:async';
27+
import '../../../../Utils/expect.dart';
28+
29+
Stream<int> generator() async* {
30+
for (int i = 1; i <= 3; i++) {
31+
yield i;
32+
}
33+
}
34+
35+
test() async {
36+
List log = [];
37+
Stream<int> s = generator();
38+
late StreamSubscription<int> ss;
39+
ss = s.listen((int i) {
40+
log.add(i);
41+
ss.cancel();
42+
});
43+
await Future.delayed(Duration(seconds: 1));
44+
Expect.listEquals([1], log);
45+
asyncEnd();
46+
}
47+
48+
main() {
49+
asyncStart();
50+
test();
51+
}

0 commit comments

Comments
 (0)