mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 06:20:13 +00:00
Migrate language_2/sync_star to NNBD.
Change-Id: I184c984b65f15dabc647c510295c3dc82be3a203 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151473 Auto-Submit: Bob Nystrom <rnystrom@google.com> Commit-Queue: Bob Nystrom <rnystrom@google.com> Commit-Queue: Srujan Gaddam <srujzs@google.com> Reviewed-by: Srujan Gaddam <srujzs@google.com>
This commit is contained in:
parent
f638fe385b
commit
7e5fe50b24
10 changed files with 535 additions and 0 deletions
19
tests/language/sync_star/covariant_type_test.dart
Normal file
19
tests/language/sync_star/covariant_type_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Copyright (c) 2018, 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.
|
||||
|
||||
// Test that TypeErrors happen for sync* methods without creating iterator.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
class D<T> {
|
||||
// Parametric covariance check is usually compiled into method.
|
||||
Iterable<T> add(T n) sync* {
|
||||
yield n;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
D<num> d = new D<int>();
|
||||
Expect.throwsTypeError(() => d.add(4.6));
|
||||
}
|
27
tests/language/sync_star/dcall_type_test.dart
Normal file
27
tests/language/sync_star/dcall_type_test.dart
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) 2018, 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.
|
||||
|
||||
// Test that TypeErrors happen for sync* methods without creating iterator.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
Iterable<int> iota(int n) sync* {
|
||||
for (int i = 0; i < n; i++) yield i;
|
||||
}
|
||||
|
||||
class C {
|
||||
Iterable<int> add(int n) sync* {
|
||||
yield n;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
dynamic f = iota;
|
||||
Expect.throwsTypeError(() => f('ten'));
|
||||
Expect.throwsTypeError(() => f(4.7));
|
||||
|
||||
dynamic o = new C();
|
||||
Expect.throwsTypeError(() => o.add('ten'));
|
||||
Expect.throwsTypeError(() => o.add(4.7));
|
||||
}
|
90
tests/language/sync_star/generator1_runtime_test.dart
Normal file
90
tests/language/sync_star/generator1_runtime_test.dart
Normal file
|
@ -0,0 +1,90 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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.
|
||||
|
||||
// Simple test program for sync* generator functions.
|
||||
|
||||
// VMOptions=--optimization_counter_threshold=10
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
sum10() sync* {
|
||||
var s = 0;
|
||||
for (var k = 1; k <= 10; k++) {
|
||||
s += k;
|
||||
yield s;
|
||||
}
|
||||
}
|
||||
|
||||
class Range {
|
||||
int start, end;
|
||||
Range(this.start, this.end);
|
||||
elements() sync* {
|
||||
var e = start;
|
||||
while (e <= end) yield e++;
|
||||
}
|
||||
|
||||
get yield sync* {
|
||||
// yield is a legal member name here.
|
||||
var e = start;
|
||||
while (e <= end) yield e++;
|
||||
}
|
||||
}
|
||||
|
||||
get sync sync* {
|
||||
// sync is a legal identifier.
|
||||
yield "sync";
|
||||
}
|
||||
|
||||
einsZwei() sync* {
|
||||
yield 1;
|
||||
yield* [2, 3];
|
||||
yield* [];
|
||||
yield 5;
|
||||
yield [6];
|
||||
}
|
||||
|
||||
dreiVier() sync* {
|
||||
// Throws type error: yielded object is not an iterable.
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
var sums = sum10();
|
||||
print(sums);
|
||||
Expect.isTrue(sums is Iterable);
|
||||
Expect.equals(10, sums.length);
|
||||
Expect.equals(1, sums.first);
|
||||
Expect.equals(55, sums.last);
|
||||
var q = "";
|
||||
for (var n in sums.take(3)) {
|
||||
q += "$n ";
|
||||
}
|
||||
Expect.equals("1 3 6 ", q);
|
||||
|
||||
var r = new Range(10, 12);
|
||||
var elems1 = r.elements();
|
||||
print(elems1);
|
||||
var elems2 = r.yield;
|
||||
print(elems2);
|
||||
// Walk the elements of each iterable and compare them.
|
||||
var i = elems1.iterator;
|
||||
Expect.isTrue(i is Iterator);
|
||||
elems2.forEach((e) {
|
||||
Expect.isTrue(i.moveNext());
|
||||
Expect.equals(e, i.current);
|
||||
});
|
||||
|
||||
print(sync);
|
||||
Expect.equals("sync", sync.single);
|
||||
|
||||
print(einsZwei());
|
||||
Expect.equals("(1, 2, 3, 5, [6])", einsZwei().toString());
|
||||
|
||||
|
||||
}
|
||||
}
|
90
tests/language/sync_star/generator1_test.dart
Normal file
90
tests/language/sync_star/generator1_test.dart
Normal file
|
@ -0,0 +1,90 @@
|
|||
// 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.
|
||||
|
||||
// Simple test program for sync* generator functions.
|
||||
|
||||
// VMOptions=--optimization_counter_threshold=10
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
sum10() sync* {
|
||||
var s = 0;
|
||||
for (var k = 1; k <= 10; k++) {
|
||||
s += k;
|
||||
yield s;
|
||||
}
|
||||
}
|
||||
|
||||
class Range {
|
||||
int start, end;
|
||||
Range(this.start, this.end);
|
||||
elements() sync* {
|
||||
var e = start;
|
||||
while (e <= end) yield e++;
|
||||
}
|
||||
|
||||
get yield sync* {
|
||||
// yield is a legal member name here.
|
||||
var e = start;
|
||||
while (e <= end) yield e++;
|
||||
}
|
||||
}
|
||||
|
||||
get sync sync* {
|
||||
// sync is a legal identifier.
|
||||
yield "sync";
|
||||
}
|
||||
|
||||
einsZwei() sync* {
|
||||
yield 1;
|
||||
yield* [2, 3];
|
||||
yield* [];
|
||||
yield 5;
|
||||
yield [6];
|
||||
}
|
||||
|
||||
dreiVier() sync* {
|
||||
// Throws type error: yielded object is not an iterable.
|
||||
yield* 3;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.YIELD_OF_INVALID_TYPE
|
||||
// [cfe] A value of type 'int' can't be assigned to a variable of type 'Iterable<dynamic>'.
|
||||
}
|
||||
|
||||
main() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
var sums = sum10();
|
||||
print(sums);
|
||||
Expect.isTrue(sums is Iterable);
|
||||
Expect.equals(10, sums.length);
|
||||
Expect.equals(1, sums.first);
|
||||
Expect.equals(55, sums.last);
|
||||
var q = "";
|
||||
for (var n in sums.take(3)) {
|
||||
q += "$n ";
|
||||
}
|
||||
Expect.equals("1 3 6 ", q);
|
||||
|
||||
var r = new Range(10, 12);
|
||||
var elems1 = r.elements();
|
||||
print(elems1);
|
||||
var elems2 = r.yield;
|
||||
print(elems2);
|
||||
// Walk the elements of each iterable and compare them.
|
||||
var i = elems1.iterator;
|
||||
Expect.isTrue(i is Iterator);
|
||||
elems2.forEach((e) {
|
||||
Expect.isTrue(i.moveNext());
|
||||
Expect.equals(e, i.current);
|
||||
});
|
||||
|
||||
print(sync);
|
||||
Expect.equals("sync", sync.single);
|
||||
|
||||
print(einsZwei());
|
||||
Expect.equals("(1, 2, 3, 5, [6])", einsZwei().toString());
|
||||
|
||||
Expect.throws(() => dreiVier().toString());
|
||||
}
|
||||
}
|
65
tests/language/sync_star/generator2_test.dart
Normal file
65
tests/language/sync_star/generator2_test.dart
Normal file
|
@ -0,0 +1,65 @@
|
|||
// 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.
|
||||
|
||||
// Simple test program for sync* generator functions.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
import "dart:async";
|
||||
|
||||
var sync = "topLevelSync";
|
||||
var async = "topLevelAync";
|
||||
var await = "topLevelAwait";
|
||||
var yield = "topLevelYield";
|
||||
|
||||
test01() sync* {
|
||||
var yield = 0; // //# 01: syntax error
|
||||
var await = 0; // //# 02: syntax error
|
||||
bool yield() => false; //# 04: syntax error
|
||||
bool await() => false; //# 05: syntax error
|
||||
|
||||
var x1 = sync;
|
||||
var x3 = await; // //# 08: syntax error
|
||||
var x4 = await 55; // //# 09: compile-time error
|
||||
var x4 = yield; // //# 10: syntax error
|
||||
|
||||
var stream = new Stream.fromIterable([1, 2, 3]);
|
||||
await for (var e in stream) print(e); // //# 11: compile-time error
|
||||
}
|
||||
|
||||
test02() sync* {
|
||||
yield 12321;
|
||||
return null; // //# 20: compile-time error
|
||||
}
|
||||
|
||||
test03() sync* => null; // //# 30: syntax error
|
||||
|
||||
get test04 sync* => null; // //# 40: syntax error
|
||||
set test04(a) sync* { print(a); } // //# 41: compile-time error
|
||||
|
||||
class K {
|
||||
K() sync* {} // //# 50: compile-time error
|
||||
get nix sync* {}
|
||||
get garnix sync* => null; // //# 51: syntax error
|
||||
set etwas(var z) sync* { } // //# 52: compile-time error
|
||||
sync() sync* {
|
||||
yield sync; // Yields a tear-off of the sync() method.
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
var x;
|
||||
x = test01();
|
||||
Expect.equals("()", x.toString());
|
||||
x = test02();
|
||||
test03(); //# 30: continued
|
||||
Expect.equals("(12321)", x.toString());
|
||||
x = test04; // //# 40: continued
|
||||
test04 = x; // //# 41: continued
|
||||
x = new K();
|
||||
print(x.garnix); //# 51: continued
|
||||
x.etwas = null; //# 52: continued
|
||||
print(x.sync().toList());
|
||||
Expect.equals(1, x.sync().length);
|
||||
// Expect.isTrue(x.sync().single is Function);
|
||||
}
|
48
tests/language/sync_star/generator3_test.dart
Normal file
48
tests/language/sync_star/generator3_test.dart
Normal file
|
@ -0,0 +1,48 @@
|
|||
// 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.
|
||||
|
||||
// Test program for sync* generator functions and yielding in try blocks.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
f() sync* {
|
||||
try {
|
||||
yield 1;
|
||||
throw "three";
|
||||
} catch (e) {
|
||||
yield 2;
|
||||
yield e;
|
||||
} finally {
|
||||
yield 4;
|
||||
}
|
||||
}
|
||||
|
||||
test1() {
|
||||
var s = f().toString();
|
||||
Expect.equals("(1, 2, three, 4)", s);
|
||||
print(s);
|
||||
}
|
||||
|
||||
g() sync* {
|
||||
try {
|
||||
yield "a";
|
||||
throw "pow!";
|
||||
} finally {
|
||||
yield "b";
|
||||
}
|
||||
}
|
||||
|
||||
test2() {
|
||||
Iterator i = g().iterator;
|
||||
Expect.isTrue(i.moveNext());
|
||||
Expect.equals("a", i.current);
|
||||
Expect.isTrue(i.moveNext());
|
||||
Expect.equals("b", i.current);
|
||||
Expect.throws(() => i.moveNext(), (error) => error == "pow!");
|
||||
}
|
||||
|
||||
main() {
|
||||
test1(); // //# test1: ok
|
||||
test2(); // //# test2: ok
|
||||
}
|
18
tests/language/sync_star/less_than_test.dart
Normal file
18
tests/language/sync_star/less_than_test.dart
Normal file
|
@ -0,0 +1,18 @@
|
|||
// Copyright (c) 2015, the Dart Team. 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:expect/expect.dart";
|
||||
|
||||
confuse(x) => [1, 'x', true, null, x].last;
|
||||
|
||||
Iterable<int> foo() sync* {
|
||||
var a = confuse(1);
|
||||
if (a < 10) {
|
||||
yield 2;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.listEquals([2], foo().toList());
|
||||
}
|
43
tests/language/sync_star/yield_star_pause_test.dart
Normal file
43
tests/language/sync_star/yield_star_pause_test.dart
Normal file
|
@ -0,0 +1,43 @@
|
|||
// 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";
|
||||
|
||||
// Regression test for http://dartbug.com/27205
|
||||
// If a yield-star completes while the stream is paused, it didn't resume.
|
||||
|
||||
main() {
|
||||
asyncTest(() {
|
||||
var c = new Completer();
|
||||
var s = yieldStream(mkStream());
|
||||
var sub;
|
||||
sub = s.listen((v) {
|
||||
sub.pause();
|
||||
print(v);
|
||||
Timer.run(sub.resume);
|
||||
}, onDone: () {
|
||||
print("DONE");
|
||||
c.complete(null);
|
||||
});
|
||||
return c.future;
|
||||
});
|
||||
}
|
||||
|
||||
Stream yieldStream(Stream s) async* {
|
||||
yield* s;
|
||||
}
|
||||
|
||||
Stream mkStream() {
|
||||
var s = new StreamController(sync: true);
|
||||
// The close event has to be sent and received between
|
||||
// the pause and resume above.
|
||||
// Using a sync controller and a Timer.run(sub.resume) ensures this.
|
||||
Timer.run(() {
|
||||
s.add("event");
|
||||
s.close();
|
||||
});
|
||||
return s.stream;
|
||||
}
|
106
tests/language/sync_star/yield_test.dart
Normal file
106
tests/language/sync_star/yield_test.dart
Normal file
|
@ -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.
|
||||
|
||||
// TODO(jmesserly): this is a copy of the language test of the same name,
|
||||
// we can remove this copy when we're running against those tests.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
Iterable<int> foo1() sync* {
|
||||
yield 1;
|
||||
}
|
||||
|
||||
Iterable<int?> foo2(p) sync* {
|
||||
bool t = false;
|
||||
yield null;
|
||||
while (true) {
|
||||
a:
|
||||
for (int i = 0; i < p; i++) {
|
||||
if (!t) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
yield -1;
|
||||
t = true;
|
||||
break a;
|
||||
}
|
||||
}
|
||||
yield i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// p is copied to all Iterators from the Iterable returned by foo3.
|
||||
// Also each iterator will have its own i.
|
||||
Iterable<int> foo3(int p) sync* {
|
||||
int i = 0;
|
||||
i++;
|
||||
p++;
|
||||
yield p + i;
|
||||
}
|
||||
|
||||
void testCapturingInSyncStar() {
|
||||
int localL0 = 0;
|
||||
|
||||
nested1(int paramL1) sync* {
|
||||
int localL1 = 0;
|
||||
localL0 += 10;
|
||||
paramL1 += 100;
|
||||
|
||||
nested2(int paramL2) sync* {
|
||||
int localL2 = 0;
|
||||
localL0 += 1000;
|
||||
paramL1 += 10000;
|
||||
localL1 += 100000;
|
||||
paramL2 += 1000000;
|
||||
localL2 += 10000000;
|
||||
|
||||
yield localL0 + paramL1 + localL1 + paramL2 + localL2;
|
||||
}
|
||||
|
||||
yield nested2(0);
|
||||
}
|
||||
|
||||
Iterable t1 = nested1(0);
|
||||
|
||||
Iterator it11 = t1.iterator;
|
||||
Iterator it12 = t1.iterator;
|
||||
it11.moveNext();
|
||||
it12.moveNext();
|
||||
|
||||
Iterable t2 = it11.current;
|
||||
Iterable t3 = it12.current;
|
||||
Iterator it21 = t2.iterator;
|
||||
Iterator it22 = t2.iterator;
|
||||
Iterator it31 = t3.iterator;
|
||||
Iterator it32 = t3.iterator;
|
||||
|
||||
it21.moveNext();
|
||||
it22.moveNext();
|
||||
it31.moveNext();
|
||||
it32.moveNext();
|
||||
|
||||
Expect.equals(11111120, it21.current);
|
||||
Expect.equals(11222120, it22.current);
|
||||
Expect.equals(11113120, it31.current);
|
||||
Expect.equals(11224120, it32.current);
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.listEquals([1], foo1().toList());
|
||||
Expect.listEquals(
|
||||
[null, -1, 0, 1, 2, 3, 0, 1, 2, 3], foo2(4).take(10).toList());
|
||||
Iterable t = foo3(0);
|
||||
Iterator it1 = t.iterator;
|
||||
Iterator it2 = t.iterator; //# copyParameters: ok
|
||||
it1.moveNext();
|
||||
it2.moveNext(); //# copyParameters: continued
|
||||
Expect.equals(2, it1.current);
|
||||
// TODO(sigurdm): Check up on the spec here.
|
||||
Expect.equals(2, it2.current); // //# copyParameters: continued
|
||||
Expect.isFalse(it1.moveNext());
|
||||
// Test that two `moveNext()` calls are fine.
|
||||
Expect.isFalse(it1.moveNext());
|
||||
Expect.isFalse(it2.moveNext()); //# copyParameters: continued
|
||||
Expect.isFalse(it2.moveNext()); //# copyParameters: continued
|
||||
|
||||
testCapturingInSyncStar(); //# capturing: ok
|
||||
}
|
29
tests/language/sync_star/yieldstar_test.dart
Normal file
29
tests/language/sync_star/yieldstar_test.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
// 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.
|
||||
|
||||
// TODO(jmesserly): this is a copy of the language test of the same name,
|
||||
// we can remove this copy when we're running against those tests.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
bar() sync* {
|
||||
int i = 1;
|
||||
int j = 1;
|
||||
while (true) {
|
||||
yield i;
|
||||
j = i + j;
|
||||
i = j - i;
|
||||
}
|
||||
}
|
||||
|
||||
foo() sync* {
|
||||
yield* [1, 2, 3];
|
||||
yield null;
|
||||
// TODO(jmesserly): added cast here to work around:
|
||||
// https://codereview.chromium.org/1213503002/
|
||||
yield* bar() as Iterable;
|
||||
}
|
||||
|
||||
main() async {
|
||||
Expect.listEquals([1, 2, 3, null, 1, 1, 2, 3, 5], foo().take(9).toList());
|
||||
}
|
Loading…
Reference in a new issue