Skip to content

Commit 6e800d1

Browse files
authored
Fixes #1505. [Records] Added tests for dynamic access to record fields (#1507)
* Fixes #1505. [Records] Added tests for dynamic access to record fields * #1505. Typo fixed
1 parent bd1d91e commit 6e800d1

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 type declares all of the members defined on [Object]. It
6+
/// also exposes getters for each named field where the name of the getter is
7+
/// the field's name and the getter's type is the field's type. For each
8+
/// positional field, it exposes a getter whose name is $ followed by the number
9+
/// of preceding positional fields and whose type is the type of the field.
10+
///
11+
/// @description Checks dynamic access to record fields
12+
/// @author [email protected]
13+
14+
// SharedOptions=--enable-experiment=records
15+
16+
import "../../Utils/expect.dart";
17+
18+
main() {
19+
dynamic r1 = ();
20+
dynamic r2 = (42,);
21+
dynamic r3 = ((),);
22+
dynamic r4 = (($100: "Lily was here"),);
23+
dynamic r5 = (name: "name");
24+
dynamic r6 = (3.14, name: "pi");
25+
26+
Expect.throws(() {r1.$0;}, (e) => e is NoSuchMethodError);
27+
Expect.throws(() {r1.any;}, (e) => e is NoSuchMethodError);
28+
29+
Expect.equals(42, r2.$0);
30+
Expect.throws(() {r2.$1;}, (e) => e is NoSuchMethodError);
31+
Expect.throws(() {r2.$100000000000000000000;}, (e) => e is NoSuchMethodError);
32+
Expect.throws(() {r2.any;}, (e) => e is NoSuchMethodError);
33+
34+
Expect.equals((), r3.$0);
35+
Expect.throws(() {r3.$1;}, (e) => e is NoSuchMethodError);
36+
Expect.throws(() {r3.$100000000000000000000;}, (e) => e is NoSuchMethodError);
37+
Expect.throws(() {r3.any;}, (e) => e is NoSuchMethodError);
38+
39+
Expect.equals("Lily was here", r4.$0.$100);
40+
Expect.throws(() {r4.$1;}, (e) => e is NoSuchMethodError);
41+
Expect.throws(() {r4.$0.$0;}, (e) => e is NoSuchMethodError);
42+
Expect.throws(() {r4.$100;}, (e) => e is NoSuchMethodError);
43+
Expect.throws(() {r4.$100000000000000000000;}, (e) => e is NoSuchMethodError);
44+
Expect.throws(() {r4.any;}, (e) => e is NoSuchMethodError);
45+
46+
Expect.equals("name", r5.name);
47+
Expect.throws(() {r5.$0;}, (e) => e is NoSuchMethodError);
48+
Expect.throws(() {r5.$100000000000000000000;}, (e) => e is NoSuchMethodError);
49+
Expect.throws(() {r5.any;}, (e) => e is NoSuchMethodError);
50+
51+
Expect.equals("pi", r6.name);
52+
Expect.equals(3.14, r6.$0);
53+
Expect.throws(() {r6.$1;}, (e) => e is NoSuchMethodError);
54+
Expect.throws(() {r6.$100000000000000000000;}, (e) => e is NoSuchMethodError);
55+
Expect.throws(() {r6.any;}, (e) => e is NoSuchMethodError);
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 type declares all of the members defined on [Object]. It
6+
/// also exposes getters for each named field where the name of the getter is
7+
/// the field's name and the getter's type is the field's type. For each
8+
/// positional field, it exposes a getter whose name is $ followed by the number
9+
/// of preceding positional fields and whose type is the type of the field.
10+
///
11+
/// @description Checks dynamic access to record fields. Test functions and
12+
/// constructors as records fields
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=records
16+
17+
import "../../Utils/expect.dart";
18+
19+
class C {
20+
static int bar(int v) => v;
21+
int x;
22+
C(): this.x = 0;
23+
C.init(this.x);
24+
}
25+
26+
String foo(String s) => s;
27+
28+
main() {
29+
dynamic r1 = (foo,);
30+
dynamic r2 = (C.init,);
31+
dynamic r3 = (C.new,);
32+
dynamic r4 = (foo: foo);
33+
dynamic r5 = (newC: C.new, initC: C.init);
34+
dynamic r6 = (foo, C.init, newC: C.new, cBar: C.bar);
35+
36+
Expect.equals("Lily was here", r1.$0("Lily was here"));
37+
Expect.throws(() {r1.$1;}, (e) => e is NoSuchMethodError);
38+
Expect.throws(() {r1.$100000000000000000000;}, (e) => e is NoSuchMethodError);
39+
Expect.throws(() {r1.any;}, (e) => e is NoSuchMethodError);
40+
41+
Expect.equals(42, r2.$0(42).x);
42+
Expect.throws(() {r2.$1;}, (e) => e is NoSuchMethodError);
43+
Expect.throws(() {r2.$100000000000000000000;}, (e) => e is NoSuchMethodError);
44+
Expect.throws(() {r2.any;}, (e) => e is NoSuchMethodError);
45+
46+
Expect.equals(0, r3.$0().x);
47+
Expect.throws(() {r3.$1;}, (e) => e is NoSuchMethodError);
48+
Expect.throws(() {r3.$100000000000000000000;}, (e) => e is NoSuchMethodError);
49+
Expect.throws(() {r3.any;}, (e) => e is NoSuchMethodError);
50+
51+
Expect.equals("Lily was here", r4.foo("Lily was here"));
52+
Expect.throws(() {r4.$0;}, (e) => e is NoSuchMethodError);
53+
Expect.throws(() {r4.$100000000000000000000;}, (e) => e is NoSuchMethodError);
54+
Expect.throws(() {r4.any;}, (e) => e is NoSuchMethodError);
55+
56+
Expect.equals(0, r5.newC().x);
57+
Expect.equals(-1, r5.initC(-1).x);
58+
Expect.throws(() {r5.$0;}, (e) => e is NoSuchMethodError);
59+
Expect.throws(() {r5.$100000000000000000000;}, (e) => e is NoSuchMethodError);
60+
Expect.throws(() {r5.any;}, (e) => e is NoSuchMethodError);
61+
62+
Expect.equals("42", r6.$0("42"));
63+
Expect.equals(-1, r6.$1(-1).x);
64+
Expect.equals(0, r6.newC().x);
65+
Expect.equals(1, r6.cBar(1));
66+
Expect.throws(() {r6.$2;}, (e) => e is NoSuchMethodError);
67+
Expect.throws(() {r6.$100000000000000000000;}, (e) => e is NoSuchMethodError);
68+
Expect.throws(() {r6.any;}, (e) => e is NoSuchMethodError);
69+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 type declares all of the members defined on [Object]. It
6+
/// also exposes getters for each named field where the name of the getter is
7+
/// the field's name and the getter's type is the field's type. For each
8+
/// positional field, it exposes a getter whose name is $ followed by the number
9+
/// of preceding positional fields and whose type is the type of the field.
10+
///
11+
/// @description Checks dynamic access to record fields. Test generic functions
12+
/// and constructors as records fields
13+
/// @author [email protected]
14+
15+
// SharedOptions=--enable-experiment=records
16+
17+
import "../../Utils/expect.dart";
18+
19+
class C<T> {
20+
static X bar<X>(X v) => v;
21+
T x;
22+
C(this.x);
23+
}
24+
25+
T foo<T>(T t) => t;
26+
27+
main() {
28+
dynamic r1 = (foo<int>,);
29+
dynamic r2 = (C<String>.new,);
30+
dynamic r3 = (foo: foo<int>);
31+
dynamic r4 = (foo<String>, newC: C<int>.new, cBar: C.bar<bool>);
32+
33+
Expect.equals(42, r1.$0(42));
34+
Expect.throws(() {r1.$0("");});
35+
Expect.throws(() {r1.$1;}, (e) => e is NoSuchMethodError);
36+
Expect.throws(() {r1.$100000000000000000000;}, (e) => e is NoSuchMethodError);
37+
Expect.throws(() {r1.any;}, (e) => e is NoSuchMethodError);
38+
39+
Expect.equals("Hi", r2.$0("Hi").x);
40+
Expect.throws(() {r2.$0(42);});
41+
Expect.throws(() {r2.$1;}, (e) => e is NoSuchMethodError);
42+
Expect.throws(() {r2.$100000000000000000000;}, (e) => e is NoSuchMethodError);
43+
Expect.throws(() {r2.any;}, (e) => e is NoSuchMethodError);
44+
45+
Expect.equals(1, r3.foo(1));
46+
Expect.throws(() {r3.foo("");});
47+
Expect.throws(() {r3.$0;}, (e) => e is NoSuchMethodError);
48+
Expect.throws(() {r3.$100000000000000000000;}, (e) => e is NoSuchMethodError);
49+
Expect.throws(() {r3.any;}, (e) => e is NoSuchMethodError);
50+
51+
Expect.equals("Lily was here", r4.$0("Lily was here"));
52+
Expect.throws(() {r4.$0(42);});
53+
Expect.equals(1, r4.newC(1).x);
54+
Expect.throws(() {r4.newC("");});
55+
Expect.isTrue(r4.cBar(true));
56+
Expect.throws(() {r4.cBar(42);});
57+
Expect.throws(() {r4.$1;}, (e) => e is NoSuchMethodError);
58+
Expect.throws(() {r4.$100000000000000000000;}, (e) => e is NoSuchMethodError);
59+
Expect.throws(() {r4.any;}, (e) => e is NoSuchMethodError);
60+
}

0 commit comments

Comments
 (0)