diff --git a/pkg/analysis_server/test/abstract_context.dart b/pkg/analysis_server/test/abstract_context.dart index a7ec58319e3..bae688b242f 100644 --- a/pkg/analysis_server/test/abstract_context.dart +++ b/pkg/analysis_server/test/abstract_context.dart @@ -34,7 +34,7 @@ class AbstractContextTest static final ByteStore _byteStore = MemoryByteStore(); /// Whether to rewrite line endings in test code based on platform. - bool useLineEndingsForPlatform = false; + bool useLineEndingsForPlatform = true; final Map _declaredVariables = {}; AnalysisContextCollectionImpl? _analysisContextCollection; diff --git a/pkg/analysis_server/test/lsp/source_edits_test.dart b/pkg/analysis_server/test/lsp/source_edits_test.dart index 8ca9e497f16..1066390a7bb 100644 --- a/pkg/analysis_server/test/lsp/source_edits_test.dart +++ b/pkg/analysis_server/test/lsp/source_edits_test.dart @@ -3,10 +3,12 @@ // BSD-style license that can be found in the LICENSE file. import 'dart:convert'; +import 'dart:io'; import 'package:analysis_server/lsp_protocol/protocol.dart'; import 'package:analysis_server/src/lsp/error_or.dart'; import 'package:analysis_server/src/lsp/source_edits.dart'; +import 'package:analyzer/src/test_utilities/test_code_format.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -319,13 +321,14 @@ Delete 1:24-1:26 /// https://github.com/Dart-Code/Dart-Code/issues/5200 Future test_minimalEdits_comment_multiLine_trailingWhitespace() async { // The initial content has a trailing space on the end of the comment. - const startContent = r''' + var startContent = + TestCode.parse(r''' /** - * line with trailing whitespace - * line with trailing whitespace + * line with trailing whitespace /**/ + * line with trailing whitespace /**/ */ int? a; -'''; +''').code; // We expect the trailing spaces to be removed. const endContent = r''' /** @@ -619,6 +622,9 @@ void g() { String? expectedFormatResult, Range? range, }) async { + start = normalizeSource(start); + end = normalizeSource(end); + await parseTestCode(start); var edits = generateEditsForFormatting(testParsedResult, range: range).result!; @@ -639,10 +645,26 @@ void g() { String? expectedFormatResult, Range? range, }) async { - start = start.trim(); - end = end.trim(); + start = normalizeSource(start.trim()); + end = normalizeSource(end.trim()); + if (expectedFormatResult != null) { + expectedFormatResult = normalizeSource(expectedFormatResult.trim()); + } expected = expected.trim(); - expectedFormatResult = expectedFormatResult?.trim(); + + // Expectation descriptions are always written with literal newlines as '\n' + // so on Windows we will need to update the expectation to '\r\n' before + // comparing. + expect( + expected, + isNot(contains(r'\r')), + reason: + 'Expectations should always be written with literal ' + r"'\n' to indicate newlines regardless of platform", + ); + if (Platform.isWindows) { + expected = expected.replaceAll(r'\n', r'\r\n'); + } await parseTestCode(start); var edits = generateMinimalEdits(testParsedResult, end, range: range); diff --git a/pkg/analysis_server/test/services/completion/postfix/postfix_completion_test.dart b/pkg/analysis_server/test/services/completion/postfix/postfix_completion_test.dart index 1d244bc0e50..653eefdd48b 100644 --- a/pkg/analysis_server/test/services/completion/postfix/postfix_completion_test.dart +++ b/pkg/analysis_server/test/services/completion/postfix/postfix_completion_test.dart @@ -37,7 +37,7 @@ class PostfixCompletionTest extends AbstractSingleUnitTest { if (change.edits.isNotEmpty) { // We use a carat in the expected code to prevent lines containing only // whitespace from being made empty. - var expectedCode = TestCode.parse(expected); + var expectedCode = TestCode.parse(normalizeSource(expected)); var resultCode = SourceEdit.applySequence( testCode, change.edits[0].edits, @@ -68,7 +68,7 @@ class PostfixCompletionTest extends AbstractSingleUnitTest { } Future _prepareProcessor(String key, String code) async { - testCodeCode = TestCode.parse(code); + testCodeCode = TestCode.parse(normalizeSource(code)); verifyNoTestUnitErrors = false; await resolveTestCode(testCodeCode.code); diff --git a/pkg/analysis_server/test/services/correction/organize_directives_test.dart b/pkg/analysis_server/test/services/correction/organize_directives_test.dart index ab55929b8bc..160685765f0 100644 --- a/pkg/analysis_server/test/services/correction/organize_directives_test.dart +++ b/pkg/analysis_server/test/services/correction/organize_directives_test.dart @@ -1001,7 +1001,7 @@ class NonLibraryAnnotation { ); var edits = organizer.organize(); var result = SourceEdit.applySequence(testCode, edits); - expect(result, expectedCode); + expect(result, normalizeSource(expectedCode)); } Future _computeUnitAndErrors(String code) async { diff --git a/pkg/analysis_server/test/services/correction/sort_members_test.dart b/pkg/analysis_server/test/services/correction/sort_members_test.dart index 71b11f863c4..7e933b4057c 100644 --- a/pkg/analysis_server/test/services/correction/sort_members_test.dart +++ b/pkg/analysis_server/test/services/correction/sort_members_test.dart @@ -1793,6 +1793,6 @@ void f() {} ); var edits = sorter.sort(); var result = SourceEdit.applySequence(testCode, edits); - expect(result, expectedCode); + expect(result, normalizeSource(expectedCode)); } } diff --git a/pkg/analysis_server/test/src/computer/color_computer_test.dart b/pkg/analysis_server/test/src/computer/color_computer_test.dart index 836b4fdb768..9bf9c7d91cb 100644 --- a/pkg/analysis_server/test/src/computer/color_computer_test.dart +++ b/pkg/analysis_server/test/src/computer/color_computer_test.dart @@ -137,8 +137,9 @@ class ColorComputerTest extends AbstractContextTest { Map expectedColorValues, { String? otherCode, }) async { - dartCode = _withCommonImports(dartCode); - otherCode = otherCode != null ? _withCommonImports(otherCode) : null; + dartCode = _withCommonImportsNormalized(dartCode); + otherCode = + otherCode != null ? _withCommonImportsNormalized(otherCode) : null; newFile(testPath, dartCode); if (otherCode != null) { @@ -460,10 +461,12 @@ final a = [[COLOR]]; await checkAllColors(testCode); } - String _withCommonImports(String code) => ''' + String _withCommonImportsNormalized(String code) { + return normalizeSource(''' import 'package:flutter/cupertino.dart'; import 'package:flutter/painting.dart'; import 'package:flutter/material.dart'; -$code'''; +$code'''); + } } diff --git a/pkg/analysis_server/test/src/computer/folding_computer_test.dart b/pkg/analysis_server/test/src/computer/folding_computer_test.dart index bcd0a67bd38..8ee9b5d4ee4 100644 --- a/pkg/analysis_server/test/src/computer/folding_computer_test.dart +++ b/pkg/analysis_server/test/src/computer/folding_computer_test.dart @@ -1041,7 +1041,7 @@ void f() {} } Future _computeRegions(String sourceContent) async { - code = TestCode.parse(sourceContent); + code = TestCode.parse(normalizeSource(sourceContent)); var file = newFile(sourcePath, code.code); var result = await getResolvedUnit(file); var computer = DartUnitFoldingComputer(result.lineInfo, result.unit); diff --git a/pkg/analysis_server/test/src/computer/highlights_computer_test.dart b/pkg/analysis_server/test/src/computer/highlights_computer_test.dart index aa91353ce1d..ca711fafd93 100644 --- a/pkg/analysis_server/test/src/computer/highlights_computer_test.dart +++ b/pkg/analysis_server/test/src/computer/highlights_computer_test.dart @@ -110,7 +110,7 @@ void f() { String content, { bool hasErrors = false, }) async { - this.content = content; + this.content = content = normalizeSource(content); var file = newFile(sourcePath, content); var result = await getResolvedUnit(file); diff --git a/pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart b/pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart index 5da946fde02..3e9e88728c1 100644 --- a/pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart +++ b/pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analysis_server/src/services/completion/dart/feature_computer.dart'; -import 'package:analyzer/src/test_utilities/test_code_format.dart'; import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -1096,11 +1095,8 @@ abstract class FeatureComputerTest extends AbstractSingleUnitTest { bool verifyNoTestUnitErrors = false; Future completeIn(String content) async { - var code = TestCode.parse(content); - cursorIndex = code.position.offset; - - content = code.code; - await resolveTestCode(code.code); + await resolveTestCode(content); + cursorIndex = parsedTestCode.position.offset; completionTarget = CompletionTarget.forOffset(testUnit, cursorIndex); } } diff --git a/pkg/analysis_server/test/src/services/correction/assist/assist_processor.dart b/pkg/analysis_server/test/src/services/correction/assist/assist_processor.dart index 1ff78494511..99acd4088a0 100644 --- a/pkg/analysis_server/test/src/services/correction/assist/assist_processor.dart +++ b/pkg/analysis_server/test/src/services/correction/assist/assist_processor.dart @@ -71,13 +71,13 @@ abstract class AssistProcessorTest extends AbstractSingleUnitTest { int index = 0, }) async { _setPositionOrRange(index); - if (useLineEndingsForPlatform) { - expected = normalizeNewlinesForPlatform(expected); - additionallyChangedFiles = additionallyChangedFiles?.map( - (key, value) => - MapEntry(key, value.map(normalizeNewlinesForPlatform).toList()), - ); - } + + expected = normalizeNewlinesForPlatform(expected); + additionallyChangedFiles = additionallyChangedFiles?.map( + (key, value) => + MapEntry(key, value.map(normalizeNewlinesForPlatform).toList()), + ); + // Remove any marker in the expected code. We allow markers to prevent an // otherwise empty line from having the leading whitespace be removed. expected = TestCode.parse(expected).code; @@ -139,12 +139,6 @@ abstract class AssistProcessorTest extends AbstractSingleUnitTest { }).toList(); } - @override - void setUp() { - super.setUp(); - useLineEndingsForPlatform = true; - } - /// Computes assists and verifies that there is an assist of the given kind. Future _assertHasAssist() async { var assists = await _computeAssists(); diff --git a/pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart b/pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart index 7aedfd32439..ed8467113d0 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/fix_processor.dart @@ -113,7 +113,7 @@ abstract class BulkFixProcessorTest extends AbstractSingleUnitTest { var fixes = (await processor.fixPubspec([analysisContext])).edits; var edits = [for (var fix in fixes) ...fix.edits]; var result = SourceEdit.applySequence(original, edits); - expect(result, expected); + expect(result, normalizeSource(expected)); } Future assertFormat(String expectedCode) async { @@ -124,7 +124,7 @@ abstract class BulkFixProcessorTest extends AbstractSingleUnitTest { var fileEdits = change.edits; expect(fileEdits, hasLength(1)); resultCode = SourceEdit.applySequence(testCode, change.edits[0].edits); - expect(resultCode, expectedCode); + expect(resultCode, normalizeSource(expectedCode)); } Future assertHasFix(String expected, {bool isParse = false}) async { @@ -136,7 +136,7 @@ abstract class BulkFixProcessorTest extends AbstractSingleUnitTest { var fileContent = testCode; resultCode = SourceEdit.applySequence(fileContent, change.edits[0].edits); - expect(resultCode, expected); + expect(resultCode, normalizeSource(expected)); } Future assertNoFix() async { @@ -153,7 +153,7 @@ abstract class BulkFixProcessorTest extends AbstractSingleUnitTest { var fileEdits = change.edits; expect(fileEdits, hasLength(1)); resultCode = SourceEdit.applySequence(testCode, change.edits[0].edits); - expect(resultCode, expectedCode); + expect(resultCode, normalizeSource(expectedCode)); } /// Computes fixes for the specified [testUnit]. diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_with_decorated_box_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_with_decorated_box_test.dart index 7bafb5a84d3..b17dd6cd1b0 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/replace_with_decorated_box_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/replace_with_decorated_box_test.dart @@ -307,8 +307,6 @@ void f() { } Future test_hierarchy() async { - useLineEndingsForPlatform = false; - await resolveTestCode(''' import 'package:flutter/material.dart'; diff --git a/pkg/analysis_server/test/src/services/flutter/widget_description.dart b/pkg/analysis_server/test/src/services/flutter/widget_description.dart index bb5cd399edc..ff5f8cf88a3 100644 --- a/pkg/analysis_server/test/src/services/flutter/widget_description.dart +++ b/pkg/analysis_server/test/src/services/flutter/widget_description.dart @@ -27,7 +27,7 @@ class WidgetDescriptionBase extends AbstractSingleUnitTest { testAnalysisResult.content, fileEdit.edits, ); - expect(actual, expected); + expect(actual, normalizeSource(expected)); } void assertPropertyJsonText( diff --git a/pkg/analysis_server/test/src/utilities/extensions/ast_test.dart b/pkg/analysis_server/test/src/utilities/extensions/ast_test.dart index 3e6919a0991..9e14c5d9edf 100644 --- a/pkg/analysis_server/test/src/utilities/extensions/ast_test.dart +++ b/pkg/analysis_server/test/src/utilities/extensions/ast_test.dart @@ -178,7 +178,7 @@ class C {} void _assertTokens(List expected) { expect( result.unit.fileHeader.map((token) => token.lexeme), - orderedEquals(expected), + orderedEquals(expected.map(normalizeSource)), ); } } diff --git a/pkg/analysis_server/test/src/utilities/selection_test.dart b/pkg/analysis_server/test/src/utilities/selection_test.dart index ac09d0681c5..ab8d88ec660 100644 --- a/pkg/analysis_server/test/src/utilities/selection_test.dart +++ b/pkg/analysis_server/test/src/utilities/selection_test.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:io'; + import 'package:analysis_server_plugin/src/utilities/selection.dart'; import 'package:analyzer/src/test_utilities/test_code_format.dart'; import 'package:test/test.dart'; @@ -1409,6 +1411,20 @@ nodesInRange void _assertSelection(_CodeSelection selection, String expected) { var buffer = StringBuffer(); _writeSelectionToBuffer(buffer, selection); + // Expectation descriptions are always written with literal newlines as '\n' + // so on Windows we will need to update the expectation to '\r\n' before + // comparing. + expect( + expected, + isNot(contains(r'\r')), + reason: + r'Expectations should always be written with literal ' + "'\n' to indicate newlines regardless of platform", + ); + if (Platform.isWindows) { + expected = expected.replaceAll(r'\n', r'\r\n'); + } + _assertTextExpectation(buffer.toString(), expected); } @@ -1423,7 +1439,7 @@ nodesInRange } Future<_CodeSelection> _computeSelection(String annotatedCode) async { - var testCode = TestCode.parse(annotatedCode); + var testCode = TestCode.parse(normalizeSource(annotatedCode)); expect(testCode.positions, isEmpty); var range = testCode.range.sourceRange;