Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkgs/checks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.3.2-wip

- Require Dart 3.7
- Improve speed of pretty printing for large collections.

## 0.3.1

Expand All @@ -13,7 +14,7 @@
combined functionality in `containsInOrder`.
- Replace `pairwiseComparesTo` with `pairwiseMatches`.
- Fix a bug where printing the result of a failed deep quality check would
fail with a `TypeError` when comparing large `Map` instances
fail with a `TypeError` when comparing large `Map` instances.
- Increase SDK constraint to ^3.5.0.
- Clarify this package is experimental.

Expand Down
37 changes: 20 additions & 17 deletions pkgs/checks/lib/src/describe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,23 @@ Iterable<String> _prettyPrint(
open = '(';
close = ')';
}
final elements = object.map(prettyPrintNested).toList();
final elements = object.map(prettyPrintNested);
return _prettyPrintCollection(
open,
close,
elements,
_maxLineLength - indentSize,
);
} else if (object is Map) {
final entries =
object.entries.map((entry) {
final key = prettyPrintNested(entry.key);
final value = prettyPrintNested(entry.value);
return [
...key.take(key.length - 1),
'${key.last}: ${value.first}',
...value.skip(1),
];
}).toList();
final entries = object.entries.map((entry) {
final key = prettyPrintNested(entry.key);
final value = prettyPrintNested(entry.value);
return [
...key.take(key.length - 1),
'${key.last}: ${value.first}',
...value.skip(1),
];
});
return _prettyPrintCollection(
'{',
'}',
Expand All @@ -86,15 +85,19 @@ Iterable<String> _prettyPrint(
Iterable<String> _prettyPrintCollection(
String open,
String close,
List<Iterable<String>> elements,
Iterable<Iterable<String>> elements,
int maxLength,
) {
if (elements.length > _maxItems) {
const ellipsisElement = [
['...'],
];
elements.replaceRange(_maxItems - 1, elements.length, ellipsisElement);
final trimmedElements = <Iterable<String>>[];
for (var element in elements) {
if (trimmedElements.length >= _maxItems) {
trimmedElements[_maxItems - 1] = ['...'];
break;
}
trimmedElements.add(element);
}
elements = trimmedElements;

if (elements.every((e) => e.length == 1)) {
final singleLine = '$open${elements.map((e) => e.single).join(', ')}$close';
if (singleLine.length <= maxLength) {
Expand Down
12 changes: 12 additions & 0 deletions pkgs/checks/test/pretty_print_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ void main() {
test('in iterables', () {
check(literal(largeList.followedBy([]))).last.equals('...)');
});
test('without processing truncated elements', () {
final poisonedList = [...largeList, _PoisonToString()];
check(() {
literal(poisonedList);
}).returnsNormally();
});
test('in maps', () {
final map = Map<int, int>.fromIterables(largeList, largeList);
check(literal(map)).last.equals('...}');
});
});
});
}

class _PoisonToString {
@override
String toString() =>
throw StateError('Truncated entry should not be processed');
}
4 changes: 4 additions & 0 deletions pkgs/matcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.19-wip

* Improve speed of pretty printing for large collections.

## 0.12.18

* Add `isSorted` and related matchers for iterables.
Expand Down
25 changes: 14 additions & 11 deletions pkgs/matcher/lib/src/pretty_print.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ String prettyPrint(Object? object, {int? maxLineLength, int? maxItems}) {
var type = object is List ? '' : '${_typeName(object)}:';

// Truncate the list of strings if it's longer than [maxItems].
var strings = object.map(pp).toList();
if (maxItems != null && strings.length > maxItems) {
strings.replaceRange(maxItems - 1, strings.length, ['...']);
var strings = <String>[];
for (var item in object) {
if (maxItems != null && strings.length >= maxItems) {
strings[maxItems - 1] = '...';
break;
}
strings.add(pp(item));
}

// If the printed string is short and doesn't contain a newline, print it
Expand All @@ -58,15 +62,14 @@ String prettyPrint(Object? object, {int? maxLineLength, int? maxItems}) {
return _indent(indent + 2) + string;
}).join(',\n')}\n${_indent(indent)}]';
} else if (object is Map) {
// Convert the contents of the map to string representations.
var strings =
object.keys.map((key) {
return '${pp(key)}: ${pp(object[key])}';
}).toList();

// Truncate the list of strings if it's longer than [maxItems].
if (maxItems != null && strings.length > maxItems) {
strings.replaceRange(maxItems - 1, strings.length, ['...']);
var strings = <String>[];
for (var key in object.keys) {
if (maxItems != null && strings.length >= maxItems) {
strings[maxItems - 1] = '...';
break;
}
strings.add('${pp(key)}: ${pp(object[key])}');
}

// If the printed string is short and doesn't contain a newline, print it
Expand Down
2 changes: 1 addition & 1 deletion pkgs/matcher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: matcher
version: 0.12.18
version: 0.12.19-wip
description: >-
Support for specifying test expectations via an extensible Matcher class.
Also includes a number of built-in Matcher implementations for common cases.
Expand Down
Loading