Files
sdk/tests/language/switch/equality_side_effect_test.dart
T
Sergey G. Grekhov 82b78916fd [tests] Remove obsolete Dart 3.0 experiments from language tests
Change-Id: If31c487e3ebe2c1ae847aff7c8994580b8b6f2f6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309660
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
2023-06-15 08:26:27 +00:00

45 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.
// 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));
}