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

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

Issue 891463004: Correctly match and print strings with escaped values (Closed) Base URL: https://github.com/dart-lang/matcher.git@master
Patch Set: also fixed prettyPrint Created 5 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
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 library matcher.core_matchers; 5 library matcher.core_matchers;
6 6
7 import 'description.dart'; 7 import 'description.dart';
8 import 'interfaces.dart'; 8 import 'interfaces.dart';
9 import 'util.dart'; 9 import 'util.dart';
10 10
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 Description describe(Description description) => 290 Description describe(Description description) =>
291 description.addDescriptionOf(_value); 291 description.addDescriptionOf(_value);
292 292
293 Description describeMismatch( 293 Description describeMismatch(
294 item, Description mismatchDescription, Map matchState, bool verbose) { 294 item, Description mismatchDescription, Map matchState, bool verbose) {
295 if (item is! String) { 295 if (item is! String) {
296 return mismatchDescription.addDescriptionOf(item).add('is not a string'); 296 return mismatchDescription.addDescriptionOf(item).add('is not a string');
297 } else { 297 } else {
298 var buff = new StringBuffer(); 298 var buff = new StringBuffer();
299 buff.write('is different.'); 299 buff.write('is different.');
300 var escapedItem = _escape(item); 300 var escapedItem = escape(item);
301 var escapedValue = _escape(_value); 301 var escapedValue = escape(_value);
302 int minLength = escapedItem.length < escapedValue.length 302 int minLength = escapedItem.length < escapedValue.length
303 ? escapedItem.length 303 ? escapedItem.length
304 : escapedValue.length; 304 : escapedValue.length;
305 int start; 305 int start;
306 for (start = 0; start < minLength; start++) { 306 for (start = 0; start < minLength; start++) {
307 if (escapedValue.codeUnitAt(start) != escapedItem.codeUnitAt(start)) { 307 if (escapedValue.codeUnitAt(start) != escapedItem.codeUnitAt(start)) {
308 break; 308 break;
309 } 309 }
310 } 310 }
311 if (start == minLength) { 311 if (start == minLength) {
(...skipping 15 matching lines...) Expand all
327 _writeTrailing(buff, escapedItem, start); 327 _writeTrailing(buff, escapedItem, start);
328 buff.write('\n '); 328 buff.write('\n ');
329 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' '); 329 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' ');
330 buff.write('^\n Differ at offset $start'); 330 buff.write('^\n Differ at offset $start');
331 } 331 }
332 332
333 return mismatchDescription.replace(buff.toString()); 333 return mismatchDescription.replace(buff.toString());
334 } 334 }
335 } 335 }
336 336
337 static String _escape(String s) =>
338 s.replaceAll('\n', '\\n').replaceAll('\r', '\\r').replaceAll('\t', '\\t');
339
340 static void _writeLeading(StringBuffer buff, String s, int start) { 337 static void _writeLeading(StringBuffer buff, String s, int start) {
341 if (start > 10) { 338 if (start > 10) {
342 buff.write('... '); 339 buff.write('... ');
343 buff.write(s.substring(start - 10, start)); 340 buff.write(s.substring(start - 10, start));
344 } else { 341 } else {
345 buff.write(s.substring(0, start)); 342 buff.write(s.substring(0, start));
346 } 343 }
347 } 344 }
348 345
349 static void _writeTrailing(StringBuffer buff, String s, int start) { 346 static void _writeTrailing(StringBuffer buff, String s, int start) {
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 .addDescriptionOf(matchState['feature']); 644 .addDescriptionOf(matchState['feature']);
648 var innerDescription = new StringDescription(); 645 var innerDescription = new StringDescription();
649 _matcher.describeMismatch( 646 _matcher.describeMismatch(
650 matchState['feature'], innerDescription, matchState['state'], verbose); 647 matchState['feature'], innerDescription, matchState['state'], verbose);
651 if (innerDescription.length > 0) { 648 if (innerDescription.length > 0) {
652 mismatchDescription.add(' which ').add(innerDescription.toString()); 649 mismatchDescription.add(' which ').add(innerDescription.toString());
653 } 650 }
654 return mismatchDescription; 651 return mismatchDescription;
655 } 652 }
656 } 653 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698