mirror of
https://github.com/dart-lang/sdk
synced 2024-11-02 12:24:24 +00:00
2496c1a69a
Update two tests to Dart 3.0 semantics: - Switch case constants can be types that don't have primitive equality. - Switch cases don't need breaks. Change-Id: Icdea5b66cf12f675580a85b9d49c09db589e29d0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287400 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com>
149 lines
2.9 KiB
Dart
149 lines
2.9 KiB
Dart
// Copyright (c) 2012, 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.
|
|
|
|
// In Dart 3.0, switch cases implicitly exit, so it's not an error to have a
|
|
// case that contains only a function that throws. Pin to 2.19 to test that
|
|
// older error-reporting behavior.
|
|
// @dart=2.19
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
switcher(val) {
|
|
var x = 0;
|
|
switch (val) {
|
|
case 1:
|
|
x = 100;
|
|
break;
|
|
case 2:
|
|
x = 200;
|
|
break;
|
|
case 3:
|
|
x = 300;
|
|
break;
|
|
default:
|
|
return 400;
|
|
break; // Intentional dead code (regression test for crash).
|
|
}
|
|
return x;
|
|
}
|
|
|
|
// Check unambiguated grammar allowing multiple labels per case/default.
|
|
switcher2(val) {
|
|
var x = 0;
|
|
switch (val) {
|
|
foo:
|
|
bar:
|
|
case 1:
|
|
baz:
|
|
case 2:
|
|
fez:
|
|
{
|
|
x = 100;
|
|
break fez;
|
|
}
|
|
break;
|
|
hest:
|
|
fisk:
|
|
case 3:
|
|
case 4:
|
|
svin:
|
|
default:
|
|
barber:
|
|
{
|
|
if (val > 2) {
|
|
x = 200;
|
|
break;
|
|
} else {
|
|
// Enable when continue to switch-case is implemented.
|
|
continue hest;
|
|
}
|
|
}
|
|
}
|
|
return x;
|
|
}
|
|
|
|
var x = 0;
|
|
|
|
@pragma('dart2js:noInline')
|
|
switcher3(val) {
|
|
switch (val) {
|
|
case 1:
|
|
default:
|
|
incrementX();
|
|
}
|
|
}
|
|
|
|
// Tests that switch cases that don't exit report an error even when the case
|
|
// body calls a function that the compiler can infer always throws, and there
|
|
// is no break in the switch case body.
|
|
switcher4(val) {
|
|
switch (val) {
|
|
case 1:
|
|
return 100;
|
|
case 2: _throw(); //# 00: compile-time error
|
|
case 3:
|
|
_throw();
|
|
break;
|
|
default:
|
|
return 300;
|
|
}
|
|
}
|
|
|
|
_throw() {
|
|
throw 'exception';
|
|
}
|
|
|
|
// Tests that we generate a break after the last case if it isn't default.
|
|
switcher5(val) {
|
|
var x = 0;
|
|
switch(val) {
|
|
case 1:
|
|
return 100;
|
|
case 2:
|
|
return 200;
|
|
case 3:
|
|
x = 300;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
incrementX() {
|
|
x++;
|
|
}
|
|
|
|
badswitches(val) {
|
|
// Test some badly formed switch bodies.
|
|
// 01 - a label/statement without a following case/default.
|
|
// 02 - a label without a following case/default or statement.
|
|
switch (val) {
|
|
foo: break; // //# 01: compile-time error
|
|
case 2: // //# 02: compile-time error
|
|
foo: // //# 02: continued
|
|
}
|
|
}
|
|
|
|
main() {
|
|
Expect.equals(100, switcher(1));
|
|
Expect.equals(200, switcher(2));
|
|
Expect.equals(300, switcher(3));
|
|
Expect.equals(400, switcher(4));
|
|
|
|
Expect.equals(100, switcher2(1));
|
|
Expect.equals(100, switcher2(2));
|
|
Expect.equals(200, switcher2(3));
|
|
Expect.equals(200, switcher2(4));
|
|
Expect.equals(200, switcher2(5));
|
|
|
|
|
|
switcher3(1);
|
|
Expect.equals(1, x);
|
|
|
|
Expect.equals(100, switcher4(1));
|
|
|
|
Expect.equals(100, switcher5(1));
|
|
Expect.equals(200, switcher5(2));
|
|
Expect.equals(300, switcher5(3));
|
|
|
|
badswitches(42);
|
|
}
|