Skip to content

Commit 86d3c73

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Remove useLineEndingsForPlatform=false from some fix tests
See #60234 Change-Id: I851779a43260a468d772dbb997a1f7e1e2395656 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/441960 Reviewed-by: Keerti Parthasarathy <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent feabbd9 commit 86d3c73

File tree

7 files changed

+41
-23
lines changed

7 files changed

+41
-23
lines changed

pkg/analysis_server/test/abstract_single_unit.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import 'abstract_context.dart';
1818
class AbstractSingleUnitTest extends AbstractContextTest {
1919
bool verifyNoTestUnitErrors = true;
2020

21-
/// Whether the test code should parse carrets as position shorthands.
21+
/// Whether the test code should parse with position and range shorthands.
2222
///
23-
/// Set this to `true` when the test code is using the carret operator.
24-
bool keepCaret = false;
23+
/// Set this to `false` when the test code contains a legitimate carret
24+
/// or contains `[!` or `!]`.
25+
bool allowTestCodeShorthand = true;
2526

2627
TestCode? _parsedTestCode;
2728
late ParsedUnitResult testParsedResult;
@@ -45,7 +46,8 @@ class AbstractSingleUnitTest extends AbstractContextTest {
4546
set testCode(String value) {
4647
parsedTestCode = TestCode.parse(
4748
normalizeSource(value),
48-
positionShorthand: !keepCaret,
49+
positionShorthand: allowTestCodeShorthand,
50+
rangeShorthand: allowTestCodeShorthand,
4951
);
5052
}
5153

pkg/analysis_server/test/src/services/correction/fix/add_null_check_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ String? f(String? s) => s!..hashCode..length;
531531
}
532532

533533
Future<void> test_spreadList() async {
534+
// expected code contains !] which looks like a range.
535+
allowTestCodeShorthand = false;
536+
534537
await resolveTestCode('''
535538
void f (List<String>? args) {
536539
[...args];

pkg/analysis_server/test/src/services/correction/fix/convert_for_each_to_for_loop_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ void f(List<String> a, List<String> b) {
4646
}
4747

4848
Future<void> test_blockBody_expressionBody() async {
49-
useLineEndingsForPlatform = false;
50-
5149
await resolveTestCode(r'''
5250
void f(List<String> a, List<String> b) {
5351
var result = <String>[];
@@ -69,8 +67,6 @@ void f(List<String> a, List<String> b) {
6967
}
7068

7169
Future<void> test_expressionBody_blockBody() async {
72-
useLineEndingsForPlatform = false;
73-
7470
await resolveTestCode(r'''
7571
void f(List<String> a, List<String> b) {
7672
var result = <String>[];
@@ -92,8 +88,6 @@ void f(List<String> a, List<String> b) {
9288
}
9389

9490
Future<void> test_expressionBody_expressionBody() async {
95-
useLineEndingsForPlatform = false;
96-
9791
await resolveTestCode(r'''
9892
void f(List<String> a, List<String> b) {
9993
var result = <String>[];

pkg/analysis_server/test/src/services/correction/fix/convert_to_boolean_expression_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ void f(bool value) {
466466
}
467467

468468
Future<void> test_xorFalse() async {
469-
keepCaret = true;
469+
allowTestCodeShorthand = false; // Test uses ^
470+
470471
await resolveTestCode(r'''
471472
void f(bool value) {
472473
if (value ^ false) print(value);
@@ -480,7 +481,8 @@ void f(bool value) {
480481
}
481482

482483
Future<void> test_xorTrue() async {
483-
keepCaret = true;
484+
allowTestCodeShorthand = false; // Test uses ^
485+
484486
await resolveTestCode(r'''
485487
void f(bool value) {
486488
if (value ^ true) print(value);

pkg/analysis_server/test/src/services/correction/fix/create_local_variable_test.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,6 @@ void f() {
222222
}
223223

224224
Future<void> test_withImport() async {
225-
useLineEndingsForPlatform = false;
226-
227225
newFile('$workspaceRootPath/pkg/lib/a/a.dart', '''
228226
class A {}
229227
''');
@@ -261,21 +259,21 @@ import 'package:pkg/c/c.dart';
261259
262260
void f() {
263261
A? a;
264-
B b;
265-
new C(a, b);
262+
/*0*/B /*1*/b;
263+
new C(a, /*2*/b);
266264
}
267265
''');
268266
var groups = change.linkedEditGroups;
269267
expect(groups, hasLength(2));
270268
var typeGroup = groups[0];
271269
var typePositions = typeGroup.positions;
272270
expect(typePositions, hasLength(1));
273-
expect(typePositions[0].offset, 115);
271+
expect(typePositions[0].offset, parsedExpectedCode.positions[0].offset);
274272
var nameGroup = groups[1];
275273
var groupPositions = nameGroup.positions;
276274
expect(groupPositions, hasLength(2));
277-
expect(groupPositions[0].offset, 117);
278-
expect(groupPositions[1].offset, 131);
275+
expect(groupPositions[0].offset, parsedExpectedCode.positions[1].offset);
276+
expect(groupPositions[1].offset, parsedExpectedCode.positions[2].offset);
279277
}
280278

281279
Future<void> test_write_assignment() async {

pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analysis_server_plugin/src/correction/fix_processor.dart';
1313
import 'package:analyzer/diagnostic/diagnostic.dart';
1414
import 'package:analyzer/error/error.dart';
1515
import 'package:analyzer/file_system/file_system.dart';
16+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
1617
import 'package:analyzer_plugin/protocol/protocol_common.dart'
1718
hide AnalysisError;
1819
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -375,20 +376,29 @@ abstract class FixProcessorLintTest extends FixProcessorTest {
375376

376377
/// A base class defining support for writing fix processor tests.
377378
abstract class FixProcessorTest extends BaseFixProcessorTest {
379+
late TestCode parsedExpectedCode;
380+
378381
/// The kind of fixes being tested by this test class.
379382
FixKind get kind;
380383

381384
/// Asserts that the resolved compilation unit has a fix which produces
382-
/// [expected] output.
385+
/// [expectedContent] output.
386+
///
387+
/// [expectedContent] will have newlines normalized and be parsed with
388+
/// [TestCode.parse], with the resulting code stored in [parsedExpectedCode].
383389
Future<void> assertHasFix(
384-
String expected, {
390+
String expectedContent, {
385391
ErrorFilter? errorFilter,
386392
String? target,
387393
int? expectedNumberOfFixesForKind,
388394
String? matchFixMessage,
389395
bool allowFixAllFixes = false,
390396
}) async {
391-
expected = normalizeSource(expected);
397+
parsedExpectedCode = TestCode.parse(
398+
normalizeSource(expectedContent),
399+
positionShorthand: allowTestCodeShorthand,
400+
rangeShorthand: allowTestCodeShorthand,
401+
);
392402
var diagnostic = await _findDiagnosticToFix(filter: errorFilter);
393403
var fix = await _assertHasFix(
394404
diagnostic,
@@ -409,7 +419,7 @@ abstract class FixProcessorTest extends BaseFixProcessorTest {
409419
}
410420

411421
resultCode = SourceEdit.applySequence(fileContent, change.edits[0].edits);
412-
expect(resultCode, expected);
422+
expect(resultCode, parsedExpectedCode.code);
413423
}
414424

415425
Future<void> assertHasFixAllFix(

pkg/analysis_server/test/src/services/correction/fix/update_sdk_constraints_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ class C {
5454
);
5555
}
5656

57+
@override
58+
void setUp() {
59+
super.setUp();
60+
61+
// These tests use ^ a lot but the base methods use TestCode.parse() that
62+
// considers them position markers by default.
63+
allowTestCodeShorthand = false;
64+
}
65+
5766
Future<void> test_any() async {
5867
await assertUpdateWithGtGtGt(from: 'any', to: '^2.14.0');
5968
}

0 commit comments

Comments
 (0)