OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.domain.analysis.notification.occurrences; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/computer/computer_occurrences.dart'; |
| 11 import 'package:analysis_server/src/computer/element.dart'; |
| 12 import 'package:analysis_server/src/constants.dart'; |
| 13 import 'package:analysis_server/src/protocol.dart'; |
| 14 import 'package:unittest/unittest.dart'; |
| 15 |
| 16 import 'analysis_abstract.dart'; |
| 17 import 'reflective_tests.dart'; |
| 18 |
| 19 |
| 20 main() { |
| 21 group('notification.occurrences', () { |
| 22 runReflectiveTests(AnalysisNotificationOccurrencesTest); |
| 23 }); |
| 24 } |
| 25 |
| 26 |
| 27 @ReflectiveTestCase() |
| 28 class AnalysisNotificationOccurrencesTest extends AbstractAnalysisTest { |
| 29 List<Occurrences> occurrencesList; |
| 30 Occurrences testOccurences; |
| 31 |
| 32 /** |
| 33 * Asserts that there is an offset of [search] in [testOccurences]. |
| 34 */ |
| 35 void assertHasOffset(String search) { |
| 36 int offset = findOffset(search); |
| 37 expect(testOccurences.offsets, contains(offset)); |
| 38 } |
| 39 |
| 40 /** |
| 41 * Validates that there is a region at the offset of [search] in [testFile]. |
| 42 * If [length] is not specified explicitly, then length of an identifier |
| 43 * from [search] is used. |
| 44 */ |
| 45 void assertHasRegion(String search, [int length = -1]) { |
| 46 int offset = findOffset(search); |
| 47 if (length == -1) { |
| 48 length = findIdentifierLength(search); |
| 49 } |
| 50 findRegion(offset, length, true); |
| 51 } |
| 52 |
| 53 /** |
| 54 * Finds an [Occurrences] with the given [offset] and [length]. |
| 55 * |
| 56 * If [exists] is `true`, then fails if such [Occurrences] does not exist. |
| 57 * Otherwise remembers this it into [testOccurences]. |
| 58 * |
| 59 * If [exists] is `false`, then fails if such [Occurrences] exists. |
| 60 */ |
| 61 void findRegion(int offset, int length, [bool exists]) { |
| 62 for (Occurrences occurrences in occurrencesList) { |
| 63 if (occurrences.length != length) { |
| 64 continue; |
| 65 } |
| 66 for (int occurrenceOffset in occurrences.offsets) { |
| 67 if (occurrenceOffset == offset) { |
| 68 if (exists == false) { |
| 69 fail('Not expected to find (offset=$offset; length=$length) in\n' |
| 70 '${occurrencesList.join('\n')}'); |
| 71 } |
| 72 testOccurences = occurrences; |
| 73 return; |
| 74 } |
| 75 } |
| 76 } |
| 77 if (exists == true) { |
| 78 fail('Expected to find (offset=$offset; length=$length) in\n' |
| 79 '${occurrencesList.join('\n')}'); |
| 80 } |
| 81 } |
| 82 |
| 83 Future prepareOccurrences(then()) { |
| 84 addAnalysisSubscription(AnalysisService.OCCURRENCES, testFile); |
| 85 return waitForTasksFinished().then((_) { |
| 86 then(); |
| 87 }); |
| 88 } |
| 89 |
| 90 void processNotification(Notification notification) { |
| 91 if (notification.event == ANALYSIS_OCCURRENCES) { |
| 92 String file = notification.getParameter(FILE); |
| 93 if (file == testFile) { |
| 94 occurrencesList = <Occurrences>[]; |
| 95 List<Map<String, Object>> jsonList = notification.getParameter( |
| 96 OCCURRENCES); |
| 97 for (Map<String, Object> json in jsonList) { |
| 98 occurrencesList.add(new Occurrences.fromJson(json)); |
| 99 } |
| 100 } |
| 101 } |
| 102 } |
| 103 |
| 104 @override |
| 105 void setUp() { |
| 106 super.setUp(); |
| 107 createProject(); |
| 108 } |
| 109 |
| 110 test_afterAnalysis() { |
| 111 addTestFile(''' |
| 112 main() { |
| 113 var vvv = 42; |
| 114 print(vvv); |
| 115 } |
| 116 '''); |
| 117 return waitForTasksFinished().then((_) { |
| 118 return prepareOccurrences(() { |
| 119 assertHasRegion('vvv ='); |
| 120 expect(testOccurences.element.kind, ElementKind.LOCAL_VARIABLE); |
| 121 expect(testOccurences.element.name, 'vvv'); |
| 122 assertHasOffset('vvv = 42'); |
| 123 assertHasOffset('vvv);'); |
| 124 }); |
| 125 }); |
| 126 } |
| 127 |
| 128 test_classType() { |
| 129 addTestFile(''' |
| 130 main() { |
| 131 int a = 1; |
| 132 int b = 2; |
| 133 int c = 3; |
| 134 } |
| 135 int VVV = 4; |
| 136 '''); |
| 137 return prepareOccurrences(() { |
| 138 assertHasRegion('int a'); |
| 139 expect(testOccurences.element.kind, ElementKind.CLASS); |
| 140 expect(testOccurences.element.name, 'int'); |
| 141 assertHasOffset('int a'); |
| 142 assertHasOffset('int b'); |
| 143 assertHasOffset('int c'); |
| 144 assertHasOffset('int VVV'); |
| 145 }); |
| 146 } |
| 147 |
| 148 test_localVariable() { |
| 149 addTestFile(''' |
| 150 main() { |
| 151 var vvv = 42; |
| 152 vvv += 5; |
| 153 print(vvv); |
| 154 } |
| 155 '''); |
| 156 return prepareOccurrences(() { |
| 157 assertHasRegion('vvv ='); |
| 158 expect(testOccurences.element.kind, ElementKind.LOCAL_VARIABLE); |
| 159 expect(testOccurences.element.name, 'vvv'); |
| 160 assertHasOffset('vvv = 42'); |
| 161 assertHasOffset('vvv += 5'); |
| 162 assertHasOffset('vvv);'); |
| 163 }); |
| 164 } |
| 165 } |
OLD | NEW |