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. | |
ahe
2012/09/07 11:38:05
General comment: there is a bunch of documentation
Lasse Reichstein Nielsen
2012/09/07 12:00:29
Some changed, some added.
| |
4 | |
5 // A function that accepts 0..2 arguments. | |
6 void poly([a, b]) { | |
7 polyCount++; | |
8 polyArg = a; | |
9 } | |
10 | |
11 // First argument of most recent call to poly. | |
12 var polyArg = 0; | |
13 | |
14 // Number of calls to poly. | |
15 var polyCount = 0; | |
16 | |
17 // (Super)classes that declare an "assert" member. | |
ahe
2012/09/07 11:38:05
classes -> class.
"assert" -> [assert].
Lasse Reichstein Nielsen
2012/09/07 12:00:29
This comment applies to the four following classes
| |
18 class SuperGet { | |
19 get assert => poly; | |
20 | |
21 void lexicalAssert(x) { | |
22 assert(x); | |
23 } | |
24 } | |
25 | |
26 class SuperMethod { | |
27 assert([a, b]) => poly(a, b); | |
28 | |
29 void lexicalAssert(x) { | |
30 assert(x); | |
31 } | |
32 } | |
33 | |
34 class SuperField { | |
35 var assert; | |
36 SuperField() : assert = poly; | |
37 | |
38 void lexicalAssert(x) { | |
39 assert(x); | |
40 } | |
41 } | |
42 | |
43 // Superclass that don't declare "assert", but will handle assert calls. | |
44 class SuperNon { | |
45 noSuchMethod(x, y) { | |
46 switch (y.length) { | |
47 case 0: return poly(); | |
48 case 1: return poly(y[0]); | |
49 case 2: return poly(y[0], y[1]); | |
50 } | |
51 } | |
52 | |
53 void lexicalAssert(x) { | |
54 // Hack, since there is no lexically enclosing 'assert' declaration here, | |
55 // so just act as if there was to avoid special casing it in the test. | |
56 poly(x); | |
57 } | |
58 } | |
59 | |
60 // Sub-classes that read/call "assert". | |
61 // In every case except "assert(exp);" this should access the superclass | |
62 // member. | |
63 | |
64 class SubGet extends SuperGet { | |
65 void getAssert(x) { | |
66 assert; | |
67 } | |
68 void assert0(x) { | |
69 assert(); | |
70 } | |
71 void assert1(x) { | |
72 assert(x); | |
73 } | |
74 void assertExp(x) { | |
75 var z = assert(x); | |
76 } | |
77 void assert2(x) { | |
78 assert(x, x); | |
79 } | |
80 } | |
81 | |
82 class SubMethod extends SuperMethod { | |
83 void getAssert(x) { | |
84 assert; | |
85 } | |
86 void assert0(x) { | |
87 assert(); | |
88 } | |
89 void assert1(x) { | |
90 assert(x); | |
91 } | |
92 void assertExp(x) { | |
93 var z = assert(x); | |
94 } | |
95 void assert2(x) { | |
96 assert(x, x); | |
97 } | |
98 } | |
99 | |
100 class SubField extends SuperField { | |
101 void getAssert(x) { | |
102 assert; | |
103 } | |
104 void assert0(x) { | |
105 assert(); | |
106 } | |
107 void assert1(x) { | |
108 assert(x); | |
109 } | |
110 void assertExp(x) { | |
111 var z = assert(x); | |
112 } | |
113 void assert2(x) { | |
114 assert(x, x); | |
115 } | |
116 } | |
117 | |
118 class SubNon extends SuperNon { | |
119 void getAssert(x) { | |
120 assert; | |
121 } | |
122 assert0(x) { | |
123 assert(); | |
124 } | |
125 void assert1(x) { | |
126 assert(x); | |
127 } | |
128 void assertExp(x) { | |
129 var z = assert(x); | |
130 } | |
131 void assert2(x) { | |
132 assert(x, x); | |
133 } | |
134 } | |
135 | |
136 | |
137 testAssertDeclared() { | |
138 var get = new SubGet(); | |
139 var method = new SubMethod(); | |
140 var field = new SubField(); | |
141 var non = new SubNon(); | |
142 | |
143 void expectCallsPoly(code, [bool noArgument = false]) { | |
144 int oldPolyCount = polyCount; | |
145 int newPolyArg = polyArg + 1; | |
146 int expectedPolyArg = noArgument ? null : newPolyArg; | |
147 code(newPolyArg); | |
148 Expect.equals(oldPolyCount + 1, polyCount); | |
149 Expect.equals(expectedPolyArg, polyArg); | |
150 if (noArgument) polyArg = newPolyArg; | |
151 } | |
152 | |
153 void expectAssert(code) { | |
154 int oldPolyCount = polyCount; | |
155 // Detect whether asserts are enabled. | |
156 bool assertsEnabled = false; | |
157 assert(assertsEnabled = true); | |
158 try { | |
159 code(polyArg + 1); | |
160 // If asserts are enabled, we should not get here. | |
161 // If they are not, the call does nothing. | |
162 if (assertsEnabled) { | |
163 Expect.fail("Didn't call assert with asserts enabled."); | |
164 } | |
165 } on AssertionError catch (e) { | |
166 if (!assertsEnabled) Expect.fail("Called assert with asserts disabled?"); | |
167 } | |
168 Expect.equals(oldPolyCount, polyCount); | |
169 } | |
170 | |
171 // Sanity check. | |
172 expectCallsPoly(poly); | |
173 | |
174 // Doesn't fail to read "assert". | |
175 get.getAssert(0); | |
176 method.getAssert(0); | |
177 field.getAssert(0); | |
178 expectCallsPoly(non.getAssert, true); // Hits 'noSuchMethod'. | |
179 | |
180 // Check when 'assert' is a superclass member declaration (or, simulated with | |
181 // noSuchMethod). | |
182 void testSuperAssert(object) { | |
183 expectCallsPoly(object.assert0, true); | |
184 expectAssert(object.assert1); | |
185 expectCallsPoly(object.assertExp); | |
186 expectCallsPoly(object.assert2); | |
187 expectCallsPoly(object.lexicalAssert); | |
188 } | |
189 | |
190 testSuperAssert(get); | |
191 testSuperAssert(method); | |
192 testSuperAssert(field); | |
193 testSuperAssert(non); | |
194 | |
195 // Local declarations | |
196 expectCallsPoly((x) { | |
197 var assert = poly; | |
198 assert(x); | |
199 }); | |
200 | |
201 expectCallsPoly((x) { | |
202 void assert(x) => poly(x); | |
ahe
2012/09/07 11:38:05
I don't understand this.
Lasse Reichstein Nielsen
2012/09/07 12:00:29
Comment added.
| |
203 assert(x); | |
204 }); | |
205 } | |
206 | |
207 main() { | |
208 testAssertDeclared(); | |
209 } | |
OLD | NEW |