Files
sdk/pkg/analysis_server/test/lsp/code_actions_assists_test.dart
T
Danny Tuppeny b44f21e691 Add assists to LSP Code Actions
Change-Id: Ic3762513fa55deee394b74da78ee79a23c8c0493
Reviewed-on: https://dart-review.googlesource.com/c/87070
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <dantup@google.com>
2018-12-12 17:59:03 +00:00

95 lines
3.1 KiB
Dart

// Copyright (c) 2018, 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/lsp_protocol/protocol_generated.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'code_actions_abstract.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(AssistsCodeActionsTest);
});
}
@reflectiveTest
class AssistsCodeActionsTest extends AbstractCodeActionsTest {
test_appliesCorrectEdits_withDocumentChangesSupport() async {
// This code should get an assist to add a show combinator.
const content = '''
import '[[dart:async]]';
Future f;
''';
const expectedContent = '''
import 'dart:async' show Future;
Future f;
''';
await newFile(mainFilePath, content: withoutMarkers(content));
await initialize(
textDocumentCapabilities: withCodeActionKinds(
emptyTextDocumentClientCapabilities, [CodeActionKind.Refactor]),
workspaceCapabilities:
withDocumentChangesSupport(emptyWorkspaceClientCapabilities),
);
final codeActions = await getCodeActions(mainFileUri.toString(),
range: rangeFromMarkers(content));
final assist = findEditAction(
codeActions, CodeActionKind.Refactor, "Add explicit 'show' combinator");
// Ensure the edit came back, and using documentChanges.
expect(assist, isNotNull);
expect(assist.edit.documentChanges, isNotNull);
expect(assist.edit.changes, isNull);
// Ensure applying the changes will give us the expected content.
final contents = {
mainFilePath: withoutMarkers(content),
};
applyDocumentChanges(contents, assist.edit.documentChanges);
expect(contents[mainFilePath], equals(expectedContent));
}
test_appliesCorrectEdits_withoutDocumentChangesSupport() async {
// This code should get an assist to add a show combinator.
const content = '''
import '[[dart:async]]';
Future f;
''';
const expectedContent = '''
import 'dart:async' show Future;
Future f;
''';
await newFile(mainFilePath, content: withoutMarkers(content));
await initialize(
textDocumentCapabilities: withCodeActionKinds(
emptyTextDocumentClientCapabilities, [CodeActionKind.Refactor]),
);
final codeActions = await getCodeActions(mainFileUri.toString(),
range: rangeFromMarkers(content));
final assistAction = findEditAction(
codeActions, CodeActionKind.Refactor, "Add explicit 'show' combinator");
// Ensure the edit came back, and using changes.
expect(assistAction, isNotNull);
expect(assistAction.edit.changes, isNotNull);
expect(assistAction.edit.documentChanges, isNull);
// Ensure applying the changes will give us the expected content.
final contents = {
mainFilePath: withoutMarkers(content),
};
applyChanges(contents, assistAction.edit.changes);
expect(contents[mainFilePath], equals(expectedContent));
}
}