1
1
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
+ import 'dart:math' as math;
4
5
5
6
import 'package:expect/expect.dart' ;
6
7
@@ -16,7 +17,7 @@ import 'utils.dart';
16
17
// escapes here like `\/`.
17
18
18
19
void main () {
19
- // Inserts analyzer, CFE, and both errors.
20
+ // Inserts single-front end errors.
20
21
expectUpdate ("""
21
22
int i = "bad";
22
23
@@ -26,32 +27,84 @@ int third = "boo";
26
27
""" , errors: [
27
28
makeError (line: 1 , column: 9 , length: 5 , analyzerError: "some.error" ),
28
29
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." ),
29
68
makeError (
30
69
line: 5 ,
31
70
column: 13 ,
32
71
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." ),
35
81
], expected: """
36
82
int i = "bad";
37
83
/\/ ^^^^^
38
84
/\/ [analyzer] some.error
85
+ /\/ [cfe] Bad.
39
86
40
87
int another = "wrong";
41
88
/\/ ^^^^^^^
42
- /\/ [cfe] Bad.
89
+ /\/ [cfe] Another bad.
90
+ /\/ [web] Web.
91
+ /\/ Error.
43
92
44
93
int third = "boo";
45
94
/\/ ^^^^^
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.
50
104
""" );
51
105
52
106
// Removes only analyzer errors.
53
- expectUpdate (
54
- """
107
+ expectUpdate ("""
55
108
int i = "bad";
56
109
/\/ ^^^^^
57
110
/\/ [analyzer] some.error
@@ -64,9 +117,7 @@ int third = "boo";
64
117
/\/ ^^^^^
65
118
/\/ [analyzer] an.error
66
119
/\/ [cfe] Wrong.
67
- """ ,
68
- removeCfe: false ,
69
- expected: """
120
+ """ , remove: {ErrorSource .analyzer}, expected: """
70
121
int i = "bad";
71
122
72
123
int another = "wrong";
@@ -79,8 +130,7 @@ int third = "boo";
79
130
""" );
80
131
81
132
// Removes only CFE errors.
82
- expectUpdate (
83
- """
133
+ expectUpdate ("""
84
134
int i = "bad";
85
135
/\/ ^^^^^
86
136
/\/ [analyzer] some.error
@@ -93,9 +143,7 @@ int third = "boo";
93
143
/\/ ^^^^^
94
144
/\/ [analyzer] an.error
95
145
/\/ [cfe] Wrong.
96
- """ ,
97
- removeAnalyzer: false ,
98
- expected: """
146
+ """ , remove: {ErrorSource .cfe}, expected: """
99
147
int i = "bad";
100
148
/\/ ^^^^^
101
149
/\/ [analyzer] some.error
@@ -105,6 +153,60 @@ int another = "wrong";
105
153
int third = "boo";
106
154
/\/ ^^^^^
107
155
/\/ [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.
108
210
""" );
109
211
110
212
// Preserves previous error's indentation if possible.
303
405
304
406
void regression () {
305
407
// https://github.com/dart-lang/sdk/issues/37990.
306
- expectUpdate (
307
- """
408
+ expectUpdate ("""
308
409
int get xx => 3;
309
410
int get yy => 3;
310
411
@@ -325,20 +426,16 @@ class A {
325
426
326
427
}
327
428
}
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: """
342
439
int get xx => 3;
343
440
int get yy => 3;
344
441
@@ -362,18 +459,62 @@ class A {
362
459
}
363
460
364
461
void 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}) {
369
463
errors ?? = const [];
464
+ remove ?? = ErrorSource .all.toSet ();
370
465
371
- var actual = updateErrorExpectations (original, errors,
372
- removeAnalyzer: removeAnalyzer, removeCfe: removeCfe);
466
+ var actual = updateErrorExpectations (original, errors, remove: remove);
373
467
if (actual != expected) {
374
468
// Not using Expect.equals() because the diffs it shows aren't helpful for
375
469
// 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 ());
378
519
}
379
520
}
0 commit comments