fbf331e0dd
Change-Id: I42b4a44fd6a2197e499e8623274b3cd1a4b5556f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394003 Commit-Queue: Samuel Rawlins <srawlins@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Bob Nystrom <rnystrom@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
176 lines
4.7 KiB
Dart
176 lines
4.7 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_analysisOptions() async {
|
|
writeTestPackageAnalysisOptionsFile(r'''
|
|
formatter:
|
|
page_width: 100
|
|
''');
|
|
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);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_longLine_analysisOptions_overridesParameter() async {
|
|
writeTestPackageAnalysisOptionsFile(r'''
|
|
formatter:
|
|
page_width: 100
|
|
''');
|
|
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: 50);
|
|
|
|
expect(formatResult.edits, isNotNull);
|
|
expect(formatResult.edits, hasLength(0));
|
|
|
|
expect(formatResult.selectionOffset, equals(0));
|
|
expect(formatResult.selectionLength, equals(3));
|
|
}
|
|
|
|
Future<void> test_format_longLine_parameter() 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,
|
|
);
|
|
}
|
|
}
|