|
| 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 | + |
| 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 | +} |
0 commit comments