| OLD | NEW |
| 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.integration.analysis.navigation; | 5 library test.integration.analysis.navigation; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 |
| 10 import '../../reflective_tests.dart'; | 10 import '../../reflective_tests.dart'; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 localVariable.field = 1; | 47 localVariable.field = 1; |
| 48 } | 48 } |
| 49 '''; | 49 '''; |
| 50 writeFile(pathname1, text1); | 50 writeFile(pathname1, text1); |
| 51 String pathname2 = sourcePath('test2.dart'); | 51 String pathname2 = sourcePath('test2.dart'); |
| 52 String text2 = r''' | 52 String text2 = r''' |
| 53 part of foo; | 53 part of foo; |
| 54 '''; | 54 '''; |
| 55 writeFile(pathname2, text2); | 55 writeFile(pathname2, text2); |
| 56 standardAnalysisSetup(); | 56 standardAnalysisSetup(); |
| 57 sendAnalysisSetSubscriptions({ | 57 sendAnalysisSetSubscriptions({AnalysisService.NAVIGATION: [pathname1]}); |
| 58 AnalysisService.NAVIGATION: [pathname1] | |
| 59 }); | |
| 60 List<NavigationRegion> regions; | 58 List<NavigationRegion> regions; |
| 61 List<NavigationTarget> targets; | 59 List<NavigationTarget> targets; |
| 62 List<String> targetFiles; | 60 List<String> targetFiles; |
| 63 onAnalysisNavigation.listen((AnalysisNavigationParams params) { | 61 onAnalysisNavigation.listen((AnalysisNavigationParams params) { |
| 64 expect(params.file, equals(pathname1)); | 62 expect(params.file, equals(pathname1)); |
| 65 regions = params.regions; | 63 regions = params.regions; |
| 66 targets = params.targets; | 64 targets = params.targets; |
| 67 targetFiles = params.files; | 65 targetFiles = params.files; |
| 68 }); | 66 }); |
| 69 return analysisFinished.then((_) { | 67 return analysisFinished.then((_) { |
| 70 // There should be a single error, due to the fact that 'dart:async' is | 68 // There should be a single error, due to the fact that 'dart:async' is |
| 71 // not used. | 69 // not used. |
| 72 expect(currentAnalysisErrors[pathname1], hasLength(1)); | 70 expect(currentAnalysisErrors[pathname1], hasLength(1)); |
| 73 expect(currentAnalysisErrors[pathname2], isEmpty); | 71 expect(currentAnalysisErrors[pathname2], isEmpty); |
| 74 NavigationTarget findTargetElement(int index) { | 72 NavigationTarget findTargetElement(int index) { |
| 75 for (NavigationRegion region in regions) { | 73 for (NavigationRegion region in regions) { |
| 76 if (region.offset <= index && index < region.offset + region.length) { | 74 if (region.offset <= index && index < region.offset + region.length) { |
| 77 expect(region.targets, hasLength(1)); | 75 expect(region.targets, hasLength(1)); |
| 78 int targetIndex = region.targets[0]; | 76 int targetIndex = region.targets[0]; |
| 79 return targets[targetIndex]; | 77 return targets[targetIndex]; |
| 80 } | 78 } |
| 81 } | 79 } |
| 82 fail('No element found for index $index'); | 80 fail('No element found for index $index'); |
| 83 return null; | 81 return null; |
| 84 } | 82 } |
| 85 void checkLocal(String source, String expectedTarget, | 83 void checkLocal( |
| 86 ElementKind expectedKind) { | 84 String source, String expectedTarget, ElementKind expectedKind) { |
| 87 int sourceIndex = text1.indexOf(source); | 85 int sourceIndex = text1.indexOf(source); |
| 88 int targetIndex = text1.indexOf(expectedTarget); | 86 int targetIndex = text1.indexOf(expectedTarget); |
| 89 NavigationTarget element = findTargetElement(sourceIndex); | 87 NavigationTarget element = findTargetElement(sourceIndex); |
| 90 expect(targetFiles[element.fileIndex], equals(pathname1)); | 88 expect(targetFiles[element.fileIndex], equals(pathname1)); |
| 91 expect(element.offset, equals(targetIndex)); | 89 expect(element.offset, equals(targetIndex)); |
| 92 expect(element.kind, equals(expectedKind)); | 90 expect(element.kind, equals(expectedKind)); |
| 93 } | 91 } |
| 94 void checkRemote(String source, String expectedTargetRegexp, | 92 void checkRemote(String source, String expectedTargetRegexp, |
| 95 ElementKind expectedKind) { | 93 ElementKind expectedKind) { |
| 96 int sourceIndex = text1.indexOf(source); | 94 int sourceIndex = text1.indexOf(source); |
| 97 NavigationTarget element = findTargetElement(sourceIndex); | 95 NavigationTarget element = findTargetElement(sourceIndex); |
| 98 expect(targetFiles[element.fileIndex], matches(expectedTargetRegexp)); | 96 expect(targetFiles[element.fileIndex], matches(expectedTargetRegexp)); |
| 99 expect(element.kind, equals(expectedKind)); | 97 expect(element.kind, equals(expectedKind)); |
| 100 } | 98 } |
| 101 // TODO(paulberry): will the element type 'CLASS_TYPE_ALIAS' ever appear | 99 // TODO(paulberry): will the element type 'CLASS_TYPE_ALIAS' ever appear |
| 102 // as a navigation target? | 100 // as a navigation target? |
| 103 checkLocal('Class<int>', 'Class<TypeParameter>', ElementKind.CLASS); | 101 checkLocal('Class<int>', 'Class<TypeParameter>', ElementKind.CLASS); |
| 104 checkRemote( | 102 checkRemote( |
| 105 "part 'test2.dart';", | 103 "part 'test2.dart';", r'test2.dart$', ElementKind.COMPILATION_UNIT); |
| 106 r'test2.dart$', | 104 checkLocal('new Class<int>.constructor', |
| 107 ElementKind.COMPILATION_UNIT); | |
| 108 checkLocal( | |
| 109 'new Class<int>.constructor', | |
| 110 'constructor(); /* constructor declaration */', | 105 'constructor(); /* constructor declaration */', |
| 111 ElementKind.CONSTRUCTOR); | 106 ElementKind.CONSTRUCTOR); |
| 112 checkLocal('field;', 'field;', ElementKind.FIELD); | 107 checkLocal('field;', 'field;', ElementKind.FIELD); |
| 113 checkLocal( | 108 checkLocal('function(() => localVariable.field)', |
| 114 'function(() => localVariable.field)', | 109 'function(FunctionTypeAlias parameter)', ElementKind.FUNCTION); |
| 115 'function(FunctionTypeAlias parameter)', | 110 checkLocal('FunctionTypeAlias parameter', 'FunctionTypeAlias();', |
| 116 ElementKind.FUNCTION); | |
| 117 checkLocal( | |
| 118 'FunctionTypeAlias parameter', | |
| 119 'FunctionTypeAlias();', | |
| 120 ElementKind.FUNCTION_TYPE_ALIAS); | 111 ElementKind.FUNCTION_TYPE_ALIAS); |
| 121 checkLocal('field)', 'field;', ElementKind.GETTER); | 112 checkLocal('field)', 'field;', ElementKind.GETTER); |
| 122 checkRemote("import 'dart:async'", r'async\.dart$', ElementKind.LIBRARY); | 113 checkRemote("import 'dart:async'", r'async\.dart$', ElementKind.LIBRARY); |
| 123 checkLocal( | 114 checkLocal( |
| 124 'localVariable.field', | 115 'localVariable.field', 'localVariable =', ElementKind.LOCAL_VARIABLE); |
| 125 'localVariable =', | |
| 126 ElementKind.LOCAL_VARIABLE); | |
| 127 checkLocal('method();', 'method() {', ElementKind.METHOD); | 116 checkLocal('method();', 'method() {', ElementKind.METHOD); |
| 128 checkLocal('parameter());', 'parameter) {', ElementKind.PARAMETER); | 117 checkLocal('parameter());', 'parameter) {', ElementKind.PARAMETER); |
| 129 checkLocal('field = 1', 'field;', ElementKind.SETTER); | 118 checkLocal('field = 1', 'field;', ElementKind.SETTER); |
| 130 checkLocal( | 119 checkLocal('topLevelVariable;', 'topLevelVariable;', |
| 131 'topLevelVariable;', | |
| 132 'topLevelVariable;', | |
| 133 ElementKind.TOP_LEVEL_VARIABLE); | 120 ElementKind.TOP_LEVEL_VARIABLE); |
| 134 checkLocal( | 121 checkLocal( |
| 135 'TypeParameter field;', | 122 'TypeParameter field;', 'TypeParameter>', ElementKind.TYPE_PARAMETER); |
| 136 'TypeParameter>', | |
| 137 ElementKind.TYPE_PARAMETER); | |
| 138 }); | 123 }); |
| 139 } | 124 } |
| 140 } | 125 } |
| OLD | NEW |