Skip to content

Commit 38d8834

Browse files
bwilkersonCommit Queue
authored andcommitted
[cq] Reduce assignments to testCode to 1
The purpose for this change is to make it easier to use `TestCode` to parse the code under test. This, in turn, will allow us to use the same markdown in all of our test code, making it easier to write and read our tests. This biggest blocker to that goal is the fact that we assign to `testCode` in several places, and that's the problem solved by this change. Change-Id: Id88463925587ff0839fe75faf6a1d083a1b3320c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415700 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent ec7c8e1 commit 38d8834

File tree

5 files changed

+71
-103
lines changed

5 files changed

+71
-103
lines changed

pkg/analysis_server/test/abstract_single_unit.dart

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,24 @@ class AbstractSingleUnitTest extends AbstractContextTest {
4343
return offset;
4444
}
4545

46-
@override
47-
Future<ParsedUnitResult> getParsedUnit(File file) async {
48-
var unitResult = await super.getParsedUnit(file);
49-
testParsedResult = unitResult;
50-
testCode = unitResult.content;
51-
testUnit = unitResult.unit;
52-
findNode = FindNode(testCode, testUnit);
53-
findElement2 = FindElement2(testUnit);
54-
return unitResult;
55-
}
56-
5746
@override
5847
Future<ResolvedUnitResult> getResolvedUnit(File file) async {
5948
var session = await this.session;
60-
testLibraryResult = await session.getResolvedContainingLibrary(file.path);
61-
var unitResult = testLibraryResult?.unitWithPath(file.path);
49+
var libraryResult = await session.getResolvedContainingLibrary(file.path);
50+
var unitResult = libraryResult?.unitWithPath(file.path);
6251
unitResult ??= await super.getResolvedUnit(file);
63-
testAnalysisResult = unitResult;
64-
testCode = testAnalysisResult.content;
65-
testUnit = testAnalysisResult.unit;
52+
53+
if (file.path == testFilePath) {
54+
testLibraryResult = libraryResult;
55+
testAnalysisResult = unitResult;
56+
testUnit = unitResult.unit;
57+
testLibraryElement = testUnit.declaredFragment!.element;
58+
findNode = FindNode(unitResult.content, testUnit);
59+
findElement2 = FindElement2(testUnit);
60+
}
6661
if (verifyNoTestUnitErrors) {
6762
expect(
68-
testAnalysisResult.errors.where((AnalysisError error) {
63+
unitResult.errors.where((AnalysisError error) {
6964
return error.errorCode != WarningCode.DEAD_CODE &&
7065
error.errorCode != WarningCode.UNUSED_CATCH_CLAUSE &&
7166
error.errorCode != WarningCode.UNUSED_CATCH_STACK &&
@@ -77,16 +72,19 @@ class AbstractSingleUnitTest extends AbstractContextTest {
7772
isEmpty,
7873
);
7974
}
75+
return unitResult;
76+
}
8077

81-
testLibraryElement = testUnit.declaredFragment!.element;
78+
Future<void> parseTestCode(String code) async {
79+
addTestSource(code);
80+
testParsedResult = await getParsedUnit(testFile);
81+
testUnit = testParsedResult.unit;
8282
findNode = FindNode(testCode, testUnit);
8383
findElement2 = FindElement2(testUnit);
84-
return testAnalysisResult;
8584
}
8685

87-
Future<void> parseTestCode(String code) async {
88-
addTestSource(code);
89-
await getParsedUnit(testFile);
86+
void putTestFileInTestDir() {
87+
testFilePath = '$testPackageTestPath/test.dart';
9088
}
9189

9290
Future<void> resolveTestCode(String code) async {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,13 @@ class StatementCompletionTest extends AbstractSingleUnitTest {
7575
bool atEnd = false,
7676
int delta = 0,
7777
}) async {
78-
testCode = sourceCode.replaceAll('////', '');
78+
verifyNoTestUnitErrors = false;
79+
await resolveTestCode(sourceCode.replaceAll('////', ''));
7980
var offset = findOffset(search);
8081
if (atEnd) {
8182
delta = search.length;
8283
}
83-
await _prepareCompletionAt(offset + delta, testCode);
84-
}
85-
86-
Future<void> _prepareCompletionAt(int offset, String sourceCode) async {
87-
verifyNoTestUnitErrors = false;
88-
await resolveTestCode(sourceCode);
89-
await _computeCompletion(offset);
84+
await _computeCompletion(offset + delta);
9085
}
9186
}
9287

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -945,18 +945,14 @@ class NonLibraryAnnotation {
945945
}
946946

947947
Future<void> _computeUnitAndErrors(String code) async {
948-
addTestSource(code);
949948
verifyNoTestUnitErrors = false;
950-
var result = await getResolvedUnit(testFile);
951-
testUnit = result.unit;
952-
testErrors = result.errors;
949+
await resolveTestCode(code);
950+
testErrors = testAnalysisResult.errors;
953951
}
954952

955953
Future<void> _parseUnitAndErrors(String code) async {
956-
addTestSource(code);
957954
verifyNoTestUnitErrors = false;
958-
var result = await getParsedUnit(testFile);
959-
testUnit = result.unit;
960-
testErrors = result.errors;
955+
await parseTestCode(code);
956+
testErrors = testParsedResult.errors;
961957
}
962958
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,22 @@ void f() {
104104
}
105105

106106
Future<void> test_createChange_inOtherFile() async {
107-
var a = convertPath('$testPackageLibPath/a.dart');
108107
var b = convertPath('$testPackageLibPath/b.dart');
109108

110-
newFile(a, r'''
109+
addTestSource(r'''
111110
class A {
112111
A({test});
113112
}
114113
''');
115114
newFile(b, r'''
116-
import 'a.dart';
115+
import 'test.dart';
117116
118117
void f() {
119118
new A(test: 2);
120119
}
121120
''');
122121
await analyzeTestPackageFiles();
123122

124-
testFilePath = a;
125123
await resolveTestFile();
126124

127125
createRenameRefactoringAtString('test});');
@@ -134,7 +132,7 @@ class A {
134132
}
135133
''');
136134
assertFileChangeResult(b, '''
137-
import 'a.dart';
135+
import 'test.dart';
138136
139137
void f() {
140138
new A(newName: 2);

pkg/analysis_server/test/src/services/correction/fix/import_library_project_test.dart

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,63 +1293,31 @@ void f(Test t) {}
12931293
}
12941294

12951295
Future<void> test_withClass_pub_other_inTest_dependencies() async {
1296-
var aaaRoot = getFolder('$packagesRootPath/aaa');
1297-
newFile('${aaaRoot.path}/lib/a.dart', '''
1298-
class Test {}
1299-
''');
1300-
1301-
updateTestPubspecFile(r'''
1302-
name: test
1303-
dependencies:
1304-
aaa: any
1305-
''');
1306-
1307-
writeTestPackageConfig(
1308-
config:
1309-
PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRoot.path),
1310-
);
1311-
1312-
var b = newFile('$testPackageTestPath/b.dart', r'''
1296+
_createPackageAaa();
1297+
putTestFileInTestDir();
1298+
await resolveTestCode(r'''
13131299
void f(Test t) {}
13141300
''');
13151301

1316-
await getResolvedUnit(b);
1317-
13181302
await assertHasFix('''
13191303
import 'package:aaa/a.dart';
13201304
13211305
void f(Test t) {}
1322-
''', target: b.path);
1306+
''');
13231307
}
13241308

13251309
Future<void> test_withClass_pub_other_inTest_devDependencies() async {
1326-
var aaaRoot = getFolder('$packagesRootPath/aaa');
1327-
newFile('${aaaRoot.path}/lib/a.dart', '''
1328-
class Test {}
1329-
''');
1330-
1331-
updateTestPubspecFile(r'''
1332-
name: test
1333-
dev_dependencies:
1334-
aaa: any
1335-
''');
1336-
1337-
writeTestPackageConfig(
1338-
config:
1339-
PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRoot.path),
1340-
);
1341-
1342-
var b = newFile('$testPackageTestPath/b.dart', r'''
1310+
_createPackageAaa();
1311+
putTestFileInTestDir();
1312+
await resolveTestCode(r'''
13431313
void f(Test t) {}
13441314
''');
13451315

1346-
await getResolvedUnit(b);
1347-
13481316
await assertHasFix('''
13491317
import 'package:aaa/a.dart';
13501318
13511319
void f(Test t) {}
1352-
''', target: b.path);
1320+
''');
13531321
}
13541322

13551323
Future<void> test_withClass_pub_this() async {
@@ -1396,17 +1364,16 @@ name: test
13961364
class Test {}
13971365
''');
13981366

1399-
var b = newFile('$testPackageTestPath/b.dart', r'''
1367+
putTestFileInTestDir();
1368+
await resolveTestCode(r'''
14001369
void f(Test t) {}
14011370
''');
14021371

1403-
await getResolvedUnit(b);
1404-
14051372
await assertHasFix('''
14061373
import 'a.dart';
14071374
14081375
void f(Test t) {}
1409-
''', target: b.path);
1376+
''');
14101377
}
14111378

14121379
Future<void> test_withClass_simpleIdentifier_lowerCase() async {
@@ -1777,6 +1744,24 @@ void f() {
17771744
}
17781745
''');
17791746
}
1747+
1748+
void _createPackageAaa() {
1749+
var aaaRoot = getFolder('$packagesRootPath/aaa');
1750+
newFile('${aaaRoot.path}/lib/a.dart', '''
1751+
class Test {}
1752+
''');
1753+
1754+
updateTestPubspecFile(r'''
1755+
name: test
1756+
dependencies:
1757+
aaa: any
1758+
''');
1759+
1760+
writeTestPackageConfig(
1761+
config:
1762+
PackageConfigFileBuilder()..add(name: 'aaa', rootPath: aaaRoot.path),
1763+
);
1764+
}
17801765
}
17811766

17821767
@reflectiveTest
@@ -2489,17 +2474,16 @@ name: test
24892474
class Test {}
24902475
''');
24912476

2492-
var b = newFile('$testPackageTestPath/b.dart', r'''
2477+
putTestFileInTestDir();
2478+
await resolveTestCode(r'''
24932479
void f(lib.Test t) {}
24942480
''');
24952481

2496-
await getResolvedUnit(b);
2497-
24982482
await assertHasFix('''
24992483
import 'package:test/src/a.dart' as lib;
25002484
25012485
void f(lib.Test t) {}
2502-
''', target: b.path);
2486+
''');
25032487
}
25042488
}
25052489

@@ -2557,17 +2541,16 @@ name: test
25572541
class Test {}
25582542
''');
25592543

2560-
var b = newFile('$testPackageTestPath/b.dart', r'''
2544+
putTestFileInTestDir();
2545+
await resolveTestCode(r'''
25612546
void f(lib.Test t) {}
25622547
''');
25632548

2564-
await getResolvedUnit(b);
2565-
25662549
await assertHasFix('''
25672550
import 'package:test/src/a.dart' as lib show Test;
25682551
25692552
void f(lib.Test t) {}
2570-
''', target: b.path);
2553+
''');
25712554
}
25722555
}
25732556

@@ -2642,17 +2625,16 @@ name: test
26422625
class Test {}
26432626
''');
26442627

2645-
var b = newFile('$testPackageTestPath/b.dart', r'''
2628+
putTestFileInTestDir();
2629+
await resolveTestCode(r'''
26462630
void f(Test t) {}
26472631
''');
26482632

2649-
await getResolvedUnit(b);
2650-
26512633
await assertHasFix('''
26522634
import 'package:test/src/a.dart';
26532635
26542636
void f(Test t) {}
2655-
''', target: b.path);
2637+
''');
26562638
}
26572639
}
26582640

@@ -2710,16 +2692,15 @@ name: test
27102692
class Test {}
27112693
''');
27122694

2713-
var b = newFile('$testPackageTestPath/b.dart', r'''
2695+
putTestFileInTestDir();
2696+
await resolveTestCode(r'''
27142697
void f(Test t) {}
27152698
''');
27162699

2717-
await getResolvedUnit(b);
2718-
27192700
await assertHasFix('''
27202701
import 'package:test/src/a.dart' show Test;
27212702
27222703
void f(Test t) {}
2723-
''', target: b.path);
2704+
''');
27242705
}
27252706
}

0 commit comments

Comments
 (0)