Skip to content

Commit 24f594a

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Move CodeActions tests to shared mixins
The next CL will make CodeActions a shared handler (so it can run for both LSP and legacy servers). This moves the tests to a shared mixin (without any changes) to keep that change smaller and easier to review if the tests do end up requiring changes. Not all tests are moved yet - plugins are not (because the plugin code is not the same across server types), not "Fix All" (because it will require some additional changes to not be LSP-specific compared to others). Change-Id: Ib4727ef1b1cc5b96d98cdbd6e17bf4b7b2791e3e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428003 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 68e917c commit 24f594a

8 files changed

+2630
-2427
lines changed

pkg/analysis_server/test/lsp/code_actions_assists_test.dart

+5-465
Large diffs are not rendered by default.

pkg/analysis_server/test/lsp/code_actions_fixes_test.dart

+12-764
Large diffs are not rendered by default.

pkg/analysis_server/test/lsp/code_actions_refactor_test.dart

+29-894
Large diffs are not rendered by default.

pkg/analysis_server/test/lsp/code_actions_source_test.dart

+9-304
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'package:linter/src/rules.dart';
1111
import 'package:test/test.dart';
1212
import 'package:test_reflective_loader/test_reflective_loader.dart';
1313

14-
import '../tool/lsp_spec/matchers.dart';
14+
import '../shared/shared_code_actions_source_tests.dart';
1515
import 'code_actions_mixin.dart';
1616
import 'server_abstract.dart';
1717

@@ -360,308 +360,13 @@ int? a;
360360
}
361361

