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

Side by Side Diff: pkg/unittest/lib/src/core_matchers.dart

Issue 12226131: Work around dart2js' broken implementation of runtimeType. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of matcher; 5 part of matcher;
6 6
7 /** 7 /**
8 * Returns a matcher that matches empty strings, maps or iterables 8 * Returns a matcher that matches empty strings, maps or iterables
9 * (including collections). 9 * (including collections).
10 */ 10 */
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 'with key <${key}> ${location}', depth+1); 166 'with key <${key}> ${location}', depth+1);
167 if (reason != null) { 167 if (reason != null) {
168 break; 168 break;
169 } 169 }
170 } 170 }
171 } 171 }
172 } 172 }
173 } 173 }
174 } else { 174 } else {
175 reason = new StringDescription(); 175 reason = new StringDescription();
176 var includeTypes = expected.runtimeType != actual.runtimeType; 176 var eType = typeName(expected);
177 var aType = typeName(actual);
178 var includeTypes = eType != aType;
177 // If we have recursed, show the expected value too; if not, 179 // If we have recursed, show the expected value too; if not,
178 // expect() will show it for us. As expect will not show type 180 // expect() will show it for us. As expect will not show type
179 // mismatches at the top level we handle those here too. 181 // mismatches at the top level we handle those here too.
180 if (includeTypes || depth > 1) { 182 if (includeTypes || depth > 1) {
181 reason.add('expected '); 183 reason.add('expected ');
182 if (includeTypes) { 184 if (includeTypes) {
183 reason.add(expected.runtimeType.toString()).add(':'); 185 reason.add(eType).add(':');
184 } 186 }
185 reason.addDescriptionOf(expected).add(' but '); 187 reason.addDescriptionOf(expected).add(' but ');
186 } 188 }
187 reason.add('was '); 189 reason.add('was ');
188 if (includeTypes) { 190 if (includeTypes) {
189 reason.add(actual.runtimeType.toString()).add(':'); 191 reason.add(aType).add(':');
190 } 192 }
191 reason.addDescriptionOf(actual); 193 reason.addDescriptionOf(actual);
192 } 194 }
193 } 195 }
194 if (reason != null && location.length > 0) { 196 if (reason != null && location.length > 0) {
195 reason.add(' ').add(location); 197 reason.add(' ').add(location);
196 } 198 }
197 return reason; 199 return reason;
198 } 200 }
199 201
202 String typeName(x) {
203 // dart2js blows up on some objects (e.g. window.navigator).
204 // So we play safe here.
Siggi Cherem (dart-lang) 2013/02/12 23:09:17 please file a bug and add a reference here, I'm pr
205 try {
206 if (x == null) return "null";
207 return x.runtimeType.toString();
208 } catch (e) {
209 return "Unknown";
210 }
211 }
212
200 String _match(expected, actual) { 213 String _match(expected, actual) {
201 Description reason = _recursiveMatch(expected, actual, '', 0); 214 Description reason = _recursiveMatch(expected, actual, '', 0);
202 return reason == null ? null : reason.toString(); 215 return reason == null ? null : reason.toString();
203 } 216 }
204 217
205 // TODO(gram) - see if we can make use of matchState here to avoid 218 // TODO(gram) - see if we can make use of matchState here to avoid
206 // recursing again in describeMismatch. 219 // recursing again in describeMismatch.
207 bool matches(item, MatchState matchState) => _match(_expected, item) == null; 220 bool matches(item, MatchState matchState) => _match(_expected, item) == null;
208 221
209 Description describe(Description description) => 222 Description describe(Description description) =>
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher); 735 description.add(_featureDescription).add(' ').addDescriptionOf(_matcher);
723 736
724 Description describeMismatch(item, Description mismatchDescription, 737 Description describeMismatch(item, Description mismatchDescription,
725 MatchState matchState, bool verbose) { 738 MatchState matchState, bool verbose) {
726 mismatchDescription.add(_featureName).add(' '); 739 mismatchDescription.add(_featureName).add(' ');
727 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription, 740 _matcher.describeMismatch(matchState.state['feature'], mismatchDescription,
728 matchState.state['innerState'], verbose); 741 matchState.state['innerState'], verbose);
729 return mismatchDescription; 742 return mismatchDescription;
730 } 743 }
731 } 744 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698