Files
sdk/pkg/analysis_server/test/lsp/code_actions_assists_test.dart
T
Danny Tuppeny 496c5ca907 [analysis_server] Minor CodeAction test cleanup
This is some more final (🙃) cleanup before having CodeActions work for LSP-over-Legacy and run all the same tests. Includes:

- Adding some additional signatures to the SharedTestInterface
- Moving some setup code (that uses those methods) into the shared test mixins
- Add a field for `testPackageName` to allow tests to handle differences between the test package name between LSP/Legacy (something that should probably consolidated but is probably not for this CL)
- Move plugin tests back out of the shared code action tests (these require some additional abstraction to work the same across both kinds of tests - TODO added)

Change-Id: Iad25f1ff1e141d3315e9b972a6d0850e71d4a4bd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/427443
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2025-05-13 11:42:48 -07:00

114 lines
3.4 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 'dart:async';
import 'package:analysis_server/lsp_protocol/protocol.dart';
import 'package:analysis_server/src/analysis_server.dart';
import 'package:analysis_server/src/lsp/extensions/code_action.dart';
import 'package:analyzer/src/test_utilities/test_code_format.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../lsp/code_actions_mixin.dart';
import '../lsp/server_abstract.dart';
import '../shared/shared_code_actions_assists_tests.dart';
import '../utils/test_code_extensions.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(AssistsCodeActionsTest);
});
}
@reflectiveTest
class AssistsCodeActionsTest extends AbstractLspAnalysisServerTest
with
LspSharedTestMixin,
CodeActionsTestMixin,
// Most tests are defined in a shared mixin.
SharedAssistsCodeActionsTests {
Future<void> test_plugin() async {
failTestOnErrorDiagnostic = false;
if (!AnalysisServer.supportsPlugins) return;
// This code should get an assist to replace 'foo' with 'bar'.'
const content = '''
[!foo!]
''';
const expectedContent = '''
bar
''';
var pluginResult = plugin.EditGetAssistsResult([
plugin.PrioritizedSourceChange(
0,
plugin.SourceChange(
"Change 'foo' to 'bar'",
edits: [
plugin.SourceFileEdit(
testFilePath,
0,
edits: [plugin.SourceEdit(0, 3, 'bar')],
),
],
id: 'fooToBar',
),
),
]);
configureTestPlugin(
handler:
(request) =>
request is plugin.EditGetAssistsParams ? pluginResult : null,
);
await verifyCodeActionLiteralEdits(
content,
expectedContent,
kind: CodeActionKind('refactor.fooToBar'),
title: "Change 'foo' to 'bar'",
command: 'dart.logAction',
commandArgs: [
{'action': 'assist from plugin'},
],
);
}
Future<void> test_plugin_sortsWithServer() async {
setSupportedCodeActionKinds([CodeActionKind.Refactor]);
if (!AnalysisServer.supportsPlugins) return;
// Produces a server assist of "Convert to single quoted string" (with a
// priority of 30).
var code = TestCode.parse('import "[!dart:async!]";');
// Provide two plugin results that should sort either side of the server assist.
var pluginResult = plugin.EditGetAssistsResult([
plugin.PrioritizedSourceChange(10, plugin.SourceChange('Low')),
plugin.PrioritizedSourceChange(100, plugin.SourceChange('High')),
]);
configureTestPlugin(
handler:
(request) =>
request is plugin.EditGetAssistsParams ? pluginResult : null,
);
createFile(testFilePath, code.code);
await initializeServer();
var codeActions = await getCodeActions(
testFileUri,
range: code.range.range,
);
var codeActionTitles = codeActions.map((action) => action.title);
expect(
codeActionTitles,
containsAllInOrder(['High', 'Convert to single quoted string', 'Low']),
);
}
}