362362
@reflectiveTest
363-
class OrganizeImportsSourceCodeActionsTest
364-
extends AbstractSourceCodeActionsTest {
365-
Future<void> test_appliesCorrectEdits_withDocumentChangesSupport() async {
366-
const content = '''
367-
import 'dart:math';
368-
import 'dart:async';
369-
import 'dart:convert';
370-
371-
Completer? foo;
372-
int minified(int x, int y) => min(x, y);
373-
''';
374-
const expectedContent = '''
375-
import 'dart:async';
376-
import 'dart:math';
377-
378-
Completer? foo;
379-
int minified(int x, int y) => min(x, y);
380-
''';
381-
382-
await verifyCodeActionLiteralEdits(
383-
content,
384-
expectedContent,
385-
command: Commands.organizeImports,
386-
);
387-
}
388-
389-
Future<void> test_appliesCorrectEdits_withoutDocumentChangesSupport() async {
390-
const content = '''
391-
import 'dart:math';
392-
import 'dart:async';
393-
import 'dart:convert';
394-
395-
Completer? foo;
396-
int minified(int x, int y) => min(x, y);
397-
''';
398-
const expectedContent = '''
399-
import 'dart:async';
400-
import 'dart:math';
401-
402-
Completer? foo;
403-
int minified(int x, int y) => min(x, y);
404-
''';
405-
406-
setDocumentChangesSupport(false);
407-
await verifyCodeActionLiteralEdits(
408-
content,
409-
expectedContent,
410-
command: Commands.organizeImports,
411-
);
412-
}
413-
414-
Future<void> test_availableAsCodeActionLiteral() async {
415-
const content = '';
416-
417-
await expectCodeActionLiteral(content, command: Commands.organizeImports);
418-
}
419-
420-
Future<void> test_availableAsCommand() async {
421-
createFile(testFilePath, '');
422-
setSupportedCodeActionKinds(null); // no codeActionLiteralSupport
423-
await initializeServer();
424-
425-
var actions = await getCodeActions(testFileUri);
426-
var action = findCommand(actions, Commands.organizeImports)!;
427-
action.map(
428-
(codeActionLiteral) => throw 'Expected command, got codeActionLiteral',
429-
(command) {},
430-
);
431-
}
432-
433-
Future<void> test_fileHasErrors_failsSilentlyForAutomatic() async {
434-
failTestOnErrorDiagnostic = false;
435-
var content = 'invalid dart code';
436-
437-
var codeAction = await expectCodeActionLiteral(
438-
content,
439-
command: Commands.organizeImports,
440-
triggerKind: CodeActionTriggerKind.Automatic,
441-
);
442-
var command = codeAction.command!;
443-
444-
// Expect a valid null result.
445-
var response = await executeCommand(command);
446-
expect(response, isNull);
447-
}
448-
449-
Future<void> test_fileHasErrors_failsWithErrorForManual() async {
450-
failTestOnErrorDiagnostic = false;
451-
var content = 'invalid dart code';
452-
453-
var codeAction = await expectCodeActionLiteral(
454-
content,
455-
command: Commands.organizeImports,
456-
);
457-
var command = codeAction.command!;
458-
459-
// Ensure the request returned an error (error responses are thrown by
460-
// the test helper to make consuming success results simpler).
461-
await expectLater(
462-
executeCommand(command),
463-
throwsA(isResponseError(ServerErrorCodes.FileHasErrors)),
464-
);
465-
}
466-
467-
Future<void> test_filtersCorrectly() async {
468-
createFile(testFilePath, '');
469-
await initializeServer();
470-
471-
ofKind(CodeActionKind kind) => getCodeActions(testFileUri, kinds: [kind]);
472-
473-
expect(await ofKind(CodeActionKind.Source), hasLength(3));
474-
expect(await ofKind(CodeActionKind.SourceOrganizeImports), hasLength(1));
475-
expect(await ofKind(DartCodeActionKind.SortMembers), hasLength(1));
476-
expect(await ofKind(DartCodeActionKind.FixAll), hasLength(1));
477-
expect(await ofKind(CodeActionKind('source.foo')), isEmpty);
478-
expect(await ofKind(CodeActionKind.Refactor), isEmpty);
479-
}
480-
481-
Future<void> test_noEdits() async {
482-
const content = '''
483-
import 'dart:async';
484-
import 'dart:math';
485-
486-
Completer? foo;
487-
int minified(int x, int y) => min(x, y);
488-
''';
489-
490-
var codeAction = await expectCodeActionLiteral(
491-
content,
492-
command: Commands.organizeImports,
493-
);
494-
var command = codeAction.command!;
495-
496-
// Execute the command and it should return without needing us to process
497-
// a workspace/applyEdit command because there were no edits.
498-
var commandResponse = await executeCommand(command);
499-
// Successful edits return an empty success() response.
500-
expect(commandResponse, isNull);
501-
}
502-
503-
Future<void> test_unavailableWhenNotRequested() async {
504-
var content = '';
505-
506-
setSupportedCodeActionKinds([CodeActionKind.Refactor]); // not Source
507-
await expectNoAction(content, command: Commands.organizeImports);
508-
}
509-
510-
Future<void> test_unavailableWithoutApplyEditSupport() async {
511-
var content = '';
512-
513-
setApplyEditSupport(false);
514-
await expectNoAction(content, command: Commands.organizeImports);
515-
}
516-
}
363+
class OrganizeImportsSourceCodeActionsTest extends AbstractSourceCodeActionsTest
364+
with
365+
// Most tests are defined in a shared mixin.
366+
SharedOrganizeImportsSourceCodeActionsTests {}
517367

