Skip to content

Commit 3c9ba02

Browse files
authored
#1399. on clause tests added (#1416)
Authored by @sgrekhov.
1 parent 749e450 commit 3c9ba02

8 files changed

+336
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function
24+
/// @author [email protected]
25+
26+
// SharedOptions=--enable-experiment=records
27+
28+
import "../../Utils/expect.dart";
29+
30+
main() {
31+
bool caught = false;
32+
try {
33+
throw (42, "Lily was here");
34+
} on String {
35+
Expect.fail("Unexpected String exception");
36+
} on (int i, String n) {
37+
caught = true;
38+
}
39+
Expect.isTrue(caught);
40+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try` and correct clause is executed
24+
/// @author [email protected]
25+
26+
// SharedOptions=--enable-experiment=records
27+
28+
import "../../Utils/expect.dart";
29+
30+
main() {
31+
bool caught = false;
32+
try {
33+
throw (42, n: "Lily was here");
34+
} on String {
35+
Expect.fail("Unexpected String exception");
36+
} on int {
37+
Expect.fail("Unexpected int exception");
38+
} on (int, int) {
39+
Expect.fail("Unexpected (int, int) exception");
40+
} on (int i, {String? x}) {
41+
Expect.fail("Unexpected (int, {String? x}) exception");
42+
} on (int i, {String? n}) {
43+
caught = true;
44+
} on (num, String) {
45+
Expect.fail("Unexpected (int, {String? x}) exception");
46+
}
47+
Expect.isTrue(caught);
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function
24+
/// @author [email protected]
25+
26+
// SharedOptions=--enable-experiment=records
27+
28+
main() {
29+
try {
30+
print(0);
31+
} on String {
32+
} on (int i, {String? n}) {
33+
}
34+
on(42, n: "Lily was here");
35+
//^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function. Test the case when `on` clause looks like a
24+
/// function with optional positional parameters
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=records
28+
29+
main() {
30+
try {
31+
print(0);
32+
} on String {
33+
} on ([int? i, String? n]) {
34+
// ^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
on();
39+
//^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function. Test the case when `on` clause looks like a
24+
/// function with optional positional parameters and default values specified
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=records
28+
29+
main() {
30+
try {
31+
print(0);
32+
} on String {
33+
} on ([int i = 0, String n = ""]) {
34+
// ^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
on();
39+
//^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function. Test the case when `on` clause looks like a
24+
/// function with required named parameters
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=records
28+
29+
main() {
30+
try {
31+
print(0);
32+
} on String {
33+
} on (int i, {required String n}) {
34+
// ^^^^^^^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
on(42, n: "");
39+
//^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function. Test the case when `on` clause looks like a
24+
/// function with default values for optional parameters specified
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=records
28+
29+
main() {
30+
try {
31+
print(0);
32+
} on String {
33+
} on (int i, {String n = ""}) {
34+
// ^^^^^^^^^^^^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
on(42);
39+
//^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion Consider:
6+
///
7+
/// void foo() {
8+
/// try {
9+
/// ;
10+
/// } on Bar {
11+
/// ;
12+
/// }
13+
/// on(a, b) {;} // <--
14+
/// }
15+
/// Before, the marked line could only be declaring a local function named `on`.
16+
/// With record types, it could be a second `on` clause for the `try` statement
17+
/// whose matched type is the record type `(a, b)`. When presented with this
18+
/// ambiguity, we disambiguate by treating `on` as a clause for `try` and not a
19+
/// local function. This is technically a breaking change, but is unlikely to
20+
/// affect any code in the wild.
21+
///
22+
/// @description Checks that in the case above `on` is treated as a clause for
23+
/// `try`, not as a local function. Test the case when `on` clause looks like a
24+
/// function with type parameter
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=records
28+
29+
main() {
30+
try {
31+
print(0);
32+
} on String {
33+
} on<T>(int i, {String? n}) {
34+
// ^^
35+
// [analyzer] unspecified
36+
// [cfe] unspecified
37+
}
38+
on(42, n: "");
39+
//^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
}

0 commit comments

Comments
 (0)