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