| 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.domain.analysis.notification.navigation; | 5 library test.domain.analysis.notification.navigation; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/computer/element.dart'; | 10 import 'package:analysis_server/src/computer/element.dart'; |
| 11 import 'package:analysis_server/src/constants.dart'; | 11 import 'package:analysis_server/src/constants.dart'; |
| 12 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 13 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 14 | 14 |
| 15 import 'analysis_abstract.dart'; | 15 import 'analysis_abstract.dart'; |
| 16 import 'reflective_tests.dart'; | 16 import 'reflective_tests.dart'; |
| 17 | 17 |
| 18 | 18 |
| 19 main() { | 19 main() { |
| 20 group('notification.navigation', () { | 20 group('notification.navigation', () { |
| 21 runReflectiveTests(_AnalysisNotificationNavigationTest); | 21 runReflectiveTests(AnalysisNotificationNavigationTest); |
| 22 }); | 22 }); |
| 23 } | 23 } |
| 24 | 24 |
| 25 | 25 |
| 26 @ReflectiveTestCase() | 26 @ReflectiveTestCase() |
| 27 class _AnalysisNotificationNavigationTest extends AbstractAnalysisTest { | 27 class AnalysisNotificationNavigationTest extends AbstractAnalysisTest { |
| 28 List<_NavigationRegion> regions; | 28 List<NavigationRegion> regions; |
| 29 _NavigationRegion testRegion; | 29 NavigationRegion testRegion; |
| 30 List<_NavigationTarget> testTargets; | 30 List<Element> testTargets; |
| 31 _NavigationTarget testTarget; | 31 Element testTarget; |
| 32 | 32 |
| 33 /** | 33 /** |
| 34 * Validates that there is a target in [testTargets] with [file], at [offset] | 34 * Validates that there is a target in [testTargets] with [file], at [offset] |
| 35 * and with the given [length]. | 35 * and with the given [length]. |
| 36 */ | 36 */ |
| 37 void assertHasFileTarget(String file, int offset, int length) { | 37 void assertHasFileTarget(String file, int offset, int length) { |
| 38 for (_NavigationTarget target in testTargets) { | 38 for (Element target in testTargets) { |
| 39 if (target.file == file && target.offset == offset && target.length == | 39 Location location = target.location; |
| 40 if (location.file == file && location.offset == offset && location.length
== |
| 40 length) { | 41 length) { |
| 41 testTarget = target; | 42 testTarget = target; |
| 42 return; | 43 return; |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 fail( | 46 fail( |
| 46 'Expected to find target (file=$file; offset=$offset; length=$length) in
\n' | 47 'Expected to find target (file=$file; offset=$offset; length=$length) in
\n' |
| 47 '${testRegion} in\n' '${regions.join('\n')}'); | 48 '${testRegion} in\n' '${regions.join('\n')}'); |
| 48 } | 49 } |
| 49 | 50 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 * Finds the navigation region with the given [offset] and [length]. | 129 * Finds the navigation region with the given [offset] and [length]. |
| 129 * If [length] is `-1`, then it is ignored. | 130 * If [length] is `-1`, then it is ignored. |
| 130 * | 131 * |
| 131 * If [exists] is `true`, then fails if such region does not exist. | 132 * If [exists] is `true`, then fails if such region does not exist. |
| 132 * Otherwise remembers this it into [testRegion]. | 133 * Otherwise remembers this it into [testRegion]. |
| 133 * Also fills [testTargets] with its targets. | 134 * Also fills [testTargets] with its targets. |
| 134 * | 135 * |
| 135 * If [exists] is `false`, then fails if such region exists. | 136 * If [exists] is `false`, then fails if such region exists. |
| 136 */ | 137 */ |
| 137 void findRegion(int offset, int length, [bool exists]) { | 138 void findRegion(int offset, int length, [bool exists]) { |
| 138 for (_NavigationRegion region in regions) { | 139 for (NavigationRegion region in regions) { |
| 139 if (region.offset == offset && (length == -1 || region.length == length)) | 140 if (region.offset == offset && (length == -1 || region.length == length)) |
| 140 { | 141 { |
| 141 if (exists == false) { | 142 if (exists == false) { |
| 142 fail('Not expected to find (offset=$offset; length=$length) in\n' | 143 fail('Not expected to find (offset=$offset; length=$length) in\n' |
| 143 '${regions.join('\n')}'); | 144 '${regions.join('\n')}'); |
| 144 } | 145 } |
| 145 testRegion = region; | 146 testRegion = region; |
| 146 testTargets = region.targets; | 147 testTargets = region.targets; |
| 147 return; | 148 return; |
| 148 } | 149 } |
| 149 } | 150 } |
| 150 if (exists == true) { | 151 if (exists == true) { |
| 151 fail('Expected to find (offset=$offset; length=$length) in\n' | 152 fail('Expected to find (offset=$offset; length=$length) in\n' |
| 152 '${regions.join('\n')}'); | 153 '${regions.join('\n')}'); |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 Future prepareNavigation(then()) { | 157 Future prepareNavigation(then()) { |
| 157 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile); | 158 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile); |
| 158 return waitForTasksFinished().then((_) { | 159 return waitForTasksFinished().then((_) { |
| 159 then(); | 160 then(); |
| 160 }); | 161 }); |
| 161 } | 162 } |
| 162 | 163 |
| 163 void processNotification(Notification notification) { | 164 void processNotification(Notification notification) { |
| 164 if (notification.event == ANALYSIS_NAVIGATION) { | 165 if (notification.event == ANALYSIS_NAVIGATION) { |
| 165 String file = notification.getParameter(FILE); | 166 String file = notification.getParameter(FILE); |
| 166 if (file == testFile) { | 167 if (file == testFile) { |
| 167 regions = []; | 168 regions = <NavigationRegion>[]; |
| 168 List<Map<String, Object>> regionsJson = notification.getParameter( | 169 List<Map<String, Object>> regionsJson = notification.getParameter( |
| 169 REGIONS); | 170 REGIONS); |
| 170 for (Map<String, Object> regionJson in regionsJson) { | 171 for (Map<String, Object> regionJson in regionsJson) { |
| 171 var regionOffset = regionJson[OFFSET]; | 172 var regionOffset = regionJson[OFFSET]; |
| 172 var regionLength = regionJson[LENGTH]; | 173 var regionLength = regionJson[LENGTH]; |
| 173 List<_NavigationTarget> targets = []; | 174 List<Element> targets = <Element>[]; |
| 174 for (Map<String, Object> targetJson in regionJson[TARGETS]) { | 175 for (Map<String, Object> targetJson in regionJson[TARGETS]) { |
| 175 var targetFile = targetJson[FILE]; | 176 targets.add(new Element.fromJson(targetJson)); |
| 176 var targetOffset = targetJson[OFFSET]; | |
| 177 var targetLength = targetJson[LENGTH]; | |
| 178 var elementJson = targetJson[ELEMENT]; | |
| 179 targets.add(new _NavigationTarget(targetFile, targetOffset, | |
| 180 targetLength, new Element.fromJson(elementJson))); | |
| 181 } | 177 } |
| 182 var region = new _NavigationRegion(regionOffset, regionLength, | 178 var region = new NavigationRegion(regionOffset, regionLength, |
| 183 targets); | 179 targets); |
| 184 regions.add(region); | 180 regions.add(region); |
| 185 } | 181 } |
| 186 } | 182 } |
| 187 } | 183 } |
| 188 } | 184 } |
| 189 | 185 |
| 190 @override | 186 @override |
| 191 void setUp() { | 187 void setUp() { |
| 192 super.setUp(); | 188 super.setUp(); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 459 |
| 464 test_targetElement() { | 460 test_targetElement() { |
| 465 addTestFile(''' | 461 addTestFile(''' |
| 466 class AAA {} | 462 class AAA {} |
| 467 main() { | 463 main() { |
| 468 AAA aaa = null; | 464 AAA aaa = null; |
| 469 } | 465 } |
| 470 '''); | 466 '''); |
| 471 return prepareNavigation(() { | 467 return prepareNavigation(() { |
| 472 assertHasRegionTarget('AAA aaa', 'AAA {}'); | 468 assertHasRegionTarget('AAA aaa', 'AAA {}'); |
| 473 Element element = testTarget.element; | 469 expect(testTarget.kind, ElementKind.CLASS); |
| 474 expect(element.kind, ElementKind.CLASS); | 470 expect(testTarget.name, 'AAA'); |
| 475 expect(element.name, 'AAA'); | 471 expect(testTarget.isAbstract, false); |
| 476 expect(element.isAbstract, false); | 472 expect(testTarget.parameters, isNull); |
| 477 expect(element.parameters, isNull); | 473 expect(testTarget.returnType, isNull); |
| 478 expect(element.returnType, isNull); | |
| 479 }); | 474 }); |
| 480 } | 475 } |
| 481 } | 476 } |
| 482 | 477 |
| 483 | 478 |
| 484 class _NavigationRegion { | 479 class NavigationRegion { |
| 485 final int offset; | 480 final int offset; |
| 486 final int length; | 481 final int length; |
| 487 final List<_NavigationTarget> targets; | 482 final List<Element> targets; |
| 488 | 483 |
| 489 _NavigationRegion(this.offset, this.length, this.targets); | 484 NavigationRegion(this.offset, this.length, this.targets); |
| 485 |
| 486 @override |
| 487 String toString() { |
| 488 return 'NavigationRegion(offset=$offset; length=$length; targets=$targets'; |
| 489 } |
| 490 } | 490 } |
| 491 | |
| 492 | |
| 493 class _NavigationTarget { | |
| 494 final String file; | |
| 495 final int offset; | |
| 496 final int length; | |
| 497 final Element element; | |
| 498 | |
| 499 _NavigationTarget(this.file, this.offset, this.length, this.element); | |
| 500 } | |
| OLD | NEW |