Skip to content

Commit 2a8dd57

Browse files
authored
#3315. Add tests for formal parameters. Part 3. (#3345)
Add tests for formal parameters. Part 3.
1 parent 8d95423 commit 2a8dd57

16 files changed

+1263
-6
lines changed

LanguageFeatures/Declaring-constructors/static_processing_A16_t02.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
2222
// SharedOptions=--enable-experiment=declaring-constructors
2323

24-
import '../../Utils/expect.dart';
25-
2624
class C1 {
2725
this.new(var int v);
2826
}

LanguageFeatures/Declaring-constructors/static_processing_A16_t03.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
2222
// SharedOptions=--enable-experiment=declaring-constructors
2323

24-
import '../../Utils/expect.dart';
25-
2624
class C1 {
2725
this(var int v);
2826
}

LanguageFeatures/Declaring-constructors/static_processing_A18_t02.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
///
1616
/// @description Check that an initializing formal parameter of a declaring
1717
/// constructor initializes the appropriate instance variable and preserves the
18-
/// default value. Test optional positioned parameters.
18+
/// default value. Test optional positional parameters.
1919
/// @author sgrekhov22@gmail.com
2020
2121
// SharedOptions=--enable-experiment=declaring-constructors

LanguageFeatures/Declaring-constructors/static_processing_A19_t02.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// @description Check that a super parameter (which has a corresponding
1717
/// initializing formal parameter in the superconstructor) of a declaring
1818
/// constructor initializes the appropriate instance variable and preserves the
19-
/// default value. Test optional positioned parameters.
19+
/// default value. Test optional positional parameters.
2020
/// @author sgrekhov22@gmail.com
2121
2222
// SharedOptions=--enable-experiment=declaring-constructors
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 The semantics of the declaring constructor is found in the
6+
/// following steps, where `D` is the class, extension type, or enum declaration
7+
/// in the program that includes a declaring constructor `k`, and `D2` is the
8+
/// result of the derivation of the semantics of `D`. The derivation step will
9+
/// delete elements that amount to the declaring constructor. Semantically, it
10+
/// will add a new constructor `k2`, and it will add zero or more instance
11+
/// variable declarations.
12+
/// ...
13+
/// Otherwise, a formal parameter (named or positional) of the form` var T p` or
14+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
15+
/// by `this.p`, along with its default value, if any. Next, a semantic instance
16+
/// variable declaration corresponding to the syntax `T p;` or `final T p;` is
17+
/// added to `D2`. It includes the modifier `final` if the parameter in `L` has
18+
/// the modifier `final`, or `D` is an extension type declaration and `k` is a
19+
/// declaring header constructor. In all cases, if `p` has the modifier
20+
/// `covariant` then this modifier is removed from the parameter in `L2`, and it
21+
/// is added to the instance variable declaration named `p`.
22+
///
23+
/// @description Check that a formal parameter of the form `var T p` and
24+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
25+
/// by `this.p` and a semantic instance variable declaration corresponding to
26+
/// the syntax `T p;` or `final T p;` is added to `D2`. Test mandatory
27+
/// parameters.
28+
/// @author sgrekhov22@gmail.com
29+
30+
// SharedOptions=--enable-experiment=declaring-constructors
31+
32+
import '../../Utils/expect.dart';
33+
34+
class C1<T>(var String v1, final T v2);
35+
36+
class C2(final String v1, var int v2);
37+
38+
class C3<T> {
39+
this(final String v1, var T v2);
40+
}
41+
42+
class C4 {
43+
this(var String v1, final int v2);
44+
}
45+
46+
extension type ET1<T>(final T v);
47+
48+
extension type ET2(final String v);
49+
50+
extension type ET3<T> {
51+
this(final T t);
52+
}
53+
54+
extension type ET4 {
55+
this(final String v);
56+
}
57+
58+
enum E1<T>(final T v) {
59+
e0<String>("E1");
60+
}
61+
62+
enum E2(final String v) {
63+
e0("E2");
64+
}
65+
66+
enum E3<T> {
67+
e0<String>("E3");
68+
const this(final T v);
69+
}
70+
71+
enum E4 {
72+
e0("E4");
73+
const this(final String v);
74+
}
75+
76+
main() {
77+
var c1 = C1<int>("C1", 2);
78+
Expect.equals("C1", c1.v1);
79+
Expect.equals(2, c1.v2);
80+
81+
var c2 = C2("C2", 2);
82+
Expect.equals("C2", c2.v1);
83+
Expect.equals(2, c2.v2);
84+
85+
var c3 = C3<int>("C3", 2);
86+
Expect.equals("C3", c3.v1);
87+
Expect.equals(2, c3.v2);
88+
89+
var c4 = C4("C4", 2);
90+
Expect.equals("C4", c4.v1);
91+
Expect.equals(2, c4.v2);
92+
93+
var et1 = ET1<String>("ET1");
94+
Expect.equals("ET1", et1.v);
95+
96+
var et2 = ET2("ET2");
97+
Expect.equals("ET2", et2.v);
98+
99+
var et3 = ET3<String>("ET3");
100+
Expect.equals("ET3", et3.v);
101+
102+
var et4 = ET4("ET4");
103+
Expect.equals("ET4", et4.v);
104+
105+
Expect.equals("E1", E1.e0.v);
106+
Expect.equals("E2", E2.e0.v);
107+
Expect.equals("E3", E3.e0.v);
108+
Expect.equals("E4", E4.e0.v);
109+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 The semantics of the declaring constructor is found in the
6+
/// following steps, where `D` is the class, extension type, or enum declaration
7+
/// in the program that includes a declaring constructor `k`, and `D2` is the
8+
/// result of the derivation of the semantics of `D`. The derivation step will
9+
/// delete elements that amount to the declaring constructor. Semantically, it
10+
/// will add a new constructor `k2`, and it will add zero or more instance
11+
/// variable declarations.
12+
/// ...
13+
/// Otherwise, a formal parameter (named or positional) of the form` var T p` or
14+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
15+
/// by `this.p`, along with its default value, if any. Next, a semantic instance
16+
/// variable declaration corresponding to the syntax `T p;` or `final T p;` is
17+
/// added to `D2`. It includes the modifier `final` if the parameter in `L` has
18+
/// the modifier `final`, or `D` is an extension type declaration and `k` is a
19+
/// declaring header constructor. In all cases, if `p` has the modifier
20+
/// `covariant` then this modifier is removed from the parameter in `L2`, and it
21+
/// is added to the instance variable declaration named `p`.
22+
///
23+
/// @description Check that a formal parameter of the form `var T p` and
24+
/// `final T p` where `T` is a type and `p` is an identifier is replaced in `L2`
25+
/// by `this.p` and a semantic instance variable declaration corresponding to
26+
/// the syntax `T p;` or `final T p;` is added to `D2`. Test optional positional
27+
/// parameters.
28+
/// @author sgrekhov22@gmail.com
29+
30+
// SharedOptions=--enable-experiment=declaring-constructors
31+
32+
import '../../Utils/expect.dart';
33+
34+
class C1<T>([var String v1 = "default", final T? v2]);
35+
36+
class C2([final String v1 = "default", var int v2 = 0]);
37+
38+
class C3<T> {
39+
this([final String v1 = "default", var T? v2]);
40+
}
41+
42+
class C4 {
43+
this([var String v1 = "default", final int v2 = 0]);
44+
}
45+
46+
extension type ET1<T>([final T? v]);
47+
48+
extension type ET2([String v = "default"]);
49+
50+
extension type ET3<T> {
51+
this([final T? t]);
52+
}
53+
54+
extension type ET4 {
55+
this([final String v = "default"]);
56+
}
57+
58+
enum E1<T>([final T? v]) {
59+
e0<String>("E1"), e1;
60+
}
61+
62+
enum E2([final String v = "default"]) {
63+
e0("E2"), e1;
64+
}
65+
66+
enum E3<T> {
67+
e0<String>("E3"), e1;
68+
const this([final T? v]);
69+
}
70+
71+
enum E4 {
72+
e0("E4"), e1;
73+
const this([final String v = "default"]);
74+
}
75+
76+
main() {
77+
var c1 = C1<int>("C1", 2);
78+
Expect.equals("C1", c1.v1);
79+
Expect.equals(2, c1.v2);
80+
c1 = C1<int>();
81+
Expect.equals("default", c1.v1);
82+
Expect.isNull(c1.v2);
83+
84+
var c2 = C2("C2", 2);
85+
Expect.equals("C2", c2.v1);
86+
Expect.equals(2, c2.v2);
87+
c2 = C2<int>();
88+
Expect.equals("default", c2.v1);
89+
Expect.equals(0, c2.v2);
90+
91+
var c3 = C3<int>("C3", 2);
92+
Expect.equals("C3", c3.v1);
93+
Expect.equals(2, c3.v2);
94+
c3 = C3<int>();
95+
Expect.equals("default", c3.v1);
96+
Expect.isNull(c3.v2);
97+
98+
var c4 = C4("C4", 2);
99+
Expect.equals("C4", c4.v1);
100+
Expect.equals(2, c4.v2);
101+
c4 = C4<int>();
102+
Expect.equals("default", c4.v1);
103+
Expect.equals(0, c4.v2);
104+
105+
var et1 = ET1<String>("ET1");
106+
Expect.equals("ET1", et1.v);
107+
et1 = ET1<String>();
108+
Expect.isNull(et1.v);
109+
110+
var et2 = ET2("ET2");
111+
Expect.equals("ET2", et2.v);
112+
et2 = ET2();
113+
Expect.equals("default", et2.v);
114+
115+
var et3 = ET3<String>("ET3");
116+
Expect.equals("ET3", et3.v);
117+
et3 = ET1<String>();
118+
Expect.isNull(et3.v);
119+
120+
var et4 = ET4("ET4");
121+
Expect.equals("ET4", et4.v);
122+
et4 = ET4();
123+
Expect.equals("default", et4.v);
124+
125+
Expect.equals("E1", E1.e0.v);
126+
Expect.isNull(E1.e1.v);
127+
Expect.equals("E2", E2.e0.v);
128+
Expect.equals("default", E2.e1.v);
129+
Expect.equals("E3", E3.e0.v);
130+
Expect.isNull(E3.e1.v);
131+
Expect.equals("E4", E4.e0.v);
132+
Expect.equals("default", E4.e1.v);
133+
}

0 commit comments

Comments
 (0)