dgrove@google.com | ebe4394 | 2011-10-05 06:22:36 +0000 | [diff] [blame] | 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 |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | // Dart test program for constructors and initializers. |
| 5 | |
floitsch@google.com | 8fd6d0a | 2013-04-05 19:43:16 +0000 | [diff] [blame] | 6 | import "package:expect/expect.dart"; |
| 7 | |
dgrove@google.com | ebe4394 | 2011-10-05 06:22:36 +0000 | [diff] [blame] | 8 | class A extends B { |
| 9 | A(x, y) : super(y), a = x { } |
| 10 | |
| 11 | var a; |
| 12 | } |
| 13 | |
| 14 | |
| 15 | class B { |
| 16 | var b; |
| 17 | |
| 18 | B(x) : b = x { } |
| 19 | |
| 20 | B.namedB(var x) : b = x {} |
| 21 | } |
| 22 | |
| 23 | |
| 24 | // Test the order of initialization: first the instance variable then |
| 25 | // the super constructor. |
| 26 | class Alpha { |
| 27 | Alpha(v) { |
| 28 | this.foo(v); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | class Beta extends Alpha { |
| 33 | Beta(v) : super(v), b = 1 {} |
| 34 | |
| 35 | foo(v) { |
| 36 | // Check that 'b' was initialized. |
| 37 | Expect.equals(1, b); |
| 38 | b = v; |
| 39 | } |
| 40 | |
| 41 | var b; |
| 42 | } |
| 43 | |
| 44 | class ConstructorTest { |
| 45 | static testMain() { |
| 46 | var o = new A(10, 2); |
| 47 | Expect.equals(10, o.a); |
| 48 | Expect.equals(2, o.b); |
| 49 | |
| 50 | var o1 = new B.namedB(10); |
| 51 | Expect.equals(10, o1.b); |
| 52 | |
| 53 | Expect.equals(22, o.a + o.b + o1.b); |
| 54 | |
| 55 | var beta = new Beta(3); |
| 56 | Expect.equals(3, beta.b); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | main() { |
| 61 | ConstructorTest.testMain(); |
| 62 | } |