OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'package:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
6 | 6 |
7 // Test that function literals are parsed correctly in initializers. | 7 // Test that function literals are parsed correctly in initializers. |
8 | 8 |
9 class A { | 9 class A { |
10 final x; | 10 final x; |
11 static foo(f) => f(); | 11 static foo(f) => f(); |
12 | 12 |
13 A.parenthesized(y) : x = (() => y); | 13 A.parenthesized(y) : x = (() => y); |
14 A.stringLiteral(y) : x = "**${() => y}--"; | 14 A.stringLiteral(y) : x = "**${() => y}--"; |
15 A.listLiteral(y) : x = [() => y]; | 15 A.listLiteral(y) : x = [() => y]; |
16 A.mapLiteral(y) : x = { "fun": () => y }; | 16 A.mapLiteral(y) : x = {"fun": () => y}; |
17 A.arg(y) : x = foo(() => y); | 17 A.arg(y) : x = foo(() => y); |
18 } | 18 } |
19 | 19 |
20 main() { | 20 main() { |
21 var a, f; | 21 var a, f; |
22 a = new A.parenthesized(499); | 22 a = new A.parenthesized(499); |
23 f = a.x; | 23 f = a.x; |
24 Expect.isTrue(f is Function); | 24 Expect.isTrue(f is Function); |
25 Expect.equals(499, f()); | 25 Expect.equals(499, f()); |
26 | 26 |
27 // The toString of closures is not specified. Just make sure that there is no | 27 // The toString of closures is not specified. Just make sure that there is no |
28 // crash. | 28 // crash. |
29 a = new A.stringLiteral(42); | 29 a = new A.stringLiteral(42); |
30 Expect.isTrue(a.x.startsWith("**")); | 30 Expect.isTrue(a.x.startsWith("**")); |
31 Expect.isTrue(a.x.endsWith("--")); | 31 Expect.isTrue(a.x.endsWith("--")); |
32 | 32 |
33 a = new A.listLiteral(99); | 33 a = new A.listLiteral(99); |
34 f = a.x[0]; | 34 f = a.x[0]; |
35 Expect.isTrue(f is Function); | 35 Expect.isTrue(f is Function); |
36 Expect.equals(99, f()); | 36 Expect.equals(99, f()); |
37 | 37 |
38 a = new A.mapLiteral(314); | 38 a = new A.mapLiteral(314); |
39 f = a.x["fun"]; | 39 f = a.x["fun"]; |
40 Expect.isTrue(f is Function); | 40 Expect.isTrue(f is Function); |
41 Expect.equals(314, f()); | 41 Expect.equals(314, f()); |
42 | 42 |
43 a = new A.arg(123); | 43 a = new A.arg(123); |
44 Expect.equals(123, a.x); | 44 Expect.equals(123, a.x); |
45 } | 45 } |
OLD | NEW |