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 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; | 5 import 'package:analysis_server/plugin/protocol/protocol_dart.dart'; |
6 import 'package:analyzer/dart/ast/ast.dart' as engine; | 6 import 'package:analyzer/dart/ast/ast.dart' as engine; |
7 import 'package:analyzer/dart/ast/visitor.dart' as engine; | 7 import 'package:analyzer/dart/ast/visitor.dart' as engine; |
8 import 'package:analyzer/dart/element/element.dart' as engine; | 8 import 'package:analyzer/dart/element/element.dart' as engine; |
9 import 'package:analyzer/dart/element/type.dart' as engine; | 9 import 'package:analyzer/dart/element/type.dart' as engine; |
10 import 'package:analyzer/error/error.dart' as engine; | 10 import 'package:analyzer/error/error.dart' as engine; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 @reflectiveTest | 97 @reflectiveTest |
98 class ElementTest extends AbstractContextTest { | 98 class ElementTest extends AbstractContextTest { |
99 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, | 99 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, |
100 [engine.ElementKind kind]) { | 100 [engine.ElementKind kind]) { |
101 return findElementsByName(unit, name) | 101 return findElementsByName(unit, name) |
102 .where((e) => kind == null || e.kind == kind) | 102 .where((e) => kind == null || e.kind == kind) |
103 .single; | 103 .single; |
104 } | 104 } |
105 | 105 |
106 test_fromElement_CLASS() async { | 106 test_fromElement_CLASS() async { |
107 engine.Source source = addSource( | 107 engine.Source source = addSource('/test.dart', ''' |
108 '/test.dart', | |
109 ''' | |
110 @deprecated | 108 @deprecated |
111 abstract class _A {} | 109 abstract class _A {} |
112 class B<K, V> {}'''); | 110 class B<K, V> {}'''); |
113 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 111 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
114 { | 112 { |
115 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); | 113 engine.ClassElement engineElement = findElementInUnit(unit, '_A'); |
116 // create notification Element | 114 // create notification Element |
117 Element element = convertElement(engineElement); | 115 Element element = convertElement(engineElement); |
118 expect(element.kind, ElementKind.CLASS); | 116 expect(element.kind, ElementKind.CLASS); |
119 expect(element.name, '_A'); | 117 expect(element.name, '_A'); |
(...skipping 18 matching lines...) Expand all Loading... |
138 // create notification Element | 136 // create notification Element |
139 Element element = convertElement(engineElement); | 137 Element element = convertElement(engineElement); |
140 expect(element.kind, ElementKind.CLASS); | 138 expect(element.kind, ElementKind.CLASS); |
141 expect(element.name, 'B'); | 139 expect(element.name, 'B'); |
142 expect(element.typeParameters, '<K, V>'); | 140 expect(element.typeParameters, '<K, V>'); |
143 expect(element.flags, 0); | 141 expect(element.flags, 0); |
144 } | 142 } |
145 } | 143 } |
146 | 144 |
147 test_fromElement_CONSTRUCTOR() async { | 145 test_fromElement_CONSTRUCTOR() async { |
148 engine.Source source = addSource( | 146 engine.Source source = addSource('/test.dart', ''' |
149 '/test.dart', | |
150 ''' | |
151 class A { | 147 class A { |
152 const A.myConstructor(int a, [String b]); | 148 const A.myConstructor(int a, [String b]); |
153 }'''); | 149 }'''); |
154 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 150 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
155 engine.ConstructorElement engineElement = | 151 engine.ConstructorElement engineElement = |
156 findElementInUnit(unit, 'myConstructor'); | 152 findElementInUnit(unit, 'myConstructor'); |
157 // create notification Element | 153 // create notification Element |
158 Element element = convertElement(engineElement); | 154 Element element = convertElement(engineElement); |
159 expect(element.kind, ElementKind.CONSTRUCTOR); | 155 expect(element.kind, ElementKind.CONSTRUCTOR); |
160 expect(element.name, 'myConstructor'); | 156 expect(element.name, 'myConstructor'); |
(...skipping 17 matching lines...) Expand all Loading... |
178 Element element = convertElement(engineElement); | 174 Element element = convertElement(engineElement); |
179 expect(element.kind, ElementKind.UNKNOWN); | 175 expect(element.kind, ElementKind.UNKNOWN); |
180 expect(element.name, 'dynamic'); | 176 expect(element.name, 'dynamic'); |
181 expect(element.location, isNull); | 177 expect(element.location, isNull); |
182 expect(element.parameters, isNull); | 178 expect(element.parameters, isNull); |
183 expect(element.returnType, isNull); | 179 expect(element.returnType, isNull); |
184 expect(element.flags, 0); | 180 expect(element.flags, 0); |
185 } | 181 } |
186 | 182 |
187 test_fromElement_ENUM() async { | 183 test_fromElement_ENUM() async { |
188 engine.Source source = addSource( | 184 engine.Source source = addSource('/test.dart', ''' |
189 '/test.dart', | |
190 ''' | |
191 @deprecated | 185 @deprecated |
192 enum _E1 { one, two } | 186 enum _E1 { one, two } |
193 enum E2 { three, four }'''); | 187 enum E2 { three, four }'''); |
194 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 188 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
195 { | 189 { |
196 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); | 190 engine.ClassElement engineElement = findElementInUnit(unit, '_E1'); |
197 expect(engineElement.isDeprecated, isTrue); | 191 expect(engineElement.isDeprecated, isTrue); |
198 // create notification Element | 192 // create notification Element |
199 Element element = convertElement(engineElement); | 193 Element element = convertElement(engineElement); |
200 expect(element.kind, ElementKind.ENUM); | 194 expect(element.kind, ElementKind.ENUM); |
(...skipping 18 matching lines...) Expand all Loading... |
219 // create notification Element | 213 // create notification Element |
220 Element element = convertElement(engineElement); | 214 Element element = convertElement(engineElement); |
221 expect(element.kind, ElementKind.ENUM); | 215 expect(element.kind, ElementKind.ENUM); |
222 expect(element.name, 'E2'); | 216 expect(element.name, 'E2'); |
223 expect(element.typeParameters, isNull); | 217 expect(element.typeParameters, isNull); |
224 expect(element.flags, 0); | 218 expect(element.flags, 0); |
225 } | 219 } |
226 } | 220 } |
227 | 221 |
228 test_fromElement_ENUM_CONSTANT() async { | 222 test_fromElement_ENUM_CONSTANT() async { |
229 engine.Source source = addSource( | 223 engine.Source source = addSource('/test.dart', ''' |
230 '/test.dart', | |
231 ''' | |
232 @deprecated | 224 @deprecated |
233 enum _E1 { one, two } | 225 enum _E1 { one, two } |
234 enum E2 { three, four }'''); | 226 enum E2 { three, four }'''); |
235 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 227 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
236 { | 228 { |
237 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); | 229 engine.FieldElement engineElement = findElementInUnit(unit, 'one'); |
238 // create notification Element | 230 // create notification Element |
239 Element element = convertElement(engineElement); | 231 Element element = convertElement(engineElement); |
240 expect(element.kind, ElementKind.ENUM_CONSTANT); | 232 expect(element.kind, ElementKind.ENUM_CONSTANT); |
241 expect(element.name, 'one'); | 233 expect(element.name, 'one'); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 expect(location.startLine, 1); | 301 expect(location.startLine, 1); |
310 expect(location.startColumn, 0); | 302 expect(location.startColumn, 0); |
311 } | 303 } |
312 expect(element.parameters, isNull); | 304 expect(element.parameters, isNull); |
313 expect(element.returnType, 'List<E2>'); | 305 expect(element.returnType, 'List<E2>'); |
314 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 306 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
315 } | 307 } |
316 } | 308 } |
317 | 309 |
318 test_fromElement_FIELD() async { | 310 test_fromElement_FIELD() async { |
319 engine.Source source = addSource( | 311 engine.Source source = addSource('/test.dart', ''' |
320 '/test.dart', | |
321 ''' | |
322 class A { | 312 class A { |
323 static const myField = 42; | 313 static const myField = 42; |
324 }'''); | 314 }'''); |
325 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 315 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
326 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); | 316 engine.FieldElement engineElement = findElementInUnit(unit, 'myField'); |
327 // create notification Element | 317 // create notification Element |
328 Element element = convertElement(engineElement); | 318 Element element = convertElement(engineElement); |
329 expect(element.kind, ElementKind.FIELD); | 319 expect(element.kind, ElementKind.FIELD); |
330 expect(element.name, 'myField'); | 320 expect(element.name, 'myField'); |
331 { | 321 { |
332 Location location = element.location; | 322 Location location = element.location; |
333 expect(location.file, '/test.dart'); | 323 expect(location.file, '/test.dart'); |
334 expect(location.offset, 25); | 324 expect(location.offset, 25); |
335 expect(location.length, 'myField'.length); | 325 expect(location.length, 'myField'.length); |
336 expect(location.startLine, 2); | 326 expect(location.startLine, 2); |
337 expect(location.startColumn, 16); | 327 expect(location.startColumn, 16); |
338 } | 328 } |
339 expect(element.parameters, isNull); | 329 expect(element.parameters, isNull); |
340 expect(element.returnType, 'int'); | 330 expect(element.returnType, 'int'); |
341 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 331 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
342 } | 332 } |
343 | 333 |
344 test_fromElement_FUNCTION_TYPE_ALIAS() async { | 334 test_fromElement_FUNCTION_TYPE_ALIAS() async { |
345 engine.Source source = addSource( | 335 engine.Source source = addSource('/test.dart', ''' |
346 '/test.dart', | |
347 ''' | |
348 typedef int F<T>(String x); | 336 typedef int F<T>(String x); |
349 '''); | 337 '''); |
350 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 338 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
351 engine.FunctionTypeAliasElement engineElement = | 339 engine.FunctionTypeAliasElement engineElement = |
352 findElementInUnit(unit, 'F'); | 340 findElementInUnit(unit, 'F'); |
353 // create notification Element | 341 // create notification Element |
354 Element element = convertElement(engineElement); | 342 Element element = convertElement(engineElement); |
355 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); | 343 expect(element.kind, ElementKind.FUNCTION_TYPE_ALIAS); |
356 expect(element.name, 'F'); | 344 expect(element.name, 'F'); |
357 expect(element.typeParameters, '<T>'); | 345 expect(element.typeParameters, '<T>'); |
358 { | 346 { |
359 Location location = element.location; | 347 Location location = element.location; |
360 expect(location.file, '/test.dart'); | 348 expect(location.file, '/test.dart'); |
361 expect(location.offset, 12); | 349 expect(location.offset, 12); |
362 expect(location.length, 'F'.length); | 350 expect(location.length, 'F'.length); |
363 expect(location.startLine, 1); | 351 expect(location.startLine, 1); |
364 expect(location.startColumn, 13); | 352 expect(location.startColumn, 13); |
365 } | 353 } |
366 expect(element.parameters, '(String x)'); | 354 expect(element.parameters, '(String x)'); |
367 expect(element.returnType, 'int'); | 355 expect(element.returnType, 'int'); |
368 expect(element.flags, 0); | 356 expect(element.flags, 0); |
369 } | 357 } |
370 | 358 |
371 test_fromElement_GETTER() async { | 359 test_fromElement_GETTER() async { |
372 engine.Source source = addSource( | 360 engine.Source source = addSource('/test.dart', ''' |
373 '/test.dart', | |
374 ''' | |
375 class A { | 361 class A { |
376 String get myGetter => 42; | 362 String get myGetter => 42; |
377 }'''); | 363 }'''); |
378 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 364 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
379 engine.PropertyAccessorElement engineElement = | 365 engine.PropertyAccessorElement engineElement = |
380 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); | 366 findElementInUnit(unit, 'myGetter', engine.ElementKind.GETTER); |
381 // create notification Element | 367 // create notification Element |
382 Element element = convertElement(engineElement); | 368 Element element = convertElement(engineElement); |
383 expect(element.kind, ElementKind.GETTER); | 369 expect(element.kind, ElementKind.GETTER); |
384 expect(element.name, 'myGetter'); | 370 expect(element.name, 'myGetter'); |
385 { | 371 { |
386 Location location = element.location; | 372 Location location = element.location; |
387 expect(location.file, '/test.dart'); | 373 expect(location.file, '/test.dart'); |
388 expect(location.offset, 23); | 374 expect(location.offset, 23); |
389 expect(location.length, 'myGetter'.length); | 375 expect(location.length, 'myGetter'.length); |
390 expect(location.startLine, 2); | 376 expect(location.startLine, 2); |
391 expect(location.startColumn, 14); | 377 expect(location.startColumn, 14); |
392 } | 378 } |
393 expect(element.parameters, isNull); | 379 expect(element.parameters, isNull); |
394 expect(element.returnType, 'String'); | 380 expect(element.returnType, 'String'); |
395 expect(element.flags, 0); | 381 expect(element.flags, 0); |
396 } | 382 } |
397 | 383 |
398 test_fromElement_LABEL() async { | 384 test_fromElement_LABEL() async { |
399 engine.Source source = addSource( | 385 engine.Source source = addSource('/test.dart', ''' |
400 '/test.dart', | |
401 ''' | |
402 main() { | 386 main() { |
403 myLabel: | 387 myLabel: |
404 while (true) { | 388 while (true) { |
405 break myLabel; | 389 break myLabel; |
406 } | 390 } |
407 }'''); | 391 }'''); |
408 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 392 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
409 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); | 393 engine.LabelElement engineElement = findElementInUnit(unit, 'myLabel'); |
410 // create notification Element | 394 // create notification Element |
411 Element element = convertElement(engineElement); | 395 Element element = convertElement(engineElement); |
412 expect(element.kind, ElementKind.LABEL); | 396 expect(element.kind, ElementKind.LABEL); |
413 expect(element.name, 'myLabel'); | 397 expect(element.name, 'myLabel'); |
414 { | 398 { |
415 Location location = element.location; | 399 Location location = element.location; |
416 expect(location.file, '/test.dart'); | 400 expect(location.file, '/test.dart'); |
417 expect(location.offset, 9); | 401 expect(location.offset, 9); |
418 expect(location.length, 'myLabel'.length); | 402 expect(location.length, 'myLabel'.length); |
419 expect(location.startLine, 2); | 403 expect(location.startLine, 2); |
420 expect(location.startColumn, 1); | 404 expect(location.startColumn, 1); |
421 } | 405 } |
422 expect(element.parameters, isNull); | 406 expect(element.parameters, isNull); |
423 expect(element.returnType, isNull); | 407 expect(element.returnType, isNull); |
424 expect(element.flags, 0); | 408 expect(element.flags, 0); |
425 } | 409 } |
426 | 410 |
427 test_fromElement_METHOD() async { | 411 test_fromElement_METHOD() async { |
428 engine.Source source = addSource( | 412 engine.Source source = addSource('/test.dart', ''' |
429 '/test.dart', | |
430 ''' | |
431 class A { | 413 class A { |
432 static List<String> myMethod(int a, {String b, int c}) { | 414 static List<String> myMethod(int a, {String b, int c}) { |
433 return null; | 415 return null; |
434 } | 416 } |
435 }'''); | 417 }'''); |
436 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 418 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
437 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); | 419 engine.MethodElement engineElement = findElementInUnit(unit, 'myMethod'); |
438 // create notification Element | 420 // create notification Element |
439 Element element = convertElement(engineElement); | 421 Element element = convertElement(engineElement); |
440 expect(element.kind, ElementKind.METHOD); | 422 expect(element.kind, ElementKind.METHOD); |
441 expect(element.name, 'myMethod'); | 423 expect(element.name, 'myMethod'); |
442 { | 424 { |
443 Location location = element.location; | 425 Location location = element.location; |
444 expect(location.file, '/test.dart'); | 426 expect(location.file, '/test.dart'); |
445 expect(location.offset, 32); | 427 expect(location.offset, 32); |
446 expect(location.length, 'myGetter'.length); | 428 expect(location.length, 'myGetter'.length); |
447 expect(location.startLine, 2); | 429 expect(location.startLine, 2); |
448 expect(location.startColumn, 23); | 430 expect(location.startColumn, 23); |
449 } | 431 } |
450 expect(element.parameters, '(int a, {String b, int c})'); | 432 expect(element.parameters, '(int a, {String b, int c})'); |
451 expect(element.returnType, 'List<String>'); | 433 expect(element.returnType, 'List<String>'); |
452 expect(element.flags, Element.FLAG_STATIC); | 434 expect(element.flags, Element.FLAG_STATIC); |
453 } | 435 } |
454 | 436 |
455 test_fromElement_SETTER() async { | 437 test_fromElement_SETTER() async { |
456 engine.Source source = addSource( | 438 engine.Source source = addSource('/test.dart', ''' |
457 '/test.dart', | |
458 ''' | |
459 class A { | 439 class A { |
460 set mySetter(String x) {} | 440 set mySetter(String x) {} |
461 }'''); | 441 }'''); |
462 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 442 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
463 engine.PropertyAccessorElement engineElement = | 443 engine.PropertyAccessorElement engineElement = |
464 findElementInUnit(unit, 'mySetter', engine.ElementKind.SETTER); | 444 findElementInUnit(unit, 'mySetter', engine.ElementKind.SETTER); |
465 // create notification Element | 445 // create notification Element |
466 Element element = convertElement(engineElement); | 446 Element element = convertElement(engineElement); |
467 expect(element.kind, ElementKind.SETTER); | 447 expect(element.kind, ElementKind.SETTER); |
468 expect(element.name, 'mySetter'); | 448 expect(element.name, 'mySetter'); |
469 { | 449 { |
470 Location location = element.location; | 450 Location location = element.location; |
471 expect(location.file, '/test.dart'); | 451 expect(location.file, '/test.dart'); |
472 expect(location.offset, 16); | 452 expect(location.offset, 16); |
473 expect(location.length, 'mySetter'.length); | 453 expect(location.length, 'mySetter'.length); |
474 expect(location.startLine, 2); | 454 expect(location.startLine, 2); |
475 expect(location.startColumn, 7); | 455 expect(location.startColumn, 7); |
476 } | 456 } |
477 expect(element.parameters, '(String x)'); | 457 expect(element.parameters, '(String x)'); |
478 expect(element.returnType, isNull); | 458 expect(element.returnType, isNull); |
479 expect(element.flags, 0); | 459 expect(element.flags, 0); |
480 } | 460 } |
481 } | 461 } |
OLD | NEW |