6be105bb24
This adds code to normalize file contents in `PubPackageAnalysisServerTest.newFile()` so all tests using this base class normalize their source newlines by default (this ensures we run with \r\n on Windows, regardless of the git settings always using \n in this repo). A flag `useLineEndingsForPlatform` allows option out of this, and any tests that currently fail in this mode set this in their setUp - with the exception of a few that were just trivial fixes. This will make it easier to fix the remaining tests (by looking at code that does `useLineEndingsForPlatform = false`, deleting it, then fixing those tests). See https://github.com/dart-lang/sdk/issues/60234 Change-Id: If05bc5f7fe3007151c1290831157b6a62d6bf651 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449880 Commit-Queue: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
142 lines
3.8 KiB
Dart
142 lines
3.8 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// 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 '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';
|
|
|
|
import '../analysis_server_base.dart';
|
|
|
|
void main() {
|
|
defineReflectiveSuite(() {
|
|
defineReflectiveTests(PostfixCompletionTest);
|
|
});
|
|
}
|
|
|
|
@reflectiveTest
|
|
class PostfixCompletionTest extends PubPackageAnalysisServerTest {
|
|
late SourceChange change;
|
|
|
|
@override
|
|
Future<void> setUp() async {
|
|
super.setUp();
|
|
await setRoots(included: [workspaceRootPath], excluded: []);
|
|
}
|
|
|
|
Future<void> test_for() async {
|
|
addTestFile('''
|
|
void f() {
|
|
[]^
|
|
}
|
|
''');
|
|
|
|
await _prepareCompletionAt(parsedTestCode.position.offset, '.for');
|
|
_assertHasChange('Expand .for', '''
|
|
void f() {
|
|
for (var value in []) {
|
|
^
|
|
}
|
|
}
|
|
''');
|
|
}
|
|
|
|
Future<void> test_invalidFilePathFormat_notAbsolute() async {
|
|
var request = EditGetPostfixCompletionParams(
|
|
'test.dart',
|
|
'.for',
|
|
0,
|
|
).toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleRequest(request);
|
|
assertResponseFailure(
|
|
response,
|
|
requestId: '0',
|
|
errorCode: RequestErrorCode.INVALID_FILE_PATH_FORMAT,
|
|
);
|
|
}
|
|
|
|
Future<void> test_invalidFilePathFormat_notNormalized() async {
|
|
var request = EditGetPostfixCompletionParams(
|
|
convertPath('/foo/../bar/test.dart'),
|
|
'.for',
|
|
0,
|
|
).toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleRequest(request);
|
|
assertResponseFailure(
|
|
response,
|
|
requestId: '0',
|
|
errorCode: RequestErrorCode.INVALID_FILE_PATH_FORMAT,
|
|
);
|
|
}
|
|
|
|
Future<void> test_notApplicable_inComment_try() async {
|
|
addTestFile('''
|
|
void f() {
|
|
() {
|
|
// comment^
|
|
};
|
|
}
|
|
''');
|
|
|
|
var result = await _isApplicable(
|
|
offset: parsedTestCode.position.offset,
|
|
key: '.try',
|
|
);
|
|
expect(result, isFalse);
|
|
}
|
|
|
|
void _assertHasChange(String message, String expected) {
|
|
var expectedCode = TestCode.parseNormalized(expected);
|
|
if (change.message != message) {
|
|
fail('Expected to find |$message| but got: ${change.message}');
|
|
}
|
|
if (change.edits.isNotEmpty) {
|
|
var resultCode = SourceEdit.applySequence(
|
|
testFileContent,
|
|
change.edits[0].edits,
|
|
);
|
|
expect(resultCode, expectedCode.code);
|
|
}
|
|
expect(change.selection?.offset, expectedCode.position.offset);
|
|
}
|
|
|
|
Future<bool> _isApplicable({required int offset, required String key}) async {
|
|
var response = await handleSuccessfulRequest(
|
|
EditIsPostfixCompletionApplicableParams(
|
|
testFile.path,
|
|
key,
|
|
offset,
|
|
).toRequest('0', clientUriConverter: server.uriConverter),
|
|
);
|
|
var result = EditIsPostfixCompletionApplicableResult.fromResponse(
|
|
response,
|
|
clientUriConverter: server.uriConverter,
|
|
);
|
|
return result.value;
|
|
}
|
|
|
|
Future<void> _prepareCompletionAt(int offset, String key) async {
|
|
var isApplicable = await _isApplicable(offset: offset, key: key);
|
|
|
|
if (!isApplicable) {
|
|
fail('Postfix completion not applicable at given location');
|
|
}
|
|
|
|
var response = await handleSuccessfulRequest(
|
|
EditGetPostfixCompletionParams(
|
|
testFile.path,
|
|
key,
|
|
offset,
|
|
).toRequest('0', clientUriConverter: server.uriConverter),
|
|
);
|
|
|
|
var result = EditGetPostfixCompletionResult.fromResponse(
|
|
response,
|
|
clientUriConverter: server.uriConverter,
|
|
);
|
|
change = result.change;
|
|
}
|
|
}
|