dart-sdk/tests/language/control_flow_collections/for_runtime_error_test.dart
Robert Nystrom a4b09ac192 Migrate language_2/control_flow_collections to NNBD.
Change-Id: I8b9b7d70abc47d672c68db65bc67d08beb7d41a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142089
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2020-04-04 00:04:27 +00:00

28 lines
1.1 KiB
Dart

// Copyright (c) 2019, 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:expect/expect.dart';
void main() {
// Cast for variable.
dynamic nonInt = "string";
Expect.throwsTypeError(() => <int>[for (int i = nonInt; false;) 1]);
Expect.throwsTypeError(() => <int, int>{for (int i = nonInt; false;) 1: 1});
Expect.throwsTypeError(() => <int>{for (int i = nonInt; false;) 1});
// Cast for-in variable.
dynamic nonIterable = 3;
Expect.throwsTypeError(() => <int>[for (int i in nonIterable) 1]);
Expect.throwsTypeError(() => <int, int>{for (int i in nonIterable) 1: 1});
Expect.throwsTypeError(() => <int>{for (int i in nonIterable) 1});
// Wrong element type.
Expect.throwsTypeError(() => <int>[for (var i = 0; i < 1; i++) nonInt]);
Expect.throwsTypeError(
() => <int, int>{for (var i = 0; i < 1; i++) nonInt: 1});
Expect.throwsTypeError(
() => <int, int>{for (var i = 0; i < 1; i++) 1: nonInt});
Expect.throwsTypeError(() => <int>{for (var i = 0; i < 1; i++) nonInt});
}