11// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
4+ import 'dart:math' as math;
45
56import 'package:expect/expect.dart' ;
67
@@ -16,7 +17,7 @@ import 'utils.dart';
1617// escapes here like `\/`.
1718
1819void main () {
19- // Inserts analyzer, CFE, and both errors.
20+ // Inserts single-front end errors.
2021 expectUpdate ("""
2122int i = "bad";
2223
@@ -26,32 +27,84 @@ int third = "boo";
2627""" , errors: [
2728 makeError (line: 1 , column: 9 , length: 5 , analyzerError: "some.error" ),
2829 makeError (line: 3 , column: 15 , length: 7 , cfeError: "Bad." ),
30+ makeError (line: 5 , column: 13 , length: 5 , webError: "Web.\n Error." ),
31+ ], expected: """
32+ int i = "bad";
33+ /\/ ^^^^^
34+ /\/ [analyzer] some.error
35+
36+ int another = "wrong";
37+ /\/ ^^^^^^^
38+ /\/ [cfe] Bad.
39+
40+ int third = "boo";
41+ /\/ ^^^^^
42+ /\/ [web] Web.
43+ /\/ Error.
44+ """ );
45+
46+ // Inserts errors for multiple front ends.
47+ expectUpdate ("""
48+ int i = "bad";
49+
50+ int another = "wrong";
51+
52+ int third = "boo";
53+
54+ int last = "oops";
55+ """ , errors: [
56+ makeError (
57+ line: 1 ,
58+ column: 9 ,
59+ length: 5 ,
60+ analyzerError: "some.error" ,
61+ cfeError: "Bad." ),
62+ makeError (
63+ line: 3 ,
64+ column: 15 ,
65+ length: 7 ,
66+ cfeError: "Another bad." ,
67+ webError: "Web.\n Error." ),
2968 makeError (
3069 line: 5 ,
3170 column: 13 ,
3271 length: 5 ,
33- analyzerError: "an.error" ,
34- cfeError: "Wrong.\n Line.\n Another." ),
72+ analyzerError: "third.error" ,
73+ webError: "Web error." ),
74+ makeError (
75+ line: 7 ,
76+ column: 12 ,
77+ length: 6 ,
78+ analyzerError: "last.error" ,
79+ cfeError: "Final.\n Error." ,
80+ webError: "Web error." ),
3581 ], expected: """
3682int i = "bad";
3783/\/ ^^^^^
3884/\/ [analyzer] some.error
85+ /\/ [cfe] Bad.
3986
4087int another = "wrong";
4188/\/ ^^^^^^^
42- /\/ [cfe] Bad.
89+ /\/ [cfe] Another bad.
90+ /\/ [web] Web.
91+ /\/ Error.
4392
4493int third = "boo";
4594/\/ ^^^^^
46- /\/ [analyzer] an.error
47- /\/ [cfe] Wrong.
48- /\/ Line.
49- /\/ Another.
95+ /\/ [analyzer] third.error
96+ /\/ [web] Web error.
97+
98+ int last = "oops";
99+ /\/ ^^^^^^
100+ /\/ [analyzer] last.error
101+ /\/ [cfe] Final.
102+ /\/ Error.
103+ /\/ [web] Web error.
50104""" );
51105
52106 // Removes only analyzer errors.
53- expectUpdate (
54- """
107+ expectUpdate ("""
55108int i = "bad";
56109/\/ ^^^^^
57110/\/ [analyzer] some.error
@@ -64,9 +117,7 @@ int third = "boo";
64117/\/ ^^^^^
65118/\/ [analyzer] an.error
66119/\/ [cfe] Wrong.
67- """ ,
68- removeCfe: false ,
69- expected: """
120+ """ , remove: {ErrorSource .analyzer}, expected: """
70121int i = "bad";
71122
72123int another = "wrong";
@@ -79,8 +130,7 @@ int third = "boo";
79130""" );
80131
81132 // Removes only CFE errors.
82- expectUpdate (
83- """
133+ expectUpdate ("""
84134int i = "bad";
85135/\/ ^^^^^
86136/\/ [analyzer] some.error
@@ -93,9 +143,7 @@ int third = "boo";
93143/\/ ^^^^^
94144/\/ [analyzer] an.error
95145/\/ [cfe] Wrong.
96- """ ,
97- removeAnalyzer: false ,
98- expected: """
146+ """ , remove: {ErrorSource .cfe}, expected: """
99147int i = "bad";
100148/\/ ^^^^^
101149/\/ [analyzer] some.error
@@ -105,6 +153,60 @@ int another = "wrong";
105153int third = "boo";
106154/\/ ^^^^^
107155/\/ [analyzer] an.error
156+ """ );
157+
158+ // Removes only web errors.
159+ expectUpdate ("""
160+ int i = "bad";
161+ /\/ ^^^^^
162+ /\/ [analyzer] some.error
163+
164+ int another = "wrong";
165+ /\/ ^^^^^^^
166+ /\/ [web] Bad.
167+
168+ int third = "boo";
169+ /\/ ^^^^^
170+ /\/ [cfe] Error.
171+ /\/ [web] Wrong.
172+ """ , remove: {ErrorSource .web}, expected: """
173+ int i = "bad";
174+ /\/ ^^^^^
175+ /\/ [analyzer] some.error
176+
177+ int another = "wrong";
178+
179+ int third = "boo";
180+ /\/ ^^^^^
181+ /\/ [cfe] Error.
182+ """ );
183+
184+ // Removes multiple error sources.
185+ expectUpdate ("""
186+ int i = "bad";
187+ /\/ ^^^^^
188+ /\/ [analyzer] some.error
189+ /\/ [cfe] CFE error.
190+
191+ int another = "wrong";
192+ /\/ ^^^^^^^
193+ /\/ [web] Bad.
194+
195+ int third = "boo";
196+ /\/ ^^^^^
197+ /\/ [analyzer] another.error
198+ /\/ [cfe] Error.
199+ /\/ [web] Wrong.
200+ """ , remove: {ErrorSource .analyzer, ErrorSource .web}, expected: """
201+ int i = "bad";
202+ /\/ ^^^^^
203+ /\/ [cfe] CFE error.
204+
205+ int another = "wrong";
206+
207+ int third = "boo";
208+ /\/ ^^^^^
209+ /\/ [cfe] Error.
108210""" );
109211
110212 // Preserves previous error's indentation if possible.
303405
304406void regression () {
305407 // https://github.com/dart-lang/sdk/issues/37990.
306- expectUpdate (
307- """
408+ expectUpdate ("""
308409int get xx => 3;
309410int get yy => 3;
310411
@@ -325,20 +426,16 @@ class A {
325426
326427 }
327428}
328- """ ,
329- removeAnalyzer: false ,
330- errors: [
331- makeError (
332- line: 6 ,
333- column: 5 ,
334- length: 14 ,
335- cfeError: "Setter not found: 'xx'." ),
336- makeError (
337- line: 16 ,
338- column: 7 ,
339- cfeError: "The method 'call' isn't defined for the class 'int'." )
340- ],
341- expected: """
429+ """ , remove: {
430+ ErrorSource .cfe
431+ }, errors: [
432+ makeError (
433+ line: 6 , column: 5 , length: 14 , cfeError: "Setter not found: 'xx'." ),
434+ makeError (
435+ line: 16 ,
436+ column: 7 ,
437+ cfeError: "The method 'call' isn't defined for the class 'int'." )
438+ ], expected: """
342439int get xx => 3;
343440int get yy => 3;
344441
@@ -362,18 +459,62 @@ class A {
362459}
363460
364461void expectUpdate (String original,
365- {List <StaticError > errors,
366- bool removeAnalyzer = true ,
367- bool removeCfe = true ,
368- String expected}) {
462+ {List <StaticError > errors, Set <ErrorSource > remove, String expected}) {
369463 errors ?? = const [];
464+ remove ?? = ErrorSource .all.toSet ();
370465
371- var actual = updateErrorExpectations (original, errors,
372- removeAnalyzer: removeAnalyzer, removeCfe: removeCfe);
466+ var actual = updateErrorExpectations (original, errors, remove: remove);
373467 if (actual != expected) {
374468 // Not using Expect.equals() because the diffs it shows aren't helpful for
375469 // strings this large.
376- Expect .fail ("Output did not match expectation. Expected:\n $expected "
377- "\n\n Was:\n $actual " );
470+ var actualLines = actual.split ("\n " );
471+ var expectedLines = expected.split ("\n " );
472+
473+ // Figure out which leading lines do match so we can ignore those and
474+ // highlight the offending ones.
475+ var matchingActual = < int > {};
476+ var matchingExpected = < int > {};
477+ for (var i = 0 ;
478+ i < math.min (actualLines.length, expectedLines.length);
479+ i++ ) {
480+ if (actualLines[i] != expectedLines[i]) break ;
481+ matchingActual.add (i);
482+ matchingExpected.add (i);
483+ }
484+
485+ // Find which trailing lines are the same so we can hide those too.
486+ for (var i = 0 ;
487+ i < math.min (actualLines.length, expectedLines.length);
488+ i++ ) {
489+ // Count backwards from the ends of each list.
490+ var actualIndex = actualLines.length - i - 1 ;
491+ var expectedIndex = expectedLines.length - i - 1 ;
492+ if (actualLines[actualIndex] != expectedLines[expectedIndex]) break ;
493+ matchingActual.add (actualIndex);
494+ matchingExpected.add (expectedIndex);
495+ }
496+
497+ var buffer = StringBuffer ();
498+ void writeLine (int index, String line, Set <int > matchingLines) {
499+ // Only show the line if it was different from the expectation.
500+ if (matchingLines.contains (index)) {
501+ buffer.writeln (" : $line " );
502+ } else {
503+ buffer.writeln ("${(index + 1 ).toString ().padLeft (4 )}: $line " );
504+ }
505+ }
506+
507+ buffer.writeln ("Output did not match expectation. Expected:" );
508+ for (var i = 0 ; i < expectedLines.length; i++ ) {
509+ writeLine (i, expectedLines[i], matchingExpected);
510+ }
511+
512+ buffer.writeln ();
513+ buffer.writeln ("Was:" );
514+ for (var i = 0 ; i < actualLines.length; i++ ) {
515+ writeLine (i, actualLines[i], matchingActual);
516+ }
517+
518+ Expect .fail (buffer.toString ());
378519 }
379520}
0 commit comments