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

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: Moved assert detection further down. Now demetered. 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 {
138 set assert(x) {}
139 void lexicalAssert(x) {
140 assert(x);
141 }
142 }
143
144
145 testAssertDeclared() {
146 var get = new SubGet();
147 var method = new SubMethod();
148 var field = new SubField();
149 var non = new SubNon();
150
151 void expectCallsPoly(code, [bool noArgument = false]) {
152 int oldPolyCount = polyCount;
153 int newPolyArg = polyArg + 1;
154 int expectedPolyArg = noArgument ? null : newPolyArg;
155 code(newPolyArg);
156 Expect.equals(oldPolyCount + 1, polyCount);
157 Expect.equals(expectedPolyArg, polyArg);
158 if (noArgument) polyArg = newPolyArg;
159 }
160
161 void expectAssert(code) {
162 int oldPolyCount = polyCount;
163 // Detect whether asserts are enabled.
164 bool assertsEnabled = false;
165 assert(assertsEnabled = true);
166 try {
167 code(polyArg + 1);
168 // If asserts are enabled, we should not get here.
169 // If they are not, the call does nothing.
170 if (assertsEnabled) {
171 Expect.fail("Didn't call assert with asserts enabled.");
172 }
173 } on AssertionError catch (e) {
174 if (!assertsEnabled) Expect.fail("Called assert with asserts disabled?");
175 }
176 Expect.equals(oldPolyCount, polyCount);
177 }
178
179 // Sanity check.
180 expectCallsPoly(poly);
181
182 // Doesn't fail to read "assert".
183 get.getAssert(0);
184 method.getAssert(0);
185 field.getAssert(0);
186 expectCallsPoly(non.getAssert, true); // Hits 'noSuchMethod'.
187
188 // Check when 'assert' is a superclass member declaration (or, simulated with
189 // noSuchMethod).
190 void testSuperAssert(object) {
191 expectCallsPoly(object.assert0, true);
192 expectAssert(object.assert1);
193 expectCallsPoly(object.assertExp);
194 expectCallsPoly(object.assert2);
195 expectCallsPoly(object.lexicalAssert);
196 }
197
198 testSuperAssert(get);
199 testSuperAssert(method);
200 testSuperAssert(field);
201 testSuperAssert(non);
202
203 // Local declarations
204 expectCallsPoly((x) {
205 var assert = poly;
206 assert(x);
207 });
208
209 expectCallsPoly((x) {
210 void assert(x) => poly(x);
211 assert(x);
212 });
213
214 // Setters don't inhibit assert.
215 expectAssert(new SetAssert().lexicalAssert);
216 }
217
218 main() {
219 testAssertDeclared();
220 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698