@@ -212,9 +212,7 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
212212 List <Diagnostic > diagnostics,
213213 List <ExpectedDiagnostic > expectedDiagnostics,
214214 ) {
215- //
216215 // Match actual diagnostics to expected diagnostics.
217- //
218216 var unmatchedActual = diagnostics.toList ();
219217 var unmatchedExpected = expectedDiagnostics.toList ();
220218 var actualIndex = 0 ;
@@ -236,99 +234,33 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
236234 actualIndex++ ;
237235 }
238236 }
239- //
240- // Write the results.
241- //
237+
238+ // Print the results to the terminal.
242239 var buffer = StringBuffer ();
243240 if (unmatchedExpected.isNotEmpty) {
244- buffer.writeln ('Expected but did not find:' );
245- for (var expected in unmatchedExpected) {
246- buffer.write (' ' );
247- if (expected is ExpectedError ) {
248- buffer.write (expected._code);
249- }
250- if (expected is ExpectedLint ) {
251- buffer.write (expected._lintName);
252- }
253- buffer.write (' [' );
254- buffer.write (expected._offset);
255- buffer.write (', ' );
256- buffer.write (expected._length);
257- if (expected._messageContains != null ) {
258- buffer.write (', messageContains: ' );
259- buffer.write (json.encode (expected._messageContains.toString ()));
260- }
261- if (expected._correctionContains != null ) {
262- buffer.write (', correctionContains: ' );
263- buffer.write (json.encode (expected._correctionContains.toString ()));
264- }
265- buffer.writeln (']' );
266- }
241+ buffer.write (missingExpectedMessage (unmatchedExpected));
267242 }
268243 if (unmatchedActual.isNotEmpty) {
269- if (buffer.isNotEmpty) {
270- buffer.writeln ();
271- }
272- buffer.writeln ('Found but did not expect:' );
273- for (var actual in unmatchedActual) {
274- buffer.write (' ' );
275- buffer.write (actual.diagnosticCode);
276- buffer.write (' [' );
277- buffer.write (actual.offset);
278- buffer.write (', ' );
279- buffer.write (actual.length);
280- buffer.write (', ' );
281- buffer.write (actual.message);
282- if (actual.correctionMessage != null ) {
283- buffer.write (', ' );
284- buffer.write (json.encode (actual.correctionMessage));
285- }
286- buffer.writeln (']' );
287- }
244+ buffer.write (unexpectedMessage (unmatchedActual));
288245 }
289- if (buffer.isNotEmpty) {
290- diagnostics.sort (
291- (first, second) => first.offset.compareTo (second.offset),
292- );
293- buffer.writeln ();
294- buffer.writeln ('To accept the current state, expect:' );
295- for (var actual in diagnostics) {
296- late String diagnosticKind;
297- Object ? description;
298- if (actual.diagnosticCode is LintCode ) {
299- diagnosticKind = 'lint' ;
300- } else {
301- diagnosticKind = 'error' ;
302- description = actual.diagnosticCode;
303- }
304- buffer.write (' $diagnosticKind (' );
305- if (description != null ) {
306- buffer.write (description);
307- buffer.write (', ' );
308- }
309- buffer.write (actual.offset);
310- buffer.write (', ' );
311- buffer.write (actual.length);
312- buffer.writeln ('),' );
313- }
246+ if (unmatchedExpected.isNotEmpty || unmatchedActual.isNotEmpty) {
247+ buffer.write (correctionMessage (diagnostics));
314248
315249 if (dumpAstOnFailures) {
316250 buffer.writeln ();
317251 buffer.writeln ();
318- try {
319- var astSink = StringBuffer ();
320252
253+ try {
321254 Spelunker (
322255 result.unit.toSource (),
323- sink: astSink ,
256+ sink: buffer ,
324257 featureSet: result.unit.featureSet,
325258 ).spelunk ();
326- buffer.write (astSink);
327- buffer.writeln ();
328- // I hereby choose to catch this type.
329259 } on ArgumentError catch (_) {
330260 // Perhaps we encountered a parsing error while spelunking.
331261 }
262+
263+ buffer.writeln ();
332264 }
333265
334266 fail (buffer.toString ());
@@ -371,6 +303,54 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
371303 Future <void > assertNoDiagnosticsInFile (String path) async =>
372304 assertDiagnosticsInFile (path, const []);
373305
306+ /// Text to display upon failure, which indicates possible corrections.
307+ @visibleForOverriding
308+ String correctionMessage (List <Diagnostic > diagnostics) {
309+ var buffer = StringBuffer ();
310+ diagnostics.sort ((first, second) => first.offset.compareTo (second.offset));
311+ buffer.writeln ();
312+ buffer.writeln ('To accept the current state, expect:' );
313+ for (var actual in diagnostics) {
314+ if (actual.diagnosticCode is LintCode ) {
315+ buffer.write (' lint(' );
316+ } else {
317+ buffer.write (' error(${actual .diagnosticCode }, ' );
318+ }
319+ buffer.write ('${actual .offset }, ${actual .length }),' );
320+ }
321+
322+ return buffer.toString ();
323+ }
324+
325+ /// Text to display upon failure, indicating that [unmatchedExpected]
326+ /// diagnostics were expected, but not found.
327+ @visibleForOverriding
328+ String missingExpectedMessage (List <ExpectedDiagnostic > unmatchedExpected) {
329+ var buffer = StringBuffer ();
330+ buffer.writeln ('Expected but did not find:' );
331+ for (var expected in unmatchedExpected) {
332+ buffer.write (' ' );
333+ if (expected is ExpectedError ) {
334+ buffer.write (expected._code);
335+ }
336+ if (expected is ExpectedLint ) {
337+ buffer.write (expected._lintName);
338+ }
339+ buffer.write (' [${expected ._offset }, ' );
340+ buffer.write (expected._length);
341+ if (expected._messageContains case Pattern messageContains) {
342+ buffer.write (', messageContains: ' );
343+ buffer.write (json.encode (messageContains.toString ()));
344+ }
345+ if (expected._correctionContains case Pattern correctionContains) {
346+ buffer.write (', correctionContains: ' );
347+ buffer.write (json.encode (correctionContains.toString ()));
348+ }
349+ buffer.writeln (']' );
350+ }
351+ return buffer.toString ();
352+ }
353+
374354 @override
375355 File newFile (String path, String content) {
376356 if (_analysisContextCollection != null && ! path.endsWith ('.dart' )) {
@@ -414,6 +394,27 @@ class PubPackageResolutionTest with MockPackagesMixin, ResourceProviderMixin {
414394 _analysisContextCollection = null ;
415395 }
416396
397+ /// Text to display upon failure, indicating that [unmatchedActual]
398+ /// diagnostics were found, but unexpected.
399+ @visibleForOverriding
400+ String unexpectedMessage (List <Diagnostic > unmatchedActual) {
401+ var buffer = StringBuffer ();
402+ if (buffer.isNotEmpty) {
403+ buffer.writeln ();
404+ }
405+ buffer.writeln ('Found but did not expect:' );
406+ for (var actual in unmatchedActual) {
407+ buffer.write (' ${actual .diagnosticCode } [' );
408+ buffer.write ('${actual .offset }, ${actual .length }, ${actual .message }' );
409+ if (actual.correctionMessage case Pattern correctionMessage) {
410+ buffer.write (', ' );
411+ buffer.write (json.encode (correctionMessage));
412+ }
413+ buffer.writeln (']' );
414+ }
415+ return buffer.toString ();
416+ }
417+
417418 void writePackageConfig (String path, PackageConfigFileBuilder config) {
418419 newFile (path, config.toContent (pathContext: pathContext));
419420 }
0 commit comments