Skip to content

Commit 13e29e2

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Starts refactoring tests migration to TestCode markers
Continuing the work to normalize all our tests to use the TestCode markers. Bug: #60234 Change-Id: I687bc35409b1e37938c60206d07155fcc7ac2ac9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433800 Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 9453462 commit 13e29e2

File tree

5 files changed

+372
-301
lines changed

5 files changed

+372
-301
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'abstract_single_unit.dart';
6+
7+
mixin SelectionMixin on AbstractSingleUnitTest {
8+
late int offset;
9+
late int length;
10+
11+
void setPosition(int index) {
12+
if (index < 0 || index >= parsedTestCode.positions.length) {
13+
throw ArgumentError('Index out of bounds for positions.');
14+
}
15+
offset = parsedTestCode.positions[index].offset;
16+
length = 0;
17+
}
18+
19+
void setPositionOrRange(int index) {
20+
if (index < 0) {
21+
throw ArgumentError('Index must be non-negative.');
22+
}
23+
if (parsedTestCode.positions.isNotEmpty) {
24+
setPosition(index);
25+
} else if (parsedTestCode.ranges.isNotEmpty) {
26+
setRange(index);
27+
} else {
28+
throw ArgumentError('Test code must contain a position or range marker.');
29+
}
30+
}
31+
32+
void setRange(int index) {
33+
if (index < 0 || index >= parsedTestCode.ranges.length) {
34+
throw ArgumentError('Index out of bounds for ranges.');
35+
}
36+
var range = parsedTestCode.ranges[index].sourceRange;
37+
offset = range.offset;
38+
length = range.length;
39+
}
40+
}

pkg/analysis_server/test/services/refactoring/legacy/abstract_refactoring.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart'
1212
import 'package:test/test.dart';
1313

1414
import '../../../abstract_single_unit.dart';
15+
import '../../../selection_mixin.dart';
1516

