Skip to content

Commit a6b4d3e

Browse files
authored
#3315. Add test checking the special rule for factory constructors (#3395)
Add test checking the special rule for factory constructors
1 parent fcb2879 commit a6b4d3e

File tree

8 files changed

+366
-0
lines changed

8 files changed

+366
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test a class.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=augmentations,declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
class C1 {
25+
final int v;
26+
const C1.foo(this.v);
27+
const factory C1(int v) = C1.foo;
28+
}
29+
30+
class C2 {
31+
int v;
32+
C2.foo(this.v);
33+
factory C2(int v) => C2.foo(v + 1);
34+
}
35+
36+
class C3 {
37+
final int v;
38+
const C3.foo(this.v);
39+
const factory C3(int v);
40+
41+
augment const factory C3(int v) = C3.foo;
42+
}
43+
44+
class C4 {
45+
int v;
46+
C4.foo(this.v);
47+
factory C4(int v);
48+
49+
augment factory C4(int v) => C4.foo(v + 1);
50+
}
51+
52+
main() {
53+
var c1 = C1.new;
54+
Expect.equals(1, c1(1).v);
55+
var c2 = C2.new;
56+
Expect.equals(2, c2(1).v);
57+
var c3 = C3.new;
58+
Expect.equals(1, c3(1).v);
59+
var c4 = C4.new;
60+
Expect.equals(2, c4(1).v);
61+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test a class and external constructors.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/static_type_helper.dart';
23+
24+
class C1 {
25+
external factory C1();
26+
}
27+
28+
class C2 {
29+
external const factory C2();
30+
}
31+
32+
main() {
33+
var c1 = C1.new;
34+
c1.expectStaticType<Exactly<C1 Function()>>();
35+
var c2 = C2.new;
36+
c2.expectStaticType<Exactly<C2 Function()>>();
37+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test a mixin class.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=augmentations,declaring-constructors
21+
22+
import '../../Utils/static_type_helper.dart';
23+
24+
mixin class C1 {
25+
const C1.foo();
26+
const factory C1() = C1.foo;
27+
}
28+
29+
mixin class C2 {
30+
C2.foo();
31+
factory C2() => C2.foo();
32+
}
33+
34+
mixin class C3 {
35+
const C3.foo();
36+
const factory C3();
37+
38+
augment const factory C3() = C3.foo;
39+
}
40+
41+
mixin class C4 {
42+
C4.foo();
43+
factory C4();
44+
45+
augment factory C4() => C4.foo();
46+
}
47+
48+
main() {
49+
var c1 = C1.new;
50+
c1.expectStaticType<Exactly<C1 Function()>>();
51+
var c2 = C2.new;
52+
c2.expectStaticType<Exactly<C2 Function()>>();
53+
var c3 = C3.new;
54+
c3.expectStaticType<Exactly<C3 Function()>>();
55+
var c4 = C4.new;
56+
c4.expectStaticType<Exactly<C4 Function()>>();
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test a mixin class and external constructors.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/static_type_helper.dart';
23+
24+
mixin class C1 {
25+
external factory C1();
26+
}
27+
28+
mixin class C2 {
29+
external const factory C2();
30+
}
31+
32+
main() {
33+
var c1 = C1.new;
34+
c1.expectStaticType<Exactly<C1 Function()>>();
35+
var c2 = C2.new;
36+
c2.expectStaticType<Exactly<C2 Function()>>();
37+
}
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test an extension type.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=augmentations,declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
extension type const ET1.foo(int v) {
25+
const factory ET1(int v) = ET1.foo;
26+
}
27+
28+
extension type ET2.foo(int v) {
29+
factory ET2(int v) => ET2.foo(v + 1);
30+
}
31+
32+
extension type const ET3._(int v) {
33+
const ET3.foo(this.v);
34+
const factory ET3(int v);
35+
36+
augment const factory ET3(int v) = ET3.foo;
37+
}
38+
39+
extension type ET4._(int v) {
40+
ET4.foo(this.v);
41+
factory ET4(int v);
42+
43+
augment factory ET4(int v) => ET4.foo(v + 1);
44+
}
45+
46+
main() {
47+
var et1 = ET1.new;
48+
Expect.equals(1, et1(1).v);
49+
var et2 = ET2.new;
50+
Expect.equals(2, et2(1).v);
51+
var et3 = ET3.new;
52+
Expect.equals(1, et3(1).v);
53+
var et4 = ET4.new;
54+
Expect.equals(2, et4(1).v);
55+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test an extension type and external constructors.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
import '../../Utils/static_type_helper.dart';
24+
25+
extension type ET1._(int v) {
26+
external factory ET1(int v);
27+
}
28+
29+
extension type ET2._(int v) {
30+
external const factory ET2(int v);
31+
}
32+
33+
main() {
34+
var et1 = ET1.new;
35+
et1.expectStaticType<Exactly<ET1 Function(int)>>();
36+
var et2 = ET2.new;
37+
et2.expectStaticType<Exactly<ET2 Function(int)>>();
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test an enum.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=augmentations,declaring-constructors
21+
22+
import '../../Utils/expect.dart';
23+
24+
enum E1 {
25+
e0.foo(1);
26+
27+
final int v;
28+
const E1.foo(this.v);
29+
factory E1() => E1.e0;
30+
}
31+
32+
enum E2 {
33+
e0.foo(2);
34+
35+
final int v;
36+
const E2.foo(this.v);
37+
factory E2();
38+
augment factory E2() => E2.e0;
39+
}
40+
41+
main() {
42+
var e1 = E1.new;
43+
Expect.equals(1, e1().v);
44+
var e2 = E2.new;
45+
Expect.equals(2, e2().v);
46+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 Consider a factory constructor declaration of the form
6+
/// `factory C(...` optionally starting with zero or more of the modifiers
7+
/// `const`, `augment`, or `external`. Assume that `C` is the name of the
8+
/// enclosing class, mixin class, enum, or extension type. In this situation,
9+
/// the declaration declares a constructor whose name is `C`.
10+
///
11+
/// Without this special rule, such a declaration would declare a constructor
12+
/// named `C.C`. With this rule it declares a constructor named `C`, which is
13+
/// the same as today.
14+
///
15+
/// @description Check that in case of a factory constructor declaration of the
16+
/// form `factory C(...` the declaration declares a constructor whose name is
17+
/// `C`. Test an enum and external constructors.
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=declaring-constructors
21+
22+
import '../../Utils/static_type_helper.dart';
23+
24+
enum E1 {
25+
e0.foo(3);
26+
27+
final int v;
28+
const E1.foo(this.v);
29+
external factory E1();
30+
}
31+
32+
main() {
33+
var e1 = E1.new;
34+
e1.expectStaticType<Exactly<E1 Function()>>();
35+
}

0 commit comments

Comments
 (0)