Skip to content

Commit f276d3c

Browse files
FMorschelbwilkerson
authored andcommitted
[cq] Using TestCode and normalizeSource for quick-fix tests
This change introduces `TestCode` and `normalizeSource` into the tests for quick fixes. The goal is to add these capabilities to `AbstractSingleUnitTest`, allowing a future update to replace offsets in tests with markers. Bug: #60234 Change-Id: I760e6b5fb13b37d4e35278073a78f4d755deda64 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415260 Reviewed-by: Phil Quitslund <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent f6cf08b commit f276d3c

File tree

7 files changed

+195
-101
lines changed

7 files changed

+195
-101
lines changed

pkg/analysis_server/test/abstract_single_unit.dart

+25-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/src/error/codes.g.dart';
1111
import 'package:analyzer/src/test_utilities/find_element2.dart';
1212
import 'package:analyzer/src/test_utilities/find_node.dart';
13+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
1314
import 'package:analyzer/src/utilities/extensions/analysis_session.dart';
1415
import 'package:test/test.dart';
1516

@@ -18,19 +19,32 @@ import 'abstract_context.dart';
1819
class AbstractSingleUnitTest extends AbstractContextTest {
1920
bool verifyNoTestUnitErrors = true;
2021

21-
late String testCode;
22+
TestCode? _parsedTestCode;
2223
late ParsedUnitResult testParsedResult;
2324
late ResolvedLibraryResult? testLibraryResult;
2425
late ResolvedUnitResult testAnalysisResult;
2526
late CompilationUnit testUnit;
2627
late FindNode findNode;
2728
late FindElement2 findElement2;
28-
2929
late LibraryElement2 testLibraryElement;
30+
TestCode get parsedTestCode => _parsedTestCode!;
31+
set parsedTestCode(TestCode value) {
32+
if (_parsedTestCode != null) {
33+
throw ArgumentError(
34+
'parsedTestCode is already set to ${_parsedTestCode!.code}',
35+
);
36+
}
37+
_parsedTestCode = value;
38+
}
39+
40+
String get testCode => parsedTestCode.code;
41+
set testCode(String value) {
42+
parsedTestCode = TestCode.parse(normalizeSource(value));
43+
}
3044

3145
void addTestSource(String code) {
3246
testCode = code;
33-
newFile(testFile.path, code);
47+
newFile(testFile.path, testCode);
3448
}
3549

3650
int findEnd(String search) {
@@ -95,4 +109,12 @@ class AbstractSingleUnitTest extends AbstractContextTest {
95109
Future<void> resolveTestFile() async {
96110
await getResolvedUnit(testFile);
97111
}
112+
113+
void updateTestSource(String code) {
114+
if (_parsedTestCode == null) {
115+
throw StateError('testCode is not set');
116+
}
117+
_parsedTestCode = null;
118+
addTestSource(code);
119+
}
98120
}

pkg/analysis_server/test/services/completion/statement/statement_completion_test.dart

+17-2
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ void f() {
10511051
''', (s) => _afterLast(s, ' '));
10521052
}
10531053

1054-
Future<void> test_noCloseParenWithSemicolon() async {
1054+
Future<void> test_noCloseParenWithSemicolon1() async {
10551055
var before = '''
10561056
void f() {
10571057
var s = 'sample'.substring(3;
@@ -1063,13 +1063,28 @@ void f() {
10631063
////
10641064
}
10651065
''';
1066-
// Check completion both before and after the semicolon.
1066+
// Check completion both before semicolon.
10671067
await _prepareCompletion('ing(3', before, atEnd: true);
10681068
_assertHasChange(
10691069
'Insert a newline at the end of the current line',
10701070
after,
10711071
(s) => _afterLast(s, ' '),
10721072
);
1073+
}
1074+
1075+
Future<void> test_noCloseParenWithSemicolon2() async {
1076+
var before = '''
1077+
void f() {
1078+
var s = 'sample'.substring(3;
1079+
}
1080+
''';
1081+
var after = '''
1082+
void f() {
1083+
var s = 'sample'.substring(3);
1084+
////
1085+
}
1086+
''';
1087+
// Check completion after the semicolon.
10731088
await _prepareCompletion('ing(3;', before, atEnd: true);
10741089
_assertHasChange(
10751090
'Insert a newline at the end of the current line',

pkg/analysis_server/test/services/correction/organize_directives_test.dart

+77-17
Original file line numberDiff line numberDiff line change
@@ -318,25 +318,85 @@ void f() {
318318
}''', removeUnused: true);
319319
}
320320

321-
Future<void> test_remove_unusedImports_hasUnresolvedError() async {
322-
Future<void> check(String declaration) async {
323-
var code = '''
321+
Future<void> test_remove_unusedImports_hasUnresolvedError1() async {
322+
var code = '''
324323
import 'dart:async';
325-
$declaration
324+
void f() { Unresolved v; }
326325
''';
327-
await _computeUnitAndErrors(code);
328-
_assertOrganize(code, removeUnused: true);
329-
}
330-
331-
await check('void f() { Unresolved v; }');
332-
await check('void f() { new Unresolved(); }');
333-
await check('void f() { const Unresolved(); }');
334-
await check('void f() { unresolvedFunction(); }');
335-
await check('void f() { print(unresolvedVariable); }');
336-
await check('void f() { unresolvedVariable = 0; }');
337-
await check('void f() { Unresolved.field = 0; }');
338-
await check('class A extends Unresolved {}');
339-
await check('List<Unresolved> v;');
326+
await _computeUnitAndErrors(code);
327+
_assertOrganize(code, removeUnused: true);
328+
}
329+
330+
Future<void> test_remove_unusedImports_hasUnresolvedError2() async {
331+
var code = '''
332+
import 'dart:async';
333+
void f() { new Unresolved(); }
334+
''';
335+
await _computeUnitAndErrors(code);
336+
_assertOrganize(code, removeUnused: true);
337+
}
338+
339+
Future<void> test_remove_unusedImports_hasUnresolvedError3() async {
340+
var code = '''
341+
import 'dart:async';
342+
void f() { const Unresolved(); }
343+
''';
344+
await _computeUnitAndErrors(code);
345+
_assertOrganize(code, removeUnused: true);
346+
}
347+
348+
Future<void> test_remove_unusedImports_hasUnresolvedError4() async {
349+
var code = '''
350+
import 'dart:async';
351+
void f() { unresolvedFunction(); }
352+
''';
353+
await _computeUnitAndErrors(code);
354+
_assertOrganize(code, removeUnused: true);
355+
}
356+
357+
Future<void> test_remove_unusedImports_hasUnresolvedError5() async {
358+
var code = '''
359+
import 'dart:async';
360+
void f() { print(unresolvedVariable); }
361+
''';
362+
await _computeUnitAndErrors(code);
363+
_assertOrganize(code, removeUnused: true);
364+
}
365+
366+
Future<void> test_remove_unusedImports_hasUnresolvedError6() async {
367+
var code = '''
368+
import 'dart:async';
369+
void f() { unresolvedVariable = 0; }
370+
''';
371+
await _computeUnitAndErrors(code);
372+
_assertOrganize(code, removeUnused: true);
373+
}
374+
375+
Future<void> test_remove_unusedImports_hasUnresolvedError7() async {
376+
var code = '''
377+
import 'dart:async';
378+
void f() { Unresolved.field = 0; }
379+
''';
380+
await _computeUnitAndErrors(code);
381+
_assertOrganize(code, removeUnused: true);
382+
}
383+
384+
Future<void> test_remove_unusedImports_hasUnresolvedError8() async {
385+
var code = '''
386+
import 'dart:async';
387+
class A extends Unresolved {}
388+
''';
389+
await _computeUnitAndErrors(code);
390+
_assertOrganize(code, removeUnused: true);
391+
}
392+
393+
Future<void> test_remove_unusedImports_hasUnresolvedError9() async {
394+
var code = '''
395+
import 'dart:async';
396+
List<Unresolved> v;
397+
''';
398+
await _computeUnitAndErrors(code);
399+
_assertOrganize(code, removeUnused: true);
340400
}
341401

342402
Future<void> test_sort() async {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,15 @@ void f() {
411411
Future<void> test_const_inList_inPrefixExpression() async {
412412
await indexTestUnit('''
413413
void f() {
414-
const [!true, 2];
414+
const [ !true, 2];
415415
}
416416
''');
417417
_createRefactoringForString('true');
418418
// apply refactoring
419419
return _assertSuccessfulRefactoring('''
420420
void f() {
421421
const res = true;
422-
const [!res, 2];
422+
const [ !res, 2];
423423
}
424424
''');
425425
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ void two() {
10391039
expect(calls, isNotEmpty);
10401040

10411041
// Modify the file so that the target offset is no longer the original item.
1042-
addTestSource(testCode.replaceAll('one()', 'three()'));
1042+
updateTestSource(testCode.replaceAll('one()', 'three()'));
10431043

10441044
// Ensure there are now no results for the original target.
10451045
calls = await findIncomingCallsForTarget(target);
@@ -1790,7 +1790,7 @@ void two() {}
17901790
expect(calls, isNotEmpty);
17911791

17921792
// Modify the file so that the target offset is no longer the original item.
1793-
addTestSource(testCode.replaceAll('one()', 'three()'));
1793+
updateTestSource(testCode.replaceAll('one()', 'three()'));
17941794

17951795
// Ensure there are now no results for the original target.
17961796
calls = await findOutgoingCallsForTarget(target);

0 commit comments

Comments
 (0)