From 073ef8bd2d04f4afee8506e26a8a7adde03bfe84 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Fri, 17 Jan 2025 11:28:34 -0800 Subject: [PATCH] Fix a type error that occurs comparing two large maps with `deepEquals`. (#2442) Closes #2442 Avoid inference forcing a `List>` which results in a type error inserting a range into a list requiring `List`. Add a test for collection truncating which would have failed with a type error before this fix. --- pkgs/checks/CHANGELOG.md | 2 ++ pkgs/checks/lib/src/describe.dart | 5 +++-- pkgs/checks/test/pretty_print_test.dart | 30 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 pkgs/checks/test/pretty_print_test.dart diff --git a/pkgs/checks/CHANGELOG.md b/pkgs/checks/CHANGELOG.md index 6588352e9..52f4c3e78 100644 --- a/pkgs/checks/CHANGELOG.md +++ b/pkgs/checks/CHANGELOG.md @@ -8,6 +8,8 @@ - Add `containsMatchingInOrder` and `containsEqualInOrder` to replace the 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 - Increase SDK constraint to ^3.5.0. ## 0.3.0 diff --git a/pkgs/checks/lib/src/describe.dart b/pkgs/checks/lib/src/describe.dart index f05a1640b..910e4c3e4 100644 --- a/pkgs/checks/lib/src/describe.dart +++ b/pkgs/checks/lib/src/describe.dart @@ -72,9 +72,10 @@ Iterable _prettyPrint( Iterable _prettyPrintCollection( String open, String close, List> elements, int maxLength) { if (elements.length > _maxItems) { - elements.replaceRange(_maxItems - 1, elements.length, [ + const ellipsisElement = [ ['...'] - ]); + ]; + elements.replaceRange(_maxItems - 1, elements.length, ellipsisElement); } if (elements.every((e) => e.length == 1)) { final singleLine = '$open${elements.map((e) => e.single).join(', ')}$close'; diff --git a/pkgs/checks/test/pretty_print_test.dart b/pkgs/checks/test/pretty_print_test.dart new file mode 100644 index 000000000..63f1cc147 --- /dev/null +++ b/pkgs/checks/test/pretty_print_test.dart @@ -0,0 +1,30 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:checks/checks.dart'; +import 'package:checks/context.dart'; +import 'package:test/scaffolding.dart'; + +void main() { + group('literal', () { + group('truncates large collections', () { + const maxUntruncatedCollection = 25; + final largeList = + List.generate(maxUntruncatedCollection + 1, (i) => i); + test('in lists', () { + check(literal(largeList)).last.equals('...]'); + }); + test('in sets', () { + check(literal(largeList.toSet())).last.equals('...}'); + }); + test('in iterables', () { + check(literal(largeList.followedBy([]))).last.equals('...)'); + }); + test('in maps', () { + final map = Map.fromIterables(largeList, largeList); + check(literal(map)).last.equals('...}'); + }); + }); + }); +}