[analysis_server] Migrate some tests from indexOf() to TestCode.parse()
See https://github.com/dart-lang/sdk/issues/60234 Change-Id: Ia5c110b40088982368512688195d70c2b51a2d50 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421121 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Phil Quitslund <pquitslund@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
committed by
Commit Queue
parent
106009e59d
commit
216eb0bef0
@@ -8,6 +8,7 @@ import 'package:analysis_server/protocol/protocol.dart';
|
||||
import 'package:analysis_server/protocol/protocol_constants.dart';
|
||||
import 'package:analysis_server/protocol/protocol_generated.dart'
|
||||
hide AnalysisOptions;
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:test/test.dart';
|
||||
@@ -36,21 +37,16 @@ class CompletionDriver with ExpectMixin {
|
||||
}
|
||||
|
||||
void addTestFile(String content, {int? offset}) {
|
||||
completionOffset = content.indexOf('^');
|
||||
var code = TestCode.parse(content);
|
||||
|
||||
if (offset != null) {
|
||||
expect(completionOffset, -1, reason: 'cannot supply offset and ^');
|
||||
expect(code.positions, isEmpty, reason: 'cannot supply offset and ^');
|
||||
completionOffset = offset;
|
||||
server.newFile(server.testFilePath, content);
|
||||
} else {
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
server.newFile(
|
||||
server.testFilePath,
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1),
|
||||
);
|
||||
completionOffset = code.position.offset;
|
||||
}
|
||||
|
||||
server.newFile(server.testFilePath, code.code);
|
||||
}
|
||||
|
||||
void assertValidId(String id) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'dart:async';
|
||||
import 'package:analysis_server/src/protocol_server.dart';
|
||||
import 'package:analyzer/file_system/file_system.dart';
|
||||
import 'package:analyzer/src/test_utilities/package_config_file_builder.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';
|
||||
|
||||
@@ -198,17 +199,10 @@ completion: Test
|
||||
required String completion,
|
||||
required String libraryUri,
|
||||
}) async {
|
||||
var completionOffset = content.indexOf('^');
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var code = TestCode.parse(content);
|
||||
var completionOffset = code.position.offset;
|
||||
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
|
||||
newFile(
|
||||
path,
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1),
|
||||
);
|
||||
newFile(path, code.code);
|
||||
|
||||
return await _getDetails(
|
||||
path: path,
|
||||
@@ -2152,17 +2146,10 @@ suggestions
|
||||
required String content,
|
||||
int maxResults = 1 << 10,
|
||||
}) async {
|
||||
var completionOffset = content.indexOf('^');
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var code = TestCode.parse(content);
|
||||
var completionOffset = code.position.offset;
|
||||
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
|
||||
newFile(
|
||||
path,
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1),
|
||||
);
|
||||
newFile(path, code.code);
|
||||
|
||||
return await _getSuggestions(
|
||||
path: path,
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'dart:async';
|
||||
import 'package:analysis_server/protocol/protocol.dart';
|
||||
import 'package:analysis_server/protocol/protocol_constants.dart';
|
||||
import 'package:analysis_server/protocol/protocol_generated.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
@@ -82,17 +83,10 @@ class AbstractCompletionDomainTest extends PubPackageAnalysisServerTest {
|
||||
required String content,
|
||||
int maxResults = 1 << 10,
|
||||
}) async {
|
||||
completionOffset = content.indexOf('^');
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var code = TestCode.parse(content);
|
||||
var completionOffset = code.position.offset;
|
||||
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
|
||||
newFile(
|
||||
path,
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1),
|
||||
);
|
||||
newFile(path, code.code);
|
||||
|
||||
return await getSuggestions(
|
||||
path: path,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import 'package:analysis_server/protocol/protocol_generated.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
@@ -29,15 +30,10 @@ class GetSuggestions2Test extends AbstractAnalysisServerIntegrationTest {
|
||||
|
||||
path = sourcePath(relPath);
|
||||
|
||||
completionOffset = content.indexOf('^');
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var code = TestCode.parse(content);
|
||||
completionOffset = code.position.offset;
|
||||
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
|
||||
this.content =
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1);
|
||||
this.content = code.code;
|
||||
}
|
||||
|
||||
Future<void> test_getSuggestions() async {
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:analysis_server/src/services/correction/assist.dart';
|
||||
import 'package:analysis_server/src/services/correction/assist_internal.dart';
|
||||
import 'package:analysis_server_plugin/edit/assist/assist.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart' show SourceEdit;
|
||||
import 'package:analyzer_plugin/utilities/assist/assist.dart';
|
||||
import 'package:analyzer_utilities/test/mock_packages/mock_packages.dart';
|
||||
@@ -123,14 +124,13 @@ void f() {
|
||||
}
|
||||
|
||||
void _updateFile(String content) {
|
||||
var offset = content.indexOf('^');
|
||||
expect(offset, isPositive, reason: 'Expected to find ^');
|
||||
expect(content.indexOf('^', offset + 1), -1, reason: 'Expected only one ^');
|
||||
var code = TestCode.parse(content);
|
||||
var offset = code.position.offset;
|
||||
|
||||
content = code.code;
|
||||
var lineInfo = LineInfo.fromContent(content);
|
||||
var location = lineInfo.getLocation(offset);
|
||||
|
||||
content = content.substring(0, offset) + content.substring(offset + 1);
|
||||
newFile(testPath, content);
|
||||
|
||||
_correctionContext = _CorrectionContext(
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:analysis_server/src/cider/completion.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/dart/analysis/results.dart';
|
||||
import 'package:analyzer/src/test_utilities/function_ast_visitor.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart'
|
||||
show CompletionSuggestion, CompletionSuggestionKind, ElementKind;
|
||||
import 'package:test/test.dart';
|
||||
@@ -855,14 +856,13 @@ import 'a.dart';
|
||||
}
|
||||
|
||||
_CompletionContext _updateFile(String content) {
|
||||
var offset = content.indexOf('^');
|
||||
expect(offset, isPositive, reason: 'Expected to find ^');
|
||||
expect(content.indexOf('^', offset + 1), -1, reason: 'Expected only one ^');
|
||||
var code = TestCode.parse(content);
|
||||
var offset = code.position.offset;
|
||||
|
||||
content = code.code;
|
||||
var lineInfo = LineInfo.fromContent(content);
|
||||
var location = lineInfo.getLocation(offset);
|
||||
|
||||
content = content.substring(0, offset) + content.substring(offset + 1);
|
||||
newFile(testPath, content);
|
||||
|
||||
return _CompletionContext(
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:analysis_server/src/cider/fixes.dart';
|
||||
import 'package:analysis_server/src/services/correction/fix.dart';
|
||||
import 'package:analysis_server_plugin/edit/fix/fix.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart' show SourceEdit;
|
||||
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
|
||||
import 'package:test/test.dart';
|
||||
@@ -219,14 +220,13 @@ var v = 0;
|
||||
}
|
||||
|
||||
void _updateFile(String content) {
|
||||
var offset = content.indexOf('^');
|
||||
expect(offset, isPositive, reason: 'Expected to find ^');
|
||||
expect(content.indexOf('^', offset + 1), -1, reason: 'Expected only one ^');
|
||||
var code = TestCode.parse(content);
|
||||
content = code.code;
|
||||
|
||||
var offset = code.position.offset;
|
||||
var lineInfo = LineInfo.fromContent(content);
|
||||
var location = lineInfo.getLocation(offset);
|
||||
|
||||
content = content.substring(0, offset) + content.substring(offset + 1);
|
||||
newFile(testPath, content);
|
||||
|
||||
_correctionContext = _CorrectionContext(
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'package:analysis_server/src/cider/rename.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
||||
import 'package:analyzer_utilities/test/mock_packages/mock_packages.dart';
|
||||
import 'package:test/test.dart';
|
||||
@@ -927,14 +928,13 @@ void f(bar a) {}
|
||||
}
|
||||
|
||||
void _updateFile(String content) {
|
||||
var offset = content.indexOf('^');
|
||||
expect(offset, isPositive, reason: 'Expected to find ^');
|
||||
expect(content.indexOf('^', offset + 1), -1, reason: 'Expected only one ^');
|
||||
var code = TestCode.parse(content);
|
||||
var offset = code.position.offset;
|
||||
|
||||
var lineInfo = LineInfo.fromContent(content);
|
||||
_testCode = code.code;
|
||||
var lineInfo = LineInfo.fromContent(_testCode);
|
||||
var location = lineInfo.getLocation(offset);
|
||||
|
||||
_testCode = content.substring(0, offset) + content.substring(offset + 1);
|
||||
newFile(testPath, _testCode);
|
||||
|
||||
_correctionContext = _CorrectionContext(
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import 'package:analysis_server/lsp_protocol/protocol.dart';
|
||||
import 'package:analysis_server/src/cider/signature_help.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
@@ -202,14 +203,13 @@ class Foo {
|
||||
}
|
||||
|
||||
void _updateFile(String content) {
|
||||
var offset = content.indexOf('^');
|
||||
expect(offset, isPositive, reason: 'Expected to find ^');
|
||||
expect(content.indexOf('^', offset + 1), -1, reason: 'Expected only one ^');
|
||||
var code = TestCode.parse(content);
|
||||
var offset = code.position.offset;
|
||||
|
||||
content = code.code;
|
||||
var lineInfo = LineInfo.fromContent(content);
|
||||
var location = lineInfo.getLocation(offset);
|
||||
|
||||
content = content.substring(0, offset) + content.substring(offset + 1);
|
||||
newFile(testPath, content);
|
||||
|
||||
_correctionContext = _CorrectionContext(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// 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';
|
||||
@@ -1095,13 +1096,11 @@ abstract class FeatureComputerTest extends AbstractSingleUnitTest {
|
||||
bool verifyNoTestUnitErrors = false;
|
||||
|
||||
Future<void> completeIn(String content) async {
|
||||
cursorIndex = content.indexOf('^');
|
||||
if (cursorIndex < 0) {
|
||||
fail('Missing node offset marker (^) in content');
|
||||
}
|
||||
content =
|
||||
content.substring(0, cursorIndex) + content.substring(cursorIndex + 1);
|
||||
await resolveTestCode(content);
|
||||
var code = TestCode.parse(content);
|
||||
cursorIndex = code.position.offset;
|
||||
|
||||
content = code.code;
|
||||
await resolveTestCode(code.code);
|
||||
completionTarget = CompletionTarget.forOffset(testUnit, cursorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-7
@@ -5,6 +5,7 @@
|
||||
import 'package:analysis_server/src/protocol_server.dart';
|
||||
import 'package:analysis_server/src/services/completion/yaml/yaml_completion_generator.dart';
|
||||
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
abstract class YamlGeneratorTest with ResourceProviderMixin {
|
||||
@@ -52,13 +53,10 @@ abstract class YamlGeneratorTest with ResourceProviderMixin {
|
||||
/// completion request is encoded in the content as a caret (`^`).
|
||||
void getCompletions(String content) {
|
||||
// Extract the completion location from the [content].
|
||||
var completionOffset = content.indexOf('^');
|
||||
expect(completionOffset, isNot(equals(-1)), reason: 'missing ^');
|
||||
var nextOffset = content.indexOf('^', completionOffset + 1);
|
||||
expect(nextOffset, equals(-1), reason: 'too many ^');
|
||||
content =
|
||||
content.substring(0, completionOffset) +
|
||||
content.substring(completionOffset + 1);
|
||||
var code = TestCode.parse(content);
|
||||
var completionOffset = code.position.offset;
|
||||
|
||||
content = code.code;
|
||||
// Add the file to the file system.
|
||||
var file = newFile('/home/test/$fileName', content);
|
||||
// Generate completions.
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/analysis/results.dart';
|
||||
import 'package:analyzer/file_system/overlay_file_system.dart';
|
||||
import 'package:analyzer/file_system/physical_file_system.dart';
|
||||
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
|
||||
import 'package:analyzer/src/test_utilities/test_code_format.dart';
|
||||
import 'package:analyzer/src/util/performance/operation_performance.dart';
|
||||
|
||||
import 'sliding_statistics.dart';
|
||||
@@ -45,22 +46,14 @@ Future<void> _runForever({
|
||||
required String path,
|
||||
required String markedCode,
|
||||
}) async {
|
||||
var offset = markedCode.indexOf('^');
|
||||
if (offset == -1) {
|
||||
throw ArgumentError('No ^ marker');
|
||||
}
|
||||
|
||||
var rawCode =
|
||||
markedCode.substring(0, offset) + markedCode.substring(offset + 1);
|
||||
if (rawCode.contains('^')) {
|
||||
throw ArgumentError('Duplicate ^ marker');
|
||||
}
|
||||
var code = TestCode.parse(markedCode);
|
||||
var offset = code.position.offset;
|
||||
|
||||
var resourceProvider = OverlayResourceProvider(
|
||||
PhysicalResourceProvider.INSTANCE,
|
||||
);
|
||||
|
||||
resourceProvider.setOverlay(path, content: rawCode, modificationStamp: -1);
|
||||
resourceProvider.setOverlay(path, content: code.code, modificationStamp: -1);
|
||||
|
||||
var collection = AnalysisContextCollectionImpl(
|
||||
resourceProvider: resourceProvider,
|
||||
|
||||
Reference in New Issue
Block a user