Skip to content

Commit c9dcebc

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Update CodeActions tests + mixin to use shared test interface
No functional changes, this is just a small refactor extracting from a future CL that makes the LSP CodeActions tests (and the supporting mixin) work against the `SharedTestInterface` so that they can be shared between the LSP server and Legacy server tests. It also removes a redundant implementation of `verifyEdit` so both LSP+Legacy tests can use the same implementation, which required fixing the LspOverLegacy base test class to correctly set the local capabilities to match the default set the legacy server uses if the client doesn't provide any explicitly. Change-Id: Ie6396a6425205aba9bfdcc3f08f34fdfa215ad92 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428002 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 7eea262 commit c9dcebc

File tree

9 files changed

+108
-88
lines changed

9 files changed

+108
-88
lines changed

pkg/analysis_server/test/lsp/code_actions_assists_test.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void main() {
2828

2929
@reflectiveTest
3030
class AssistsCodeActionsTest extends AbstractLspAnalysisServerTest
31-
with CodeActionsTestMixin {
31+
with LspSharedTestMixin, CodeActionsTestMixin {
3232
@override
3333
void setUp() {
3434
super.setUp();
@@ -107,7 +107,7 @@ Widget build() {
107107
expect(action.isCodeActionLiteral, true);
108108

109109
await verifyCodeActionEdits(action, r'''
110-
>>>>>>>>>> lib/main.dart
110+
>>>>>>>>>> lib/test.dart
111111
import 'package:flutter/widgets.dart';
112112
Widget build() {
113113
return Center($0child: Text(''));
@@ -133,7 +133,7 @@ Widget build() {
133133
command: Commands.applyCodeAction,
134134
commandArgs: [
135135
{
136-
'textDocument': {'uri': mainFileUri.toString(), 'version': 1},
136+
'textDocument': {'uri': testFileUri.toString(), 'version': 1},
137137
'range': code.range.range.toJson(),
138138
'kind': 'refactor.flutter.wrap.center',
139139
'loggedAction': 'dart.assist.flutter.wrap.center',
@@ -148,7 +148,7 @@ Widget build() {
148148
// Verify that executing the command produces the correct edits (which will
149149
// come back via `workspace/applyEdit`).
150150
await verifyCommandEdits(command, r'''
151-
>>>>>>>>>> lib/main.dart
151+
>>>>>>>>>> lib/test.dart
152152
import 'package:flutter/widgets.dart';
153153
Widget build() {
154154
return Center($0child: Text(''));
@@ -169,15 +169,15 @@ Widget build() {
169169
// indicating this is not a valid (Dart) int.
170170
// https://github.com/dart-lang/sdk/issues/42786
171171

172-
newFile(mainFilePath, '');
172+
newFile(testFilePath, '');
173173
await initialize();
174174

175175
var request = makeRequest(
176176
Method.textDocument_codeAction,
177177
_RawParams('''
178178
{
179179
"textDocument": {
180-
"uri": "$mainFileUri"
180+
"uri": "$testFileUri"
181181
},
182182
"context": {
183183
"diagnostics": []
@@ -283,7 +283,7 @@ bar
283283
"Change 'foo' to 'bar'",
284284
edits: [
285285
plugin.SourceFileEdit(
286-
mainFilePath,
286+
testFilePath,
287287
0,
288288
edits: [plugin.SourceEdit(0, 3, 'bar')],
289289
),
@@ -329,11 +329,11 @@ bar
329329
request is plugin.EditGetAssistsParams ? pluginResult : null,
330330
);
331331

332-
newFile(mainFilePath, code.code);
332+
newFile(testFilePath, code.code);
333333
await initialize();
334334

335335
var codeActions = await getCodeActions(
336-
mainFileUri,
336+
testFileUri,
337337
range: code.range.range,
338338
);
339339
var codeActionTitles = codeActions.map((action) => action.title);
@@ -512,11 +512,11 @@ import 'package:flutter/widgets.dart';
512512
build() => Contai^ner(child: Container());
513513
''');
514514

515-
newFile(mainFilePath, code.code);
515+
newFile(testFilePath, code.code);
516516
await initialize();
517517

518518
var codeActions = await getCodeActions(
519-
mainFileUri,
519+
testFileUri,
520520
position: code.position.position,
521521
);
522522
var codeActionTitles = codeActions.map((action) => action.title);

pkg/analysis_server/test/lsp/code_actions_fixes_test.dart

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DeprecatedCamelCaseTypes extends LintRule {
4949

5050
@reflectiveTest
5151
class FixesCodeActionsTest extends AbstractLspAnalysisServerTest
52-
with CodeActionsTestMixin {
52+
with LspSharedTestMixin, CodeActionsTestMixin {
5353
/// Helper to check plugin fixes for [filePath].
5454
///
5555
/// Used to ensure that both Dart and non-Dart files fixes are returned.
@@ -129,11 +129,11 @@ bar
129129
MyCla^ss? a;
130130
''');
131131

132-
newFile(mainFilePath, code.code);
132+
newFile(testFilePath, code.code);
133133
await initialize();
134134

135135
var codeActions = await getCodeActions(
136-
mainFileUri,
136+
testFileUri,
137137
position: code.position.position,
138138
);
139139
var codeActionTitles = codeActions.map((action) => action.title);
@@ -157,11 +157,11 @@ MyCla^ss? a;
157157
MyCla^ss? a;
158158
''');
159159

160-
newFile(mainFilePath, code.code);
160+
newFile(testFilePath, code.code);
161161
await initialize();
162162

163163
var codeActions = await getCodeActions(
164-
mainFileUri,
164+
testFileUri,
165165
position: code.position.position,
166166
);
167167
var codeActionTitles = codeActions.map((action) => action.title);
@@ -181,11 +181,11 @@ MyCla^ss? a;
181181
MyCla^ss? a;
182182
''');
183183

184-
newFile(mainFilePath, code.code);
184+
newFile(testFilePath, code.code);
185185
await initialize();
186186

187187
var codeActions = await getCodeActions(
188-
mainFileUri,
188+
testFileUri,
189189
position: code.position.position,
190190
);
191191
var codeActionTitles = codeActions.map((action) => action.title);
@@ -317,11 +317,11 @@ import 'dart:async';
317317
318318
Future foo;
319319
''');
320-
newFile(mainFilePath, code.code);
320+
newFile(testFilePath, code.code);
321321
await initialize();
322322

323323
ofKind(CodeActionKind kind) =>
324-
getCodeActions(mainFileUri, range: code.range.range, kinds: [kind]);
324+
getCodeActions(testFileUri, range: code.range.range, kinds: [kind]);
325325

326326
// The code above will return a 'quickfix.remove.unusedImport'.
327327
expect(await ofKind(CodeActionKind.QuickFix), isNotEmpty);
@@ -358,10 +358,10 @@ var a = [!foo!]();
358358
var b = bar();
359359
''');
360360

361-
newFile(mainFilePath, code.code);
361+
newFile(testFilePath, code.code);
362362
await initialize();
363363

364-
var allFixes = await getCodeActions(mainFileUri, range: code.range.range);
364+
var allFixes = await getCodeActions(testFileUri, range: code.range.range);
365365

366366
// Expect only the single-fix, there should be no apply-all.
367367
expect(allFixes, hasLength(1));
@@ -446,12 +446,12 @@ void main() {
446446
}
447447
''');
448448

449-
newFile(mainFilePath, code.code);
449+
newFile(testFilePath, code.code);
450450
await initialize();
451451

452452
var position = code.position.position;
453453
var range = Range(start: position, end: position);
454-
var codeActions = await getCodeActions(mainFileUri, range: range);
454+
var codeActions = await getCodeActions(testFileUri, range: range);
455455
var codeActionKinds = codeActions.map(
456456
(item) =>
457457
item.map((literal) => literal.kind?.toString(), (command) => null),
@@ -536,11 +536,11 @@ Future foo;
536536
var code = TestCode.parse('''
537537
[!import!] 'dart:convert';
538538
''');
539-
newFile(mainFilePath, code.code);
539+
newFile(testFilePath, code.code);
540540
await initialize();
541541

542542
var codeActions = await getCodeActions(
543-
mainFileUri,
543+
testFileUri,
544544
range: code.range.range,
545545
);
546546
var fixAction =
@@ -572,11 +572,11 @@ int foo() {
572572
}
573573
''');
574574

575-
newFile(mainFilePath, code.code);
575+
newFile(testFilePath, code.code);
576576
await initialize();
577577

578578
var codeActions = await getCodeActions(
579-
mainFileUri,
579+
testFileUri,
580580
range: code.range.range,
581581
);
582582
var fixAction = findCodeActionLiteral(
@@ -600,11 +600,11 @@ void f() {
600600
}
601601
''');
602602

603-
newFile(mainFilePath, code.code);
603+
newFile(testFilePath, code.code);
604604
await initialize();
605605

606606
var codeActions = await getCodeActions(
607-
mainFileUri,
607+
testFileUri,
608608
position: code.position.position,
609609
);
610610
var removeNnaAction =
@@ -628,11 +628,11 @@ void f() {
628628
var a = [Test, Test, Te[!!]st];
629629
''');
630630

631-
newFile(mainFilePath, code.code);
631+
newFile(testFilePath, code.code);
632632
await initialize();
633633

634634
var codeActions = await getCodeActions(
635-
mainFileUri,
635+
testFileUri,
636636
range: code.range.range,
637637
);
638638
var createClassAction =
@@ -654,11 +654,11 @@ var a = [Test, Test, Te[!!]st];
654654
var a = [Test, Test, Te[!!]st];
655655
''');
656656

657-
newFile(mainFilePath, code.code);
657+
newFile(testFilePath, code.code);
658658
await initialize();
659659

660660
var codeActions = await getCodeActions(
661-
mainFileUri,
661+
testFileUri,
662662
range: code.range.range,
663663
);
664664
var createClassActions =
@@ -719,7 +719,7 @@ ProcessInfo b;
719719

720720
Future<void> test_plugin_dart() async {
721721
if (!AnalysisServer.supportsPlugins) return;
722-
return await checkPluginResults(mainFilePath);
722+
return await checkPluginResults(testFilePath);
723723
}
724724

725725
Future<void> test_plugin_nonDart() async {
@@ -741,7 +741,7 @@ ProcessInfo b;
741741
plugin.AnalysisError(
742742
plugin.AnalysisErrorSeverity.ERROR,
743743
plugin.AnalysisErrorType.HINT,
744-
plugin.Location(mainFilePath, 0, 3, 0, 0),
744+
plugin.Location(testFilePath, 0, 3, 0, 0),
745745
'Dummy error',
746746
'dummy',
747747
),
@@ -757,11 +757,11 @@ ProcessInfo b;
757757
request is plugin.EditGetFixesParams ? pluginResult : null,
758758
);
759759

760-
newFile(mainFilePath, code.code);
760+
newFile(testFilePath, code.code);
761761
await initialize();
762762

763763
var codeActions = await getCodeActions(
764-
mainFileUri,
764+
testFileUri,
765765
range: code.range.range,
766766
);
767767
var codeActionTitles = codeActions.map((action) => action.title);

pkg/analysis_server/test/lsp/code_actions_mixin.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ import 'package:analyzer/src/test_utilities/test_code_format.dart';
99
import 'package:collection/collection.dart';
1010
import 'package:test/test.dart';
1111

12+
import '../shared/shared_test_interface.dart';
1213
import '../utils/lsp_protocol_extensions.dart';
1314
import '../utils/test_code_extensions.dart';
1415
import 'change_verifier.dart';
15-
import 'server_abstract.dart';
16+
import 'request_helpers_mixin.dart';
1617

17-
mixin CodeActionsTestMixin on AbstractLspAnalysisServerTest {
18+
mixin CodeActionsTestMixin
19+
on
20+
SharedTestInterface,
21+
LspRequestHelpersMixin,
22+
LspEditHelpersMixin,
23+
LspVerifyEditHelpersMixin {
1824
/// Initializes the server with some basic configuration and expects to find
1925
/// a [CodeAction] with [kind]/[command]/[title].
2026
Future<CodeAction> expectCodeAction(
@@ -27,10 +33,10 @@ mixin CodeActionsTestMixin on AbstractLspAnalysisServerTest {
2733
String? filePath,
2834
bool openTargetFile = false,
2935
}) async {
30-
filePath ??= mainFilePath;
31-
newFile(filePath, code.code);
36+
filePath ??= testFilePath;
37+
createFile(filePath, code.code);
3238

33-
await initialize();
39+
await initializeServer();
3440

3541
var fileUri = uriConverter.toClientUri(filePath);
3642
if (openTargetFile) {
@@ -92,14 +98,14 @@ mixin CodeActionsTestMixin on AbstractLspAnalysisServerTest {
9298
String? title,
9399
ProgressToken? workDoneToken,
94100
}) async {
95-
filePath ??= mainFilePath;
101+
filePath ??= testFilePath;
96102
var code = TestCode.parse(content);
97-
newFile(filePath, code.code);
103+
createFile(filePath, code.code);
98104

99105
if (workDoneToken != null) {
100106
setWorkDoneProgressSupport();
101107
}
102-
await initialize();
108+
await initializeServer();
103109

104110
var codeActions = await getCodeActions(
105111
uriConverter.toClientUri(filePath),
@@ -265,7 +271,7 @@ mixin CodeActionsTestMixin on AbstractLspAnalysisServerTest {
265271
ProgressToken? commandWorkDoneToken,
266272
bool openTargetFile = false,
267273
}) async {
268-
filePath ??= mainFilePath;
274+
filePath ??= testFilePath;
269275

270276
// For convenience, if a test doesn't provide an full set of edits
271277
// we assume only a single edit of the file that was being modified.

pkg/analysis_server/test/lsp/code_actions_refactor_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void f() {
151151
}
152152
''';
153153
const expectedContent = '''
154-
>>>>>>>>>> lib/main.dart
154+
>>>>>>>>>> lib/test.dart
155155
void f() {
156156
print('Test!');
157157
newMethod();
@@ -223,7 +223,7 @@ void f() {
223223
try {
224224
// Send an edit request immediately after the refactor request.
225225
var req1 = executeCommand(codeAction.command!);
226-
var req2 = replaceFile(100, mainFileUri, '// new test content');
226+
var req2 = replaceFile(100, testFileUri, '// new test content');
227227
completer.complete();
228228

229229
// Expect the first to fail because of the modified content.
@@ -249,11 +249,11 @@ void f() {
249249
}
250250
''';
251251
var code = TestCode.parse(content);
252-
newFile(mainFilePath, code.code);
252+
newFile(testFilePath, code.code);
253253
await initialize();
254254

255255
ofKind(CodeActionKind kind) =>
256-
getCodeActions(mainFileUri, range: code.range.range, kinds: [kind]);
256+
getCodeActions(testFileUri, range: code.range.range, kinds: [kind]);
257257

258258
// The code above will return a 'refactor.extract' (as well as some other
259259
// refactors, but not rewrite).
@@ -979,7 +979,7 @@ void foo2() {
979979
}
980980

981981
abstract class RefactorCodeActionsTest extends AbstractLspAnalysisServerTest
982-
with CodeActionsTestMixin {
982+
with LspSharedTestMixin, CodeActionsTestMixin {
983983
@override
984984
void setUp() {
985985
super.setUp();

0 commit comments

Comments
 (0)