Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: pkg/analysis_server/test/services/correction/fix_test.dart

Issue 2615303002: Fix Quick Fix tests that used AnalysisContext. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/analysis_server/test/abstract_context.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.services.correction.fix; 5 library test.services.correction.fix;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart'; 9 import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart'; 10 import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
(...skipping 12 matching lines...) Expand all
23 import 'package:analyzer/src/generated/source.dart'; 23 import 'package:analyzer/src/generated/source.dart';
24 import 'package:test/test.dart'; 24 import 'package:test/test.dart';
25 import 'package:test_reflective_loader/test_reflective_loader.dart'; 25 import 'package:test_reflective_loader/test_reflective_loader.dart';
26 26
27 import '../../abstract_context.dart'; 27 import '../../abstract_context.dart';
28 import '../../abstract_single_unit.dart'; 28 import '../../abstract_single_unit.dart';
29 29
30 main() { 30 main() {
31 defineReflectiveSuite(() { 31 defineReflectiveSuite(() {
32 defineReflectiveTests(FixProcessorTest); 32 defineReflectiveTests(FixProcessorTest);
33 defineReflectiveTests(LintFixTest);
33 defineReflectiveTests(FixProcessorTest_Driver); 34 defineReflectiveTests(FixProcessorTest_Driver);
34 defineReflectiveTests(LintFixTest); 35 defineReflectiveTests(LintFixTest_Driver);
35 }); 36 });
36 } 37 }
37 38
38 typedef bool AnalysisErrorFilter(AnalysisError error); 39 typedef bool AnalysisErrorFilter(AnalysisError error);
39 40
40 /** 41 /**
41 * Base class for fix processor tests. 42 * Base class for fix processor tests.
42 */ 43 */
43 class BaseFixProcessorTest extends AbstractSingleUnitTest { 44 class BaseFixProcessorTest extends AbstractSingleUnitTest {
44 AnalysisErrorFilter errorFilter = (AnalysisError error) { 45 AnalysisErrorFilter errorFilter = (AnalysisError error) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 138
138 void _assertLinkedGroup(LinkedEditGroup group, List<String> expectedStrings, 139 void _assertLinkedGroup(LinkedEditGroup group, List<String> expectedStrings,
139 [List<LinkedEditSuggestion> expectedSuggestions]) { 140 [List<LinkedEditSuggestion> expectedSuggestions]) {
140 List<Position> expectedPositions = _findResultPositions(expectedStrings); 141 List<Position> expectedPositions = _findResultPositions(expectedStrings);
141 expect(group.positions, unorderedEquals(expectedPositions)); 142 expect(group.positions, unorderedEquals(expectedPositions));
142 if (expectedSuggestions != null) { 143 if (expectedSuggestions != null) {
143 expect(group.suggestions, unorderedEquals(expectedSuggestions)); 144 expect(group.suggestions, unorderedEquals(expectedSuggestions));
144 } 145 }
145 } 146 }
146 147
148 Future<List<AnalysisError>> _computeErrors() async {
149 if (enableNewAnalysisDriver) {
150 return (await driver.getResult(testFile)).errors;
151 } else {
152 return context.computeErrors(testSource);
153 }
154 }
155
147 /** 156 /**
148 * Computes fixes for the given [error] in [testUnit]. 157 * Computes fixes for the given [error] in [testUnit].
149 */ 158 */
150 Future<List<Fix>> _computeFixes(AnalysisError error) async { 159 Future<List<Fix>> _computeFixes(AnalysisError error) async {
151 if (enableNewAnalysisDriver) { 160 if (enableNewAnalysisDriver) {
152 DartFixContext fixContext = new _DartFixContextImpl( 161 DartFixContext fixContext = new _DartFixContextImpl(
153 provider, 162 provider,
154 driver.getTopLevelNameDeclarations, 163 driver.getTopLevelNameDeclarations,
155 resolutionMap.elementDeclaredByCompilationUnit(testUnit).context, 164 resolutionMap.elementDeclaredByCompilationUnit(testUnit).context,
156 testUnit, 165 testUnit,
(...skipping 12 matching lines...) Expand all
169 */ 178 */
170 void _configureMyPkg(Map<String, String> pathToCode) { 179 void _configureMyPkg(Map<String, String> pathToCode) {
171 pathToCode.forEach((path, code) { 180 pathToCode.forEach((path, code) {
172 provider.newFile('$myPkgLibPath/$path', code); 181 provider.newFile('$myPkgLibPath/$path', code);
173 }); 182 });
174 // configure SourceFactory 183 // configure SourceFactory
175 Folder myPkgFolder = provider.getResource(myPkgLibPath); 184 Folder myPkgFolder = provider.getResource(myPkgLibPath);
176 UriResolver pkgResolver = new PackageMapUriResolver(provider, { 185 UriResolver pkgResolver = new PackageMapUriResolver(provider, {
177 'my_pkg': [myPkgFolder] 186 'my_pkg': [myPkgFolder]
178 }); 187 });
179 context.sourceFactory = new SourceFactory( 188 SourceFactory sourceFactory = new SourceFactory(
180 [AbstractContextTest.SDK_RESOLVER, pkgResolver, resourceResolver]); 189 [AbstractContextTest.SDK_RESOLVER, pkgResolver, resourceResolver]);
190 if (enableNewAnalysisDriver) {
191 driver.configure(sourceFactory: sourceFactory);
192 } else {
193 context.sourceFactory = sourceFactory;
194 }
181 // force 'my_pkg' resolution 195 // force 'my_pkg' resolution
182 addSource( 196 addSource(
183 '/tmp/other.dart', 197 '/tmp/other.dart',
184 pathToCode.keys 198 pathToCode.keys
185 .map((path) => "import 'package:my_pkg/$path';") 199 .map((path) => "import 'package:my_pkg/$path';")
186 .join('\n')); 200 .join('\n'));
187 } 201 }
188 202
189 Future<AnalysisError> _findErrorToFix() async { 203 Future<AnalysisError> _findErrorToFix() async {
190 List<AnalysisError> errors; 204 List<AnalysisError> errors = await _computeErrors();
191 if (enableNewAnalysisDriver) {
192 errors = (await driver.getResult(testFile)).errors;
193 } else {
194 errors = context.computeErrors(testSource);
195 }
196 if (errorFilter != null) { 205 if (errorFilter != null) {
197 errors = errors.where(errorFilter).toList(); 206 errors = errors.where(errorFilter).toList();
198 } 207 }
199 expect(errors, hasLength(1)); 208 expect(errors, hasLength(1));
200 return errors[0]; 209 return errors[0];
201 } 210 }
202 211
203 List<Position> _findResultPositions(List<String> searchStrings) { 212 List<Position> _findResultPositions(List<String> searchStrings) {
204 List<Position> positions = <Position>[]; 213 List<Position> positions = <Position>[];
205 for (String search in searchStrings) { 214 for (String search in searchStrings) {
206 int offset = resultCode.indexOf(search); 215 int offset = resultCode.indexOf(search);
207 positions.add(new Position(testFile, offset)); 216 positions.add(new Position(testFile, offset));
208 } 217 }
209 return positions; 218 return positions;
210 } 219 }
211
212 void _performAnalysis() {
213 while (context.performAnalysisTask().hasMoreWork);
214 }
215 } 220 }
216 221
217 @reflectiveTest 222 @reflectiveTest
218 class FixProcessorTest extends BaseFixProcessorTest { 223 class FixProcessorTest extends BaseFixProcessorTest {
219 test_addFieldFormalParameters_hasRequiredParameter() async { 224 test_addFieldFormalParameters_hasRequiredParameter() async {
220 await resolveTestUnit(''' 225 await resolveTestUnit('''
221 class Test { 226 class Test {
222 final int a; 227 final int a;
223 final int b; 228 final int b;
224 final int c; 229 final int c;
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 // Comment first. 430 // Comment first.
426 // Comment second. 431 // Comment second.
427 432
428 class A {} 433 class A {}
429 '''; 434 ''';
430 addSource('/part.dart', partCode); 435 addSource('/part.dart', partCode);
431 await resolveTestUnit(''' 436 await resolveTestUnit('''
432 library my.lib; 437 library my.lib;
433 part 'part.dart'; 438 part 'part.dart';
434 '''); 439 ''');
435 _performAnalysis(); 440 performAllAnalysisTasks();
436 AnalysisError error = await _findErrorToFix(); 441 AnalysisError error = await _findErrorToFix();
437 fix = await _assertHasFix(DartFixKind.ADD_PART_OF, error); 442 fix = await _assertHasFix(DartFixKind.ADD_PART_OF, error);
438 change = fix.change; 443 change = fix.change;
439 // apply to "file" 444 // apply to "file"
440 List<SourceFileEdit> fileEdits = change.edits; 445 List<SourceFileEdit> fileEdits = change.edits;
441 expect(fileEdits, hasLength(1)); 446 expect(fileEdits, hasLength(1));
442 SourceFileEdit fileEdit = change.edits[0]; 447 SourceFileEdit fileEdit = change.edits[0];
443 expect(fileEdit.file, '/part.dart'); 448 expect(fileEdit.file, '/part.dart');
444 expect( 449 expect(
445 SourceEdit.applySequence(partCode, fileEdit.edits), 450 SourceEdit.applySequence(partCode, fileEdit.edits),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 await assertNoFix(DartFixKind.ADD_ASYNC); 486 await assertNoFix(DartFixKind.ADD_ASYNC);
482 } 487 }
483 488
484 test_addSync_blockFunctionBody() async { 489 test_addSync_blockFunctionBody() async {
485 await resolveTestUnit(''' 490 await resolveTestUnit('''
486 foo() {} 491 foo() {}
487 main() { 492 main() {
488 await foo(); 493 await foo();
489 } 494 }
490 '''); 495 ''');
491 List<AnalysisError> errors = context.computeErrors(testSource); 496 List<AnalysisError> errors = await _computeErrors();
492 expect(errors, hasLength(2)); 497 expect(errors, hasLength(2));
493 errors.sort((a, b) => a.message.compareTo(b.message)); 498 errors.sort((a, b) => a.message.compareTo(b.message));
494 // No fix for ";". 499 // No fix for ";".
495 { 500 {
496 AnalysisError error = errors[0]; 501 AnalysisError error = errors[0];
497 expect(error.message, "Expected to find ';'."); 502 expect(error.message, "Expected to find ';'.");
498 List<Fix> fixes = await _computeFixes(error); 503 List<Fix> fixes = await _computeFixes(error);
499 expect(fixes, isEmpty); 504 expect(fixes, isEmpty);
500 } 505 }
501 // Has fix for "await". 506 // Has fix for "await".
(...skipping 3317 matching lines...) Expand 10 before | Expand all | Expand 10 after
3819 } 3824 }
3820 } 3825 }
3821 '''); 3826 ''');
3822 } 3827 }
3823 3828
3824 test_noException_1() async { 3829 test_noException_1() async {
3825 await resolveTestUnit(''' 3830 await resolveTestUnit('''
3826 main(p) { 3831 main(p) {
3827 p i s Null; 3832 p i s Null;
3828 }'''); 3833 }''');
3829 List<AnalysisError> errors = context.computeErrors(testSource); 3834 List<AnalysisError> errors = await _computeErrors();
3830 for (var error in errors) { 3835 for (var error in errors) {
3831 await _computeFixes(error); 3836 await _computeFixes(error);
3832 } 3837 }
3833 } 3838 }
3834 3839
3835 test_nonBoolCondition_addNotNull() async { 3840 test_nonBoolCondition_addNotNull() async {
3836 await resolveTestUnit(''' 3841 await resolveTestUnit('''
3837 main(String p) { 3842 main(String p) {
3838 if (p) { 3843 if (p) {
3839 print(p); 3844 print(p);
(...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after
5395 } 5400 }
5396 5401
5397 @failingTest 5402 @failingTest
5398 @override 5403 @override
5399 test_addPartOfDirective() { 5404 test_addPartOfDirective() {
5400 return super.test_addPartOfDirective(); 5405 return super.test_addPartOfDirective();
5401 } 5406 }
5402 5407
5403 @failingTest 5408 @failingTest
5404 @override 5409 @override
5405 test_addSync_blockFunctionBody() {
5406 return super.test_addSync_blockFunctionBody();
5407 }
5408
5409 @failingTest
5410 @override
5411 test_createFile_forPart_inPackageLib() { 5410 test_createFile_forPart_inPackageLib() {
5412 return super.test_createFile_forPart_inPackageLib(); 5411 return super.test_createFile_forPart_inPackageLib();
5413 } 5412 }
5414 5413
5415 @failingTest 5414 @failingTest
5416 @override 5415 @override
5417 test_importLibraryPackage_preferDirectOverExport() {
5418 return super.test_importLibraryPackage_preferDirectOverExport();
5419 }
5420
5421 @failingTest
5422 @override
5423 test_importLibraryPackage_preferDirectOverExport_src() {
5424 return super.test_importLibraryPackage_preferDirectOverExport_src();
5425 }
5426
5427 @failingTest
5428 @override
5429 test_importLibraryPackage_preferPublicOverPrivate() {
5430 return super.test_importLibraryPackage_preferPublicOverPrivate();
5431 }
5432
5433 @failingTest
5434 @override
5435 test_importLibraryProject_withClass_annotation() {
5436 return super.test_importLibraryProject_withClass_annotation();
5437 }
5438
5439 @failingTest
5440 @override
5441 test_importLibraryProject_withClass_constInstanceCreation() {
5442 return super.test_importLibraryProject_withClass_constInstanceCreation();
5443 }
5444
5445 @failingTest
5446 @override
5447 test_importLibraryProject_withClass_hasOtherLibraryWithPrefix() {
5448 return super
5449 .test_importLibraryProject_withClass_hasOtherLibraryWithPrefix();
5450 }
5451
5452 @failingTest
5453 @override
5454 test_importLibraryProject_withClass_inParentFolder() {
5455 return super.test_importLibraryProject_withClass_inParentFolder();
5456 }
5457
5458 @failingTest
5459 @override
5460 test_importLibraryProject_withClass_inRelativeFolder() {
5461 return super.test_importLibraryProject_withClass_inRelativeFolder();
5462 }
5463
5464 @failingTest
5465 @override
5466 test_importLibraryProject_withClass_inSameFolder() {
5467 return super.test_importLibraryProject_withClass_inSameFolder();
5468 }
5469
5470 @failingTest
5471 @override
5472 test_importLibraryProject_withFunction() {
5473 return super.test_importLibraryProject_withFunction();
5474 }
5475
5476 @failingTest
5477 @override
5478 test_importLibraryProject_withFunction_unresolvedMethod() {
5479 return super.test_importLibraryProject_withFunction_unresolvedMethod();
5480 }
5481
5482 @failingTest
5483 @override
5484 test_importLibraryProject_withFunctionTypeAlias() {
5485 return super.test_importLibraryProject_withFunctionTypeAlias();
5486 }
5487
5488 @failingTest
5489 @override
5490 test_importLibraryProject_withTopLevelVariable() {
5491 return super.test_importLibraryProject_withTopLevelVariable();
5492 }
5493
5494 @failingTest
5495 @override
5496 test_importLibrarySdk_withClass_itemOfList() {
5497 return super.test_importLibrarySdk_withClass_itemOfList();
5498 }
5499
5500 @failingTest
5501 @override
5502 test_importLibrarySdk_withTopLevelVariable() {
5503 return super.test_importLibrarySdk_withTopLevelVariable();
5504 }
5505
5506 @failingTest
5507 @override
5508 test_importLibrarySdk_withTopLevelVariable_annotation() {
5509 return super.test_importLibrarySdk_withTopLevelVariable_annotation();
5510 }
5511
5512 @failingTest
5513 @override
5514 test_importLibraryShow_project() {
5515 return super.test_importLibraryShow_project();
5516 }
5517
5518 @failingTest
5519 @override
5520 test_noException_1() {
5521 return super.test_noException_1();
5522 }
5523
5524 @failingTest
5525 @override
5526 test_replaceImportUri_inProject() { 5416 test_replaceImportUri_inProject() {
5527 return super.test_replaceImportUri_inProject(); 5417 return super.test_replaceImportUri_inProject();
5528 } 5418 }
5529 5419
5530 @failingTest 5420 @failingTest
5531 @override 5421 @override
5532 test_replaceImportUri_package() { 5422 test_replaceImportUri_package() {
5533 return super.test_replaceImportUri_package(); 5423 return super.test_replaceImportUri_package();
5534 } 5424 }
5535 } 5425 }
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
5755 print('v: $v'); 5645 print('v: $v');
5756 } 5646 }
5757 '''); 5647 ''');
5758 } 5648 }
5759 5649
5760 void verifyResult(String expectedResult) { 5650 void verifyResult(String expectedResult) {
5761 expect(resultCode, expectedResult); 5651 expect(resultCode, expectedResult);
5762 } 5652 }
5763 } 5653 }
5764 5654
5655 @reflectiveTest
5656 class LintFixTest_Driver extends LintFixTest {
5657 @override
5658 bool get enableNewAnalysisDriver => true;
5659 }
5660
5765 class _DartFixContextImpl implements DartFixContext { 5661 class _DartFixContextImpl implements DartFixContext {
5766 @override 5662 @override
5767 final ResourceProvider resourceProvider; 5663 final ResourceProvider resourceProvider;
5768 5664
5769 @override 5665 @override
5770 final GetTopLevelDeclarations getTopLevelDeclarations; 5666 final GetTopLevelDeclarations getTopLevelDeclarations;
5771 5667
5772 @override 5668 @override
5773 final AnalysisContext analysisContext; 5669 final AnalysisContext analysisContext;
5774 5670
5775 @override 5671 @override
5776 final CompilationUnit unit; 5672 final CompilationUnit unit;
5777 5673
5778 @override 5674 @override
5779 final AnalysisError error; 5675 final AnalysisError error;
5780 5676
5781 _DartFixContextImpl(this.resourceProvider, this.getTopLevelDeclarations, 5677 _DartFixContextImpl(this.resourceProvider, this.getTopLevelDeclarations,
5782 this.analysisContext, this.unit, this.error); 5678 this.analysisContext, this.unit, this.error);
5783 } 5679 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/abstract_context.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698