Skip to content

Commit d9ecb2c

Browse files
keertippull[bot]
authored andcommitted
Add tests for rename with macro code.
Change-Id: Ifa0d5245918c06fa3eda1532560523263682bd43 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353780 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent 469d73b commit d9ecb2c

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

pkg/analysis_server/test/edit/refactoring_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,23 @@ class A {
16681668
''');
16691669
}
16701670

1671+
Future<void> test_class_macros() {
1672+
addTestFile('''
1673+
import 'macros.dart';
1674+
1675+
@DeclareInType(' /// named\\n C.named();')
1676+
class C {}
1677+
''');
1678+
return assertSuccessfulRefactoring(() {
1679+
return sendRenameRequest('C {', 'NewName');
1680+
}, '''
1681+
import 'macros.dart';
1682+
1683+
@DeclareInType(' /// named\\n C.named();')
1684+
class NewName {}
1685+
''');
1686+
}
1687+
16711688
Future<void> test_class_method_in_objectPattern() {
16721689
addTestFile('''
16731690
void f(Object? x) {

pkg/analysis_server/test/integration/edit/get_refactoring_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,42 @@ void bar() {
6565
await analysisFinished;
6666
expect(currentAnalysisErrors[pathname], isEmpty);
6767
}
68+
69+
Future<void> test_rename_macro() async {
70+
addMacros([declareInTypeMacro()]);
71+
var pathname = sourcePath('lib/test.dart');
72+
var text = r'''
73+
import 'macros.dart';
74+
75+
@DeclareInType(' /// named\\n C.named();')
76+
class C {}
77+
''';
78+
writeFile(pathname, text);
79+
await standardAnalysisSetup();
80+
await analysisFinished;
81+
expect(currentAnalysisErrors[pathname], isEmpty);
82+
83+
// expect a valid rename refactoring
84+
var result = await sendEditGetRefactoring(
85+
RefactoringKind.RENAME, pathname, text.indexOf('C {'), 0, false,
86+
options: RenameOptions('Coo'));
87+
expect(result.initialProblems, isEmpty);
88+
expect(result.optionsProblems, isEmpty);
89+
expect(result.finalProblems, isEmpty);
90+
expect(result.potentialEdits, isNull);
91+
92+
var change = result.change!;
93+
expect(change.edits.length, 1);
94+
var fileEdit = change.edits.first;
95+
96+
// apply the refactoring, expect that the new code has no errors
97+
expect(fileEdit.edits.length, 1);
98+
for (var edit in fileEdit.edits) {
99+
text = text.replaceRange(edit.offset, edit.end, edit.replacement);
100+
}
101+
await sendAnalysisUpdateContent({pathname: AddContentOverlay(text)});
102+
103+
await analysisFinished;
104+
expect(currentAnalysisErrors[pathname], isEmpty);
105+
}
68106
}

pkg/analysis_server/test/integration/support/integration_tests.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import 'package:analysis_server/protocol/protocol_constants.dart';
1111
import 'package:analysis_server/protocol/protocol_generated.dart';
1212
import 'package:analysis_server/src/services/pub/pub_command.dart';
1313
import 'package:analyzer/file_system/physical_file_system.dart';
14+
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
1415
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1516
import 'package:meta/meta.dart';
1617
import 'package:path/path.dart' as path;
1718
import 'package:test/test.dart';
1819

20+
import '../../analysis_server_base.dart' show AnalysisOptionsFileConfig;
1921
import '../../src/utilities/mock_packages.dart';
2022
import '../../support/configuration_files.dart';
23+
import '../../test_macros.dart' as macros;
2124
import 'integration_test_methods.dart';
2225
import 'protocol_matchers.dart';
2326

@@ -86,7 +89,7 @@ typedef NotificationProcessor = void Function(
8689

8790
/// Base class for analysis server integration tests.
8891
abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
89-
with MockPackagesMixin, ConfigurationFilesMixin {
92+
with MockPackagesMixin, ConfigurationFilesMixin, macros.TestMacros {
9093
/// Amount of time to give the server to respond to a shutdown request before
9194
/// forcibly terminating it.
9295
static const Duration SHUTDOWN_TIMEOUT = Duration(seconds: 60);
@@ -151,6 +154,24 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
151154
@override
152155
String get testPackageRootPath => sourceDirectory.path;
153156

157+
/// Adds support for macros to the `package_config.json` file and creates a
158+
/// `macros.dart` file that defines the given [macros]. The macros should not
159+
/// include imports, the imports for macros will be added automatically.
160+
void addMacros(List<String> macros) {
161+
writeTestPackageConfig(
162+
macro: true,
163+
);
164+
writeFile(
165+
'$testPackageRootPath/lib/macros.dart',
166+
[
167+
'''
168+
// There is no public API exposed yet, the in-progress API lives here.
169+
import 'package:_fe_analyzer_shared/src/macros/api.dart';
170+
''',
171+
...macros
172+
].join('\n'));
173+
}
174+
154175
/// Print out any messages exchanged with the server. If some messages have
155176
/// already been exchanged with the server, they are printed out immediately.
156177
void debugStdio() {
@@ -193,6 +214,12 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
193214
..createSync();
194215
writeTestPackageConfig();
195216

217+
writeTestPackageAnalysisOptionsFile(
218+
AnalysisOptionsFileConfig(
219+
experiments: ['macros'],
220+
),
221+
);
222+
196223
onAnalysisErrors.listen((AnalysisErrorsParams params) {
197224
currentAnalysisErrors[params.file] = params.errors;
198225
});
@@ -302,6 +329,12 @@ abstract class AbstractAnalysisServerIntegrationTest extends IntegrationTest
302329
file.writeAsStringSync(contents);
303330
return file.resolveSymbolicLinksSync();
304331
}
332+
333+
void writeTestPackageAnalysisOptionsFile(AnalysisOptionsFileConfig config) {
334+
String filePath =
335+
path.join(testPackageRootPath, file_paths.analysisOptionsYaml);
336+
writeFile(filePath, config.toContent());
337+
}
305338
}
306339

307340
/// Wrapper class for Matcher which doesn't create the underlying Matcher object

0 commit comments

Comments
 (0)