| 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/constants.dart'; | |
| 10 import 'package:analysis_server/src/protocol.dart'; | |
| 11 import 'reflective_tests.dart'; | |
| 12 import 'package:unittest/unittest.dart'; | |
| 13 | |
| 14 import 'analysis_abstract.dart'; | |
| 15 | |
| 16 | |
| 17 main() { | |
| 18 groupSep = ' | '; | |
| 19 runReflectiveTests(AnalysisNotificationOccurrencesTest); | |
| 20 } | |
| 21 | |
| 22 | |
| 23 @ReflectiveTestCase() | |
| 24 class AnalysisNotificationOccurrencesTest extends AbstractAnalysisTest { | |
| 25 List<Occurrences> occurrencesList; | |
| 26 Occurrences testOccurences; | |
| 27 | |
| 28 /** | |
| 29 * Asserts that there is an offset of [search] in [testOccurences]. | |
| 30 */ | |
| 31 void assertHasOffset(String search) { | |
| 32 int offset = findOffset(search); | |
| 33 expect(testOccurences.offsets, contains(offset)); | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Validates that there is a region at the offset of [search] in [testFile]. | |
| 38 * If [length] is not specified explicitly, then length of an identifier | |
| 39 * from [search] is used. | |
| 40 */ | |
| 41 void assertHasRegion(String search, [int length = -1]) { | |
| 42 int offset = findOffset(search); | |
| 43 if (length == -1) { | |
| 44 length = findIdentifierLength(search); | |
| 45 } | |
| 46 findRegion(offset, length, true); | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Finds an [Occurrences] with the given [offset] and [length]. | |
| 51 * | |
| 52 * If [exists] is `true`, then fails if such [Occurrences] does not exist. | |
| 53 * Otherwise remembers this it into [testOccurences]. | |
| 54 * | |
| 55 * If [exists] is `false`, then fails if such [Occurrences] exists. | |
| 56 */ | |
| 57 void findRegion(int offset, int length, [bool exists]) { | |
| 58 for (Occurrences occurrences in occurrencesList) { | |
| 59 if (occurrences.length != length) { | |
| 60 continue; | |
| 61 } | |
| 62 for (int occurrenceOffset in occurrences.offsets) { | |
| 63 if (occurrenceOffset == offset) { | |
| 64 if (exists == false) { | |
| 65 fail( | |
| 66 'Not expected to find (offset=$offset; length=$length) in\n' | |
| 67 '${occurrencesList.join('\n')}'); | |
| 68 } | |
| 69 testOccurences = occurrences; | |
| 70 return; | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 if (exists == true) { | |
| 75 fail( | |
| 76 'Expected to find (offset=$offset; length=$length) in\n' | |
| 77 '${occurrencesList.join('\n')}'); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 Future prepareOccurrences() { | |
| 82 addAnalysisSubscription(AnalysisService.OCCURRENCES, testFile); | |
| 83 return waitForTasksFinished(); | |
| 84 } | |
| 85 | |
| 86 void processNotification(Notification notification) { | |
| 87 if (notification.event == ANALYSIS_OCCURRENCES) { | |
| 88 var params = new AnalysisOccurrencesParams.fromNotification(notification); | |
| 89 if (params.file == testFile) { | |
| 90 occurrencesList = params.occurrences; | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 @override | |
| 96 void setUp() { | |
| 97 super.setUp(); | |
| 98 createProject(); | |
| 99 } | |
| 100 | |
| 101 test_afterAnalysis() { | |
| 102 addTestFile(''' | |
| 103 main() { | |
| 104 var vvv = 42; | |
| 105 print(vvv); | |
| 106 } | |
| 107 '''); | |
| 108 return waitForTasksFinished().then((_) { | |
| 109 return prepareOccurrences().then((_) { | |
| 110 assertHasRegion('vvv ='); | |
| 111 expect(testOccurences.element.kind, ElementKind.LOCAL_VARIABLE); | |
| 112 expect(testOccurences.element.name, 'vvv'); | |
| 113 assertHasOffset('vvv = 42'); | |
| 114 assertHasOffset('vvv);'); | |
| 115 }); | |
| 116 }); | |
| 117 } | |
| 118 | |
| 119 test_field() { | |
| 120 addTestFile(''' | |
| 121 class A { | |
| 122 int fff; | |
| 123 A(this.fff); // constructor | |
| 124 main() { | |
| 125 fff = 42; | |
| 126 print(fff); // print | |
| 127 } | |
| 128 } | |
| 129 '''); | |
| 130 return prepareOccurrences().then((_) { | |
| 131 assertHasRegion('fff;'); | |
| 132 expect(testOccurences.element.kind, ElementKind.FIELD); | |
| 133 assertHasOffset('fff); // constructor'); | |
| 134 assertHasOffset('fff = 42;'); | |
| 135 assertHasOffset('fff); // print'); | |
| 136 }); | |
| 137 } | |
| 138 | |
| 139 test_localVariable() { | |
| 140 addTestFile(''' | |
| 141 main() { | |
| 142 var vvv = 42; | |
| 143 vvv += 5; | |
| 144 print(vvv); | |
| 145 } | |
| 146 '''); | |
| 147 return prepareOccurrences().then((_) { | |
| 148 assertHasRegion('vvv ='); | |
| 149 expect(testOccurences.element.kind, ElementKind.LOCAL_VARIABLE); | |
| 150 expect(testOccurences.element.name, 'vvv'); | |
| 151 assertHasOffset('vvv = 42'); | |
| 152 assertHasOffset('vvv += 5'); | |
| 153 assertHasOffset('vvv);'); | |
| 154 }); | |
| 155 } | |
| 156 | |
| 157 test_memberField() { | |
| 158 addTestFile(''' | |
| 159 class A<T> { | |
| 160 T fff; | |
| 161 } | |
| 162 main() { | |
| 163 var a = new A<int>(); | |
| 164 var b = new A<String>(); | |
| 165 a.fff = 1; | |
| 166 b.fff = 2; | |
| 167 } | |
| 168 '''); | |
| 169 return prepareOccurrences().then((_) { | |
| 170 assertHasRegion('fff;'); | |
| 171 expect(testOccurences.element.kind, ElementKind.FIELD); | |
| 172 assertHasOffset('fff = 1;'); | |
| 173 assertHasOffset('fff = 2;'); | |
| 174 }); | |
| 175 } | |
| 176 | |
| 177 test_memberMethod() { | |
| 178 addTestFile(''' | |
| 179 class A<T> { | |
| 180 T mmm() {} | |
| 181 } | |
| 182 main() { | |
| 183 var a = new A<int>(); | |
| 184 var b = new A<String>(); | |
| 185 a.mmm(); // a | |
| 186 b.mmm(); // b | |
| 187 } | |
| 188 '''); | |
| 189 return prepareOccurrences().then((_) { | |
| 190 assertHasRegion('mmm() {}'); | |
| 191 expect(testOccurences.element.kind, ElementKind.METHOD); | |
| 192 assertHasOffset('mmm(); // a'); | |
| 193 assertHasOffset('mmm(); // b'); | |
| 194 }); | |
| 195 } | |
| 196 | |
| 197 test_topLevelVariable() { | |
| 198 addTestFile(''' | |
| 199 var VVV = 1; | |
| 200 main() { | |
| 201 VVV = 2; | |
| 202 print(VVV); | |
| 203 } | |
| 204 '''); | |
| 205 return prepareOccurrences().then((_) { | |
| 206 assertHasRegion('VVV = 1;'); | |
| 207 expect(testOccurences.element.kind, ElementKind.TOP_LEVEL_VARIABLE); | |
| 208 assertHasOffset('VVV = 2;'); | |
| 209 assertHasOffset('VVV);'); | |
| 210 }); | |
| 211 } | |
| 212 | |
| 213 test_type_class() { | |
| 214 addTestFile(''' | |
| 215 main() { | |
| 216 int a = 1; | |
| 217 int b = 2; | |
| 218 int c = 3; | |
| 219 } | |
| 220 int VVV = 4; | |
| 221 '''); | |
| 222 return prepareOccurrences().then((_) { | |
| 223 assertHasRegion('int a'); | |
| 224 expect(testOccurences.element.kind, ElementKind.CLASS); | |
| 225 expect(testOccurences.element.name, 'int'); | |
| 226 assertHasOffset('int a'); | |
| 227 assertHasOffset('int b'); | |
| 228 assertHasOffset('int c'); | |
| 229 assertHasOffset('int VVV'); | |
| 230 }); | |
| 231 } | |
| 232 | |
| 233 test_type_dynamic() { | |
| 234 addTestFile(''' | |
| 235 main() { | |
| 236 dynamic a = 1; | |
| 237 dynamic b = 2; | |
| 238 } | |
| 239 dynamic V = 3; | |
| 240 '''); | |
| 241 return prepareOccurrences().then((_) { | |
| 242 int offset = findOffset('dynamic a'); | |
| 243 findRegion(offset, 'dynamic'.length, false); | |
| 244 }); | |
| 245 } | |
| 246 | |
| 247 test_type_void() { | |
| 248 addTestFile(''' | |
| 249 void main() { | |
| 250 } | |
| 251 '''); | |
| 252 return prepareOccurrences().then((_) { | |
| 253 int offset = findOffset('void main()'); | |
| 254 findRegion(offset, 'void'.length, false); | |
| 255 }); | |
| 256 } | |
| 257 } | |
| OLD | NEW |