| Index: tests/compiler/dart2js/type_inference_switch_test.dart
|
| diff --git a/tests/compiler/dart2js/type_inference_switch_test.dart b/tests/compiler/dart2js/type_inference_switch_test.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..795684cdff0417a96c1f84cde53e9e4b17ff0f39
|
| --- /dev/null
|
| +++ b/tests/compiler/dart2js/type_inference_switch_test.dart
|
| @@ -0,0 +1,159 @@
|
| +// Copyright (c) 2011, 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_helper.dart";
|
| +import "package:expect/expect.dart";
|
| +import 'compiler_helper.dart';
|
| +import 'type_mask_test_helper.dart';
|
| +
|
| +import 'dart:async';
|
| +
|
| +const String TEST1 = r"""
|
| +foo(int x) {
|
| + var a = "one";
|
| + switch (x) {
|
| + case 1:
|
| + a = "two";
|
| + break;
|
| + case 2:
|
| + break;
|
| + }
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +const String TEST2 = r"""
|
| +foo(int x) {
|
| + var a;
|
| + switch (x) {
|
| + case 1:
|
| + a = "two";
|
| + break;
|
| + case 2:
|
| + break;
|
| + }
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +const String TEST3 = r"""
|
| +foo(int x) {
|
| + var a;
|
| + switch (x) {
|
| + case 1:
|
| + a = 1;
|
| + case 2: // illegal fall through
|
| + a = 2;
|
| + break;
|
| + }
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +const String TEST4 = r"""
|
| +foo(int x) {
|
| + var a;
|
| + switch (x) {
|
| + case 1:
|
| + a = 1;
|
| + case 2: // illegal fall through
|
| + a = 2;
|
| + break;
|
| + default:
|
| + a = 0;
|
| + }
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +const String TEST5 = r"""
|
| +foo(int x) {
|
| + var a;
|
| + switch (x) {
|
| + case 1:
|
| + a = 1;
|
| + break;
|
| + case 2:
|
| + a = 2;
|
| + break;
|
| + default:
|
| + }
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +const String TEST6 = r"""
|
| +foo(int x) {
|
| + var a;
|
| + do { // add extra locals scope
|
| + switch (x) {
|
| + case 1:
|
| + a = 1;
|
| + break;
|
| + case 2:
|
| + a = 2;
|
| + break;
|
| + }
|
| + } while (false);
|
| +
|
| + return a;
|
| +}
|
| +
|
| +main() {
|
| + foo(new DateTime.now().millisecondsSinceEpoch);
|
| +}
|
| +""";
|
| +
|
| +Future runTest(String test, checker) {
|
| + Uri uri = new Uri(scheme: 'source');
|
| + var compiler = compilerFor(test, uri);
|
| +
|
| + checkTypeOf(String name, TypeMask type) {
|
| + var typesTask = compiler.typesTask;
|
| + var typesInferrer = typesTask.typesInferrer;
|
| + var element = findElement(compiler, name);
|
| + var mask = typesInferrer.getReturnTypeOfElement(element);
|
| + Expect.equals(type, simplify(mask, compiler));
|
| + }
|
| +
|
| + return compiler.run(uri).then((_) {
|
| + checker(compiler.typesTask, checkTypeOf);
|
| + });
|
| +}
|
| +
|
| +main() {
|
| + asyncTest(() async {
|
| + await runTest(TEST1, (t, c) => c("foo", t.stringType));
|
| + await runTest(TEST2, (t, c) => c("foo", t.stringType.nullable()));
|
| + await runTest(TEST3, (t, c) => c("foo", t.uint31Type.nullable()));
|
| + await runTest(TEST4, (t, c) => c("foo", t.uint31Type));
|
| + await runTest(TEST5, (t, c) => c("foo", t.uint31Type.nullable()));
|
| + await runTest(TEST6, (t, c) => c("foo", t.uint31Type.nullable()));
|
| + });
|
| +}
|
|
|