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

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: cleanup 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 'escape.dart';
8 import 'interfaces.dart'; 9 import 'interfaces.dart';
9 import 'util.dart'; 10 import 'util.dart';
10 11
11 /// Returns a matcher that matches the isEmpty property. 12 /// Returns a matcher that matches the isEmpty property.
12 const Matcher isEmpty = const _Empty(); 13 const Matcher isEmpty = const _Empty();
13 14
14 class _Empty extends Matcher { 15 class _Empty extends Matcher {
15 const _Empty(); 16 const _Empty();
16 17
17 bool matches(item, Map matchState) => item.isEmpty; 18 bool matches(item, Map matchState) => item.isEmpty;
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 Description describe(Description description) => 291 Description describe(Description description) =>
291 description.addDescriptionOf(_value); 292 description.addDescriptionOf(_value);
292 293
293 Description describeMismatch( 294 Description describeMismatch(
294 item, Description mismatchDescription, Map matchState, bool verbose) { 295 item, Description mismatchDescription, Map matchState, bool verbose) {
295 if (item is! String) { 296 if (item is! String) {
296 return mismatchDescription.addDescriptionOf(item).add('is not a string'); 297 return mismatchDescription.addDescriptionOf(item).add('is not a string');
297 } else { 298 } else {
298 var buff = new StringBuffer(); 299 var buff = new StringBuffer();
299 buff.write('is different.'); 300 buff.write('is different.');
300 var escapedItem = _escape(item); 301 var escapedItem = escape(item);
301 var escapedValue = _escape(_value); 302 var escapedValue = escape(_value);
302 int minLength = escapedItem.length < escapedValue.length 303 int minLength = escapedItem.length < escapedValue.length
303 ? escapedItem.length 304 ? escapedItem.length
304 : escapedValue.length; 305 : escapedValue.length;
305 int start; 306 int start;
306 for (start = 0; start < minLength; start++) { 307 for (start = 0; start < minLength; start++) {
307 if (escapedValue.codeUnitAt(start) != escapedItem.codeUnitAt(start)) { 308 if (escapedValue.codeUnitAt(start) != escapedItem.codeUnitAt(start)) {
308 break; 309 break;
309 } 310 }
310 } 311 }
311 if (start == minLength) { 312 if (start == minLength) {
(...skipping 15 matching lines...) Expand all
327 _writeTrailing(buff, escapedItem, start); 328 _writeTrailing(buff, escapedItem, start);
328 buff.write('\n '); 329 buff.write('\n ');
329 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' '); 330 for (int i = (start > 10 ? 14 : start); i > 0; i--) buff.write(' ');
330 buff.write('^\n Differ at offset $start'); 331 buff.write('^\n Differ at offset $start');
331 } 332 }
332 333
333 return mismatchDescription.replace(buff.toString()); 334 return mismatchDescription.replace(buff.toString());
334 } 335 }
335 } 336 }
336 337
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) { 338 static void _writeLeading(StringBuffer buff, String s, int start) {
341 if (start > 10) { 339 if (start > 10) {
342 buff.write('... '); 340 buff.write('... ');
343 buff.write(s.substring(start - 10, start)); 341 buff.write(s.substring(start - 10, start));
344 } else { 342 } else {
345 buff.write(s.substring(0, start)); 343 buff.write(s.substring(0, start));
346 } 344 }
347 } 345 }
348 346
349 static void _writeTrailing(StringBuffer buff, String s, int start) { 347 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']); 645 .addDescriptionOf(matchState['feature']);
648 var innerDescription = new StringDescription(); 646 var innerDescription = new StringDescription();
649 _matcher.describeMismatch( 647 _matcher.describeMismatch(
650 matchState['feature'], innerDescription, matchState['state'], verbose); 648 matchState['feature'], innerDescription, matchState['state'], verbose);
651 if (innerDescription.length > 0) { 649 if (innerDescription.length > 0) {
652 mismatchDescription.add(' which ').add(innerDescription.toString()); 650 mismatchDescription.add(' which ').add(innerDescription.toString());
653 } 651 }
654 return mismatchDescription; 652 return mismatchDescription;
655 } 653 }
656 } 654 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698