[analysis_server] Fix assist test on Windows

Change-Id: I2a85c699140c2fd31ee03524fd2ecac6a0482540
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252121
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/test/abstract_single_unit.dart b/pkg/analysis_server/test/abstract_single_unit.dart
index 0177328..5a0daa7 100644
--- a/pkg/analysis_server/test/abstract_single_unit.dart
+++ b/pkg/analysis_server/test/abstract_single_unit.dart
@@ -32,16 +32,12 @@
 
   @override
   void addSource(String path, String content) {
-    if (useLineEndingsForPlatform) {
-      content = normalizeNewlinesForPlatform(content);
-    }
+    content = normalizeSource(content);
     super.addSource(path, content);
   }
 
   void addTestSource(String code) {
-    if (useLineEndingsForPlatform) {
-      code = normalizeNewlinesForPlatform(code);
-    }
+    code = normalizeSource(code);
     testCode = code;
     addSource(testFile, code);
   }
@@ -58,12 +54,15 @@
 
   @override
   File newFile(String path, String content) {
-    if (useLineEndingsForPlatform) {
-      content = normalizeNewlinesForPlatform(content);
-    }
+    content = normalizeSource(content);
     return super.newFile(path, content);
   }
 
+  /// Convenient function to normalize newlines in [code] for the current
+  /// platform if [useLineEndingsForPlatform] is `true`.
+  String normalizeSource(String code) =>
+      useLineEndingsForPlatform ? normalizeNewlinesForPlatform(code) : code;
+
   Future<void> resolveFile2(String path) async {
     var result =
         await (await session).getResolvedUnit(path) as ResolvedUnitResult;
diff --git a/pkg/analysis_server/test/services/refactoring/abstract_refactoring.dart b/pkg/analysis_server/test/services/refactoring/abstract_refactoring.dart
index 4fa30e3..010d536 100644
--- a/pkg/analysis_server/test/services/refactoring/abstract_refactoring.dart
+++ b/pkg/analysis_server/test/services/refactoring/abstract_refactoring.dart
@@ -7,7 +7,6 @@
 import 'package:analysis_server/src/services/search/search_engine.dart';
 import 'package:analysis_server/src/services/search/search_engine_internal.dart';
 import 'package:analyzer/src/generated/source.dart';
-import 'package:analyzer/src/test_utilities/platform.dart';
 import 'package:analyzer_plugin/protocol/protocol_common.dart'
     show RefactoringProblemSeverity, SourceChange, SourceEdit;
 import 'package:test/test.dart';
@@ -39,9 +38,7 @@
   /// Asserts that [refactoringChange] contains a [FileEdit] for the file
   /// with the given [path], and it results the [expectedCode].
   void assertFileChangeResult(String path, String expectedCode) {
-    if (useLineEndingsForPlatform) {
-      expectedCode = normalizeNewlinesForPlatform(expectedCode);
-    }
+    expectedCode = normalizeSource(expectedCode);
     // prepare FileEdit
     var fileEdit = refactoringChange.getFileEdit(convertPath(path));
     if (fileEdit == null) {
@@ -120,9 +117,7 @@
   /// Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and
   /// it results the [expectedCode].
   void assertTestChangeResult(String expectedCode) {
-    if (useLineEndingsForPlatform) {
-      expectedCode = normalizeNewlinesForPlatform(expectedCode);
-    }
+    expectedCode = normalizeSource(expectedCode);
     // prepare FileEdit
     var fileEdit = refactoringChange.getFileEdit(testFile);
     if (fileEdit == null) {
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 96a9ead..cd65f80 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
@@ -37,9 +37,7 @@
 
   @override
   void addTestSource(String code) {
-    if (useLineEndingsForPlatform) {
-      code = normalizeNewlinesForPlatform(code);
-    }
+    code = normalizeSource(code);
     final eol = code.contains('\r\n') ? '\r\n' : '\n';
     var offset = code.indexOf('/*caret*/');
     if (offset >= 0) {
@@ -122,9 +120,7 @@
   /// given [snippet] which produces the [expected] code when applied to [testCode].
   Future<void> assertHasAssistAt(String snippet, String expected,
       {int length = 0}) async {
-    if (useLineEndingsForPlatform) {
-      expected = normalizeNewlinesForPlatform(expected);
-    }
+    expected = normalizeSource(expected);
     _offset = findOffset(snippet);
     _length = length;
     var assist = await _assertHasAssist();
diff --git a/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_center_test.dart b/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_center_test.dart
index f8ac287..7ad2c40 100644
--- a/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_center_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_center_test.dart
@@ -142,7 +142,10 @@
 
     var assist = await assertHasAssist(expected);
 
-    expect(assist.selection!.offset, expected.indexOf('child: '));
+    expect(
+      assist.selection!.offset,
+      normalizeSource(expected).indexOf('child: '),
+    );
     expect(assist.selectionLength, 0);
   }
 
diff --git a/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_generic_test.dart b/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_generic_test.dart
index 3f9c655..e6dfe49 100644
--- a/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_generic_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/assist/flutter_wrap_generic_test.dart
@@ -53,7 +53,7 @@
     var editGroup = assist.linkedEditGroups.first;
     expect(editGroup.length, 'widget'.length);
     var pos = editGroup.positions.single;
-    expect(pos.offset, expected.indexOf('widget('));
+    expect(pos.offset, normalizeSource(expected).indexOf('widget('));
   }
 
   Future<void> test_minimal() async {
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 c0f1f6e..111c333 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
@@ -11,7 +11,6 @@
 import 'package:analyzer/src/dart/analysis/byte_store.dart';
 import 'package:analyzer/src/dart/error/lint_codes.dart';
 import 'package:analyzer/src/services/available_declarations.dart';
-import 'package:analyzer/src/test_utilities/platform.dart';
 import 'package:analyzer_plugin/protocol/protocol_common.dart'
     hide AnalysisError;
 import 'package:analyzer_plugin/utilities/change_builder/change_workspace.dart';
@@ -162,9 +161,7 @@
     var fileEdits = fix.change.edits;
     expect(fileEdits, hasLength(1));
 
-    if (useLineEndingsForPlatform) {
-      expected = normalizeNewlinesForPlatform(expected);
-    }
+    expected = normalizeSource(expected);
 
     var fileContent = testCode;
     resultCode = SourceEdit.applySequence(fileContent, fileEdits[0].edits);
@@ -256,9 +253,7 @@
       int? expectedNumberOfFixesForKind,
       String? matchFixMessage,
       bool allowFixAllFixes = false}) async {
-    if (useLineEndingsForPlatform) {
-      expected = normalizeNewlinesForPlatform(expected);
-    }
+    expected = normalizeSource(expected);
     var error = await _findErrorToFix(
       errorFilter: errorFilter,
       length: length,
@@ -285,9 +280,7 @@
 
   Future<void> assertHasFixAllFix(ErrorCode errorCode, String expected,
       {String? target}) async {
-    if (useLineEndingsForPlatform) {
-      expected = normalizeNewlinesForPlatform(expected);
-    }
+    expected = normalizeSource(expected);
     var error = await _findErrorToFixOfType(errorCode);
     var fix = await _assertHasFixAllFix(error);
     change = fix.change;