mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 08:51:21 +00:00
[test] Add test for switch on values with primitive equality
Change-Id: I8de3234f1224e7b0e68b4c1ed190eaa49cbae68f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290500 Commit-Queue: Johnni Winther <johnniwinther@google.com> Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
parent
f20e6d3fa0
commit
fd63f7ac64
120
tests/language/patterns/primitive_switch_test.dart
Normal file
120
tests/language/patterns/primitive_switch_test.dart
Normal file
|
@ -0,0 +1,120 @@
|
|||
// Copyright (c) 2023, 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.
|
||||
|
||||
// SharedOptions=--enable-experiment=patterns,records
|
||||
|
||||
// Verify that switches using only values with primitive equality (excluding
|
||||
// the exceptions Symbol and Type) are lowered correctly. These are special
|
||||
// cased by the CFE.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
abstract class I {}
|
||||
|
||||
enum A implements I {
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
}
|
||||
|
||||
enum B implements I {
|
||||
a,
|
||||
b,
|
||||
c,
|
||||
}
|
||||
|
||||
int switchExpressionA(A a) => switch (a) {
|
||||
A.a => 0,
|
||||
A.b => 1,
|
||||
A.c => 2,
|
||||
};
|
||||
|
||||
int switchStatementA(A a) {
|
||||
switch (a) {
|
||||
case A.a:
|
||||
return 0;
|
||||
case A.b:
|
||||
return 1;
|
||||
case A.c:
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
int switchExpressionB(B b) => switch (b) {
|
||||
B.a => 3,
|
||||
B.b => 4,
|
||||
B.c => 5,
|
||||
};
|
||||
|
||||
int switchStatementB(B b) {
|
||||
switch (b) {
|
||||
case B.a:
|
||||
return 3;
|
||||
case B.b:
|
||||
return 4;
|
||||
case B.c:
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
|
||||
int switchExpressionI(I i) => switch (i) {
|
||||
A.a => 0,
|
||||
A.b => 1,
|
||||
A.c => 2,
|
||||
B.a => 3,
|
||||
B.b => 4,
|
||||
B.c => 5,
|
||||
_ => -1,
|
||||
};
|
||||
|
||||
int switchStatementI(I i) {
|
||||
switch (i) {
|
||||
case A.a:
|
||||
return 0;
|
||||
case A.b:
|
||||
return 1;
|
||||
case A.c:
|
||||
return 2;
|
||||
case B.a:
|
||||
return 3;
|
||||
case B.b:
|
||||
return 4;
|
||||
case B.c:
|
||||
return 5;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.equals(0, switchExpressionA(A.a));
|
||||
Expect.equals(1, switchExpressionA(A.b));
|
||||
Expect.equals(2, switchExpressionA(A.c));
|
||||
|
||||
Expect.equals(0, switchStatementA(A.a));
|
||||
Expect.equals(1, switchStatementA(A.b));
|
||||
Expect.equals(2, switchStatementA(A.c));
|
||||
|
||||
Expect.equals(3, switchExpressionB(B.a));
|
||||
Expect.equals(4, switchExpressionB(B.b));
|
||||
Expect.equals(5, switchExpressionB(B.c));
|
||||
|
||||
Expect.equals(3, switchStatementB(B.a));
|
||||
Expect.equals(4, switchStatementB(B.b));
|
||||
Expect.equals(5, switchStatementB(B.c));
|
||||
|
||||
Expect.equals(0, switchExpressionI(A.a));
|
||||
Expect.equals(1, switchExpressionI(A.b));
|
||||
Expect.equals(2, switchExpressionI(A.c));
|
||||
Expect.equals(3, switchExpressionI(B.a));
|
||||
Expect.equals(4, switchExpressionI(B.b));
|
||||
Expect.equals(5, switchExpressionI(B.c));
|
||||
|
||||
Expect.equals(0, switchStatementI(A.a));
|
||||
Expect.equals(1, switchStatementI(A.b));
|
||||
Expect.equals(2, switchStatementI(A.c));
|
||||
Expect.equals(3, switchStatementI(B.a));
|
||||
Expect.equals(4, switchStatementI(B.b));
|
||||
Expect.equals(5, switchStatementI(B.c));
|
||||
}
|
Loading…
Reference in a new issue