Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: tests/language/assert_lexical_scope_test.dart

Issue 10915083: Change assert implementation to not depend on a top-level function called 'assert'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revert to using lexicalScope Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // 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.
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 class SetAssert {
Lasse Reichstein Nielsen 2012/09/07 10:18:57 I'll remove this again until there is agreement on
138 set assert(x) {}
139 void lexicalAssert(x) {
140 // The setter should not inhibit the assert,
141 assert(x);
142 }
143 }
144
145
146 testAssertDeclared() {
147 var get = new SubGet();
148 var method = new SubMethod();
149 var field = new SubField();
150 var non = new SubNon();
151
152 void expectCallsPoly(code, [bool noArgument = false]) {
153 int oldPolyCount = polyCount;
154 int newPolyArg = polyArg + 1;
155 int expectedPolyArg = noArgument ? null : newPolyArg;
156 code(newPolyArg);
157 Expect.equals(oldPolyCount + 1, polyCount);
158 Expect.equals(expectedPolyArg, polyArg);
159 if (noArgument) polyArg = newPolyArg;
160 }
161
162 void expectAssert(code) {
163 int oldPolyCount = polyCount;
164 // Detect whether asserts are enabled.
165 bool assertsEnabled = false;
166 assert(assertsEnabled = true);
167 try {
168 code(polyArg + 1);
169 // If asserts are enabled, we should not get here.
170 // If they are not, the call does nothing.
171 if (assertsEnabled) {
172 Expect.fail("Didn't call assert with asserts enabled.");
173 }
174 } on AssertionError catch (e) {
175 if (!assertsEnabled) Expect.fail("Called assert with asserts disabled?");
176 }
177 Expect.equals(oldPolyCount, polyCount);
178 }
179
180 // Sanity check.
181 expectCallsPoly(poly);
182
183 // Doesn't fail to read "assert".
184 get.getAssert(0);
185 method.getAssert(0);
186 field.getAssert(0);
187 expectCallsPoly(non.getAssert, true); // Hits 'noSuchMethod'.
188
189 // Check when 'assert' is a superclass member declaration (or, simulated with
190 // noSuchMethod).
191 void testSuperAssert(object) {
192 expectCallsPoly(object.assert0, true);
193 expectAssert(object.assert1);
194 expectCallsPoly(object.assertExp);
195 expectCallsPoly(object.assert2);
196 expectCallsPoly(object.lexicalAssert);
197 }
198
199 testSuperAssert(get);
200 testSuperAssert(method);
201 testSuperAssert(field);
202 testSuperAssert(non);
203
204 // Local declarations
205 expectCallsPoly((x) {
206 var assert = poly;
207 assert(x);
208 });
209
210 expectCallsPoly((x) {
211 void assert(x) => poly(x);
212 assert(x);
213 });
214
215 // Setters don't inhibit assert.
216 expectAssert(new SetAssert().lexicalAssert);
217 }
218
219 main() {
220 testAssertDeclared();
221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698