[tests] Enum shorthands - Additional bool in implicit contexts tests.
Testing `bool` static members and constructors for language constructs that have a context type of `bool`, such as if-statements and while loops.
Follow up to Lasse's suggestions here: https://dart-review.googlesource.com/c/sdk/+/397182/comment/8db4ffcc_d04110bf/
Bug: https://github.com/dart-lang/sdk/issues/57038
Change-Id: I539f904b30e680d9749906f4db1f204125a572e6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398020
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
diff --git a/tests/language/enum_shorthands/simple/simple_identifier_bool_context_error_test.dart b/tests/language/enum_shorthands/simple/simple_identifier_bool_context_error_test.dart
index d4e7315..52ed2c3 100644
--- a/tests/language/enum_shorthands/simple/simple_identifier_bool_context_error_test.dart
+++ b/tests/language/enum_shorthands/simple/simple_identifier_bool_context_error_test.dart
@@ -10,6 +10,11 @@
import '../enum_shorthand_helper.dart';
+extension type const Bool(bool _) implements bool {
+ static const Bool isTrue = Bool(true);
+ static const Bool isFalse = Bool(false);
+}
+
void main() {
if (.one) {
// ^
@@ -17,7 +22,19 @@
// [cfe] unspecified
print('not ok');
}
- while (.two) {
+ if (.isTrue) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ if (!.one) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ if (!.isTrue) {
// ^
// [analyzer] unspecified
// [cfe] unspecified
@@ -29,4 +46,57 @@
// [cfe] unspecified
print('not ok');
}
+ if (.isTrue || .isFalse) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ if (.one && .two) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ if (.isTrue && .isFalse) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ while (.two) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ while (.isTrue) {
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ print('not ok');
+ }
+ var counter = 0;
+ do {
+ counter++;
+ if (counter > 2) break;
+ } while (.two);
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ do {
+ counter++;
+ if (counter > 2) break;
+ } while (.isTrue);
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ assert(.two, '');
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
+ assert(.isTrue, '');
+ // ^
+ // [analyzer] unspecified
+ // [cfe] unspecified
}
diff --git a/tests/language/enum_shorthands/simple/simple_identifier_bool_from_environment_test.dart b/tests/language/enum_shorthands/simple/simple_identifier_bool_from_environment_test.dart
new file mode 100644
index 0000000..4d28dbd
--- /dev/null
+++ b/tests/language/enum_shorthands/simple/simple_identifier_bool_from_environment_test.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2024, 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.
+
+// When the context type is a language-defined bool (`if`, `||`, `while`),
+// using an enum shorthand will match the `fromEnvironment` member in the
+// `bool` class.
+
+// SharedOptions=--enable-experiment=enum-shorthands -Da=true
+
+import 'package:expect/expect.dart';
+
+void main() {
+ var now = DateTime.now().millisecondsSinceEpoch;
+ var yes = now > 0;
+ var no = now < 0;
+
+ Expect.equals(no || const .fromEnvironment("a"), true, "||");
+ Expect.equals(const .fromEnvironment("a") || no, true);
+ Expect.equals(yes && const .fromEnvironment("a"), true);
+ Expect.equals(const .fromEnvironment("a") && yes, true);
+ Expect.equals(!const .fromEnvironment("a"), false);
+
+ if (const .fromEnvironment("a")) {
+ // Success.
+ } else {
+ Expect.fail("Didn't find a");
+ }
+
+ var counter = 0;
+ while (const .fromEnvironment("a")) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "while loop condition");
+
+ counter = 0;
+ do {
+ counter++;
+ if (counter == 2) break;
+ } while (const .fromEnvironment("a"));
+ Expect.equals(counter, 2, "do-while loop condition");
+
+ counter = 0;
+ for (; const .fromEnvironment("a");) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "for loop condition");
+
+ if (now case > 0 when const .fromEnvironment("a")) {
+ // Success.
+ } else {
+ Expect.fail("if-case when");
+ }
+
+ switch (now) {
+ case > 0 when const .fromEnvironment("a"):
+ break;
+ case _:
+ Expect.fail("switch case when");
+ }
+
+ var expressionResult = switch (now) {
+ > 0 when const .fromEnvironment("a") => "success",
+ _ => "failure",
+ };
+ Expect.equals(expressionResult, "success", "switch expression case when");
+}
diff --git a/tests/language/enum_shorthands/simple/simple_identifier_bool_parse_test.dart b/tests/language/enum_shorthands/simple/simple_identifier_bool_parse_test.dart
new file mode 100644
index 0000000..27d2986
--- /dev/null
+++ b/tests/language/enum_shorthands/simple/simple_identifier_bool_parse_test.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2024, 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.
+
+// When the context type is a language-defined bool (`if`, `||`, `while`),
+// using an enum shorthand will match the `parse` member in the `bool` class.
+
+// SharedOptions=--enable-experiment=enum-shorthands
+
+import 'package:expect/expect.dart';
+
+void main() {
+ var now = DateTime.now().millisecondsSinceEpoch;
+ var yes = now > 0;
+ var no = now < 0;
+ var text = 'true';
+
+ Expect.equals(no || .parse(text), true, "||");
+ Expect.equals(.parse(text) || no, true);
+ Expect.equals(yes && .parse(text), true);
+ Expect.equals(.parse(text) && yes, true);
+ Expect.equals(!.parse(text), false);
+
+ if (.parse(text)) {
+ // Success.
+ } else {
+ Expect.fail("Didn't find banana");
+ }
+
+ var counter = 0;
+ while (.parse(text)) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "while loop condition");
+
+ counter = 0;
+ do {
+ counter++;
+ if (counter == 2) break;
+ } while (.parse(text));
+ Expect.equals(counter, 2, "do-while loop condition");
+
+ counter = 0;
+ for (; .parse(text);) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "for loop condition");
+
+ if (now case > 0 when .parse(text)) {
+ // Success.
+ } else {
+ Expect.fail("if-case when");
+ }
+
+ switch (now) {
+ case > 0 when .parse(text):
+ break;
+ case _:
+ Expect.fail("switch case when");
+ }
+
+ var expressionResult = switch (now) {
+ > 0 when .parse(text) => "success",
+ _ => "failure",
+ };
+ Expect.equals(expressionResult, "success", "switch expression case when");
+}
diff --git a/tests/language/enum_shorthands/simple/simple_identifier_bool_try_parse_test.dart b/tests/language/enum_shorthands/simple/simple_identifier_bool_try_parse_test.dart
new file mode 100644
index 0000000..94e409c
--- /dev/null
+++ b/tests/language/enum_shorthands/simple/simple_identifier_bool_try_parse_test.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2024, 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.
+
+// When the context type is a language-defined bool (`if`, `||`, `while`),
+// using an enum shorthand will match the `tryParse` member in the `bool` class.
+
+// SharedOptions=--enable-experiment=enum-shorthands
+
+import 'package:expect/expect.dart';
+
+void main() {
+ var now = DateTime.now().millisecondsSinceEpoch;
+ var yes = now > 0;
+ var no = now < 0;
+ var text = 'true';
+
+ Expect.equals(no || .tryParse(text)!, true, "||");
+ Expect.equals(.tryParse(text)! || no, true);
+ Expect.equals(yes && .tryParse(text)!, true);
+ Expect.equals(.tryParse(text)! && yes, true);
+ Expect.equals(!.tryParse(text)!, false);
+
+ if (.tryParse(text)!) {
+ // Success.
+ } else {
+ Expect.fail("Didn't find banana");
+ }
+
+ var counter = 0;
+ while (.tryParse(text)!) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "while loop condition");
+
+ counter = 0;
+ do {
+ counter++;
+ if (counter == 2) break;
+ } while (.tryParse(text)!);
+ Expect.equals(counter, 2, "do-while loop condition");
+
+ counter = 0;
+ for (; .tryParse(text)!;) {
+ counter++;
+ break;
+ }
+ Expect.equals(counter, 1, "for loop condition");
+
+ if (now case > 0 when .tryParse(text)!) {
+ // Success.
+ } else {
+ Expect.fail("if-case when");
+ }
+
+ switch (now) {
+ case > 0 when .tryParse(text)!:
+ break;
+ case _:
+ Expect.fail("switch case when");
+ }
+
+ var expressionResult = switch (now) {
+ > 0 when .tryParse(text)! => "success",
+ _ => "failure",
+ };
+ Expect.equals(expressionResult, "success", "switch expression case when");
+}
diff --git a/tests/language/enum_shorthands/simple/simple_identifier_iterable_context_test.dart b/tests/language/enum_shorthands/simple/simple_identifier_iterable_context_test.dart
new file mode 100644
index 0000000..f252fb7
--- /dev/null
+++ b/tests/language/enum_shorthands/simple/simple_identifier_iterable_context_test.dart
@@ -0,0 +1,17 @@
+// Copyright (c) 2024, 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.
+
+// Context type is language-defined `Iterable` or `Stream`.
+
+// SharedOptions=--enable-experiment=enum-shorthands
+
+void main() async {
+ var iter = [1, 2];
+ for (var x in .castFrom(iter)) {
+ print(x);
+ }
+ await for (var x in .fromIterable(iter)) {
+ print(x);
+ }
+}