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

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

Issue 562333002: Issue 17024. Quick Fix to find imported library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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 | Annotate | Revision Log
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 'package:analysis_server/src/protocol.dart' hide AnalysisError; 7 import 'package:analysis_server/src/protocol.dart' hide AnalysisError;
8 import 'package:analysis_server/src/services/correction/fix.dart'; 8 import 'package:analysis_server/src/services/correction/fix.dart';
9 import 'package:analysis_server/src/services/index/index.dart'; 9 import 'package:analysis_server/src/services/index/index.dart';
10 import 'package:analysis_server/src/services/index/local_memory_index.dart'; 10 import 'package:analysis_server/src/services/index/local_memory_index.dart';
(...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 } 849 }
850 '''); 850 ''');
851 assertHasFix(FixKind.INSERT_SEMICOLON, ''' 851 assertHasFix(FixKind.INSERT_SEMICOLON, '''
852 main() { 852 main() {
853 print(0); 853 print(0);
854 } 854 }
855 '''); 855 ''');
856 } 856 }
857 857
858 void test_importLibraryPackage_withType() { 858 void test_importLibraryPackage_withType() {
859 provider.newFile('/packages/my_pkg/lib/my_lib.dart', ''' 859 _configureMyPkg('''
860 library my_lib; 860 library my_lib;
861 class Test {} 861 class Test {}
862 '''); 862 ''');
863 {
864 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib');
865 UriResolver pkgResolver = new PackageMapUriResolver(provider, {
866 'my_pkg': [myPkgFolder]
867 });
868 context.sourceFactory = new SourceFactory(
869 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]);
870 }
871 // force 'my_pkg' resolution
872 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';");
873 // try to find a fix 863 // try to find a fix
874 _indexTestUnit(''' 864 _indexTestUnit('''
875 main() { 865 main() {
876 Test test = null; 866 Test test = null;
877 } 867 }
878 '''); 868 ''');
879 performAllAnalysisTasks(); 869 performAllAnalysisTasks();
880 assertHasFix(FixKind.IMPORT_LIBRARY_PROJECT, ''' 870 assertHasFix(FixKind.IMPORT_LIBRARY_PROJECT, '''
881 import 'package:my_pkg/my_lib.dart'; 871 import 'package:my_pkg/my_lib.dart';
882 872
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 'dart:math'; 1269 'dart:math';
1280 main() { 1270 main() {
1281 } 1271 }
1282 '''); 1272 ''');
1283 assertHasFix(FixKind.REMOVE_UNUSED_IMPORT, ''' 1273 assertHasFix(FixKind.REMOVE_UNUSED_IMPORT, '''
1284 main() { 1274 main() {
1285 } 1275 }
1286 '''); 1276 ''');
1287 } 1277 }
1288 1278
1279 void test_replaceImportUri_inProject() {
1280 testFile = '/project/bin/test.dart';
1281 addSource('/project/foo/bar/lib.dart', '');
1282 _indexTestUnit('''
1283 import 'no/matter/lib.dart';
1284 ''');
1285 performAllAnalysisTasks();
1286 assertHasFix(FixKind.REPLACE_IMPORT_URI, '''
1287 import '../foo/bar/lib.dart';
1288 ''');
1289 }
1290
1291 void test_replaceImportUri_package() {
1292 _configureMyPkg('');
1293 _indexTestUnit('''
1294 import 'no/matter/my_lib.dart';
1295 ''');
1296 performAllAnalysisTasks();
1297 assertHasFix(FixKind.REPLACE_IMPORT_URI, '''
1298 import 'package:my_pkg/my_lib.dart';
1299 ''');
1300 }
1301
1289 void test_replaceWithConstInstanceCreation() { 1302 void test_replaceWithConstInstanceCreation() {
1290 _indexTestUnit(''' 1303 _indexTestUnit('''
1291 class A { 1304 class A {
1292 const A(); 1305 const A();
1293 } 1306 }
1294 const a = new A(); 1307 const a = new A();
1295 '''); 1308 ''');
1296 assertHasFix(FixKind.USE_CONST, ''' 1309 assertHasFix(FixKind.USE_CONST, '''
1297 class A { 1310 class A {
1298 const A(); 1311 const A();
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
1840 1853
1841 void _assertLinkedGroup(LinkedEditGroup group, List<String> expectedStrings, 1854 void _assertLinkedGroup(LinkedEditGroup group, List<String> expectedStrings,
1842 [List<LinkedEditSuggestion> expectedSuggestions]) { 1855 [List<LinkedEditSuggestion> expectedSuggestions]) {
1843 List<Position> expectedPositions = _findResultPositions(expectedStrings); 1856 List<Position> expectedPositions = _findResultPositions(expectedStrings);
1844 expect(group.positions, unorderedEquals(expectedPositions)); 1857 expect(group.positions, unorderedEquals(expectedPositions));
1845 if (expectedSuggestions != null) { 1858 if (expectedSuggestions != null) {
1846 expect(group.suggestions, unorderedEquals(expectedSuggestions)); 1859 expect(group.suggestions, unorderedEquals(expectedSuggestions));
1847 } 1860 }
1848 } 1861 }
1849 1862
1863 /**
1864 * Configures the [SourceFactory] to have the `my_pkg` package in
1865 * `/packages/my_pkg/lib` folder.
1866 */
1867 void _configureMyPkg(String myLibCode) {
1868 provider.newFile('/packages/my_pkg/lib/my_lib.dart', myLibCode);
1869 // configure SourceFactory
1870 Folder myPkgFolder = provider.getResource('/packages/my_pkg/lib');
1871 UriResolver pkgResolver = new PackageMapUriResolver(provider, {
1872 'my_pkg': [myPkgFolder]
1873 });
1874 context.sourceFactory = new SourceFactory(
1875 [AbstractContextTest.SDK_RESOLVER, resourceResolver, pkgResolver]);
1876 // force 'my_pkg' resolution
1877 addSource('/tmp/other.dart', "import 'package:my_pkg/my_lib.dart';");
1878 }
1879
1850 AnalysisError _findErrorToFix() { 1880 AnalysisError _findErrorToFix() {
1851 List<AnalysisError> errors = context.computeErrors(testSource); 1881 List<AnalysisError> errors = context.computeErrors(testSource);
1852 expect( 1882 expect(
1853 errors, 1883 errors,
1854 hasLength(1), 1884 hasLength(1),
1855 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' + 1885 reason: 'Exactly 1 error expected, but ${errors.length} found:\n' +
1856 errors.join('\n')); 1886 errors.join('\n'));
1857 return errors[0]; 1887 return errors[0];
1858 } 1888 }
1859 1889
1860 List<Position> _findResultPositions(List<String> searchStrings) { 1890 List<Position> _findResultPositions(List<String> searchStrings) {
1861 List<Position> positions = <Position>[]; 1891 List<Position> positions = <Position>[];
1862 for (String search in searchStrings) { 1892 for (String search in searchStrings) {
1863 int offset = resultCode.indexOf(search); 1893 int offset = resultCode.indexOf(search);
1864 int length = getLeadingIdentifierLength(search); 1894 int length = getLeadingIdentifierLength(search);
1865 positions.add(new Position(testFile, offset)); 1895 positions.add(new Position(testFile, offset));
1866 } 1896 }
1867 return positions; 1897 return positions;
1868 } 1898 }
1869 1899
1870 void _indexTestUnit(String code) { 1900 void _indexTestUnit(String code) {
1871 resolveTestUnit(code); 1901 resolveTestUnit(code);
1872 index.indexUnit(context, testUnit); 1902 index.indexUnit(context, testUnit);
1873 } 1903 }
1874 } 1904 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698