1617
int findIdentifierLength(String search) {
1718
var length = 0;
@@ -28,7 +29,8 @@ int findIdentifierLength(String search) {
2829
}
2930

3031
/// The base class for all [Refactoring] tests.
31-
abstract class RefactoringTest extends AbstractSingleUnitTest {
32+
abstract class RefactoringTest extends AbstractSingleUnitTest
33+
with SelectionMixin {
3234
late RefactoringWorkspace refactoringWorkspace;
3335
late SearchEngine searchEngine;
3436

@@ -80,6 +82,7 @@ abstract class RefactoringTest extends AbstractSingleUnitTest {
8082
String? expectedMessage,
8183
SourceRange? expectedContextRange,
8284
String? expectedContextSearch,
85+
int? rangeIndex,
8386
}) {
8487
expect(status.severity, expectedSeverity, reason: status.toString());
8588
if (expectedSeverity != null) {
@@ -99,6 +102,11 @@ abstract class RefactoringTest extends AbstractSingleUnitTest {
99102
var expectedLength = findIdentifierLength(expectedContextSearch);
100103
expect(location.offset, expectedOffset);
101104
expect(location.length, expectedLength);
105+
} else if (rangeIndex != null) {
106+
var location = problem.location!;
107+
setRange(rangeIndex);
108+
expect(location.offset, offset);
109+
expect(location.length, length);
102110
}
103111
}
104112
}

pkg/analysis_server/test/services/refactoring/legacy/abstract_rename.dart

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import 'package:analysis_server/src/services/correction/namespace.dart';
66
import 'package:analysis_server/src/services/refactoring/legacy/refactoring.dart';
7+
import 'package:analysis_server_plugin/src/utilities/selection.dart';
78
import 'package:analyzer/dart/ast/ast.dart';
89
import 'package:analyzer/dart/element/element.dart';
910
import 'package:analyzer/src/dart/ast/element_locator.dart';
1011
import 'package:analyzer/src/utilities/extensions/element.dart';
1112
import 'package:analyzer_plugin/protocol/protocol_common.dart' hide Element;
13+
import 'package:collection/collection.dart';
1214
import 'package:test/test.dart';
1315

1416
import 'abstract_refactoring.dart';
@@ -20,11 +22,30 @@ class RenameRefactoringTest extends RefactoringTest {
2022

2123
/// Asserts that [refactoring] has potential edits in [testFile] at offset
2224
/// of the given [searches].
23-
void assertPotentialEdits(List<String> searches) {
25+
///
26+
/// If [searches] is `null`, it will use the positions/ranges marked in
27+
/// [parsedTestCode] to determine the offsets.
28+
void assertPotentialEdits({List<String>? searches, List<int>? indexes}) {
2429
var expectedOffsets = <int>{};
25-
for (var search in searches) {
26-
var offset = findOffset(search);
27-
expectedOffsets.add(offset);
30+
if (searches != null) {
31+
for (var search in searches) {
32+
var offset = findOffset(search);
33+
expectedOffsets.add(offset);
34+
}
35+
} else if (parsedTestCode.positions.isNotEmpty) {
36+
for (var position in parsedTestCode.positions.whereIndexed(
37+
(index, _) => indexes?.contains(index) ?? true,
38+
)) {
39+
expectedOffsets.add(position.offset);
40+
}
41+
} else if (parsedTestCode.ranges.isNotEmpty) {
42+
for (var range in parsedTestCode.ranges.whereIndexed(
43+
(index, _) => indexes?.contains(index) ?? true,
44+
)) {
45+
expectedOffsets.add(range.sourceRange.offset);
46+
}
47+
} else {
48+
fail('No searches or positions provided for potential edits.');
2849
}
2950
// remove offset marked as potential
3051
for (var potentialId in refactoring.potentialEditIds) {
@@ -36,24 +57,35 @@ class RenameRefactoringTest extends RefactoringTest {
3657
expect(expectedOffsets, isEmpty);
3758
}
3859

60+
/// Creates a refactoring and sets the offset and length from the
61+
/// [parsedTestCode] position/range at the given [index].
62+
void createRenameRefactoring([int index = 0]) {
63+
setPositionOrRange(index);
64+
var unit = testAnalysisResult.unit;
65+
var node = unit.select(length: length, offset: offset)?.coveringNode;
66+
if (node == null) {
67+
fail('No node found at offset $offset with length $length.');
68+
}
69+
createRenameRefactoringForNode(node);
70+
}
71+
3972
/// Creates a new [RenameRefactoring] in [refactoring] for the element of
4073
/// the [SimpleIdentifier] at the given [search] pattern.
4174
void createRenameRefactoringAtString(String search) {
4275
var node = findNode.any(search);
4376

44-
Element? element;
45-
switch (node) {
46-
case ImportDirective():
47-
element = MockLibraryImportElement(node.libraryImport!);
48-
default:
49-
element = ElementLocator.locate(node);
77+
if (node is ImportDirective) {
78+
return createRenameRefactoringForElement2(
79+
MockLibraryImportElement(node.libraryImport!),
80+
);
5081
}
51-
52-
if (node is SimpleIdentifier && element is PrefixElement) {
53-
element = MockLibraryImportElement(getImportElement(node)!);
82+
var element = ElementLocator.locate(node);
83+
if (node is! SimpleIdentifier || element is! PrefixElement) {
84+
return createRenameRefactoringForElement2(element);
5485
}
55-
56-
createRenameRefactoringForElement2(element);
86+
createRenameRefactoringForElement2(
87+
MockLibraryImportElement(getImportElement(node)!),
88+
);
5789
}
5890

5991
/// Creates a new [RenameRefactoring] in [refactoring] for [element].
@@ -71,7 +103,23 @@ class RenameRefactoringTest extends RefactoringTest {
71103
this.refactoring = refactoring;
72104
}
73105

74-
/// Returns the [Edit] with the given [id], maybe `null`.
106+
void createRenameRefactoringForNode(AstNode node) {
107+
Element? element;
108+
switch (node) {
109+
case ImportDirective():
110+
element = MockLibraryImportElement(node.libraryImport!);
111+
default:
112+
element = ElementLocator.locate(node);
113+
}
114+
115+
if (node is SimpleIdentifier && element is PrefixElement) {
116+
element = MockLibraryImportElement(getImportElement(node)!);
117+
}
118+
119+
createRenameRefactoringForElement2(element);
120+
}
121+
122+
/// Returns the [SourceEdit] with the given [id], maybe `null`.
75123
SourceEdit findEditById(String id) {
76124
for (var fileEdit in refactoringChange.edits) {
77125
for (var edit in fileEdit.edits) {

0 commit comments

Comments
 (0)