518368
@reflectiveTest
519-
class SortMembersSourceCodeActionsTest extends AbstractSourceCodeActionsTest {
520-
Future<void> test_appliesCorrectEdits_withDocumentChangesSupport() async {
521-
const content = '''
522-
String? b;
523-
String? a;
524-
''';
525-
const expectedContent = '''
526-
String? a;
527-
String? b;
528-
''';
529-
530-
await verifyCodeActionLiteralEdits(
531-
content,
532-
expectedContent,
533-
command: Commands.sortMembers,
534-
);
535-
}
536-
537-
Future<void> test_appliesCorrectEdits_withoutDocumentChangesSupport() async {
538-
const content = '''
539-
String? b;
540-
String? a;
541-
''';
542-
const expectedContent = '''
543-
String? a;
544-
String? b;
545-
''';
546-
547-
setDocumentChangesSupport(false);
548-
await verifyCodeActionLiteralEdits(
549-
content,
550-
expectedContent,
551-
command: Commands.sortMembers,
552-
);
553-
}
554-
555-
Future<void> test_availableAsCodeActionLiteral() async {
556-
const content = '';
557-
558-
await expectCodeActionLiteral(content, command: Commands.sortMembers);
559-
}
560-
561-
Future<void> test_availableAsCommand() async {
562-
createFile(testFilePath, '');
563-
setSupportedCodeActionKinds(null); // no codeActionLiteralSupport
564-
await initializeServer();
565-
566-
var actions = await getCodeActions(testFileUri);
567-
var action = findCommand(actions, Commands.sortMembers)!;
568-
action.map(
569-
(codeActionLiteral) => throw 'Expected command, got codeActionLiteral',
570-
(command) {},
571-
);
572-
}
573-
574-
Future<void> test_failsIfClientDoesntApplyEdits() async {
575-
const content = '''
576-
String? b;
577-
String? a;
578-
''';
579-
580-
var codeAction = await expectCodeActionLiteral(
581-
content,
582-
command: Commands.sortMembers,
583-
);
584-
var command = codeAction.command!;
585-
586-
var commandResponse = handleExpectedRequest<
587-
Object?,
588-
ApplyWorkspaceEditParams,
589-
ApplyWorkspaceEditResult
590-
>(
591-
Method.workspace_applyEdit,
592-
ApplyWorkspaceEditParams.fromJson,
593-
() => executeCommand(command),
594-
// Claim that we failed tpo apply the edits. This is what the client
595-
// would do if the edits provided were for an old version of the
596-
// document.
597-
handler:
598-
(edit) => ApplyWorkspaceEditResult(
599-
applied: false,
600-
failureReason: 'Document changed',
601-
),
602-
);
603-
604-
// Ensure the request returned an error (error responses are thrown by
605-
// the test helper to make consuming success results simpler).
606-
await expectLater(
607-
commandResponse,
608-
throwsA(isResponseError(ServerErrorCodes.ClientFailedToApplyEdit)),
609-
);
610-
}
611-
612-
Future<void> test_fileHasErrors_failsSilentlyForAutomatic() async {
613-
failTestOnErrorDiagnostic = false;
614-
var content = 'invalid dart code';
615-
616-
var codeAction = await expectCodeActionLiteral(
617-
content,
618-
command: Commands.sortMembers,
619-
triggerKind: CodeActionTriggerKind.Automatic,
620-
);
621-
var command = codeAction.command!;
622-
623-
// Expect a valid null result.
624-
var response = await executeCommand(command);
625-
expect(response, isNull);
626-
}
627-
628-
Future<void> test_fileHasErrors_failsWithErrorForManual() async {
629-
failTestOnErrorDiagnostic = false;
630-
var content = 'invalid dart code';
631-
632-
var codeAction = await expectCodeActionLiteral(
633-
content,
634-
command: Commands.sortMembers,
635-
);
636-
var command = codeAction.command!;
637-
638-
// Ensure the request returned an error (error responses are thrown by
639-
// the test helper to make consuming success results simpler).
640-
await expectLater(
641-
executeCommand(command),
642-
throwsA(isResponseError(ServerErrorCodes.FileHasErrors)),
643-
);
644-
}
645-
646-
Future<void> test_nonDartFile() async {
647-
await expectNoAction(
648-
filePath: pubspecFilePath,
649-
simplePubspecContent,
650-
command: Commands.sortMembers,
651-
);
652-
}
653-
654-
Future<void> test_unavailableWhenNotRequested() async {
655-
var content = '';
656-
657-
setSupportedCodeActionKinds([CodeActionKind.Refactor]); // not Source
658-
await expectNoAction(content, command: Commands.sortMembers);
659-
}
660-
661-
Future<void> test_unavailableWithoutApplyEditSupport() async {
662-
var content = '';
663-
664-
setApplyEditSupport(false);
665-
await expectNoAction(content, command: Commands.sortMembers);
666-
}
667-
}
369+
class SortMembersSourceCodeActionsTest extends AbstractSourceCodeActionsTest
370+
with
371+
// Most tests are defined in a shared mixin.
372+
SharedSortMembersSourceCodeActionsTests {}

0 commit comments

Comments
 (0)