From 398fdee2afdc3ab8c94371fff2448244fecda0fc Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 27 May 2025 21:29:31 -0500 Subject: [PATCH] dev/bots: improve service worker test code (#169231) Use `foundError` instead of throwing - as is recommended DRASTICALLY improve the error output by copying (more) from pkg:matcher --- dev/bots/service_worker_test.dart | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/dev/bots/service_worker_test.dart b/dev/bots/service_worker_test.dart index b1ebb403519..9efb604b168 100644 --- a/dev/bots/service_worker_test.dart +++ b/dev/bots/service_worker_test.dart @@ -222,7 +222,50 @@ void expect(Object? actual, Object? expected) { } final StringDescription mismatchDescription = StringDescription(); matcher.describeMismatch(actual, mismatchDescription, matchState, true); - throw TestFailure(mismatchDescription.toString()); + + // + // COPIED FROM pkg:matcher - formatFailure() - 2025-05-21 + // + final String which = mismatchDescription.toString(); + final StringBuffer buffer = StringBuffer(); + buffer.writeln(_indent(_prettyPrint(expected), first: 'Expected: ')); + buffer.writeln(_indent(_prettyPrint(actual), first: ' Actual: ')); + if (which.isNotEmpty) { + buffer.writeln(_indent(which, first: ' Which: ')); + } + // + // ENDE pkg:matcher copy + // + + foundError([buffer.toString(), StackTrace.current.toString()]); +} + +/// Returns a pretty-printed representation of [value]. +/// +/// The matcher package doesn't expose its pretty-print function directly, but +/// we can use it through StringDescription. +// COPIED FROM pkg:matcher - 2025-05-21 +String _prettyPrint(Object? value) => StringDescription().addDescriptionOf(value).toString(); + +/// Indent each line in [text] by [first] spaces. +/// +/// [first] is used in place of the first line's indentation. +// COPIED FROM pkg:matcher - 2025-05-21 +String _indent(String text, {required String first}) { + final String prefix = ' ' * first.length; + final List lines = text.split('\n'); + if (lines.length == 1) { + return '$first$text'; + } + + final StringBuffer buffer = StringBuffer('$first${lines.first}\n'); + + // Write out all but the first and last lines with [prefix]. + for (final String line in lines.skip(1).take(lines.length - 2)) { + buffer.writeln('$prefix$line'); + } + buffer.write('$prefix${lines.last}'); + return buffer.toString(); } Future runWebServiceWorkerTest({