| 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/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 | 13 |
| 14 import 'analysis_abstract.dart'; | 14 import 'analysis_abstract.dart'; |
| 15 import 'reflective_tests.dart'; | 15 import 'reflective_tests.dart'; |
| 16 | 16 |
| 17 | 17 |
| 18 main() { | 18 main() { |
| 19 group('notification.navigation', () { | 19 group('notification.navigation', () { |
| 20 runReflectiveTests(AnalysisNotificationNavigationTest); | 20 runReflectiveTests(AnalysisNotificationNavigationTest); |
| 21 }); | 21 }); |
| 22 } | 22 } |
| 23 | 23 |
| 24 | 24 |
| 25 @ReflectiveTestCase() | 25 @ReflectiveTestCase() |
| 26 class AnalysisNotificationNavigationTest extends AbstractAnalysisTest { | 26 class AnalysisNotificationNavigationTest extends AbstractAnalysisTest { |
| 27 List<Map<String, Object>> regions; | 27 List<_NavigationRegion> regions; |
| 28 Map<String, Object> testRegion; | 28 _NavigationRegion testRegion; |
| 29 List<Map<String, Object>> testTargets; | 29 List<_NavigationTarget> testTargets; |
| 30 | 30 |
| 31 void processNotification(Notification notification) { | 31 void processNotification(Notification notification) { |
| 32 if (notification.event == NOTIFICATION_NAVIGATION) { | 32 if (notification.event == NOTIFICATION_NAVIGATION) { |
| 33 String file = notification.getParameter(FILE); | 33 String file = notification.getParameter(FILE); |
| 34 if (file == testFile) { | 34 if (file == testFile) { |
| 35 regions = notification.getParameter(REGIONS); | 35 regions = []; |
| 36 List<Map<String, Object>> regionsJson = notification.getParameter(REGION
S); |
| 37 for (Map<String, Object> regionJson in regionsJson) { |
| 38 var regionOffset = regionJson['offset']; |
| 39 var regionLength = regionJson['length']; |
| 40 List<_NavigationTarget> targets = []; |
| 41 for (Map<String, Object> targetJson in regionJson['targets']) { |
| 42 var targetFile = targetJson['file']; |
| 43 var targetOffset = targetJson['offset']; |
| 44 var targetLength = targetJson['length']; |
| 45 targets.add(new _NavigationTarget(targetFile, targetOffset, targetLe
ngth)); |
| 46 } |
| 47 var region = new _NavigationRegion(regionOffset, regionLength, targets
); |
| 48 regions.add(region); |
| 49 } |
| 36 } | 50 } |
| 37 } | 51 } |
| 38 } | 52 } |
| 39 | 53 |
| 40 Future prepareNavigation(then()) { | 54 Future prepareNavigation(then()) { |
| 41 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile); | 55 addAnalysisSubscription(AnalysisService.NAVIGATION, testFile); |
| 42 return waitForTasksFinished().then((_) { | 56 return waitForTasksFinished().then((_) { |
| 43 then(); | 57 then(); |
| 44 }); | 58 }); |
| 45 } | 59 } |
| 46 | 60 |
| 47 /** | 61 /** |
| 48 * Finds the navigation region with the given [offset] and [length]. | 62 * Finds the navigation region with the given [offset] and [length]. |
| 49 * If [length] is `-1`, then it is ignored. | 63 * If [length] is `-1`, then it is ignored. |
| 50 * | 64 * |
| 51 * If [exists] is `true`, then fails if such region does not exist. | 65 * If [exists] is `true`, then fails if such region does not exist. |
| 52 * Otherwise remembers this it into [testRegion]. | 66 * Otherwise remembers this it into [testRegion]. |
| 53 * Also fills [testTargets] with its targets. | 67 * Also fills [testTargets] with its targets. |
| 54 * | 68 * |
| 55 * If [exists] is `false`, then fails if such region exists. | 69 * If [exists] is `false`, then fails if such region exists. |
| 56 */ | 70 */ |
| 57 void findRegion(int offset, int length, [bool exists]) { | 71 void findRegion(int offset, int length, [bool exists]) { |
| 58 for (Map<String, Object> region in regions) { | 72 for (_NavigationRegion region in regions) { |
| 59 if (region['offset'] == offset && | 73 if (region.offset == offset && |
| 60 (length == -1 || region['length'] == length)) { | 74 (length == -1 || region.length == length)) { |
| 61 if (exists == false) { | 75 if (exists == false) { |
| 62 fail('Not expected to find (offset=$offset; length=$length) in\n' | 76 fail('Not expected to find (offset=$offset; length=$length) in\n' |
| 63 '${regions.join('\n')}'); | 77 '${regions.join('\n')}'); |
| 64 } | 78 } |
| 65 testRegion = region; | 79 testRegion = region; |
| 66 testTargets = region['targets']; | 80 testTargets = region.targets; |
| 67 return; | 81 return; |
| 68 } | 82 } |
| 69 } | 83 } |
| 70 if (exists == true) { | 84 if (exists == true) { |
| 71 fail('Expected to find (offset=$offset; length=$length) in\n' | 85 fail('Expected to find (offset=$offset; length=$length) in\n' |
| 72 '${regions.join('\n')}'); | 86 '${regions.join('\n')}'); |
| 73 } | 87 } |
| 74 } | 88 } |
| 75 | 89 |
| 76 /** | 90 /** |
| (...skipping 22 matching lines...) Expand all Loading... |
| 99 /** | 113 /** |
| 100 * Validates that there is a target in [testTargets] with [testFile], at the | 114 * Validates that there is a target in [testTargets] with [testFile], at the |
| 101 * offset of [search] in [testFile], and with the given [length] or the length | 115 * offset of [search] in [testFile], and with the given [length] or the length |
| 102 * of an leading identifier in [search]. | 116 * of an leading identifier in [search]. |
| 103 */ | 117 */ |
| 104 void assertHasTarget(String search, [int length = -1]) { | 118 void assertHasTarget(String search, [int length = -1]) { |
| 105 int offset = findOffset(search); | 119 int offset = findOffset(search); |
| 106 if (length == -1) { | 120 if (length == -1) { |
| 107 length = findIdentifierLength(search); | 121 length = findIdentifierLength(search); |
| 108 } | 122 } |
| 109 for (Map<String, Object> target in testTargets) { | 123 assertHasFileTarget(testFile, offset, length); |
| 110 if (target['file'] == testFile && | |
| 111 target['offset'] == offset && | |
| 112 target['length'] == length) { | |
| 113 return; | |
| 114 } | |
| 115 } | |
| 116 fail('Expected to find target (offset=$offset; length=$length) in\n' | |
| 117 '${testRegion} in\n' | |
| 118 '${regions.join('\n')}'); | |
| 119 } | 124 } |
| 120 | 125 |
| 121 /** | 126 /** |
| 122 * Validates that there is a target in [testTargets] with [file], at [offset] | 127 * Validates that there is a target in [testTargets] with [file], at [offset] |
| 123 * and with the given [length]. | 128 * and with the given [length]. |
| 124 */ | 129 */ |
| 125 void assertHasFileTarget(String file, int offset, int length) { | 130 void assertHasFileTarget(String file, int offset, int length) { |
| 126 for (Map<String, Object> target in testTargets) { | 131 for (_NavigationTarget target in testTargets) { |
| 127 if (target['file'] == file && | 132 if (target.file == file && |
| 128 target['offset'] == offset && | 133 target.offset == offset && |
| 129 target['length'] == length) { | 134 target.length == length) { |
| 130 return; | 135 return; |
| 131 } | 136 } |
| 132 } | 137 } |
| 133 fail('Expected to find target (file=$file; offset=$offset; length=$length) i
n\n' | 138 fail('Expected to find target (file=$file; offset=$offset; length=$length) i
n\n' |
| 134 '${testRegion} in\n' | 139 '${testRegion} in\n' |
| 135 '${regions.join('\n')}'); | 140 '${regions.join('\n')}'); |
| 136 } | 141 } |
| 137 | 142 |
| 138 /** | 143 /** |
| 139 * Validates that there is an identifier region at [regionSearch] with target | 144 * Validates that there is an identifier region at [regionSearch] with target |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 // This Source/File does not exist. | 430 // This Source/File does not exist. |
| 426 // addTestFile(''' | 431 // addTestFile(''' |
| 427 //library lib; | 432 //library lib; |
| 428 //part "test_unit.dart"; | 433 //part "test_unit.dart"; |
| 429 //'''); | 434 //'''); |
| 430 // return prepareNavigation(() { | 435 // return prepareNavigation(() { |
| 431 // assertNoRegionString('part "test_unit.dart"'); | 436 // assertNoRegionString('part "test_unit.dart"'); |
| 432 // }); | 437 // }); |
| 433 } | 438 } |
| 434 } | 439 } |
| 440 |
| 441 |
| 442 class _NavigationRegion { |
| 443 final int offset; |
| 444 final int length; |
| 445 final List<_NavigationTarget> targets; |
| 446 |
| 447 _NavigationRegion(this.offset, this.length, this.targets); |
| 448 } |
| 449 |
| 450 |
| 451 class _NavigationTarget { |
| 452 final String file; |
| 453 final int offset; |
| 454 final int length; |
| 455 |
| 456 _NavigationTarget(this.file, this.offset, this.length); |
| 457 } |
| OLD | NEW |