916272331b
This avoids accidentally using the converter when talking to plugins, (at least for now) URIs are never expected, and file paths should always be used regardless of which mode the server is in. This unfortunately touches a _lot_ of code, so I've pushed in many separate patch sets to Gerrit. Fixes https://github.com/Dart-Code/Dart-Code/issues/5156 Change-Id: I312c3e2cbc35a05a078aaa0138aec7288b3c7dd6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373745 Reviewed-by: Samuel Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
118 lines
3.3 KiB
Dart
118 lines
3.3 KiB
Dart
// Copyright (c) 2015, 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:test/test.dart';
|
|
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
|
|
|
import '../analysis_server_base.dart';
|
|
import '../mocks.dart';
|
|
|
|
void main() {
|
|
defineReflectiveSuite(() {
|
|
defineReflectiveTests(FormatTest);
|
|
});
|
|
}
|
|
|
|
@reflectiveTest
|
|
class FormatTest extends PubPackageAnalysisServerTest {
|
|
@override
|
|
Future<void> setUp() async {
|
|
super.setUp();
|
|
await setRoots(included: [workspaceRootPath], excluded: []);
|
|
}
|
|
|
|
Future<void> test_format_longLine() async {
|
|
var content = '''
|
|
fun(firstParam, secondParam, thirdParam, fourthParam) {
|
|
if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthParam.noNull) {}
|
|
}
|
|
''';
|
|
addTestFile(content);
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3, lineLength: 100);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_noOp() async {
|
|
// Already formatted source
|
|
addTestFile('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3);
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
}
|
|
|
|
Future<void> test_format_noSelection() async {
|
|
addTestFile('''
|
|
void f() { int x = 3; }
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 0);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(1));
|
|
|
|
var edit = formatResult.edits[0];
|
|
expect(edit.replacement, equals('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
'''));
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(0));
|
|
}
|
|
|
|
Future<void> test_format_simple() async {
|
|
addTestFile('''
|
|
void f() { int x = 3; }
|
|
''');
|
|
await waitForTasksFinished();
|
|
var formatResult = await _formatAt(0, 3);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(1));
|
|
|
|
var edit = formatResult.edits[0];
|
|
expect(edit.replacement, equals('''
|
|
void f() {
|
|
int x = 3;
|
|
}
|
|
'''));
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_withErrors() async {
|
|
addTestFile('''
|
|
void f() { int x =
|
|
''');
|
|
await waitForTasksFinished();
|
|
var request = EditFormatParams(testFile.path, 0, 3)
|
|
.toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleRequest(request);
|
|
expect(response, isResponseFailure('0'));
|
|
}
|
|
|
|
Future<EditFormatResult> _formatAt(int selectionOffset, int selectionLength,
|
|
{int? lineLength}) async {
|
|
var request = EditFormatParams(
|
|
testFile.path, selectionOffset, selectionLength,
|
|
lineLength: lineLength)
|
|
.toRequest('0', clientUriConverter: server.uriConverter);
|
|
var response = await handleSuccessfulRequest(request);
|
|
return EditFormatResult.fromResponse(response,
|
|
clientUriConverter: server.uriConverter);
|
|
}
|
|
}
|