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