Add tests for non-primitive equality in switch cases.

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>
This commit is contained in:
Robert Nystrom 2023-03-10 02:39:28 +00:00 committed by Commit Queue
parent 74fb9e190f
commit d52fe19dee
2 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,46 @@
// 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));
}

View file

@ -0,0 +1,77 @@
// 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
// With Dart 3.0, switch cases are pattersn and constants in cases are no
// longer required to have primitive equality.
import "package:expect/expect.dart";
class ValueType {
static String test(int value) {
switch (ValueType(value)) {
case const ValueType(1):
return 'one';
case const ValueType(2):
return 'two';
default:
return 'other';
}
}
final int value;
const ValueType(this.value);
bool operator ==(Object other) => other is ValueType && value == other.value;
}
class EquatableToString {
static String test(String value) {
switch (value) {
case const EquatableToString('ape'):
return 'primate';
case const EquatableToString('bat'):
return 'chiroptera';
default:
return 'other';
}
}
final String value;
const EquatableToString(this.value);
bool operator ==(Object other) => other is String && value == other;
}
String testDouble(double value) {
switch (value) {
case 0.0:
return 'nothing';
case 1.1:
return 'one-ish';
case 2.2:
return 'two-ish';
default:
return 'other';
}
}
main() {
Expect.equals('one', ValueType.test(1));
Expect.equals('two', ValueType.test(2));
Expect.equals('other', ValueType.test(3));
Expect.equals('primate', EquatableToString.test('ape'));
Expect.equals('chiroptera', EquatableToString.test('bat'));
Expect.equals('other', EquatableToString.test('cat'));
Expect.equals('nothing', testDouble(0.0));
Expect.equals('nothing', testDouble(-0.0));
Expect.equals('one-ish', testDouble(1.1));
Expect.equals('two-ish', testDouble(2.2));
Expect.equals('other', testDouble(3.3));
}