| 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'; |
| 11 import '../integration_tests.dart'; | 11 import '../integration_tests.dart'; |
| 12 | 12 |
| 13 @ReflectiveTestCase() | 13 @ReflectiveTestCase() |
| 14 class AnalysisNavigationTest extends AbstractAnalysisServerIntegrationTest { | 14 class AnalysisNavigationTest extends AbstractAnalysisServerIntegrationTest { |
| 15 test_navigation() { | 15 test_navigation() { |
| 16 String pathname1 = sourcePath('test1.dart'); | 16 String pathname1 = sourcePath('test1.dart'); |
| 17 String text1 = | 17 String text1 = r''' |
| 18 r''' | |
| 19 library foo; | 18 library foo; |
| 20 | 19 |
| 21 import 'dart:async'; | 20 import 'dart:async'; |
| 22 part 'test2.dart'; | 21 part 'test2.dart'; |
| 23 | 22 |
| 24 class Class<TypeParameter> { | 23 class Class<TypeParameter> { |
| 25 Class.constructor(); /* constructor declaration */ | 24 Class.constructor(); /* constructor declaration */ |
| 26 | 25 |
| 27 TypeParameter field; | 26 TypeParameter field; |
| 28 | 27 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 | 38 |
| 40 main() { | 39 main() { |
| 41 Class<int> localVariable = new Class<int>.constructor(); | 40 Class<int> localVariable = new Class<int>.constructor(); |
| 42 function(() => localVariable.field); | 41 function(() => localVariable.field); |
| 43 localVariable.method(); | 42 localVariable.method(); |
| 44 localVariable.field = 1; | 43 localVariable.field = 1; |
| 45 } | 44 } |
| 46 '''; | 45 '''; |
| 47 writeFile(pathname1, text1); | 46 writeFile(pathname1, text1); |
| 48 String pathname2 = sourcePath('test2.dart'); | 47 String pathname2 = sourcePath('test2.dart'); |
| 49 String text2 = | 48 String text2 = r''' |
| 50 r''' | |
| 51 part of foo; | 49 part of foo; |
| 52 '''; | 50 '''; |
| 53 writeFile(pathname2, text2); | 51 writeFile(pathname2, text2); |
| 54 standardAnalysisSetup(); | 52 standardAnalysisSetup(); |
| 55 sendAnalysisSetSubscriptions({AnalysisService.NAVIGATION: [pathname1]}); | 53 sendAnalysisSetSubscriptions({ |
| 56 List regions; | 54 AnalysisService.NAVIGATION: [pathname1] |
| 57 onAnalysisNavigation.listen((params) { | 55 }); |
| 58 expect(params['file'], equals(pathname1)); | 56 List<NavigationRegion> regions; |
| 59 regions = params['regions']; | 57 onAnalysisNavigation.listen((AnalysisNavigationParams params) { |
| 58 expect(params.file, equals(pathname1)); |
| 59 regions = params.regions; |
| 60 }); | 60 }); |
| 61 return analysisFinished.then((_) { | 61 return analysisFinished.then((_) { |
| 62 // There should be a single error, due to the fact that 'dart:async' is | 62 // There should be a single error, due to the fact that 'dart:async' is |
| 63 // not used. | 63 // not used. |
| 64 expect(currentAnalysisErrors[pathname1], hasLength(1)); | 64 expect(currentAnalysisErrors[pathname1], hasLength(1)); |
| 65 expect(currentAnalysisErrors[pathname2], isEmpty); | 65 expect(currentAnalysisErrors[pathname2], isEmpty); |
| 66 Map findTargetElement(int index) { | 66 Element findTargetElement(int index) { |
| 67 for (Map region in regions) { | 67 for (NavigationRegion region in regions) { |
| 68 if (region['offset'] <= index && index < region['offset'] + region['le
ngth']) { | 68 if (region.offset <= index && index < region.offset + region.length) { |
| 69 expect(region['targets'], hasLength(1)); | 69 expect(region.targets, hasLength(1)); |
| 70 return region['targets'][0]; | 70 return region.targets[0]; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 fail('No element found for index $index'); | 73 fail('No element found for index $index'); |
| 74 return null; | 74 return null; |
| 75 } | 75 } |
| 76 void checkLocal(String source, String expectedTarget, String expectedKind)
{ | 76 void checkLocal(String source, String expectedTarget, |
| 77 ElementKind expectedKind) { |
| 77 int sourceIndex = text1.indexOf(source); | 78 int sourceIndex = text1.indexOf(source); |
| 78 int targetIndex = text1.indexOf(expectedTarget); | 79 int targetIndex = text1.indexOf(expectedTarget); |
| 79 Map element = findTargetElement(sourceIndex); | 80 Element element = findTargetElement(sourceIndex); |
| 80 expect(element['location']['file'], equals(pathname1)); | 81 expect(element.location.file, equals(pathname1)); |
| 81 expect(element['location']['offset'], equals(targetIndex)); | 82 expect(element.location.offset, equals(targetIndex)); |
| 82 expect(element['kind'], equals(expectedKind)); | 83 expect(element.kind, equals(expectedKind)); |
| 83 } | 84 } |
| 84 void checkRemote(String source, String expectedTargetRegexp, String expect
edKind) { | 85 void checkRemote(String source, String expectedTargetRegexp, |
| 86 ElementKind expectedKind) { |
| 85 int sourceIndex = text1.indexOf(source); | 87 int sourceIndex = text1.indexOf(source); |
| 86 Map element = findTargetElement(sourceIndex); | 88 Element element = findTargetElement(sourceIndex); |
| 87 expect(element['location']['file'], matches(expectedTargetRegexp)); | 89 expect(element.location.file, matches(expectedTargetRegexp)); |
| 88 expect(element['kind'], equals(expectedKind)); | 90 expect(element.kind, equals(expectedKind)); |
| 89 } | 91 } |
| 90 // TODO(paulberry): will the element type 'CLASS_TYPE_ALIAS' ever appear | 92 // TODO(paulberry): will the element type 'CLASS_TYPE_ALIAS' ever appear |
| 91 // as a navigation target? | 93 // as a navigation target? |
| 92 checkLocal('Class<int>', 'Class<TypeParameter>', 'CLASS'); | 94 checkLocal('Class<int>', 'Class<TypeParameter>', ElementKind.CLASS); |
| 93 checkRemote("part 'test2.dart';", r'test2.dart$', 'COMPILATION_UNIT'); | 95 checkRemote( |
| 94 checkLocal('new Class<int>.constructor', 'constructor(); /* constructor de
claration */', 'CONSTRUCTOR'); | 96 "part 'test2.dart';", |
| 95 checkLocal('field;', 'field;', 'FIELD'); | 97 r'test2.dart$', |
| 96 checkLocal('function(() => localVariable.field)', 'function(FunctionTypeAl
ias parameter)', 'FUNCTION'); | 98 ElementKind.COMPILATION_UNIT); |
| 97 checkLocal('FunctionTypeAlias parameter', 'FunctionTypeAlias();', 'FUNCTIO
N_TYPE_ALIAS'); | 99 checkLocal( |
| 98 checkLocal('field)', 'field;', 'GETTER'); | 100 'new Class<int>.constructor', |
| 99 checkRemote("import 'dart:async'", r'async\.dart$', 'LIBRARY'); | 101 'constructor(); /* constructor declaration */', |
| 100 checkLocal('localVariable.field', 'localVariable =', 'LOCAL_VARIABLE'); | 102 ElementKind.CONSTRUCTOR); |
| 101 checkLocal('method();', 'method() {', 'METHOD'); | 103 checkLocal('field;', 'field;', ElementKind.FIELD); |
| 102 checkLocal('parameter());', 'parameter) {', 'PARAMETER'); | 104 checkLocal( |
| 103 checkLocal('field = 1', 'field;', 'SETTER'); | 105 'function(() => localVariable.field)', |
| 104 checkLocal('topLevelVariable;', 'topLevelVariable;', 'TOP_LEVEL_VARIABLE')
; | 106 'function(FunctionTypeAlias parameter)', |
| 105 checkLocal('TypeParameter field;', 'TypeParameter>', 'TYPE_PARAMETER'); | 107 ElementKind.FUNCTION); |
| 108 checkLocal( |
| 109 'FunctionTypeAlias parameter', |
| 110 'FunctionTypeAlias();', |
| 111 ElementKind.FUNCTION_TYPE_ALIAS); |
| 112 checkLocal('field)', 'field;', ElementKind.GETTER); |
| 113 checkRemote("import 'dart:async'", r'async\.dart$', ElementKind.LIBRARY); |
| 114 checkLocal( |
| 115 'localVariable.field', |
| 116 'localVariable =', |
| 117 ElementKind.LOCAL_VARIABLE); |
| 118 checkLocal('method();', 'method() {', ElementKind.METHOD); |
| 119 checkLocal('parameter());', 'parameter) {', ElementKind.PARAMETER); |
| 120 checkLocal('field = 1', 'field;', ElementKind.SETTER); |
| 121 checkLocal( |
| 122 'topLevelVariable;', |
| 123 'topLevelVariable;', |
| 124 ElementKind.TOP_LEVEL_VARIABLE); |
| 125 checkLocal( |
| 126 'TypeParameter field;', |
| 127 'TypeParameter>', |
| 128 ElementKind.TYPE_PARAMETER); |
| 106 }); | 129 }); |
| 107 } | 130 } |
| 108 } | 131 } |
| 109 | 132 |
| 110 main() { | 133 main() { |
| 111 runReflectiveTests(AnalysisNavigationTest); | 134 runReflectiveTests(AnalysisNavigationTest); |
| 112 } | 135 } |
| OLD | NEW |