OLD | NEW |
| (Empty) |
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 | |
5 // Tests basic classes and methods. | |
6 class ClassTest { | |
7 ClassTest() {} | |
8 | |
9 static testMain() { | |
10 var test = new ClassTest(); | |
11 test.testSuperCalls(); | |
12 test.testVirtualCalls(); | |
13 test.testStaticCalls(); | |
14 test.testInheritedField(); | |
15 test.testMemberRefInClosure(); | |
16 test.testFactory(); | |
17 test.testNamedConstructors(); | |
18 test.testDefaultImplementation(); | |
19 test.testFunctionParameter((int a) { return a;}); | |
20 } | |
21 | |
22 testFunctionParameter(int func(int a)) { | |
23 Expect.equals(1, func(1)); | |
24 } | |
25 | |
26 testSuperCalls() { | |
27 var sub = new Sub(); | |
28 Expect.equals(43, sub.methodX()); | |
29 Expect.equals(84, sub.methodK()); | |
30 } | |
31 | |
32 testVirtualCalls() { | |
33 var sub = new Sub(); | |
34 Expect.equals(41, sub.method2()); | |
35 Expect.equals(41, sub.method3()); | |
36 } | |
37 | |
38 testStaticCalls() { | |
39 var sub = new Sub(); | |
40 Expect.equals(-42, Sub.method4()); | |
41 Expect.equals(-41, sub.method5()); | |
42 } | |
43 | |
44 testInheritedField() { | |
45 var sub = new Sub(); | |
46 Expect.equals(42, sub.method6()); | |
47 } | |
48 | |
49 testMemberRefInClosure() { | |
50 var sub = new Sub(); | |
51 Expect.equals(1, sub.closureRef()); | |
52 Expect.equals(2, sub.closureRef()); | |
53 // Make sure it is actually on the object, not the global 'this'. | |
54 sub = new Sub(); | |
55 Expect.equals(1, sub.closureRef()); | |
56 Expect.equals(2, sub.closureRef()); | |
57 } | |
58 | |
59 testFactory() { | |
60 var sup = new Sup.named(); | |
61 Expect.equals(43, sup.methodX()); | |
62 Expect.equals(84, sup.methodK()); | |
63 } | |
64 | |
65 testNamedConstructors() { | |
66 var sup = new Sup.fromInt(4); | |
67 Expect.equals(4, sup.methodX()); | |
68 Expect.equals(0, sup.methodK()); | |
69 } | |
70 | |
71 testDefaultImplementation() { | |
72 var x = new Inter(4); | |
73 Expect.equals(4, x.methodX()); | |
74 Expect.equals(8, x.methodK()); | |
75 | |
76 x = new Inter.fromInt(4); | |
77 Expect.equals(4, x.methodX()); | |
78 Expect.equals(0, x.methodK()); | |
79 | |
80 x = new Inter.named(); | |
81 Expect.equals(43, x.methodX()); | |
82 Expect.equals(84, x.methodK()); | |
83 | |
84 x = new Inter.factory(); | |
85 Expect.equals(43, x.methodX()); | |
86 Expect.equals(84, x.methodK()); | |
87 } | |
88 } | |
89 | |
90 interface Inter default Sup { | |
91 Inter.named(); | |
92 Inter.fromInt(int x); | |
93 Inter(int x); | |
94 Inter.factory(); | |
95 int methodX(); | |
96 int methodK(); | |
97 int x_; | |
98 } | |
99 | |
100 class Sup implements Inter { | |
101 int x_; | |
102 int k_; | |
103 | |
104 factory Sup.named() { | |
105 return new Sub(); | |
106 } | |
107 | |
108 factory Sup.factory() { | |
109 return new Sub(); | |
110 } | |
111 | |
112 Sup.fromInt(int x) { | |
113 x_ = x; | |
114 k_ = 0; | |
115 } | |
116 | |
117 int methodX() { | |
118 return x_; | |
119 } | |
120 | |
121 int methodK() { | |
122 return k_; | |
123 } | |
124 | |
125 Sup(int x) : this.x_ = x { | |
126 k_ = x * 2; | |
127 } | |
128 | |
129 int method2() { | |
130 return x_ - 1; | |
131 } | |
132 } | |
133 | |
134 class Sub extends Sup { | |
135 int y_; | |
136 | |
137 // Override | |
138 int methodX() { | |
139 return super.methodX() + 1; | |
140 } | |
141 | |
142 int method3() { | |
143 return method2(); | |
144 } | |
145 | |
146 static int method4() { | |
147 return -42; | |
148 } | |
149 | |
150 int method5() { | |
151 return method4() + 1; | |
152 } | |
153 | |
154 int method6() { | |
155 return x_ + y_; | |
156 } | |
157 | |
158 int closureRef() { | |
159 var f = () {y_ += 1; return y_;}; | |
160 return f(); | |
161 } | |
162 | |
163 Sub() : super(42) { | |
164 y_ = 0; | |
165 } | |
166 } | |
167 | |
168 main() { | |
169 ClassTest.testMain(); | |
170 } | |
OLD | NEW |