Skip to content

Commit 2f88cd8

Browse files
authored
#3329. Add private named parameters tests. (#3362)
Add private named parameters tests.
1 parent 3b2747e commit 2f88cd8

11 files changed

+718
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// - The parameter name of the parameter in the constructor is the public name
10+
/// `n`. This means that the parameter has a public name in the constructor's
11+
/// function signature, and arguments for this parameter are given using the
12+
/// public name. All uses of the constructor, outside of its own code, see
13+
/// only the public name.
14+
///
15+
/// @description Check that the name of the parameter in the constructor
16+
/// signature is the corresponding public name `n`.
17+
/// @author [email protected]
18+
19+
// SharedOptions=--enable-experiment=private-named-parameters
20+
21+
import '../../Utils/expect.dart';
22+
import '../../Utils/static_type_helper.dart';
23+
24+
class C {
25+
String? _p;
26+
27+
C({this._p});
28+
C.named({required this._p});
29+
}
30+
31+
extension type ET._(String? _p) {
32+
ET({this._p});
33+
ET.named({required this._p});
34+
}
35+
36+
enum E {
37+
e0(p: "0"), e1.named(p: "1");
38+
39+
final String? _p;
40+
41+
const E({this._p});
42+
const E.named({required this._p});
43+
}
44+
45+
main() {
46+
C.new.expectStaticType<Exactly<C Function({String? p})>>();
47+
C.named.expectStaticType<Exactly<C Function({required String? p})>>();
48+
ET.new.expectStaticType<Exactly<ET Function({String? p})>>();
49+
ET.named.expectStaticType<Exactly<ET Function({required String? p})>>();
50+
Expect.equals("0", E.e0._p);
51+
Expect.equals("1", E.e1._p);
52+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// - The parameter name of the parameter in the constructor is the public name
10+
/// `n`. This means that the parameter has a public name in the constructor's
11+
/// function signature, and arguments for this parameter are given using the
12+
/// public name. All uses of the constructor, outside of its own code, see
13+
/// only the public name.
14+
///
15+
/// @description Check that the name of the parameter in the constructor
16+
/// signature is the corresponding public name `n`. Test declaring constructors.
17+
/// @author [email protected]
18+
19+
// SharedOptions=--enable-experiment=private-named-parameters,declaring-constructors
20+
21+
import '../../Utils/expect.dart';
22+
import '../../Utils/static_type_helper.dart';
23+
24+
class C1({var String? _p});
25+
26+
class C2 {
27+
this({required final String _p});
28+
}
29+
30+
extension type ET1 {
31+
this({final String? _p});
32+
}
33+
34+
extension type ET2 {
35+
this({required final String _p});
36+
}
37+
38+
enum E1({final String? _p}) {
39+
e0(p: "E1");
40+
}
41+
42+
enum E2 {
43+
e0(p: "E2");
44+
45+
const this({required final String _p});
46+
}
47+
48+
main() {
49+
C1.new.expectStaticType<Exactly<C1 Function({String? p})>>();
50+
C2.new.expectStaticType<Exactly<C2 Function({required String p})>>();
51+
ET1.new.expectStaticType<Exactly<ET1 Function({String? p})>>();
52+
ET2.new.expectStaticType<Exactly<ET2 Function({required String p})>>();
53+
Expect.equals("E1", E1.e0._p);
54+
Expect.equals("E2", E2.e1._p);
55+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// - The parameter name of the parameter in the constructor is the public name
10+
/// `n`. This means that the parameter has a public name in the constructor's
11+
/// function signature, and arguments for this parameter are given using the
12+
/// public name. All uses of the constructor, outside of its own code, see
13+
/// only the public name.
14+
///
15+
/// @description Check that it is a compile-time error to use a private name `p`
16+
/// as the name of the parameter in the constructor signature.
17+
/// @author [email protected]
18+
19+
// SharedOptions=--enable-experiment=private-named-parameters
20+
21+
class C {
22+
String? _p;
23+
24+
C({this._p});
25+
C.named({required this._p});
26+
}
27+
28+
extension type ET._(String? _p) {
29+
ET({this._p});
30+
ET.named({required this._p});
31+
}
32+
33+
enum E {
34+
e0(_p: "0"),
35+
// ^^
36+
// [analyzer] unspecified
37+
// [cfe] unspecified
38+
e1.named(_p: "1");
39+
// ^^
40+
// [analyzer] unspecified
41+
// [cfe] unspecified
42+
43+
final String? _p;
44+
45+
const E({this._p});
46+
const E.named({required this._p});
47+
}
48+
49+
main() {
50+
C(_p: "");
51+
// ^^
52+
// [analyzer] unspecified
53+
// [cfe] unspecified
54+
55+
C.named(_p: "");
56+
// ^^
57+
// [analyzer] unspecified
58+
// [cfe] unspecified
59+
60+
ET(_p: "");
61+
// ^^
62+
// [analyzer] unspecified
63+
// [cfe] unspecified
64+
65+
ET.named(_p: "");
66+
// ^^
67+
// [analyzer] unspecified
68+
// [cfe] unspecified
69+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// - The parameter name of the parameter in the constructor is the public name
10+
/// `n`. This means that the parameter has a public name in the constructor's
11+
/// function signature, and arguments for this parameter are given using the
12+
/// public name. All uses of the constructor, outside of its own code, see
13+
/// only the public name.
14+
///
15+
/// @description Check that it is a compile-time error to use a private name `p`
16+
/// as the name of the parameter in the constructor invocation. Test declaring
17+
/// constructors.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=private-named-parameters,declaring-constructors
21+
22+
class C1({var String? _p});
23+
24+
class C2 {
25+
this({required final String _p});
26+
}
27+
28+
extension type ET1 {
29+
this({final String? _p});
30+
}
31+
32+
extension type ET2 {
33+
this({required final String _p});
34+
}
35+
36+
enum E1({final String? _p}) {
37+
e0(_p: "E1");
38+
// ^^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
}
42+
43+
enum E2 {
44+
e0(_p: "E2");
45+
// ^^
46+
// [analyzer] unspecified
47+
// [cfe] unspecified
48+
49+
const this({required final String _p});
50+
}
51+
52+
main() {
53+
C1(_p: "");
54+
// ^^
55+
// [analyzer] unspecified
56+
// [cfe] unspecified
57+
58+
C2(_p: "");
59+
// ^^
60+
// [analyzer] unspecified
61+
// [cfe] unspecified
62+
63+
ET1(_p: "");
64+
// ^^
65+
// [analyzer] unspecified
66+
// [cfe] unspecified
67+
68+
ET2(_p: "");
69+
// ^^
70+
// [analyzer] unspecified
71+
// [cfe] unspecified
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// ...
10+
/// - The local variable introduced by the parameter, accessible only in the
11+
/// initializer list, still has the private name `p`.
12+
///
13+
/// @description Check that in the initializer list the name of the local
14+
/// variable introduced by the parameter is the private name `p`.
15+
/// @author [email protected]
16+
17+
// SharedOptions=--enable-experiment=private-named-parameters
18+
19+
import '../../Utils/expect.dart';
20+
21+
class C {
22+
String? _p1;
23+
String? p2;
24+
25+
C({this._p1}) : p2 = _p1;
26+
C.named({required this._p1}) : p2 = _p1;
27+
}
28+
29+
String log = "";
30+
31+
extension type ET._(String? _p) {
32+
ET({this._p}) : assert(() {log = _p; return true;}());
33+
ET.named({required this._p}) : assert(() {log = _p; return true;}());
34+
}
35+
36+
enum E {
37+
e0(p: "0"), e1.named(p: "1");
38+
39+
final String? _p1;
40+
final String? p2;
41+
42+
const E({this._p1}) : p2 = _p1;
43+
const E.named({required this._p1}) : p2 = _p1;
44+
}
45+
46+
main() {
47+
Expect.equals("one", C(p1: "one").p2);
48+
Expect.equals("two", C.named(p1: "two").p2);
49+
ET(p: "one");
50+
if (log.isNotEmpty) {
51+
Expect.equals("one", log);
52+
log = "";
53+
}
54+
ET.named(p: "two");
55+
if (log.isNotEmpty) {
56+
Expect.equals("two", log);
57+
}
58+
Expect.equals("0", E.e0.p2);
59+
Expect.equals("1", E.e1.p2);
60+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2025, 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 Given a named initializing formal or field parameter (for a
6+
/// primary constructor) with private name `p` in constructor `C`:
7+
/// ...
8+
/// If there is no error then:
9+
/// ...
10+
/// - The local variable introduced by the parameter, accessible only in the
11+
/// initializer list, still has the private name `p`.
12+
///
13+
/// @description Check that in the initializer list the name of the local
14+
/// variable introduced by the parameter is the private name `p`. Test declaring
15+
/// constructors.
16+
/// @author [email protected]
17+
18+
// SharedOptions=--enable-experiment=private-named-parameters,declaring-constructors
19+
20+
import '../../Utils/expect.dart';
21+
22+
class C1({var String? _p1}) {
23+
String? p2;
24+
this : p2 = _p1;
25+
}
26+
27+
class C2 {
28+
String p2;
29+
this({required final String _p1}) : p2 = _p1;
30+
}
31+
32+
String log = "";
33+
34+
extension type ET1 {
35+
this({final String? _p}) : assert(() {log = _p; return true;}());
36+
}
37+
38+
extension type ET2 {
39+
this({required final String _p}) : assert(() {log = _p; return true;}());
40+
}
41+
42+
enum E1({final String? _p1}) {
43+
e0(p1: "E1");
44+
45+
final String? p2;
46+
this : p2 = _p1;
47+
}
48+
49+
enum E2 {
50+
e0(p1: "E2");
51+
52+
final String p2;
53+
const this({final String? _p1}) : p2 = _p1;
54+
}
55+
56+
main() {
57+
Expect.equals("one", C1(p1: "one").p2);
58+
Expect.equals("two", C2(p1: "two").p2);
59+
ET1(p: "one");
60+
if (log.isNotEmpty) {
61+
Expect.equals("one", log);
62+
log = "";
63+
}
64+
ET2(p: "two");
65+
if (log.isNotEmpty) {
66+
Expect.equals("two", log);
67+
}
68+
Expect.equals("E1", E1.e0.p2);
69+
Expect.equals("E2", E2.e0.p2);
70+
}

0 commit comments

Comments
 (0)