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 class A extends B { | 8 class A extends B { |
9 A(x, y) : super(y), a = x { } | 9 A(x, y) |
| 10 : super(y), |
| 11 a = x {} |
10 | 12 |
11 var a; | 13 var a; |
12 } | 14 } |
13 | 15 |
14 | |
15 class B { | 16 class B { |
16 var b; | 17 var b; |
17 | 18 |
18 B(x) : b = x { } | 19 B(x) : b = x {} |
19 | 20 |
20 B.namedB(var x) : b = x {} | 21 B.namedB(var x) : b = x {} |
21 } | 22 } |
22 | 23 |
23 | |
24 // Test the order of initialization: first the instance variable then | 24 // Test the order of initialization: first the instance variable then |
25 // the super constructor. | 25 // the super constructor. |
26 abstract class Alpha { | 26 abstract class Alpha { |
27 Alpha(v) { | 27 Alpha(v) { |
28 this.foo(v); | 28 this.foo(v); |
29 } | 29 } |
30 foo(v) => throw 'Alpha.foo should never be called.'; | 30 foo(v) => throw 'Alpha.foo should never be called.'; |
31 } | 31 } |
32 | 32 |
33 class Beta extends Alpha { | 33 class Beta extends Alpha { |
34 Beta(v) : super(v), b = 1 {} | 34 Beta(v) |
| 35 : super(v), |
| 36 b = 1 {} |
35 | 37 |
36 foo(v) { | 38 foo(v) { |
37 // Check that 'b' was initialized. | 39 // Check that 'b' was initialized. |
38 Expect.equals(1, b); | 40 Expect.equals(1, b); |
39 b = v; | 41 b = v; |
40 } | 42 } |
41 | 43 |
42 var b; | 44 var b; |
43 } | 45 } |
44 | 46 |
45 class ConstructorTest { | 47 class ConstructorTest { |
46 static testMain() { | 48 static testMain() { |
47 var o = new A(10, 2); | 49 var o = new A(10, 2); |
48 Expect.equals(10, o.a); | 50 Expect.equals(10, o.a); |
49 Expect.equals(2, o.b); | 51 Expect.equals(2, o.b); |
50 | 52 |
51 var o1 = new B.namedB(10); | 53 var o1 = new B.namedB(10); |
52 Expect.equals(10, o1.b); | 54 Expect.equals(10, o1.b); |
53 | 55 |
54 Expect.equals(22, o.a + o.b + o1.b); | 56 Expect.equals(22, o.a + o.b + o1.b); |
55 | 57 |
56 var beta = new Beta(3); | 58 var beta = new Beta(3); |
57 Expect.equals(3, beta.b); | 59 Expect.equals(3, beta.b); |
58 } | 60 } |
59 } | 61 } |
60 | 62 |
61 main() { | 63 main() { |
62 ConstructorTest.testMain(); | 64 ConstructorTest.testMain(); |
63 } | 65 } |
OLD | NEW |