Skip to content

Commit b18d74e

Browse files
authored
#1401. [Patterns] Object pattern test added (#1652)
Test objectPattern with omitted getter name in a declaration context
1 parent cd25041 commit b18d74e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 objectPattern ::= typeName typeArguments? '(' patternFields? ')'
6+
/// ...
7+
/// It is a compile-time error if:
8+
///
9+
/// `typeNam`e does not refer to a type.
10+
///
11+
/// A type argument list is present and does not match the arity of the type of
12+
/// `typeName`.
13+
///
14+
/// A `patternField` is of the form `pattern`. Positional fields aren't allowed.
15+
///
16+
/// Any two named fields have the same name. This applies to both explicit and
17+
/// inferred field names.
18+
///
19+
/// It is a compile-time error if a name cannot be inferred for a named getter
20+
/// pattern with the getter name omitted (see name inference below).
21+
///
22+
/// @description Checks that it is a compile-time error if a name cannot be
23+
/// inferred for a named getter pattern with the getter name omitted. Test the
24+
/// case when class has a getter named `_`
25+
/// @author [email protected]
26+
27+
// SharedOptions=--enable-experiment=patterns,records
28+
29+
class C {
30+
int get _ => 42;
31+
}
32+
33+
main() {
34+
C c = C();
35+
switch (c) {
36+
case C(:var _):
37+
// ^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
print("matched");
41+
break;
42+
default:
43+
print("not matched");
44+
}
45+
46+
if (c case C(:var _)) {
47+
// ^
48+
// [analyzer] unspecified
49+
// [cfe] unspecified
50+
}
51+
var x = switch(c) {
52+
C(:final _) => "match",
53+
// ^
54+
// [analyzer] unspecified
55+
// [cfe] unspecified
56+
_ => "no match"
57+
};
58+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2023, 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 objectPattern ::= typeName typeArguments? '(' patternFields? ')'
6+
/// ...
7+
/// It is a compile-time error if:
8+
///
9+
/// `typeNam`e does not refer to a type.
10+
///
11+
/// A type argument list is present and does not match the arity of the type of
12+
/// `typeName`.
13+
///
14+
/// A `patternField` is of the form `pattern`. Positional fields aren't allowed.
15+
///
16+
/// Any two named fields have the same name. This applies to both explicit and
17+
/// inferred field names.
18+
///
19+
/// It is a compile-time error if a name cannot be inferred for a named getter
20+
/// pattern with the getter name omitted (see name inference below).
21+
///
22+
/// @description Check that wildcard `_` can be used in Object pattern in a
23+
/// declaration context. Getter is called, but its value is ignored
24+
/// @author [email protected]
25+
26+
// SharedOptions=--enable-experiment=patterns,records
27+
28+
import "patterns_lib.dart";
29+
import "../../Utils/expect.dart";
30+
31+
String log = "";
32+
33+
void logger(String s) {
34+
log += s;
35+
}
36+
37+
main() {
38+
var Square(area: _) = Square(2, logger);
39+
Expect.equals("Square.area", log);
40+
41+
var Square(areaAsInt: int _) = Square(1);
42+
43+
Expect.throws(() {
44+
var Square(areaAsInt: _ as String) = Square(1);
45+
});
46+
}

0 commit comments

Comments
 (0)