OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Dart test program for constructors and initializers. | 4 // Dart test program for constructors and initializers. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 // Super initializer and super constructor body are executed in with the same | 8 // Super initializer and super constructor body are executed in with the same |
9 // bindings. | 9 // bindings. |
10 | 10 |
11 String trace = ""; | 11 String trace = ""; |
12 | 12 |
13 int E(int i) { | 13 int E(int i) { |
14 trace = "$trace$i-"; | 14 trace = "$trace$i-"; |
15 return i; | 15 return i; |
16 } | 16 } |
17 | 17 |
18 class A { | 18 class A { |
19 A({arg1: 100, arg2: 200}) : a1 = E(arg1++), a2 = E(arg2++) { | 19 A({arg1: 100, arg2: 200}) |
| 20 : a1 = E(arg1++), |
| 21 a2 = E(arg2++) { |
20 // b2 should be initialized between the above initializers and the following | 22 // b2 should be initialized between the above initializers and the following |
21 // statements. | 23 // statements. |
22 E(arg1); // 101 | 24 E(arg1); // 101 |
23 E(arg2); // 51 | 25 E(arg2); // 51 |
24 } | 26 } |
25 var a1; | 27 var a1; |
26 var a2; | 28 var a2; |
27 } | 29 } |
28 | 30 |
29 class B extends A { | 31 class B extends A { |
30 // Initializers in order: b1, super, b2. | 32 // Initializers in order: b1, super, b2. |
31 B(x, y) : b1 = E(x++), super(arg2: 50), b2 = E(y++) { | 33 B(x, y) |
| 34 : b1 = E(x++), |
| 35 super(arg2: 50), |
| 36 b2 = E(y++) { |
32 // Implicit super call to A's body happens here. | 37 // Implicit super call to A's body happens here. |
33 E(x); // 11 | 38 E(x); // 11 |
34 E(y); // 21 | 39 E(y); // 21 |
35 } | 40 } |
36 var b1; | 41 var b1; |
37 var b2; | 42 var b2; |
38 } | 43 } |
39 | 44 |
40 class C extends B { | 45 class C extends B { |
41 C() : super(10, 20); | 46 C() : super(10, 20); |
42 } | 47 } |
43 | 48 |
44 main() { | 49 main() { |
45 var c = new C(); | 50 var c = new C(); |
46 Expect.equals(100, c.a1); | 51 Expect.equals(100, c.a1); |
47 Expect.equals(50, c.a2); | 52 Expect.equals(50, c.a2); |
48 Expect.equals(10, c.b1); | 53 Expect.equals(10, c.b1); |
49 Expect.equals(20, c.b2); | 54 Expect.equals(20, c.b2); |
50 | 55 |
51 Expect.equals("10-100-50-20-101-51-11-21-", trace); | 56 Expect.equals("10-100-50-20-101-51-11-21-", trace); |
52 } | 57 } |
OLD | NEW |