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; |
11 import 'package:analyzer/src/dart/ast/utilities.dart' as engine; | 11 import 'package:analyzer/src/dart/ast/utilities.dart' as engine; |
12 import 'package:analyzer/src/dart/element/element.dart' as engine; | 12 import 'package:analyzer/src/dart/element/element.dart' as engine; |
13 import 'package:analyzer/src/error/codes.dart' as engine; | 13 import 'package:analyzer/src/error/codes.dart' as engine; |
14 import 'package:analyzer/src/generated/source.dart' as engine; | 14 import 'package:analyzer/src/generated/source.dart' as engine; |
15 import 'package:analyzer_plugin/protocol/protocol_common.dart'; | 15 import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
16 import 'package:test/test.dart'; | 16 import 'package:test/test.dart'; |
17 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 17 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
18 | 18 |
19 import '../abstract_context.dart'; | 19 import '../abstract_context.dart'; |
20 | 20 |
21 main() { | 21 main() { |
22 defineReflectiveSuite(() { | 22 defineReflectiveSuite(() { |
23 defineReflectiveTests(ElementTest); | 23 defineReflectiveTests(ElementTest); |
24 defineReflectiveTests(ElementKindTest); | 24 defineReflectiveTests(ElementKindTest); |
25 }); | 25 }); |
26 } | 26 } |
27 | 27 |
28 /** | |
29 * Search the [unit] for the [engine.Element]s with the given [name]. | |
30 */ | |
31 List<engine.Element> findElementsByName( | |
Brian Wilkerson
2017/07/06 18:19:18
Can we use the copy in pkg/analyzer/test/utils.dar
| |
32 engine.CompilationUnit unit, String name) { | |
33 var finder = new _ElementsByNameFinder(name); | |
34 unit.accept(finder); | |
35 return finder.elements; | |
36 } | |
37 | |
28 @reflectiveTest | 38 @reflectiveTest |
29 class ElementKindTest { | 39 class ElementKindTest { |
30 void test_fromEngine() { | 40 void test_fromEngine() { |
31 expect(convertElementKind(engine.ElementKind.CLASS), ElementKind.CLASS); | 41 expect(convertElementKind(engine.ElementKind.CLASS), ElementKind.CLASS); |
32 expect(convertElementKind(engine.ElementKind.COMPILATION_UNIT), | 42 expect(convertElementKind(engine.ElementKind.COMPILATION_UNIT), |
33 ElementKind.COMPILATION_UNIT); | 43 ElementKind.COMPILATION_UNIT); |
34 expect(convertElementKind(engine.ElementKind.CONSTRUCTOR), | 44 expect(convertElementKind(engine.ElementKind.CONSTRUCTOR), |
35 ElementKind.CONSTRUCTOR); | 45 ElementKind.CONSTRUCTOR); |
36 expect(convertElementKind(engine.ElementKind.FIELD), ElementKind.FIELD); | 46 expect(convertElementKind(engine.ElementKind.FIELD), ElementKind.FIELD); |
37 expect( | 47 expect( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); | 100 expect(ElementKind.CLASS.toString(), 'ElementKind.CLASS'); |
91 expect(ElementKind.COMPILATION_UNIT.toString(), | 101 expect(ElementKind.COMPILATION_UNIT.toString(), |
92 'ElementKind.COMPILATION_UNIT'); | 102 'ElementKind.COMPILATION_UNIT'); |
93 } | 103 } |
94 } | 104 } |
95 | 105 |
96 @reflectiveTest | 106 @reflectiveTest |
97 class ElementTest extends AbstractContextTest { | 107 class ElementTest extends AbstractContextTest { |
98 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, | 108 engine.Element findElementInUnit(engine.CompilationUnit unit, String name, |
99 [engine.ElementKind kind]) { | 109 [engine.ElementKind kind]) { |
100 return findChildElement(unit.element, name, kind); | 110 return findElementsByName(unit, name) |
111 .where((e) => kind == null || e.kind == kind) | |
112 .single; | |
101 } | 113 } |
102 | 114 |
103 test_fromElement_CLASS() async { | 115 test_fromElement_CLASS() async { |
104 engine.Source source = addSource( | 116 engine.Source source = addSource( |
105 '/test.dart', | 117 '/test.dart', |
106 ''' | 118 ''' |
107 @deprecated | 119 @deprecated |
108 abstract class _A {} | 120 abstract class _A {} |
109 class B<K, V> {}'''); | 121 class B<K, V> {}'''); |
110 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 122 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 expect(location.offset, 44); | 278 expect(location.offset, 44); |
267 expect(location.length, 'three'.length); | 279 expect(location.length, 'three'.length); |
268 expect(location.startLine, 3); | 280 expect(location.startLine, 3); |
269 expect(location.startColumn, 11); | 281 expect(location.startColumn, 11); |
270 } | 282 } |
271 expect(element.parameters, isNull); | 283 expect(element.parameters, isNull); |
272 expect(element.returnType, 'E2'); | 284 expect(element.returnType, 'E2'); |
273 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); | 285 expect(element.flags, Element.FLAG_CONST | Element.FLAG_STATIC); |
274 } | 286 } |
275 { | 287 { |
276 engine.FieldElement engineElement = findElementInUnit(unit, 'index'); | 288 engine.FieldElement engineElement = |
289 unit.element.enums[1].getField('index'); | |
277 // create notification Element | 290 // create notification Element |
278 Element element = convertElement(engineElement); | 291 Element element = convertElement(engineElement); |
279 expect(element.kind, ElementKind.FIELD); | 292 expect(element.kind, ElementKind.FIELD); |
280 expect(element.name, 'index'); | 293 expect(element.name, 'index'); |
281 { | 294 { |
282 Location location = element.location; | 295 Location location = element.location; |
283 expect(location.file, '/test.dart'); | 296 expect(location.file, '/test.dart'); |
284 expect(location.offset, -1); | 297 expect(location.offset, -1); |
285 expect(location.length, 'index'.length); | 298 expect(location.length, 'index'.length); |
286 expect(location.startLine, 1); | 299 expect(location.startLine, 1); |
287 expect(location.startColumn, 0); | 300 expect(location.startColumn, 0); |
288 } | 301 } |
289 expect(element.parameters, isNull); | 302 expect(element.parameters, isNull); |
290 expect(element.returnType, 'int'); | 303 expect(element.returnType, 'int'); |
291 expect(element.flags, Element.FLAG_FINAL); | 304 expect(element.flags, Element.FLAG_FINAL); |
292 } | 305 } |
293 { | 306 { |
294 engine.FieldElement engineElement = findElementInUnit(unit, 'values'); | 307 engine.FieldElement engineElement = |
308 unit.element.enums[1].getField('values'); | |
295 // create notification Element | 309 // create notification Element |
296 Element element = convertElement(engineElement); | 310 Element element = convertElement(engineElement); |
297 expect(element.kind, ElementKind.FIELD); | 311 expect(element.kind, ElementKind.FIELD); |
298 expect(element.name, 'values'); | 312 expect(element.name, 'values'); |
299 { | 313 { |
300 Location location = element.location; | 314 Location location = element.location; |
301 expect(location.file, '/test.dart'); | 315 expect(location.file, '/test.dart'); |
302 expect(location.offset, -1); | 316 expect(location.offset, -1); |
303 expect(location.length, 'values'.length); | 317 expect(location.length, 'values'.length); |
304 expect(location.startLine, 1); | 318 expect(location.startLine, 1); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 } | 462 } |
449 | 463 |
450 test_fromElement_SETTER() async { | 464 test_fromElement_SETTER() async { |
451 engine.Source source = addSource( | 465 engine.Source source = addSource( |
452 '/test.dart', | 466 '/test.dart', |
453 ''' | 467 ''' |
454 class A { | 468 class A { |
455 set mySetter(String x) {} | 469 set mySetter(String x) {} |
456 }'''); | 470 }'''); |
457 engine.CompilationUnit unit = await resolveLibraryUnit(source); | 471 engine.CompilationUnit unit = await resolveLibraryUnit(source); |
458 engine.FieldElement engineFieldElement = | 472 engine.PropertyAccessorElement engineElement = |
459 findElementInUnit(unit, 'mySetter', engine.ElementKind.FIELD); | 473 findElementInUnit(unit, 'mySetter', engine.ElementKind.SETTER); |
460 engine.PropertyAccessorElement engineElement = engineFieldElement.setter; | |
461 // create notification Element | 474 // create notification Element |
462 Element element = convertElement(engineElement); | 475 Element element = convertElement(engineElement); |
463 expect(element.kind, ElementKind.SETTER); | 476 expect(element.kind, ElementKind.SETTER); |
464 expect(element.name, 'mySetter'); | 477 expect(element.name, 'mySetter'); |
465 { | 478 { |
466 Location location = element.location; | 479 Location location = element.location; |
467 expect(location.file, '/test.dart'); | 480 expect(location.file, '/test.dart'); |
468 expect(location.offset, 16); | 481 expect(location.offset, 16); |
469 expect(location.length, 'mySetter'.length); | 482 expect(location.length, 'mySetter'.length); |
470 expect(location.startLine, 2); | 483 expect(location.startLine, 2); |
471 expect(location.startColumn, 7); | 484 expect(location.startColumn, 7); |
472 } | 485 } |
473 expect(element.parameters, '(String x)'); | 486 expect(element.parameters, '(String x)'); |
474 expect(element.returnType, isNull); | 487 expect(element.returnType, isNull); |
475 expect(element.flags, 0); | 488 expect(element.flags, 0); |
476 } | 489 } |
477 } | 490 } |
491 | |
492 class _ElementsByNameFinder extends engine.RecursiveAstVisitor<Null> { | |
Brian Wilkerson
2017/07/06 18:19:18
Was this in an earlier CL? If so, perhaps create a
| |
493 final String name; | |
494 final List<engine.Element> elements = []; | |
495 | |
496 _ElementsByNameFinder(this.name); | |
497 | |
498 @override | |
499 visitSimpleIdentifier(engine.SimpleIdentifier node) { | |
500 if (node.name == name && node.inDeclarationContext()) { | |
501 elements.add(node.staticElement); | |
502 } | |
503 } | |
504 } | |
OLD | NEW |