Skip to content

Commit b5d2d5c

Browse files
authored
#1399. Tests for record types. Part 1 (#1395)
Authored by @sgrekhov. Record tests added.
1 parent 5543f20 commit b5d2d5c

37 files changed

+1939
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that all records have [Record] as a superclass
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
import "../../Utils/expect.dart";
16+
17+
main() {
18+
var record1 = (42, "Lily was here");
19+
var record2 = (42, name: "Lily was here");
20+
var record3 = (name: "Lily was here", type: "Record");
21+
Expect.isTrue(record1 is Record);
22+
Expect.isTrue(record2 is Record);
23+
Expect.isTrue(record3 is Record);
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that it is a compile error to construct [Record]
11+
/// instance
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=records
15+
16+
main() {
17+
Record();
18+
//^^^^^^
19+
// [analyzer] unspecified
20+
// [cfe] unspecified
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that it is a compile error to extend [Record] class
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
class R extends Record {
16+
// ^^^^^^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
}
20+
21+
main() {
22+
R();
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that it is a compile error to mix-in [Record] class
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
mixin R on Record {
16+
// ^^^^^^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
}
20+
21+
main() {
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that it is a compile error to mix-in [Record] class
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
class R = Object with Record;
16+
// ^^^^^^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
20+
main() {
21+
R();
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that it is a compile error to implement [Record] class
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
class R implements Record {
16+
// ^^^^^^
17+
// [analyzer] unspecified
18+
// [cfe] unspecified
19+
}
20+
21+
main() {
22+
R();
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that class [Record] may have an extension
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
import "../../Utils/expect.dart";
16+
17+
extension on Record {
18+
int foo() => 42;
19+
}
20+
21+
main() {
22+
var r = (3, 1, 4, name: "Pi");
23+
Expect.equals(42, r.foo());
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 A built-in class [Record] with no members except those inherited
6+
/// from [Object]. All record types are a subtype of this class. This type
7+
/// cannot be constructed, extended, mixed in, or implemented by user-defined
8+
/// classes.
9+
///
10+
/// @description Checks that all records have [Record] as a superclass
11+
/// @author [email protected]
12+
13+
// SharedOptions=--enable-experiment=records
14+
15+
Record foo(Record r) => (42, "Lily was here");
16+
17+
main() {
18+
foo(foo(("42", true)));
19+
foo(foo((42, name: "Lily was here")));
20+
foo(foo((name: "Lily was here", type: "Record")));
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 A record is created using a record expression. The grammar is:
6+
///
7+
/// literal ::= record
8+
/// | // Existing literal productions...
9+
/// record ::= '(' recordField ( ',' recordField )* ','? ')'
10+
/// recordField ::= (identifier ':' )? expression
11+
///
12+
/// This is identical to the grammar for a function call argument list. There
13+
/// are a couple of syntactic restrictions not captured by the grammar. It is a
14+
/// compile-time error if a record has any of:
15+
///
16+
/// The same field name more than once.
17+
///
18+
/// No named fields and only one positional field. This avoids ambiguity with
19+
/// parenthesized expressions.
20+
///
21+
/// A field named hashCode, runtimeType, noSuchMethod, or toString.
22+
///
23+
/// A field name that starts with an underscore.
24+
///
25+
/// @description Checks that it is a compile-time error if a record has the same
26+
/// field name more than once
27+
/// @author [email protected]
28+
29+
// SharedOptions=--enable-experiment=records
30+
31+
main() {
32+
var record1 = (42, name: "Lily was here", name: "Hello");
33+
// ^^^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
37+
var record2 = (x: 42, x: 42);
38+
// ^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 A record is created using a record expression. The grammar is:
6+
///
7+
/// literal ::= record
8+
/// | // Existing literal productions...
9+
/// record ::= '(' recordField ( ',' recordField )* ','? ')'
10+
/// recordField ::= (identifier ':' )? expression
11+
///
12+
/// This is identical to the grammar for a function call argument list. There
13+
/// are a couple of syntactic restrictions not captured by the grammar. It is a
14+
/// compile-time error if a record has any of:
15+
///
16+
/// The same field name more than once.
17+
///
18+
/// No named fields and only one positional field. This avoids ambiguity with
19+
/// parenthesized expressions.
20+
///
21+
/// A field named hashCode, runtimeType, noSuchMethod, or toString.
22+
///
23+
/// A field name that starts with an underscore.
24+
///
25+
/// @description Checks that it is a compile-time error if a record has the same
26+
/// field name more than once
27+
/// @author [email protected]
28+
29+
// SharedOptions=--enable-experiment=records
30+
31+
Record foo() => (42, name: "Lily was here", name: "Hello");
32+
// ^^^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
36+
void bar(Record r) {}
37+
38+
main() {
39+
bar((x: 42, x: 42));
40+
// ^
41+
// [analyzer] unspecified
42+
// [cfe] unspecified
43+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 A record is created using a record expression. The grammar is:
6+
///
7+
/// literal ::= record
8+
/// | // Existing literal productions...
9+
/// record ::= '(' recordField ( ',' recordField )* ','? ')'
10+
/// recordField ::= (identifier ':' )? expression
11+
///
12+
/// This is identical to the grammar for a function call argument list. There
13+
/// are a couple of syntactic restrictions not captured by the grammar. It is a
14+
/// compile-time error if a record has any of:
15+
///
16+
/// The same field name more than once.
17+
///
18+
/// No named fields and only one positional field. This avoids ambiguity with
19+
/// parenthesized expressions.
20+
///
21+
/// A field named hashCode, runtimeType, noSuchMethod, or toString.
22+
///
23+
/// A field name that starts with an underscore.
24+
///
25+
/// @description Checks that it is a compile-time error if a record has no named
26+
/// fields and only one positional field
27+
/// @author [email protected]
28+
29+
// SharedOptions=--enable-experiment=records
30+
31+
main() {
32+
Record r1 = (42);
33+
// ^^
34+
// [analyzer] unspecified
35+
// [cfe] unspecified
36+
37+
Record r2 = ((42));
38+
// ^^^^
39+
// [analyzer] unspecified
40+
// [cfe] unspecified
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 A record is created using a record expression. The grammar is:
6+
///
7+
/// literal ::= record
8+
/// | // Existing literal productions...
9+
/// record ::= '(' recordField ( ',' recordField )* ','? ')'
10+
/// recordField ::= (identifier ':' )? expression
11+
///
12+
/// This is identical to the grammar for a function call argument list. There
13+
/// are a couple of syntactic restrictions not captured by the grammar. It is a
14+
/// compile-time error if a record has any of:
15+
///
16+
/// The same field name more than once.
17+
///
18+
/// No named fields and only one positional field. This avoids ambiguity with
19+
/// parenthesized expressions.
20+
///
21+
/// A field named hashCode, runtimeType, noSuchMethod, or toString.
22+
///
23+
/// A field name that starts with an underscore.
24+
///
25+
/// @description Checks that it is a compile-time error if a record has no named
26+
/// fields and only one positional field
27+
/// @author [email protected]
28+
29+
// SharedOptions=--enable-experiment=records
30+
31+
Record foo1() => (42);
32+
// ^^^^
33+
// [analyzer] unspecified
34+
// [cfe] unspecified
35+
36+
Record foo2() => ((42));
37+
// ^^^^^^
38+
// [analyzer] unspecified
39+
// [cfe] unspecified
40+
41+
void bar(Record r) {}
42+
43+
main() {
44+
bar(("Hello"));
45+
// ^^^^^^^^^
46+
// [analyzer] unspecified
47+
// [cfe] unspecified
48+
49+
bar((("Hello")));
50+
// ^^^^^^^^^^^
51+
// [analyzer] unspecified
52+
// [cfe] unspecified
53+
}

0 commit comments

Comments
 (0)