Skip to content

Commit c1012cb

Browse files
committed
Use matchState and addDescriptionOf to properly quote elements.
1 parent d199a08 commit c1012cb

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

pkgs/matcher/lib/src/iterable_matchers.dart

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -436,35 +436,34 @@ class _IsSorted<T, K> extends _IterableMatcher<T> {
436436
: _keyOf = keyOf,
437437
_compare = compare;
438438

439-
String? _test(Iterable<T> item, Map matchState) {
439+
@override
440+
bool typedMatches(Iterable<T> item, Map matchState) {
440441
var iterator = item.iterator;
441-
if (!iterator.moveNext()) return null;
442+
if (!iterator.moveNext()) return true;
442443
var previousElement = iterator.current;
443444
var previousKey = _keyOf(previousElement);
444445
while (iterator.moveNext()) {
445446
var element = iterator.current;
446447
var key = _keyOf(element);
447448
if (_compare(previousKey, key) > 0) {
448-
return StringDescription(
449-
'found elements out of order: <$previousElement> and '
450-
'<$element>')
451-
.toString();
449+
addStateInfo(matchState, {'first': previousElement, 'second': element});
450+
return false;
452451
}
453452
previousElement = element;
454453
previousKey = key;
455454
}
456-
return null;
455+
return true;
457456
}
458457

459-
@override
460-
bool typedMatches(Iterable<T> item, Map matchState) =>
461-
_test(item, matchState) == null;
462-
463458
@override
464459
Description describe(Description description) => description.add('is sorted');
465460

466461
@override
467462
Description describeTypedMismatch(Iterable<T> item,
468463
Description mismatchDescription, Map matchState, bool verbose) =>
469-
mismatchDescription.add(_test(item, matchState)!);
464+
mismatchDescription
465+
.add('found elements out of order: ')
466+
.addDescriptionOf(matchState['first'])
467+
.add(' and ')
468+
.addDescriptionOf(matchState['second']);
470469
}

pkgs/matcher/test/iterable_matchers_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void main() {
416416
isSortedBy<String, num>((String s) => s.length),
417417
'Expected: is sorted '
418418
'Actual: [\'y\', \'bbbb\', \'aaaa\', \'zz\'] '
419-
'Which: found elements out of order: <aaaa> and <zz>');
419+
'Which: found elements out of order: \'aaaa\' and \'zz\'');
420420
});
421421

422422
test('isSortedByCompare', () {
@@ -430,6 +430,6 @@ void main() {
430430
isSortedByCompare((String s) => s.length, (a, b) => b.compareTo(a)),
431431
'Expected: is sorted '
432432
'Actual: [\'y\', \'bbbb\', \'aaaa\', \'zz\'] '
433-
'Which: found elements out of order: <y> and <bbbb>');
433+
'Which: found elements out of order: \'y\' and \'bbbb\'');
434434
});
435435
}

0 commit comments

Comments
 (0)