Skip to content

Commit e7eda9a

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Migrate a few more tests to TestCode from old markers
+ tweak indenting to be consistent with other tests. Most of the larger test files are done with this change, and there are around 40 remaining calls to withoutMarkers() spread across around 20 files. Change-Id: Icfa516f7ca869e09822442b02a1b2eafca692b86 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330423 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent f4d9c95 commit e7eda9a

File tree

4 files changed

+452
-417
lines changed

4 files changed

+452
-417
lines changed

pkg/analysis_server/test/lsp/format_test.dart

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import 'package:analysis_server/lsp_protocol/protocol.dart';
66
import 'package:analysis_server/src/lsp/constants.dart';
7+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
78
import 'package:test/test.dart';
89
import 'package:test_reflective_loader/test_reflective_loader.dart';
910

1011
import '../tool/lsp_spec/matchers.dart';
12+
import '../utils/test_code_extensions.dart';
1113
import 'server_abstract.dart';
1214

1315
void main() {
@@ -27,16 +29,16 @@ class FormatTest extends AbstractLspAnalysisServerTest {
2729
}
2830

2931
Future<List<TextEdit>> expectRangeFormattedContents(
30-
Uri uri, String original, String expected) async {
31-
final formatEdits = (await formatRange(uri, rangeFromMarkers(original)))!;
32-
final formattedContents =
33-
applyTextEdits(withoutMarkers(original), formatEdits);
32+
Uri uri, TestCode code, String expected) async {
33+
final formatEdits = (await formatRange(uri, code.range.range))!;
34+
final formattedContents = applyTextEdits(code.code, formatEdits);
3435
expect(formattedContents, equals(expected));
3536
return formatEdits;
3637
}
3738

3839
Future<void> test_alreadyFormatted() async {
39-
const contents = '''void f() {
40+
const contents = '''
41+
void f() {
4042
print('test');
4143
}
4244
''';
@@ -159,23 +161,23 @@ ErrorOr<Pair<A, List<B>>> c(
159161

160162
Future<void> test_formatOnType_simple() async {
161163
const contents = '''
162-
void f ()
163-
{
164+
void f ()
165+
{
164166
165-
print('test');
166-
^}
167+
print('test');
168+
^}
167169
''';
168170
final expected = '''void f() {
169171
print('test');
170172
}
171173
''';
174+
final code = TestCode.parse(contents);
172175
await initialize();
173-
await openFile(mainFileUri, withoutMarkers(contents));
176+
await openFile(mainFileUri, code.code);
174177

175178
final formatEdits =
176-
(await formatOnType(mainFileUri, positionFromMarker(contents), '}'))!;
177-
final formattedContents =
178-
applyTextEdits(withoutMarkers(contents), formatEdits);
179+
(await formatOnType(mainFileUri, code.position.position, '}'))!;
180+
final formattedContents = applyTextEdits(code.code, formatEdits);
179181
expect(formattedContents, equals(expected));
180182
}
181183

@@ -185,9 +187,9 @@ ErrorOr<Pair<A, List<B>>> c(
185187
const contents = '''
186188
void f()
187189
{
188-
[[ print('test');
190+
[! print('test');
189191
print('test');
190-
]] print('test');
192+
!] print('test');
191193
}
192194
''';
193195
final expected = '''
@@ -198,19 +200,20 @@ void f()
198200
print('test');
199201
}
200202
''';
203+
final code = TestCode.parse(contents);
201204
await initialize();
202-
await openFile(mainFileUri, withoutMarkers(contents));
203-
await expectRangeFormattedContents(mainFileUri, contents, expected);
205+
await openFile(mainFileUri, code.code);
206+
await expectRangeFormattedContents(mainFileUri, code, expected);
204207
}
205208

206209
Future<void> test_formatRange_expandsLeadingWhitespaceToNearestLine() async {
207210
const contents = '''
208211
void f()
209212
{
210213
211-
[[ print('test'); // line 2
214+
[! print('test'); // line 2
212215
print('test'); // line 3
213-
print('test'); // line 4]]
216+
print('test'); // line 4!]
214217
}
215218
''';
216219
const expected = '''
@@ -222,9 +225,10 @@ void f()
222225
print('test'); // line 4
223226
}
224227
''';
228+
final code = TestCode.parse(contents);
225229
await initialize();
226-
await openFile(mainFileUri, withoutMarkers(contents));
227-
await expectRangeFormattedContents(mainFileUri, contents, expected);
230+
await openFile(mainFileUri, code.code);
231+
await expectRangeFormattedContents(mainFileUri, code, expected);
228232
}
229233

230234
Future<void> test_formatRange_invalidRange() async {
@@ -234,8 +238,9 @@ void f()
234238
print('test');
235239
}
236240
''';
241+
final code = TestCode.parse(contents);
237242
await initialize();
238-
await openFile(mainFileUri, withoutMarkers(contents));
243+
await openFile(mainFileUri, code.code);
239244
final formatRangeRequest = formatRange(
240245
mainFileUri,
241246
Range(
@@ -254,11 +259,11 @@ main ()
254259
print('test');
255260
}
256261
257-
[[main2 ()
262+
[!main2 ()
258263
{
259264
260265
print('test');
261-
}]]
266+
}!]
262267
263268
main3 ()
264269
{
@@ -283,33 +288,36 @@ main3 ()
283288
print('test');
284289
}
285290
''';
291+
final code = TestCode.parse(contents);
286292
await initialize();
287-
await openFile(mainFileUri, withoutMarkers(contents));
288-
await expectRangeFormattedContents(mainFileUri, contents, expected);
293+
await openFile(mainFileUri, code.code);
294+
await expectRangeFormattedContents(mainFileUri, code, expected);
289295
}
290296

291297
Future<void> test_formatRange_trailingNewline_47702() async {
292298
// Check we complete when a formatted block ends with a newline.
293299
// https://github.com/dart-lang/sdk/issues/47702
294300
const contents = '''
295301
int a;
296-
[[
302+
[!
297303
int b;
298-
]]
304+
!]
299305
''';
300306
final expected = '''
301307
int a;
302308
303309
int b;
304310
305311
''';
312+
final code = TestCode.parse(contents);
306313
await initialize();
307-
await openFile(mainFileUri, withoutMarkers(contents));
308-
await expectRangeFormattedContents(mainFileUri, contents, expected);
314+
await openFile(mainFileUri, code.code);
315+
await expectRangeFormattedContents(mainFileUri, code, expected);
309316
}
310317

311318
Future<void> test_invalidSyntax() async {
312-
const contents = '''void f(((( {
319+
const contents = '''
320+
void f(((( {
313321
print('test');
314322
}
315323
''';
@@ -322,15 +330,18 @@ int b;
322330

323331
Future<void> test_lineLength() async {
324332
const contents = '''
325-
void f() =>
326-
print(
327-
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'
328-
);
333+
void f() =>
334+
print(
335+
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'
336+
);
329337
''';
330-
final expectedDefault = '''void f() => print(
331-
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n''';
332-
final expectedLongLines =
333-
'''void f() => print('123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n''';
338+
final expectedDefault = '''
339+
void f() => print(
340+
'123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');
341+
''';
342+
final expectedLongLines = '''
343+
void f() => print('123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');
344+
''';
334345

335346
// Initialize with config support, supplying an empty config when requested.
336347
await provideConfig(
@@ -659,7 +670,8 @@ void f() {
659670
print('test');
660671
}
661672
''';
662-
final expected = '''void f() {
673+
final expected = '''
674+
void f() {
663675
print('test');
664676
}
665677
''';
@@ -676,7 +688,8 @@ void f() {
676688
print('test');
677689
}
678690
''';
679-
final expected = '''void f() {
691+
final expected = '''
692+
void f() {
680693
print('test');
681694
}
682695
''';
@@ -686,13 +699,15 @@ void f() {
686699
}
687700

688701
Future<void> test_validSyntax_withErrors() async {
689-
// We should still be able to format syntactically valid code even if it has analysis
690-
// errors.
691-
const contents = '''void f() {
702+
// We should still be able to format syntactically valid code even if it has
703+
// analysis errors.
704+
const contents = '''
705+
void f() {
692706
print(a);
693707
}
694708
''';
695-
const expected = '''void f() {
709+
const expected = '''
710+
void f() {
696711
print(a);
697712
}
698713
''';

0 commit comments

Comments
 (0)