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 | 4 |
5 // Tests static and instance fields initialization. | 5 // Tests static and instance fields initialization. |
6 class DefaultInitTest { | 6 class DefaultInitTest { |
7 static testMain() { | 7 static testMain() { |
8 Expect.equals(0, A.a); | 8 Expect.equals(0, A.a); |
9 Expect.equals(2, A.b); | 9 Expect.equals(2, A.b); |
10 Expect.equals(null, A.c); | 10 Expect.equals(null, A.c); |
11 | 11 |
12 A a1 = new A(42); | 12 A a1 = new A(42); |
13 Expect.equals(42, a1.d); | 13 Expect.equals(42, a1.d); |
14 Expect.equals(null, a1.e); | 14 Expect.equals(null, a1.e); |
15 | 15 |
16 A a2 = new A.named(43); | 16 A a2 = new A.named(43); |
17 Expect.equals(null, a2.d); | 17 Expect.equals(null, a2.d); |
18 Expect.equals(43, a2.e); | 18 Expect.equals(43, a2.e); |
19 | 19 |
20 Expect.equals(42, B.instance.x); | 20 Expect.equals(42, B.instance.x); |
21 Expect.equals(3, C.instance.z); | 21 Expect.equals(3, C.instance.z); |
22 } | 22 } |
23 } | 23 } |
24 | 24 |
25 class A { | 25 class A { |
26 static final int a = 0; | 26 static const int a = 0; |
27 static final int b = 2; | 27 static const int b = 2; |
28 static int c; | 28 static int c; |
29 int d; | 29 int d; |
30 int e; | 30 int e; |
31 | 31 |
32 A(int val) { | 32 A(int val) { |
33 d = val; | 33 d = val; |
34 } | 34 } |
35 | 35 |
36 A.named(int val) { | 36 A.named(int val) { |
37 e = val; | 37 e = val; |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 // The following tests cover cases described in b/4101270 | 41 // The following tests cover cases described in b/4101270 |
42 | 42 |
43 class B { | 43 class B { |
44 static final B instance = const B(); | 44 static const B instance = const B(); |
45 // by putting this field after the static initializer above, the JS code gen | 45 // by putting this field after the static initializer above, the JS code gen |
46 // was calling the constructor before the setter of this property was defined. | 46 // was calling the constructor before the setter of this property was defined. |
47 final int x; | 47 final int x; |
48 const B() : this.x = (41 + 1); | 48 const B() : this.x = (41 + 1); |
49 } | 49 } |
50 | 50 |
51 class C { | 51 class C { |
52 // forward reference to another class | 52 // forward reference to another class |
53 static final D instance = const D(); | 53 static const D instance = const D(); |
54 C() {} | 54 C() {} |
55 } | 55 } |
56 | 56 |
57 class D { | 57 class D { |
58 const D(): this.z = 3; | 58 const D(): this.z = 3; |
59 final int z; | 59 final int z; |
60 } | 60 } |
61 | 61 |
62 main() { | 62 main() { |
63 DefaultInitTest.testMain(); | 63 DefaultInitTest.testMain(); |
64 } | 64 } |
OLD | NEW |