Skip to content

Commit 853bc7b

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[analyzer] Add selection range tests for null-aware elements
Part of #56989 Change-Id: I500469cd6a08811165d4759ec0c50099da6baf0e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412300 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent c706117 commit 853bc7b

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed

pkg/analysis_server/test/lsp/selection_range_test.dart

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,162 @@ class Foo {
6363
);
6464
}
6565

66+
Future<void> test_nullAwareElements_inList() async {
67+
var code = TestCode.parse('''
68+
class Foo<T> {
69+
List<int> a(String b) {
70+
return [?(1 ^+ 2) * 3];
71+
}
72+
}
73+
''');
74+
75+
await initialize();
76+
await openFile(mainFileUri, code.code);
77+
var lineInfo = LineInfo.fromContent(code.code);
78+
79+
// The returned List corresponds to the input list of positions, and not
80+
// the set of ranges - each range within that list has a (recursive) parent
81+
// to walk up all ranges for that position.
82+
var regions = await getSelectionRanges(mainFileUri, [
83+
code.position.position,
84+
]);
85+
expect(regions!.length, equals(1)); // Only one position was sent.
86+
var regionTexts =
87+
_getSelectionRangeText(lineInfo, code.code, regions.first).toList();
88+
89+
expect(
90+
regionTexts,
91+
equals([
92+
'1 + 2',
93+
'(1 + 2)',
94+
'(1 + 2) * 3',
95+
'?(1 + 2) * 3',
96+
'[?(1 + 2) * 3]',
97+
'return [?(1 + 2) * 3];',
98+
'{\n return [?(1 + 2) * 3];\n }',
99+
'List<int> a(String b) {\n return [?(1 + 2) * 3];\n }',
100+
'class Foo<T> {\n List<int> a(String b) {\n return [?(1 + 2) * 3];\n }\n}',
101+
]),
102+
);
103+
}
104+
105+
Future<void> test_nullAwareElements_inMapKey() async {
106+
var code = TestCode.parse('''
107+
class Foo<T> {
108+
Map<int, String> a(String b) {
109+
return {?(1 ^+ 2) * 3: b};
110+
}
111+
}
112+
''');
113+
114+
await initialize();
115+
await openFile(mainFileUri, code.code);
116+
var lineInfo = LineInfo.fromContent(code.code);
117+
118+
// The returned List corresponds to the input list of positions, and not
119+
// the set of ranges - each range within that list has a (recursive) parent
120+
// to walk up all ranges for that position.
121+
var regions = await getSelectionRanges(mainFileUri, [
122+
code.position.position,
123+
]);
124+
expect(regions!.length, equals(1)); // Only one position was sent.
125+
var regionTexts =
126+
_getSelectionRangeText(lineInfo, code.code, regions.first).toList();
127+
128+
expect(
129+
regionTexts,
130+
equals([
131+
'1 + 2',
132+
'(1 + 2)',
133+
'(1 + 2) * 3',
134+
'?(1 + 2) * 3: b',
135+
'{?(1 + 2) * 3: b}',
136+
'return {?(1 + 2) * 3: b};',
137+
'{\n return {?(1 + 2) * 3: b};\n }',
138+
'Map<int, String> a(String b) {\n return {?(1 + 2) * 3: b};\n }',
139+
'class Foo<T> {\n Map<int, String> a(String b) {\n return {?(1 + 2) * 3: b};\n }\n}',
140+
]),
141+
);
142+
}
143+
144+
Future<void> test_nullAwareElements_inMapValue() async {
145+
var code = TestCode.parse('''
146+
class Foo<T> {
147+
Map<String, int> a(String b) {
148+
return {b: ?(1 ^+ 2) * 3};
149+
}
150+
}
151+
''');
152+
153+
await initialize();
154+
await openFile(mainFileUri, code.code);
155+
var lineInfo = LineInfo.fromContent(code.code);
156+
157+
// The returned List corresponds to the input list of positions, and not
158+
// the set of ranges - each range within that list has a (recursive) parent
159+
// to walk up all ranges for that position.
160+
var regions = await getSelectionRanges(mainFileUri, [
161+
code.position.position,
162+
]);
163+
expect(regions!.length, equals(1)); // Only one position was sent.
164+
var regionTexts =
165+
_getSelectionRangeText(lineInfo, code.code, regions.first).toList();
166+
167+
expect(
168+
regionTexts,
169+
equals([
170+
'1 + 2',
171+
'(1 + 2)',
172+
'(1 + 2) * 3',
173+
'b: ?(1 + 2) * 3',
174+
'{b: ?(1 + 2) * 3}',
175+
'return {b: ?(1 + 2) * 3};',
176+
'{\n return {b: ?(1 + 2) * 3};\n }',
177+
'Map<String, int> a(String b) {\n return {b: ?(1 + 2) * 3};\n }',
178+
'class Foo<T> {\n Map<String, int> a(String b) {\n return {b: ?(1 + 2) * 3};\n }\n}',
179+
]),
180+
);
181+
}
182+
183+
Future<void> test_nullAwareElements_inSet() async {
184+
var code = TestCode.parse('''
185+
class Foo<T> {
186+
Set<int> a(String b) {
187+
return {?(1 ^+ 2) * 3};
188+
}
189+
}
190+
''');
191+
192+
await initialize();
193+
await openFile(mainFileUri, code.code);
194+
var lineInfo = LineInfo.fromContent(code.code);
195+
196+
// The returned List corresponds to the input list of positions, and not
197+
// the set of ranges - each range within that list has a (recursive) parent
198+
// to walk up all ranges for that position.
199+
var regions = await getSelectionRanges(mainFileUri, [
200+
code.position.position,
201+
]);
202+
expect(regions!.length, equals(1)); // Only one position was sent.
203+
var regionTexts =
204+
_getSelectionRangeText(lineInfo, code.code, regions.first).toList();
205+
206+
expect(
207+
regionTexts,
208+
equals([
209+
'1 + 2',
210+
'(1 + 2)',
211+
'(1 + 2) * 3',
212+
'?(1 + 2) * 3',
213+
'{?(1 + 2) * 3}',
214+
'return {?(1 + 2) * 3};',
215+
'{\n return {?(1 + 2) * 3};\n }',
216+
'Set<int> a(String b) {\n return {?(1 + 2) * 3};\n }',
217+
'class Foo<T> {\n Set<int> a(String b) {\n return {?(1 + 2) * 3};\n }\n}',
218+
]),
219+
);
220+
}
221+
66222
Future<void> test_single() async {
67223
var code = TestCode.parse('''
68224
class Foo<T> {

pkg/analysis_server/test/src/computer/selection_range_computer_test.dart

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,98 @@ augment class Foo {
264264
]);
265265
}
266266

267+
Future<void> test_method_withNullAwareElements_inList() async {
268+
var content = TestCode.parse('''
269+
class Foo<T> {
270+
List<int> a(String b) {
271+
return [?(1 ^+ 2) * 3];
272+
}
273+
}
274+
''');
275+
276+
var regions = await _computeSelectionRanges(content);
277+
_expectRegions(regions, content, [
278+
'1 + 2',
279+
'(1 + 2)',
280+
'(1 + 2) * 3',
281+
'?(1 + 2) * 3',
282+
'[?(1 + 2) * 3]',
283+
'return [?(1 + 2) * 3];',
284+
'{\n return [?(1 + 2) * 3];\n }',
285+
'List<int> a(String b) {\n return [?(1 + 2) * 3];\n }',
286+
'class Foo<T> {\n List<int> a(String b) {\n return [?(1 + 2) * 3];\n }\n}',
287+
]);
288+
}
289+
290+
Future<void> test_method_withNullAwareElements_inMapKey() async {
291+
var content = TestCode.parse('''
292+
class Foo<T> {
293+
Map<int, String> a(String b) {
294+
return {?(1 ^+ 2) * 3: b};
295+
}
296+
}
297+
''');
298+
299+
var regions = await _computeSelectionRanges(content);
300+
_expectRegions(regions, content, [
301+
'1 + 2',
302+
'(1 + 2)',
303+
'(1 + 2) * 3',
304+
'?(1 + 2) * 3: b',
305+
'{?(1 + 2) * 3: b}',
306+
'return {?(1 + 2) * 3: b};',
307+
'{\n return {?(1 + 2) * 3: b};\n }',
308+
'Map<int, String> a(String b) {\n return {?(1 + 2) * 3: b};\n }',
309+
'class Foo<T> {\n Map<int, String> a(String b) {\n return {?(1 + 2) * 3: b};\n }\n}',
310+
]);
311+
}
312+
313+
Future<void> test_method_withNullAwareElements_inMapValue() async {
314+
var content = TestCode.parse('''
315+
class Foo<T> {
316+
Map<String, int> a(String b) {
317+
return {b: ?(1 ^+ 2) * 3};
318+
}
319+
}
320+
''');
321+
322+
var regions = await _computeSelectionRanges(content);
323+
_expectRegions(regions, content, [
324+
'1 + 2',
325+
'(1 + 2)',
326+
'(1 + 2) * 3',
327+
'b: ?(1 + 2) * 3',
328+
'{b: ?(1 + 2) * 3}',
329+
'return {b: ?(1 + 2) * 3};',
330+
'{\n return {b: ?(1 + 2) * 3};\n }',
331+
'Map<String, int> a(String b) {\n return {b: ?(1 + 2) * 3};\n }',
332+
'class Foo<T> {\n Map<String, int> a(String b) {\n return {b: ?(1 + 2) * 3};\n }\n}',
333+
]);
334+
}
335+
336+
Future<void> test_method_withNullAwareElements_inSet() async {
337+
var content = TestCode.parse('''
338+
class Foo<T> {
339+
Set<int> a(String b) {
340+
return {?(1 ^+ 2) * 3};
341+
}
342+
}
343+
''');
344+
345+
var regions = await _computeSelectionRanges(content);
346+
_expectRegions(regions, content, [
347+
'1 + 2',
348+
'(1 + 2)',
349+
'(1 + 2) * 3',
350+
'?(1 + 2) * 3',
351+
'{?(1 + 2) * 3}',
352+
'return {?(1 + 2) * 3};',
353+
'{\n return {?(1 + 2) * 3};\n }',
354+
'Set<int> a(String b) {\n return {?(1 + 2) * 3};\n }',
355+
'class Foo<T> {\n Set<int> a(String b) {\n return {?(1 + 2) * 3};\n }\n}',
356+
]);
357+
}
358+
267359
Future<void> test_methodLambda() async {
268360
var content = TestCode.parse('''
269361
class Foo<T> {

0 commit comments

Comments
 (0)