Add tests for rename with macro code.

Change-Id: Ifa0d5245918c06fa3eda1532560523263682bd43
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353780
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
This commit is contained in:
Keerti Parthasarathy 2024-02-22 19:22:57 +00:00 committed by Commit Queue
parent 5eeaf42939
commit 167c0a1b78
3 changed files with 89 additions and 1 deletions

View file

@ -1668,6 +1668,23 @@ class A {
''');
}
Future<void> test_class_macros() {
addTestFile('''
import 'macros.dart';
@DeclareInType(' /// named\\n C.named();')
class C {}
''');
return assertSuccessfulRefactoring(() {
return sendRenameRequest('C {', 'NewName');
}, '''
import 'macros.dart';
@DeclareInType(' /// named\\n C.named();')
class NewName {}
''');
}
Future<void> test_class_method_in_objectPattern() {
addTestFile('''
void f(Object? x) {

View file

@ -65,4 +65,42 @@ void bar() {
await analysisFinished;
expect(currentAnalysisErrors[pathname], isEmpty);
}
Future<void> test_rename_macro() async {
addMacros([declareInTypeMacro()]);
var pathname = sourcePath('lib/test.dart');
var text = r'''
import 'macros.dart';
@DeclareInType(' /// named\\n C.named();')
class C {}
''';
writeFile(pathname, text);
await standardAnalysisSetup();
await analysisFinished;
expect(currentAnalysisErrors[pathname], isEmpty);
// expect a valid rename refactoring
var result = await sendEditGetRefactoring(
RefactoringKind.RENAME, pathname, text.indexOf('C {'), 0, false,
options: RenameOptions('Coo'));
expect(result.initialProblems, isEmpty);
expect(result.optionsProblems, isEmpty);
expect(result.finalProblems, isEmpty);
expect(result.potentialEdits, isNull);
var change = result.change!;
expect(change.edits.length, 1);
var fileEdit = change.edits.first;
// apply the refactoring, expect that the new code has no errors
expect(fileEdit.edits.length, 1);
for (var edit in fileEdit.edits) {
text = text.replaceRange(edit.offset, edit.end, edit.replacement);
}
await sendAnalysisUpdateContent({pathname: AddContentOverlay(text)});
await analysisFinished;
expect(currentAnalysisErrors[pathname], isEmpty);
}
}

View file

@ -11,13 +11,16 @@ import 'package:analysis_server/protocol/protocol_constants.dart';
import 'package:analysis_server/protocol/protocol_generated.dart';
import 'package:analysis_server/src/services/pub/pub_command.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import '../../analysis_server_base.dart' show AnalysisOptionsFileConfig;
import '../../src/utilities/mock_packages.dart';
import '../../support/configuration_files.dart';
import '../../test_macros.dart' as macros;
import 'integration_test_methods.dart';
import 'protocol_matchers.dart';
@ -86,7 +89,7 @@ typedef NotificationProcessor = void Function(
/// Base class for analysis server integration tests.
abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
with MockPackagesMixin, ConfigurationFilesMixin {
with MockPackagesMixin, ConfigurationFilesMixin, macros.TestMacros {
/// Amount of time to give the server to respond to a shutdown request before
/// forcibly terminating it.
static const Duration SHUTDOWN_TIMEOUT = Duration(seconds: 60);
@ -151,6 +154,24 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
@override
String get testPackageRootPath => sourceDirectory.path;
/// Adds support for macros to the `package_config.json` file and creates a
/// `macros.dart` file that defines the given [macros]. The macros should not
/// include imports, the imports for macros will be added automatically.
void addMacros(List<String> macros) {
writeTestPackageConfig(
macro: true,
);
writeFile(
'$testPackageRootPath/lib/macros.dart',
[
'''
// There is no public API exposed yet, the in-progress API lives here.
import 'package:_fe_analyzer_shared/src/macros/api.dart';
''',
...macros
].join('\n'));
}
/// Print out any messages exchanged with the server. If some messages have
/// already been exchanged with the server, they are printed out immediately.
void debugStdio() {
@ -193,6 +214,12 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
..createSync();
writeTestPackageConfig();
writeTestPackageAnalysisOptionsFile(
AnalysisOptionsFileConfig(
experiments: ['macros'],
),
);
onAnalysisErrors.listen((AnalysisErrorsParams params) {
currentAnalysisErrors[params.file] = params.errors;
});
@ -302,6 +329,12 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
file.writeAsStringSync(contents);
return file.resolveSymbolicLinksSync();
}
void writeTestPackageAnalysisOptionsFile(AnalysisOptionsFileConfig config) {
String filePath =
path.join(testPackageRootPath, file_paths.analysisOptionsYaml);
writeFile(filePath, config.toContent());
}
}
/// Wrapper class for Matcher which doesn't create the underlying Matcher object