| // 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'; |
| |
| void main() { |
| defineReflectiveSuite(() { |
| defineReflectiveTests(AssistsCodeActionsTest); |
| }); |
| } |
| |
| @reflectiveTest |
| class AssistsCodeActionsTest extends AbstractCodeActionsTest { |
| Future<void> 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; |
| '''; |
| 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.showCombinator'), |
| "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)); |
| } |
| |
| Future<void> 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; |
| '''; |
| 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.showCombinator'), |
| "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)); |
| } |
| |
| Future<void> test_nonDartFile() async { |
| newFile(pubspecFilePath, content: simplePubspecContent); |
| await initialize( |
| textDocumentCapabilities: withCodeActionKinds( |
| emptyTextDocumentClientCapabilities, [CodeActionKind.Refactor]), |
| ); |
| |
| final codeActions = |
| await getCodeActions(pubspecFileUri.toString(), range: startOfDocRange); |
| expect(codeActions, isEmpty); |
| } |
| } |