Skip to content

Commit 5e4181a

Browse files
authored
#1400. Tests for constructors of inline classes added (#2065)
1 parent e4211d8 commit 5e4181a

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 Constructor tear-off of inline classes happens in the same way as
6+
/// for regular classes
7+
/// @note Inline classes specification doesn't contain this statement but
8+
/// assumes it
9+
///
10+
/// @description Checks static type of an inline class constructor tear-off
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=inline-class
14+
15+
import "../../Utils/static_type_helper.dart";
16+
17+
inline class IC1 {
18+
final int id;
19+
IC1(this.id);
20+
IC1.n1(this.id, [String s = ""]);
21+
IC1.n2(this.id, {String s = ""});
22+
IC1.n3(this.id, {required String s});
23+
}
24+
25+
inline class IC2<T extends num> {
26+
final T id;
27+
IC2(this.id);
28+
IC2.n1(this.id, [String s = ""]);
29+
IC2.n2(this.id, {String s = ""});
30+
IC2.n3(this.id, {required String s});
31+
}
32+
33+
main() {
34+
IC1.new.expectStaticType<Exactly<IC1 Function(int)>>();
35+
IC1.n1.expectStaticType<Exactly<IC1 Function(int, [String])>>();
36+
IC1.n2.expectStaticType<Exactly<IC1 Function(int, {String s})>>();
37+
IC1.n3.expectStaticType<Exactly<IC1 Function(int, {required String s})>>();
38+
39+
IC2<num>.new.expectStaticType<Exactly<IC2 Function(num)>>();
40+
IC2<int>.n1.expectStaticType<Exactly<IC2<int> Function(int, [String])>>();
41+
IC2<int>.n2.expectStaticType<Exactly<IC2<int> Function(int, {String s})>>();
42+
IC2<int>
43+
.n3
44+
.expectStaticType<Exactly<IC2<int> Function(int, {required String s})>>();
45+
46+
IC2.new.expectStaticType<Exactly<IC2<T> Function<T extends num>(T)>>();
47+
IC2.n1
48+
.expectStaticType<Exactly<IC2<T> Function<T extends num>(T, [String])>>();
49+
IC2.n2.expectStaticType<
50+
Exactly<IC2<T> Function<T extends num>(T, {String s})>>();
51+
IC2.n3.expectStaticType<
52+
Exactly<IC2<T> Function<T extends num>(T, {required String s})>>();
53+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 A rule for <inlineClassDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in inline class declarations:
7+
///
8+
/// <inlineClassDeclaration> ::=
9+
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
10+
/// '{'
11+
/// (<metadata> <inlineMemberDeclaration>)*
12+
/// '}'
13+
///
14+
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
15+
/// ...
16+
///
17+
/// @description Checks that inline classes may have constant constructors
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=inline-class
21+
22+
inline class IC1 {
23+
final int id;
24+
const IC1(this.id);
25+
}
26+
27+
inline class IC2<T> {
28+
final T id;
29+
const IC2(this.id);
30+
}
31+
32+
inline class IC3<T extends Object> {
33+
final T id;
34+
const IC3(this.id);
35+
}
36+
37+
inline class IC4<T extends num> {
38+
final T id;
39+
const IC4(this.id);
40+
}
41+
42+
const v1 = const IC1(42);
43+
const v2 = const IC2<bool>(true);
44+
const v3 = const IC3<String>("42");
45+
const v4 = const IC4<int>(42);
46+
47+
void foo1([IC1 v = v1]) {}
48+
void foo2([IC2 v = v2]) {}
49+
void foo3([IC3 v = v3]) {}
50+
void foo4([IC4 v = v4]) {}
51+
52+
main() {
53+
foo1();
54+
foo2();
55+
foo3();
56+
foo4();
57+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 A rule for <inlineClassDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in inline class declarations:
7+
///
8+
/// <inlineClassDeclaration> ::=
9+
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
10+
/// '{'
11+
/// (<metadata> <inlineMemberDeclaration>)*
12+
/// '}'
13+
///
14+
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
15+
/// ...
16+
///
17+
/// @description Checks that inline classes may have factory constructors
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=inline-class
21+
22+
import "../../Utils/expect.dart";
23+
24+
inline class IC1 {
25+
final int id;
26+
IC1(this.id);
27+
28+
factory IC1.f1(int id) = IC1;
29+
factory IC1.f2(int id) => IC1(id);
30+
}
31+
32+
inline class IC2<T> {
33+
final T id;
34+
IC2(this.id);
35+
36+
factory IC2.f1(T id) = IC2;
37+
factory IC2.f2(T id) => IC2(id);
38+
}
39+
40+
inline class IC3<T extends Object> {
41+
final T id;
42+
IC3(this.id);
43+
44+
factory IC3.f1(T id) = IC3;
45+
factory IC3.f2(T id) => IC3(id);
46+
}
47+
48+
inline class IC4<T extends num> {
49+
final T id;
50+
const IC4(this.id);
51+
52+
factory IC4.f1(T id) = IC4;
53+
factory IC4.f2(T id) => IC4(id);
54+
}
55+
56+
main() {
57+
var ic1_1 = IC1.f1(1);
58+
Expect.equals(1, ic1_1.id);
59+
var ic1_2 = IC1.f2(1);
60+
Expect.equals(1, ic1_2.id);
61+
62+
var ic2_1 = IC2<int>.f1(2);
63+
Expect.equals(2, ic2_1.id);
64+
var ic2_2 = IC2.f2(2);
65+
Expect.equals(2, ic2_2.id);
66+
67+
var ic3_1 = IC3<int>.f1(3);
68+
Expect.equals(3, ic3_1.id);
69+
var ic3_2 = IC3.f2(3);
70+
Expect.equals(3, ic3_2.id);
71+
72+
var ic4_1 = IC4<int>.f1(4);
73+
Expect.equals(4, ic4_1.id);
74+
var ic4_2 = IC4.f2(4);
75+
Expect.equals(4, ic4_2.id);
76+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 A rule for <inlineClassDeclaration> is added to the grammar,
6+
/// along with some rules for elements used in inline class declarations:
7+
///
8+
/// <inlineClassDeclaration> ::=
9+
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
10+
/// '{'
11+
/// (<metadata> <inlineMemberDeclaration>)*
12+
/// '}'
13+
///
14+
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
15+
/// ...
16+
///
17+
/// @description Checks that inline classes may have redirecting constructors
18+
/// @author [email protected]
19+
20+
// SharedOptions=--enable-experiment=inline-class
21+
22+
import "../../Utils/expect.dart";
23+
24+
inline class IC1 {
25+
final int id;
26+
IC1(this.id);
27+
28+
IC1.r(int id) : this(id);
29+
}
30+
31+
inline class IC2<T> {
32+
final T id;
33+
IC2(this.id);
34+
35+
IC2.r(T id) : this(id);
36+
}
37+
38+
inline class IC3<T extends Object> {
39+
final T id;
40+
IC3(this.id);
41+
42+
IC3.r(T id) : this(id);
43+
}
44+
45+
inline class IC4<T extends num> {
46+
final T id;
47+
const IC4(this.id);
48+
49+
IC4.r(T id) : this(id);
50+
}
51+
52+
main() {
53+
var ic1 = IC1.r(1);
54+
Expect.equals(1, ic1.id);
55+
56+
var ic2 = IC2<int>.r(2);
57+
Expect.equals(2, ic2.id);
58+
59+
var ic3 = IC3<int>.r(3);
60+
Expect.equals(3, ic3.id);
61+
62+
var ic4 = IC4<int>.r(4);
63+
Expect.equals(4, ic4.id);
64+
}

0 commit comments

Comments
 (0)