d52fe19dee
Even when a switch case only contains a constant (and not some more complex pattern), Dart 3.0 changes the semantics and allows constants of types that don't have primitive equality. Add some tests for that. Change-Id: Id3578d017822695451d7e44590fa349a8191a310 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287663 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Erik Ernst <eernst@google.com>
47 lines
1.2 KiB
Dart
47 lines
1.2 KiB
Dart
// 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
|
|
|
|
// Switch cases may call user-defined `==` methods, which can have arbitrary
|
|
// side effects. Test that the cases are tried in order.
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
class SideEffect {
|
|
static final effects = <String>[];
|
|
|
|
static String test(int value) {
|
|
effects.clear();
|
|
|
|
switch (SideEffect(value)) {
|
|
case const SideEffect(1):
|
|
effects.add('match one');
|
|
case const SideEffect(2):
|
|
effects.add('match two');
|
|
default:
|
|
effects.add('no match');
|
|
}
|
|
|
|
return effects.join(', ');
|
|
}
|
|
|
|
final int value;
|
|
|
|
const SideEffect(this.value);
|
|
|
|
String toString() => 'S($value)';
|
|
|
|
bool operator ==(Object other) {
|
|
effects.add('$this == $other');
|
|
return other is SideEffect && value == other.value;
|
|
}
|
|
}
|
|
|
|
main() {
|
|
Expect.equals('S(1) == S(1), match one', SideEffect.test(1));
|
|
Expect.equals('S(1) == S(2), S(2) == S(2), match two', SideEffect.test(2));
|
|
Expect.equals('S(1) == S(3), S(2) == S(3), no match', SideEffect.test(3));
|
|
}
|