| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 analyzer.test.generated.element_resolver_test; |
| 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/dart/element/type.dart'; |
| 11 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 12 import 'package:analyzer/src/dart/element/element.dart'; |
| 13 import 'package:analyzer/src/generated/element_resolver.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/resolver.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/testing/ast_factory.dart'; |
| 18 import 'package:analyzer/src/generated/testing/element_factory.dart'; |
| 19 import 'package:analyzer/src/generated/testing/test_type_provider.dart'; |
| 20 import 'package:analyzer/src/source/source_resource.dart'; |
| 21 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 22 import 'package:unittest/unittest.dart'; |
| 23 |
| 24 import '../utils.dart'; |
| 25 import 'analysis_context_factory.dart'; |
| 26 import 'resolver_test_case.dart'; |
| 27 import 'test_support.dart'; |
| 28 |
| 29 main() { |
| 30 initializeTestEnvironment(); |
| 31 defineReflectiveTests(ElementResolverCodeTest); |
| 32 defineReflectiveTests(ElementResolverTest); |
| 33 } |
| 34 |
| 35 @reflectiveTest |
| 36 class ElementResolverCodeTest extends ResolverTestCase { |
| 37 void test_annotation_class_namedConstructor() { |
| 38 addNamedSource( |
| 39 '/a.dart', |
| 40 r''' |
| 41 class A { |
| 42 const A.named(); |
| 43 } |
| 44 '''); |
| 45 _validateAnnotation('', '@A.named()', (SimpleIdentifier name1, |
| 46 SimpleIdentifier name2, |
| 47 SimpleIdentifier name3, |
| 48 Element annotationElement) { |
| 49 expect(name1, isNotNull); |
| 50 expect(name1.staticElement, new isInstanceOf<ClassElement>()); |
| 51 expect(name1.staticElement.displayName, 'A'); |
| 52 expect(name2, isNotNull); |
| 53 expect(name2.staticElement, new isInstanceOf<ConstructorElement>()); |
| 54 expect(name2.staticElement.displayName, 'named'); |
| 55 expect(name3, isNull); |
| 56 if (annotationElement is ConstructorElement) { |
| 57 expect(annotationElement, same(name2.staticElement)); |
| 58 expect(annotationElement.enclosingElement, name1.staticElement); |
| 59 expect(annotationElement.displayName, 'named'); |
| 60 expect(annotationElement.parameters, isEmpty); |
| 61 } else { |
| 62 fail('Expected "annotationElement" is ConstructorElement, ' |
| 63 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 64 } |
| 65 }); |
| 66 } |
| 67 |
| 68 void test_annotation_class_prefixed_namedConstructor() { |
| 69 addNamedSource( |
| 70 '/a.dart', |
| 71 r''' |
| 72 class A { |
| 73 const A.named(); |
| 74 } |
| 75 '''); |
| 76 _validateAnnotation('as p', '@p.A.named()', (SimpleIdentifier name1, |
| 77 SimpleIdentifier name2, |
| 78 SimpleIdentifier name3, |
| 79 Element annotationElement) { |
| 80 expect(name1, isNotNull); |
| 81 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); |
| 82 expect(name1.staticElement.displayName, 'p'); |
| 83 expect(name2, isNotNull); |
| 84 expect(name2.staticElement, new isInstanceOf<ClassElement>()); |
| 85 expect(name2.staticElement.displayName, 'A'); |
| 86 expect(name3, isNotNull); |
| 87 expect(name3.staticElement, new isInstanceOf<ConstructorElement>()); |
| 88 expect(name3.staticElement.displayName, 'named'); |
| 89 if (annotationElement is ConstructorElement) { |
| 90 expect(annotationElement, same(name3.staticElement)); |
| 91 expect(annotationElement.enclosingElement, name2.staticElement); |
| 92 expect(annotationElement.displayName, 'named'); |
| 93 expect(annotationElement.parameters, isEmpty); |
| 94 } else { |
| 95 fail('Expected "annotationElement" is ConstructorElement, ' |
| 96 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 97 } |
| 98 }); |
| 99 } |
| 100 |
| 101 void test_annotation_class_prefixed_staticConstField() { |
| 102 addNamedSource( |
| 103 '/a.dart', |
| 104 r''' |
| 105 class A { |
| 106 static const V = 0; |
| 107 } |
| 108 '''); |
| 109 _validateAnnotation('as p', '@p.A.V', (SimpleIdentifier name1, |
| 110 SimpleIdentifier name2, |
| 111 SimpleIdentifier name3, |
| 112 Element annotationElement) { |
| 113 expect(name1, isNotNull); |
| 114 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); |
| 115 expect(name1.staticElement.displayName, 'p'); |
| 116 expect(name2, isNotNull); |
| 117 expect(name2.staticElement, new isInstanceOf<ClassElement>()); |
| 118 expect(name2.staticElement.displayName, 'A'); |
| 119 expect(name3, isNotNull); |
| 120 expect(name3.staticElement, new isInstanceOf<PropertyAccessorElement>()); |
| 121 expect(name3.staticElement.displayName, 'V'); |
| 122 if (annotationElement is PropertyAccessorElement) { |
| 123 expect(annotationElement, same(name3.staticElement)); |
| 124 expect(annotationElement.enclosingElement, name2.staticElement); |
| 125 expect(annotationElement.displayName, 'V'); |
| 126 } else { |
| 127 fail('Expected "annotationElement" is PropertyAccessorElement, ' |
| 128 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 129 } |
| 130 }); |
| 131 } |
| 132 |
| 133 void test_annotation_class_prefixed_unnamedConstructor() { |
| 134 addNamedSource( |
| 135 '/a.dart', |
| 136 r''' |
| 137 class A { |
| 138 const A(); |
| 139 } |
| 140 '''); |
| 141 _validateAnnotation('as p', '@p.A', (SimpleIdentifier name1, |
| 142 SimpleIdentifier name2, |
| 143 SimpleIdentifier name3, |
| 144 Element annotationElement) { |
| 145 expect(name1, isNotNull); |
| 146 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); |
| 147 expect(name1.staticElement.displayName, 'p'); |
| 148 expect(name2, isNotNull); |
| 149 expect(name2.staticElement, new isInstanceOf<ClassElement>()); |
| 150 expect(name2.staticElement.displayName, 'A'); |
| 151 expect(name3, isNull); |
| 152 if (annotationElement is ConstructorElement) { |
| 153 expect(annotationElement.enclosingElement, name2.staticElement); |
| 154 expect(annotationElement.displayName, ''); |
| 155 expect(annotationElement.parameters, isEmpty); |
| 156 } else { |
| 157 fail('Expected "annotationElement" is ConstructorElement, ' |
| 158 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 159 } |
| 160 }); |
| 161 } |
| 162 |
| 163 void test_annotation_class_staticConstField() { |
| 164 addNamedSource( |
| 165 '/a.dart', |
| 166 r''' |
| 167 class A { |
| 168 static const V = 0; |
| 169 } |
| 170 '''); |
| 171 _validateAnnotation('', '@A.V', (SimpleIdentifier name1, |
| 172 SimpleIdentifier name2, |
| 173 SimpleIdentifier name3, |
| 174 Element annotationElement) { |
| 175 expect(name1, isNotNull); |
| 176 expect(name1.staticElement, new isInstanceOf<ClassElement>()); |
| 177 expect(name1.staticElement.displayName, 'A'); |
| 178 expect(name2, isNotNull); |
| 179 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>()); |
| 180 expect(name2.staticElement.displayName, 'V'); |
| 181 expect(name3, isNull); |
| 182 if (annotationElement is PropertyAccessorElement) { |
| 183 expect(annotationElement, same(name2.staticElement)); |
| 184 expect(annotationElement.enclosingElement, name1.staticElement); |
| 185 expect(annotationElement.displayName, 'V'); |
| 186 } else { |
| 187 fail('Expected "annotationElement" is PropertyAccessorElement, ' |
| 188 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 189 } |
| 190 }); |
| 191 } |
| 192 |
| 193 void test_annotation_class_unnamedConstructor() { |
| 194 addNamedSource( |
| 195 '/a.dart', |
| 196 r''' |
| 197 class A { |
| 198 const A(); |
| 199 } |
| 200 '''); |
| 201 _validateAnnotation('', '@A', (SimpleIdentifier name1, |
| 202 SimpleIdentifier name2, |
| 203 SimpleIdentifier name3, |
| 204 Element annotationElement) { |
| 205 expect(name1, isNotNull); |
| 206 expect(name1.staticElement, new isInstanceOf<ClassElement>()); |
| 207 expect(name1.staticElement.displayName, 'A'); |
| 208 expect(name2, isNull); |
| 209 expect(name3, isNull); |
| 210 if (annotationElement is ConstructorElement) { |
| 211 expect(annotationElement.enclosingElement, name1.staticElement); |
| 212 expect(annotationElement.displayName, ''); |
| 213 expect(annotationElement.parameters, isEmpty); |
| 214 } else { |
| 215 fail('Expected "annotationElement" is ConstructorElement, ' |
| 216 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 217 } |
| 218 }); |
| 219 } |
| 220 |
| 221 void test_annotation_topLevelVariable() { |
| 222 addNamedSource( |
| 223 '/a.dart', |
| 224 r''' |
| 225 const V = 0; |
| 226 '''); |
| 227 _validateAnnotation('', '@V', (SimpleIdentifier name1, |
| 228 SimpleIdentifier name2, |
| 229 SimpleIdentifier name3, |
| 230 Element annotationElement) { |
| 231 expect(name1, isNotNull); |
| 232 expect(name1.staticElement, new isInstanceOf<PropertyAccessorElement>()); |
| 233 expect(name1.staticElement.displayName, 'V'); |
| 234 expect(name2, isNull); |
| 235 expect(name3, isNull); |
| 236 if (annotationElement is PropertyAccessorElement) { |
| 237 expect(annotationElement, same(name1.staticElement)); |
| 238 expect(annotationElement.enclosingElement, |
| 239 new isInstanceOf<CompilationUnitElement>()); |
| 240 expect(annotationElement.displayName, 'V'); |
| 241 } else { |
| 242 fail('Expected "annotationElement" is PropertyAccessorElement, ' |
| 243 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 244 } |
| 245 }); |
| 246 } |
| 247 |
| 248 void test_annotation_topLevelVariable_prefixed() { |
| 249 addNamedSource( |
| 250 '/a.dart', |
| 251 r''' |
| 252 const V = 0; |
| 253 '''); |
| 254 _validateAnnotation('as p', '@p.V', (SimpleIdentifier name1, |
| 255 SimpleIdentifier name2, |
| 256 SimpleIdentifier name3, |
| 257 Element annotationElement) { |
| 258 expect(name1, isNotNull); |
| 259 expect(name1.staticElement, new isInstanceOf<PrefixElement>()); |
| 260 expect(name1.staticElement.displayName, 'p'); |
| 261 expect(name2, isNotNull); |
| 262 expect(name2.staticElement, new isInstanceOf<PropertyAccessorElement>()); |
| 263 expect(name2.staticElement.displayName, 'V'); |
| 264 expect(name3, isNull); |
| 265 if (annotationElement is PropertyAccessorElement) { |
| 266 expect(annotationElement, same(name2.staticElement)); |
| 267 expect(annotationElement.enclosingElement, |
| 268 new isInstanceOf<CompilationUnitElement>()); |
| 269 expect(annotationElement.displayName, 'V'); |
| 270 } else { |
| 271 fail('Expected "annotationElement" is PropertyAccessorElement, ' |
| 272 'but (${annotationElement?.runtimeType}) $annotationElement found.')
; |
| 273 } |
| 274 }); |
| 275 } |
| 276 |
| 277 void _validateAnnotation( |
| 278 String annotationPrefix, |
| 279 String annotationText, |
| 280 validator(SimpleIdentifier name1, SimpleIdentifier name2, |
| 281 SimpleIdentifier name3, Element annotationElement)) { |
| 282 CompilationUnit unit = resolveSource(''' |
| 283 import 'a.dart' $annotationPrefix; |
| 284 $annotationText |
| 285 class C {} |
| 286 '''); |
| 287 var clazz = unit.declarations.single as ClassDeclaration; |
| 288 Annotation annotation = clazz.metadata.single; |
| 289 Identifier name = annotation.name; |
| 290 Element annotationElement = annotation.element; |
| 291 if (name is SimpleIdentifier) { |
| 292 validator(name, null, annotation.constructorName, annotationElement); |
| 293 } else if (name is PrefixedIdentifier) { |
| 294 validator(name.prefix, name.identifier, annotation.constructorName, |
| 295 annotationElement); |
| 296 } else { |
| 297 fail('Uknown "name": ${name?.runtimeType} $name'); |
| 298 } |
| 299 } |
| 300 } |
| 301 |
| 302 @reflectiveTest |
| 303 class ElementResolverTest extends EngineTestCase { |
| 304 /** |
| 305 * The error listener to which errors will be reported. |
| 306 */ |
| 307 GatheringErrorListener _listener; |
| 308 |
| 309 /** |
| 310 * The type provider used to access the types. |
| 311 */ |
| 312 TestTypeProvider _typeProvider; |
| 313 |
| 314 /** |
| 315 * The library containing the code being resolved. |
| 316 */ |
| 317 LibraryElementImpl _definingLibrary; |
| 318 |
| 319 /** |
| 320 * The resolver visitor that maintains the state for the resolver. |
| 321 */ |
| 322 ResolverVisitor _visitor; |
| 323 |
| 324 /** |
| 325 * The resolver being used to resolve the test cases. |
| 326 */ |
| 327 ElementResolver _resolver; |
| 328 |
| 329 void fail_visitExportDirective_combinators() { |
| 330 fail("Not yet tested"); |
| 331 // Need to set up the exported library so that the identifier can be |
| 332 // resolved. |
| 333 ExportDirective directive = AstFactory.exportDirective2(null, [ |
| 334 AstFactory.hideCombinator2(["A"]) |
| 335 ]); |
| 336 _resolveNode(directive); |
| 337 _listener.assertNoErrors(); |
| 338 } |
| 339 |
| 340 void fail_visitFunctionExpressionInvocation() { |
| 341 fail("Not yet tested"); |
| 342 _listener.assertNoErrors(); |
| 343 } |
| 344 |
| 345 void fail_visitImportDirective_combinators_noPrefix() { |
| 346 fail("Not yet tested"); |
| 347 // Need to set up the imported library so that the identifier can be |
| 348 // resolved. |
| 349 ImportDirective directive = AstFactory.importDirective3(null, null, [ |
| 350 AstFactory.showCombinator2(["A"]) |
| 351 ]); |
| 352 _resolveNode(directive); |
| 353 _listener.assertNoErrors(); |
| 354 } |
| 355 |
| 356 void fail_visitImportDirective_combinators_prefix() { |
| 357 fail("Not yet tested"); |
| 358 // Need to set up the imported library so that the identifiers can be |
| 359 // resolved. |
| 360 String prefixName = "p"; |
| 361 _definingLibrary.imports = <ImportElement>[ |
| 362 ElementFactory.importFor(null, ElementFactory.prefix(prefixName)) |
| 363 ]; |
| 364 ImportDirective directive = AstFactory.importDirective3(null, prefixName, [ |
| 365 AstFactory.showCombinator2(["A"]), |
| 366 AstFactory.hideCombinator2(["B"]) |
| 367 ]); |
| 368 _resolveNode(directive); |
| 369 _listener.assertNoErrors(); |
| 370 } |
| 371 |
| 372 void fail_visitRedirectingConstructorInvocation() { |
| 373 fail("Not yet tested"); |
| 374 _listener.assertNoErrors(); |
| 375 } |
| 376 |
| 377 @override |
| 378 void setUp() { |
| 379 super.setUp(); |
| 380 _listener = new GatheringErrorListener(); |
| 381 _typeProvider = new TestTypeProvider(); |
| 382 _resolver = _createResolver(); |
| 383 } |
| 384 |
| 385 void test_lookUpMethodInInterfaces() { |
| 386 InterfaceType intType = _typeProvider.intType; |
| 387 // |
| 388 // abstract class A { int operator[](int index); } |
| 389 // |
| 390 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 391 MethodElement operator = |
| 392 ElementFactory.methodElement("[]", intType, [intType]); |
| 393 classA.methods = <MethodElement>[operator]; |
| 394 // |
| 395 // class B implements A {} |
| 396 // |
| 397 ClassElementImpl classB = ElementFactory.classElement2("B"); |
| 398 classB.interfaces = <InterfaceType>[classA.type]; |
| 399 // |
| 400 // class C extends Object with B {} |
| 401 // |
| 402 ClassElementImpl classC = ElementFactory.classElement2("C"); |
| 403 classC.mixins = <InterfaceType>[classB.type]; |
| 404 // |
| 405 // class D extends C {} |
| 406 // |
| 407 ClassElementImpl classD = ElementFactory.classElement("D", classC.type); |
| 408 // |
| 409 // D a; |
| 410 // a[i]; |
| 411 // |
| 412 SimpleIdentifier array = AstFactory.identifier3("a"); |
| 413 array.staticType = classD.type; |
| 414 IndexExpression expression = |
| 415 AstFactory.indexExpression(array, AstFactory.identifier3("i")); |
| 416 expect(_resolveIndexExpression(expression), same(operator)); |
| 417 _listener.assertNoErrors(); |
| 418 } |
| 419 |
| 420 void test_visitAssignmentExpression_compound() { |
| 421 InterfaceType intType = _typeProvider.intType; |
| 422 SimpleIdentifier leftHandSide = AstFactory.identifier3("a"); |
| 423 leftHandSide.staticType = intType; |
| 424 AssignmentExpression assignment = AstFactory.assignmentExpression( |
| 425 leftHandSide, TokenType.PLUS_EQ, AstFactory.integer(1)); |
| 426 _resolveNode(assignment); |
| 427 expect( |
| 428 assignment.staticElement, same(getMethod(_typeProvider.numType, "+"))); |
| 429 _listener.assertNoErrors(); |
| 430 } |
| 431 |
| 432 void test_visitAssignmentExpression_simple() { |
| 433 AssignmentExpression expression = AstFactory.assignmentExpression( |
| 434 AstFactory.identifier3("x"), TokenType.EQ, AstFactory.integer(0)); |
| 435 _resolveNode(expression); |
| 436 expect(expression.staticElement, isNull); |
| 437 _listener.assertNoErrors(); |
| 438 } |
| 439 |
| 440 void test_visitBinaryExpression_bangEq() { |
| 441 // String i; |
| 442 // var j; |
| 443 // i == j |
| 444 InterfaceType stringType = _typeProvider.stringType; |
| 445 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 446 left.staticType = stringType; |
| 447 BinaryExpression expression = AstFactory.binaryExpression( |
| 448 left, TokenType.BANG_EQ, AstFactory.identifier3("j")); |
| 449 _resolveNode(expression); |
| 450 var stringElement = stringType.element; |
| 451 expect(expression.staticElement, isNotNull); |
| 452 expect( |
| 453 expression.staticElement, |
| 454 stringElement.lookUpMethod( |
| 455 TokenType.EQ_EQ.lexeme, stringElement.library)); |
| 456 expect(expression.propagatedElement, isNull); |
| 457 _listener.assertNoErrors(); |
| 458 } |
| 459 |
| 460 void test_visitBinaryExpression_eq() { |
| 461 // String i; |
| 462 // var j; |
| 463 // i == j |
| 464 InterfaceType stringType = _typeProvider.stringType; |
| 465 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 466 left.staticType = stringType; |
| 467 BinaryExpression expression = AstFactory.binaryExpression( |
| 468 left, TokenType.EQ_EQ, AstFactory.identifier3("j")); |
| 469 _resolveNode(expression); |
| 470 var stringElement = stringType.element; |
| 471 expect( |
| 472 expression.staticElement, |
| 473 stringElement.lookUpMethod( |
| 474 TokenType.EQ_EQ.lexeme, stringElement.library)); |
| 475 expect(expression.propagatedElement, isNull); |
| 476 _listener.assertNoErrors(); |
| 477 } |
| 478 |
| 479 void test_visitBinaryExpression_plus() { |
| 480 // num i; |
| 481 // var j; |
| 482 // i + j |
| 483 InterfaceType numType = _typeProvider.numType; |
| 484 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 485 left.staticType = numType; |
| 486 BinaryExpression expression = AstFactory.binaryExpression( |
| 487 left, TokenType.PLUS, AstFactory.identifier3("j")); |
| 488 _resolveNode(expression); |
| 489 expect(expression.staticElement, getMethod(numType, "+")); |
| 490 expect(expression.propagatedElement, isNull); |
| 491 _listener.assertNoErrors(); |
| 492 } |
| 493 |
| 494 void test_visitBinaryExpression_plus_propagatedElement() { |
| 495 // var i = 1; |
| 496 // var j; |
| 497 // i + j |
| 498 InterfaceType numType = _typeProvider.numType; |
| 499 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 500 left.propagatedType = numType; |
| 501 BinaryExpression expression = AstFactory.binaryExpression( |
| 502 left, TokenType.PLUS, AstFactory.identifier3("j")); |
| 503 _resolveNode(expression); |
| 504 expect(expression.staticElement, isNull); |
| 505 expect(expression.propagatedElement, getMethod(numType, "+")); |
| 506 _listener.assertNoErrors(); |
| 507 } |
| 508 |
| 509 void test_visitBreakStatement_withLabel() { |
| 510 // loop: while (true) { |
| 511 // break loop; |
| 512 // } |
| 513 String label = "loop"; |
| 514 LabelElementImpl labelElement = new LabelElementImpl.forNode( |
| 515 AstFactory.identifier3(label), false, false); |
| 516 BreakStatement breakStatement = AstFactory.breakStatement2(label); |
| 517 Expression condition = AstFactory.booleanLiteral(true); |
| 518 WhileStatement whileStatement = |
| 519 AstFactory.whileStatement(condition, breakStatement); |
| 520 expect(_resolveBreak(breakStatement, labelElement, whileStatement), |
| 521 same(labelElement)); |
| 522 expect(breakStatement.target, same(whileStatement)); |
| 523 _listener.assertNoErrors(); |
| 524 } |
| 525 |
| 526 void test_visitBreakStatement_withoutLabel() { |
| 527 BreakStatement statement = AstFactory.breakStatement(); |
| 528 _resolveStatement(statement, null, null); |
| 529 _listener.assertNoErrors(); |
| 530 } |
| 531 |
| 532 void test_visitCommentReference_prefixedIdentifier_class_getter() { |
| 533 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 534 // set accessors |
| 535 String propName = "p"; |
| 536 PropertyAccessorElement getter = |
| 537 ElementFactory.getterElement(propName, false, _typeProvider.intType); |
| 538 PropertyAccessorElement setter = |
| 539 ElementFactory.setterElement(propName, false, _typeProvider.intType); |
| 540 classA.accessors = <PropertyAccessorElement>[getter, setter]; |
| 541 // set name scope |
| 542 _visitor.nameScope = new EnclosedScope(null) |
| 543 ..defineNameWithoutChecking('A', classA); |
| 544 // prepare "A.p" |
| 545 PrefixedIdentifier prefixed = AstFactory.identifier5('A', 'p'); |
| 546 CommentReference commentReference = new CommentReference(null, prefixed); |
| 547 // resolve |
| 548 _resolveNode(commentReference); |
| 549 expect(prefixed.prefix.staticElement, classA); |
| 550 expect(prefixed.identifier.staticElement, getter); |
| 551 _listener.assertNoErrors(); |
| 552 } |
| 553 |
| 554 void test_visitCommentReference_prefixedIdentifier_class_method() { |
| 555 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 556 // set method |
| 557 MethodElement method = |
| 558 ElementFactory.methodElement("m", _typeProvider.intType); |
| 559 classA.methods = <MethodElement>[method]; |
| 560 // set name scope |
| 561 _visitor.nameScope = new EnclosedScope(null) |
| 562 ..defineNameWithoutChecking('A', classA); |
| 563 // prepare "A.m" |
| 564 PrefixedIdentifier prefixed = AstFactory.identifier5('A', 'm'); |
| 565 CommentReference commentReference = new CommentReference(null, prefixed); |
| 566 // resolve |
| 567 _resolveNode(commentReference); |
| 568 expect(prefixed.prefix.staticElement, classA); |
| 569 expect(prefixed.identifier.staticElement, method); |
| 570 _listener.assertNoErrors(); |
| 571 } |
| 572 |
| 573 void test_visitCommentReference_prefixedIdentifier_class_operator() { |
| 574 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 575 // set method |
| 576 MethodElement method = |
| 577 ElementFactory.methodElement("==", _typeProvider.boolType); |
| 578 classA.methods = <MethodElement>[method]; |
| 579 // set name scope |
| 580 _visitor.nameScope = new EnclosedScope(null) |
| 581 ..defineNameWithoutChecking('A', classA); |
| 582 // prepare "A.==" |
| 583 PrefixedIdentifier prefixed = AstFactory.identifier5('A', '=='); |
| 584 CommentReference commentReference = new CommentReference(null, prefixed); |
| 585 // resolve |
| 586 _resolveNode(commentReference); |
| 587 expect(prefixed.prefix.staticElement, classA); |
| 588 expect(prefixed.identifier.staticElement, method); |
| 589 _listener.assertNoErrors(); |
| 590 } |
| 591 |
| 592 void test_visitConstructorName_named() { |
| 593 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 594 String constructorName = "a"; |
| 595 ConstructorElement constructor = |
| 596 ElementFactory.constructorElement2(classA, constructorName); |
| 597 classA.constructors = <ConstructorElement>[constructor]; |
| 598 ConstructorName name = AstFactory.constructorName( |
| 599 AstFactory.typeName(classA), constructorName); |
| 600 _resolveNode(name); |
| 601 expect(name.staticElement, same(constructor)); |
| 602 _listener.assertNoErrors(); |
| 603 } |
| 604 |
| 605 void test_visitConstructorName_unnamed() { |
| 606 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 607 String constructorName = null; |
| 608 ConstructorElement constructor = |
| 609 ElementFactory.constructorElement2(classA, constructorName); |
| 610 classA.constructors = <ConstructorElement>[constructor]; |
| 611 ConstructorName name = AstFactory.constructorName( |
| 612 AstFactory.typeName(classA), constructorName); |
| 613 _resolveNode(name); |
| 614 expect(name.staticElement, same(constructor)); |
| 615 _listener.assertNoErrors(); |
| 616 } |
| 617 |
| 618 void test_visitContinueStatement_withLabel() { |
| 619 // loop: while (true) { |
| 620 // continue loop; |
| 621 // } |
| 622 String label = "loop"; |
| 623 LabelElementImpl labelElement = new LabelElementImpl.forNode( |
| 624 AstFactory.identifier3(label), false, false); |
| 625 ContinueStatement continueStatement = AstFactory.continueStatement(label); |
| 626 Expression condition = AstFactory.booleanLiteral(true); |
| 627 WhileStatement whileStatement = |
| 628 AstFactory.whileStatement(condition, continueStatement); |
| 629 expect(_resolveContinue(continueStatement, labelElement, whileStatement), |
| 630 same(labelElement)); |
| 631 expect(continueStatement.target, same(whileStatement)); |
| 632 _listener.assertNoErrors(); |
| 633 } |
| 634 |
| 635 void test_visitContinueStatement_withoutLabel() { |
| 636 ContinueStatement statement = AstFactory.continueStatement(); |
| 637 _resolveStatement(statement, null, null); |
| 638 _listener.assertNoErrors(); |
| 639 } |
| 640 |
| 641 void test_visitEnumDeclaration() { |
| 642 CompilationUnitElementImpl compilationUnitElement = |
| 643 ElementFactory.compilationUnit('foo.dart'); |
| 644 EnumElementImpl enumElement = |
| 645 ElementFactory.enumElement(_typeProvider, ('E')); |
| 646 compilationUnitElement.enums = <ClassElement>[enumElement]; |
| 647 EnumDeclaration enumNode = AstFactory.enumDeclaration2('E', []); |
| 648 Annotation annotationNode = |
| 649 AstFactory.annotation(AstFactory.identifier3('a')); |
| 650 annotationNode.element = ElementFactory.classElement2('A'); |
| 651 annotationNode.elementAnnotation = |
| 652 new ElementAnnotationImpl(compilationUnitElement); |
| 653 enumNode.metadata.add(annotationNode); |
| 654 enumNode.name.staticElement = enumElement; |
| 655 List<ElementAnnotation> metadata = <ElementAnnotation>[ |
| 656 annotationNode.elementAnnotation |
| 657 ]; |
| 658 _resolveNode(enumNode); |
| 659 expect(metadata[0].element, annotationNode.element); |
| 660 } |
| 661 |
| 662 void test_visitExportDirective_noCombinators() { |
| 663 ExportDirective directive = AstFactory.exportDirective2(null); |
| 664 directive.element = ElementFactory |
| 665 .exportFor(ElementFactory.library(_definingLibrary.context, "lib")); |
| 666 _resolveNode(directive); |
| 667 _listener.assertNoErrors(); |
| 668 } |
| 669 |
| 670 void test_visitFieldFormalParameter() { |
| 671 String fieldName = "f"; |
| 672 InterfaceType intType = _typeProvider.intType; |
| 673 FieldElementImpl fieldElement = |
| 674 ElementFactory.fieldElement(fieldName, false, false, false, intType); |
| 675 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 676 classA.fields = <FieldElement>[fieldElement]; |
| 677 FieldFormalParameter parameter = |
| 678 AstFactory.fieldFormalParameter2(fieldName); |
| 679 FieldFormalParameterElementImpl parameterElement = |
| 680 ElementFactory.fieldFormalParameter(parameter.identifier); |
| 681 parameterElement.field = fieldElement; |
| 682 parameterElement.type = intType; |
| 683 parameter.identifier.staticElement = parameterElement; |
| 684 _resolveInClass(parameter, classA); |
| 685 expect(parameter.element.type, same(intType)); |
| 686 } |
| 687 |
| 688 void test_visitImportDirective_noCombinators_noPrefix() { |
| 689 ImportDirective directive = AstFactory.importDirective3(null, null); |
| 690 directive.element = ElementFactory.importFor( |
| 691 ElementFactory.library(_definingLibrary.context, "lib"), null); |
| 692 _resolveNode(directive); |
| 693 _listener.assertNoErrors(); |
| 694 } |
| 695 |
| 696 void test_visitImportDirective_noCombinators_prefix() { |
| 697 String prefixName = "p"; |
| 698 ImportElement importElement = ElementFactory.importFor( |
| 699 ElementFactory.library(_definingLibrary.context, "lib"), |
| 700 ElementFactory.prefix(prefixName)); |
| 701 _definingLibrary.imports = <ImportElement>[importElement]; |
| 702 ImportDirective directive = AstFactory.importDirective3(null, prefixName); |
| 703 directive.element = importElement; |
| 704 _resolveNode(directive); |
| 705 _listener.assertNoErrors(); |
| 706 } |
| 707 |
| 708 void test_visitImportDirective_withCombinators() { |
| 709 ShowCombinator combinator = AstFactory.showCombinator2(["A", "B", "C"]); |
| 710 ImportDirective directive = |
| 711 AstFactory.importDirective3(null, null, [combinator]); |
| 712 LibraryElementImpl library = |
| 713 ElementFactory.library(_definingLibrary.context, "lib"); |
| 714 TopLevelVariableElementImpl varA = |
| 715 ElementFactory.topLevelVariableElement2("A"); |
| 716 TopLevelVariableElementImpl varB = |
| 717 ElementFactory.topLevelVariableElement2("B"); |
| 718 TopLevelVariableElementImpl varC = |
| 719 ElementFactory.topLevelVariableElement2("C"); |
| 720 CompilationUnitElementImpl unit = |
| 721 library.definingCompilationUnit as CompilationUnitElementImpl; |
| 722 unit.accessors = <PropertyAccessorElement>[ |
| 723 varA.getter, |
| 724 varA.setter, |
| 725 varB.getter, |
| 726 varC.setter |
| 727 ]; |
| 728 unit.topLevelVariables = <TopLevelVariableElement>[varA, varB, varC]; |
| 729 directive.element = ElementFactory.importFor(library, null); |
| 730 _resolveNode(directive); |
| 731 expect(combinator.shownNames[0].staticElement, same(varA)); |
| 732 expect(combinator.shownNames[1].staticElement, same(varB)); |
| 733 expect(combinator.shownNames[2].staticElement, same(varC)); |
| 734 _listener.assertNoErrors(); |
| 735 } |
| 736 |
| 737 void test_visitIndexExpression_get() { |
| 738 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 739 InterfaceType intType = _typeProvider.intType; |
| 740 MethodElement getter = |
| 741 ElementFactory.methodElement("[]", intType, [intType]); |
| 742 classA.methods = <MethodElement>[getter]; |
| 743 SimpleIdentifier array = AstFactory.identifier3("a"); |
| 744 array.staticType = classA.type; |
| 745 IndexExpression expression = |
| 746 AstFactory.indexExpression(array, AstFactory.identifier3("i")); |
| 747 expect(_resolveIndexExpression(expression), same(getter)); |
| 748 _listener.assertNoErrors(); |
| 749 } |
| 750 |
| 751 void test_visitIndexExpression_set() { |
| 752 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 753 InterfaceType intType = _typeProvider.intType; |
| 754 MethodElement setter = |
| 755 ElementFactory.methodElement("[]=", intType, [intType]); |
| 756 classA.methods = <MethodElement>[setter]; |
| 757 SimpleIdentifier array = AstFactory.identifier3("a"); |
| 758 array.staticType = classA.type; |
| 759 IndexExpression expression = |
| 760 AstFactory.indexExpression(array, AstFactory.identifier3("i")); |
| 761 AstFactory.assignmentExpression( |
| 762 expression, TokenType.EQ, AstFactory.integer(0)); |
| 763 expect(_resolveIndexExpression(expression), same(setter)); |
| 764 _listener.assertNoErrors(); |
| 765 } |
| 766 |
| 767 void test_visitInstanceCreationExpression_named() { |
| 768 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 769 String constructorName = "a"; |
| 770 ConstructorElement constructor = |
| 771 ElementFactory.constructorElement2(classA, constructorName); |
| 772 classA.constructors = <ConstructorElement>[constructor]; |
| 773 ConstructorName name = AstFactory.constructorName( |
| 774 AstFactory.typeName(classA), constructorName); |
| 775 name.staticElement = constructor; |
| 776 InstanceCreationExpression creation = |
| 777 AstFactory.instanceCreationExpression(Keyword.NEW, name); |
| 778 _resolveNode(creation); |
| 779 expect(creation.staticElement, same(constructor)); |
| 780 _listener.assertNoErrors(); |
| 781 } |
| 782 |
| 783 void test_visitInstanceCreationExpression_unnamed() { |
| 784 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 785 String constructorName = null; |
| 786 ConstructorElement constructor = |
| 787 ElementFactory.constructorElement2(classA, constructorName); |
| 788 classA.constructors = <ConstructorElement>[constructor]; |
| 789 ConstructorName name = AstFactory.constructorName( |
| 790 AstFactory.typeName(classA), constructorName); |
| 791 name.staticElement = constructor; |
| 792 InstanceCreationExpression creation = |
| 793 AstFactory.instanceCreationExpression(Keyword.NEW, name); |
| 794 _resolveNode(creation); |
| 795 expect(creation.staticElement, same(constructor)); |
| 796 _listener.assertNoErrors(); |
| 797 } |
| 798 |
| 799 void test_visitInstanceCreationExpression_unnamed_namedParameter() { |
| 800 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 801 String constructorName = null; |
| 802 ConstructorElementImpl constructor = |
| 803 ElementFactory.constructorElement2(classA, constructorName); |
| 804 String parameterName = "a"; |
| 805 ParameterElement parameter = ElementFactory.namedParameter(parameterName); |
| 806 constructor.parameters = <ParameterElement>[parameter]; |
| 807 classA.constructors = <ConstructorElement>[constructor]; |
| 808 ConstructorName name = AstFactory.constructorName( |
| 809 AstFactory.typeName(classA), constructorName); |
| 810 name.staticElement = constructor; |
| 811 InstanceCreationExpression creation = AstFactory.instanceCreationExpression( |
| 812 Keyword.NEW, |
| 813 name, |
| 814 [AstFactory.namedExpression2(parameterName, AstFactory.integer(0))]); |
| 815 _resolveNode(creation); |
| 816 expect(creation.staticElement, same(constructor)); |
| 817 expect( |
| 818 (creation.argumentList.arguments[0] as NamedExpression) |
| 819 .name |
| 820 .label |
| 821 .staticElement, |
| 822 same(parameter)); |
| 823 _listener.assertNoErrors(); |
| 824 } |
| 825 |
| 826 void test_visitMethodInvocation() { |
| 827 InterfaceType numType = _typeProvider.numType; |
| 828 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 829 left.staticType = numType; |
| 830 String methodName = "abs"; |
| 831 MethodInvocation invocation = AstFactory.methodInvocation(left, methodName); |
| 832 _resolveNode(invocation); |
| 833 expect(invocation.methodName.staticElement, |
| 834 same(getMethod(numType, methodName))); |
| 835 _listener.assertNoErrors(); |
| 836 } |
| 837 |
| 838 void test_visitMethodInvocation_namedParameter() { |
| 839 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 840 String methodName = "m"; |
| 841 String parameterName = "p"; |
| 842 MethodElementImpl method = ElementFactory.methodElement(methodName, null); |
| 843 ParameterElement parameter = ElementFactory.namedParameter(parameterName); |
| 844 method.parameters = <ParameterElement>[parameter]; |
| 845 classA.methods = <MethodElement>[method]; |
| 846 SimpleIdentifier left = AstFactory.identifier3("i"); |
| 847 left.staticType = classA.type; |
| 848 MethodInvocation invocation = AstFactory.methodInvocation(left, methodName, |
| 849 [AstFactory.namedExpression2(parameterName, AstFactory.integer(0))]); |
| 850 _resolveNode(invocation); |
| 851 expect(invocation.methodName.staticElement, same(method)); |
| 852 expect( |
| 853 (invocation.argumentList.arguments[0] as NamedExpression) |
| 854 .name |
| 855 .label |
| 856 .staticElement, |
| 857 same(parameter)); |
| 858 _listener.assertNoErrors(); |
| 859 } |
| 860 |
| 861 void test_visitPostfixExpression() { |
| 862 InterfaceType numType = _typeProvider.numType; |
| 863 SimpleIdentifier operand = AstFactory.identifier3("i"); |
| 864 operand.staticType = numType; |
| 865 PostfixExpression expression = |
| 866 AstFactory.postfixExpression(operand, TokenType.PLUS_PLUS); |
| 867 _resolveNode(expression); |
| 868 expect(expression.staticElement, getMethod(numType, "+")); |
| 869 _listener.assertNoErrors(); |
| 870 } |
| 871 |
| 872 void test_visitPrefixedIdentifier_dynamic() { |
| 873 DartType dynamicType = _typeProvider.dynamicType; |
| 874 SimpleIdentifier target = AstFactory.identifier3("a"); |
| 875 VariableElementImpl variable = ElementFactory.localVariableElement(target); |
| 876 variable.type = dynamicType; |
| 877 target.staticElement = variable; |
| 878 target.staticType = dynamicType; |
| 879 PrefixedIdentifier identifier = |
| 880 AstFactory.identifier(target, AstFactory.identifier3("b")); |
| 881 _resolveNode(identifier); |
| 882 expect(identifier.staticElement, isNull); |
| 883 expect(identifier.identifier.staticElement, isNull); |
| 884 _listener.assertNoErrors(); |
| 885 } |
| 886 |
| 887 void test_visitPrefixedIdentifier_nonDynamic() { |
| 888 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 889 String getterName = "b"; |
| 890 PropertyAccessorElement getter = |
| 891 ElementFactory.getterElement(getterName, false, _typeProvider.intType); |
| 892 classA.accessors = <PropertyAccessorElement>[getter]; |
| 893 SimpleIdentifier target = AstFactory.identifier3("a"); |
| 894 VariableElementImpl variable = ElementFactory.localVariableElement(target); |
| 895 variable.type = classA.type; |
| 896 target.staticElement = variable; |
| 897 target.staticType = classA.type; |
| 898 PrefixedIdentifier identifier = |
| 899 AstFactory.identifier(target, AstFactory.identifier3(getterName)); |
| 900 _resolveNode(identifier); |
| 901 expect(identifier.staticElement, same(getter)); |
| 902 expect(identifier.identifier.staticElement, same(getter)); |
| 903 _listener.assertNoErrors(); |
| 904 } |
| 905 |
| 906 void test_visitPrefixedIdentifier_staticClassMember_getter() { |
| 907 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 908 // set accessors |
| 909 String propName = "b"; |
| 910 PropertyAccessorElement getter = |
| 911 ElementFactory.getterElement(propName, false, _typeProvider.intType); |
| 912 PropertyAccessorElement setter = |
| 913 ElementFactory.setterElement(propName, false, _typeProvider.intType); |
| 914 classA.accessors = <PropertyAccessorElement>[getter, setter]; |
| 915 // prepare "A.m" |
| 916 SimpleIdentifier target = AstFactory.identifier3("A"); |
| 917 target.staticElement = classA; |
| 918 target.staticType = classA.type; |
| 919 PrefixedIdentifier identifier = |
| 920 AstFactory.identifier(target, AstFactory.identifier3(propName)); |
| 921 // resolve |
| 922 _resolveNode(identifier); |
| 923 expect(identifier.staticElement, same(getter)); |
| 924 expect(identifier.identifier.staticElement, same(getter)); |
| 925 _listener.assertNoErrors(); |
| 926 } |
| 927 |
| 928 void test_visitPrefixedIdentifier_staticClassMember_method() { |
| 929 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 930 // set methods |
| 931 String propName = "m"; |
| 932 MethodElement method = |
| 933 ElementFactory.methodElement("m", _typeProvider.intType); |
| 934 classA.methods = <MethodElement>[method]; |
| 935 // prepare "A.m" |
| 936 SimpleIdentifier target = AstFactory.identifier3("A"); |
| 937 target.staticElement = classA; |
| 938 target.staticType = classA.type; |
| 939 PrefixedIdentifier identifier = |
| 940 AstFactory.identifier(target, AstFactory.identifier3(propName)); |
| 941 AstFactory.assignmentExpression( |
| 942 identifier, TokenType.EQ, AstFactory.nullLiteral()); |
| 943 // resolve |
| 944 _resolveNode(identifier); |
| 945 expect(identifier.staticElement, same(method)); |
| 946 expect(identifier.identifier.staticElement, same(method)); |
| 947 _listener.assertNoErrors(); |
| 948 } |
| 949 |
| 950 void test_visitPrefixedIdentifier_staticClassMember_setter() { |
| 951 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 952 // set accessors |
| 953 String propName = "b"; |
| 954 PropertyAccessorElement getter = |
| 955 ElementFactory.getterElement(propName, false, _typeProvider.intType); |
| 956 PropertyAccessorElement setter = |
| 957 ElementFactory.setterElement(propName, false, _typeProvider.intType); |
| 958 classA.accessors = <PropertyAccessorElement>[getter, setter]; |
| 959 // prepare "A.b = null" |
| 960 SimpleIdentifier target = AstFactory.identifier3("A"); |
| 961 target.staticElement = classA; |
| 962 target.staticType = classA.type; |
| 963 PrefixedIdentifier identifier = |
| 964 AstFactory.identifier(target, AstFactory.identifier3(propName)); |
| 965 AstFactory.assignmentExpression( |
| 966 identifier, TokenType.EQ, AstFactory.nullLiteral()); |
| 967 // resolve |
| 968 _resolveNode(identifier); |
| 969 expect(identifier.staticElement, same(setter)); |
| 970 expect(identifier.identifier.staticElement, same(setter)); |
| 971 _listener.assertNoErrors(); |
| 972 } |
| 973 |
| 974 void test_visitPrefixExpression() { |
| 975 InterfaceType numType = _typeProvider.numType; |
| 976 SimpleIdentifier operand = AstFactory.identifier3("i"); |
| 977 operand.staticType = numType; |
| 978 PrefixExpression expression = |
| 979 AstFactory.prefixExpression(TokenType.PLUS_PLUS, operand); |
| 980 _resolveNode(expression); |
| 981 expect(expression.staticElement, getMethod(numType, "+")); |
| 982 _listener.assertNoErrors(); |
| 983 } |
| 984 |
| 985 void test_visitPropertyAccess_getter_identifier() { |
| 986 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 987 String getterName = "b"; |
| 988 PropertyAccessorElement getter = |
| 989 ElementFactory.getterElement(getterName, false, _typeProvider.intType); |
| 990 classA.accessors = <PropertyAccessorElement>[getter]; |
| 991 SimpleIdentifier target = AstFactory.identifier3("a"); |
| 992 target.staticType = classA.type; |
| 993 PropertyAccess access = AstFactory.propertyAccess2(target, getterName); |
| 994 _resolveNode(access); |
| 995 expect(access.propertyName.staticElement, same(getter)); |
| 996 _listener.assertNoErrors(); |
| 997 } |
| 998 |
| 999 void test_visitPropertyAccess_getter_super() { |
| 1000 // |
| 1001 // class A { |
| 1002 // int get b; |
| 1003 // } |
| 1004 // class B { |
| 1005 // ... super.m ... |
| 1006 // } |
| 1007 // |
| 1008 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 1009 String getterName = "b"; |
| 1010 PropertyAccessorElement getter = |
| 1011 ElementFactory.getterElement(getterName, false, _typeProvider.intType); |
| 1012 classA.accessors = <PropertyAccessorElement>[getter]; |
| 1013 SuperExpression target = AstFactory.superExpression(); |
| 1014 target.staticType = ElementFactory.classElement("B", classA.type).type; |
| 1015 PropertyAccess access = AstFactory.propertyAccess2(target, getterName); |
| 1016 AstFactory.methodDeclaration2( |
| 1017 null, |
| 1018 null, |
| 1019 null, |
| 1020 null, |
| 1021 AstFactory.identifier3("m"), |
| 1022 AstFactory.formalParameterList(), |
| 1023 AstFactory.expressionFunctionBody(access)); |
| 1024 _resolveNode(access); |
| 1025 expect(access.propertyName.staticElement, same(getter)); |
| 1026 _listener.assertNoErrors(); |
| 1027 } |
| 1028 |
| 1029 void test_visitPropertyAccess_setter_this() { |
| 1030 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 1031 String setterName = "b"; |
| 1032 PropertyAccessorElement setter = |
| 1033 ElementFactory.setterElement(setterName, false, _typeProvider.intType); |
| 1034 classA.accessors = <PropertyAccessorElement>[setter]; |
| 1035 ThisExpression target = AstFactory.thisExpression(); |
| 1036 target.staticType = classA.type; |
| 1037 PropertyAccess access = AstFactory.propertyAccess2(target, setterName); |
| 1038 AstFactory.assignmentExpression( |
| 1039 access, TokenType.EQ, AstFactory.integer(0)); |
| 1040 _resolveNode(access); |
| 1041 expect(access.propertyName.staticElement, same(setter)); |
| 1042 _listener.assertNoErrors(); |
| 1043 } |
| 1044 |
| 1045 void test_visitSimpleIdentifier_classScope() { |
| 1046 InterfaceType doubleType = _typeProvider.doubleType; |
| 1047 String fieldName = "NAN"; |
| 1048 SimpleIdentifier node = AstFactory.identifier3(fieldName); |
| 1049 _resolveInClass(node, doubleType.element); |
| 1050 expect(node.staticElement, getGetter(doubleType, fieldName)); |
| 1051 _listener.assertNoErrors(); |
| 1052 } |
| 1053 |
| 1054 void test_visitSimpleIdentifier_dynamic() { |
| 1055 SimpleIdentifier node = AstFactory.identifier3("dynamic"); |
| 1056 _resolveIdentifier(node); |
| 1057 expect(node.staticElement, same(_typeProvider.dynamicType.element)); |
| 1058 expect(node.staticType, same(_typeProvider.typeType)); |
| 1059 _listener.assertNoErrors(); |
| 1060 } |
| 1061 |
| 1062 void test_visitSimpleIdentifier_lexicalScope() { |
| 1063 SimpleIdentifier node = AstFactory.identifier3("i"); |
| 1064 VariableElementImpl element = ElementFactory.localVariableElement(node); |
| 1065 expect(_resolveIdentifier(node, [element]), same(element)); |
| 1066 _listener.assertNoErrors(); |
| 1067 } |
| 1068 |
| 1069 void test_visitSimpleIdentifier_lexicalScope_field_setter() { |
| 1070 InterfaceType intType = _typeProvider.intType; |
| 1071 ClassElementImpl classA = ElementFactory.classElement2("A"); |
| 1072 String fieldName = "a"; |
| 1073 FieldElement field = |
| 1074 ElementFactory.fieldElement(fieldName, false, false, false, intType); |
| 1075 classA.fields = <FieldElement>[field]; |
| 1076 classA.accessors = <PropertyAccessorElement>[field.getter, field.setter]; |
| 1077 SimpleIdentifier node = AstFactory.identifier3(fieldName); |
| 1078 AstFactory.assignmentExpression(node, TokenType.EQ, AstFactory.integer(0)); |
| 1079 _resolveInClass(node, classA); |
| 1080 Element element = node.staticElement; |
| 1081 EngineTestCase.assertInstanceOf((obj) => obj is PropertyAccessorElement, |
| 1082 PropertyAccessorElement, element); |
| 1083 expect((element as PropertyAccessorElement).isSetter, isTrue); |
| 1084 _listener.assertNoErrors(); |
| 1085 } |
| 1086 |
| 1087 void test_visitSuperConstructorInvocation() { |
| 1088 ClassElementImpl superclass = ElementFactory.classElement2("A"); |
| 1089 ConstructorElementImpl superConstructor = |
| 1090 ElementFactory.constructorElement2(superclass, null); |
| 1091 superclass.constructors = <ConstructorElement>[superConstructor]; |
| 1092 ClassElementImpl subclass = |
| 1093 ElementFactory.classElement("B", superclass.type); |
| 1094 ConstructorElementImpl subConstructor = |
| 1095 ElementFactory.constructorElement2(subclass, null); |
| 1096 subclass.constructors = <ConstructorElement>[subConstructor]; |
| 1097 SuperConstructorInvocation invocation = |
| 1098 AstFactory.superConstructorInvocation(); |
| 1099 AstFactory.classDeclaration(null, 'C', null, null, null, null, [ |
| 1100 AstFactory.constructorDeclaration(null, 'C', null, [invocation]) |
| 1101 ]); |
| 1102 _resolveInClass(invocation, subclass); |
| 1103 expect(invocation.staticElement, superConstructor); |
| 1104 _listener.assertNoErrors(); |
| 1105 } |
| 1106 |
| 1107 void test_visitSuperConstructorInvocation_namedParameter() { |
| 1108 ClassElementImpl superclass = ElementFactory.classElement2("A"); |
| 1109 ConstructorElementImpl superConstructor = |
| 1110 ElementFactory.constructorElement2(superclass, null); |
| 1111 String parameterName = "p"; |
| 1112 ParameterElement parameter = ElementFactory.namedParameter(parameterName); |
| 1113 superConstructor.parameters = <ParameterElement>[parameter]; |
| 1114 superclass.constructors = <ConstructorElement>[superConstructor]; |
| 1115 ClassElementImpl subclass = |
| 1116 ElementFactory.classElement("B", superclass.type); |
| 1117 ConstructorElementImpl subConstructor = |
| 1118 ElementFactory.constructorElement2(subclass, null); |
| 1119 subclass.constructors = <ConstructorElement>[subConstructor]; |
| 1120 SuperConstructorInvocation invocation = AstFactory |
| 1121 .superConstructorInvocation([ |
| 1122 AstFactory.namedExpression2(parameterName, AstFactory.integer(0)) |
| 1123 ]); |
| 1124 AstFactory.classDeclaration(null, 'C', null, null, null, null, [ |
| 1125 AstFactory.constructorDeclaration(null, 'C', null, [invocation]) |
| 1126 ]); |
| 1127 _resolveInClass(invocation, subclass); |
| 1128 expect(invocation.staticElement, superConstructor); |
| 1129 expect( |
| 1130 (invocation.argumentList.arguments[0] as NamedExpression) |
| 1131 .name |
| 1132 .label |
| 1133 .staticElement, |
| 1134 same(parameter)); |
| 1135 _listener.assertNoErrors(); |
| 1136 } |
| 1137 |
| 1138 /** |
| 1139 * Create and return the resolver used by the tests. |
| 1140 */ |
| 1141 ElementResolver _createResolver() { |
| 1142 MemoryResourceProvider resourceProvider = new MemoryResourceProvider(); |
| 1143 InternalAnalysisContext context = AnalysisContextFactory.contextWithCore( |
| 1144 resourceProvider: resourceProvider); |
| 1145 Source source = new FileSource(resourceProvider.getFile("/test.dart")); |
| 1146 CompilationUnitElementImpl unit = |
| 1147 new CompilationUnitElementImpl("test.dart"); |
| 1148 unit.librarySource = unit.source = source; |
| 1149 _definingLibrary = ElementFactory.library(context, "test"); |
| 1150 _definingLibrary.definingCompilationUnit = unit; |
| 1151 _visitor = new ResolverVisitor( |
| 1152 _definingLibrary, source, _typeProvider, _listener, |
| 1153 nameScope: new LibraryScope(_definingLibrary)); |
| 1154 return _visitor.elementResolver; |
| 1155 } |
| 1156 |
| 1157 /** |
| 1158 * Return the element associated with the label of [statement] after the |
| 1159 * resolver has resolved it. [labelElement] is the label element to be |
| 1160 * defined in the statement's label scope, and [labelTarget] is the statement |
| 1161 * the label resolves to. |
| 1162 */ |
| 1163 Element _resolveBreak(BreakStatement statement, LabelElementImpl labelElement, |
| 1164 Statement labelTarget) { |
| 1165 _resolveStatement(statement, labelElement, labelTarget); |
| 1166 return statement.label.staticElement; |
| 1167 } |
| 1168 |
| 1169 /** |
| 1170 * Return the element associated with the label [statement] after the |
| 1171 * resolver has resolved it. [labelElement] is the label element to be |
| 1172 * defined in the statement's label scope, and [labelTarget] is the AST node |
| 1173 * the label resolves to. |
| 1174 * |
| 1175 * @param statement the statement to be resolved |
| 1176 * @param labelElement the label element to be defined in the statement's labe
l scope |
| 1177 * @return the element to which the statement's label was resolved |
| 1178 */ |
| 1179 Element _resolveContinue(ContinueStatement statement, |
| 1180 LabelElementImpl labelElement, AstNode labelTarget) { |
| 1181 _resolveStatement(statement, labelElement, labelTarget); |
| 1182 return statement.label.staticElement; |
| 1183 } |
| 1184 |
| 1185 /** |
| 1186 * Return the element associated with the given identifier after the resolver
has resolved the |
| 1187 * identifier. |
| 1188 * |
| 1189 * @param node the expression to be resolved |
| 1190 * @param definedElements the elements that are to be defined in the scope in
which the element is |
| 1191 * being resolved |
| 1192 * @return the element to which the expression was resolved |
| 1193 */ |
| 1194 Element _resolveIdentifier(Identifier node, [List<Element> definedElements]) { |
| 1195 _resolveNode(node, definedElements); |
| 1196 return node.staticElement; |
| 1197 } |
| 1198 |
| 1199 /** |
| 1200 * Return the element associated with the given identifier after the resolver
has resolved the |
| 1201 * identifier. |
| 1202 * |
| 1203 * @param node the expression to be resolved |
| 1204 * @param enclosingClass the element representing the class enclosing the iden
tifier |
| 1205 * @return the element to which the expression was resolved |
| 1206 */ |
| 1207 void _resolveInClass(AstNode node, ClassElement enclosingClass) { |
| 1208 Scope outerScope = _visitor.nameScope; |
| 1209 try { |
| 1210 _visitor.enclosingClass = enclosingClass; |
| 1211 EnclosedScope innerScope = new ClassScope( |
| 1212 new TypeParameterScope(outerScope, enclosingClass), enclosingClass); |
| 1213 _visitor.nameScope = innerScope; |
| 1214 node.accept(_resolver); |
| 1215 } finally { |
| 1216 _visitor.enclosingClass = null; |
| 1217 _visitor.nameScope = outerScope; |
| 1218 } |
| 1219 } |
| 1220 |
| 1221 /** |
| 1222 * Return the element associated with the given expression after the resolver
has resolved the |
| 1223 * expression. |
| 1224 * |
| 1225 * @param node the expression to be resolved |
| 1226 * @param definedElements the elements that are to be defined in the scope in
which the element is |
| 1227 * being resolved |
| 1228 * @return the element to which the expression was resolved |
| 1229 */ |
| 1230 Element _resolveIndexExpression(IndexExpression node, |
| 1231 [List<Element> definedElements]) { |
| 1232 _resolveNode(node, definedElements); |
| 1233 return node.staticElement; |
| 1234 } |
| 1235 |
| 1236 /** |
| 1237 * Return the element associated with the given identifier after the resolver
has resolved the |
| 1238 * identifier. |
| 1239 * |
| 1240 * @param node the expression to be resolved |
| 1241 * @param definedElements the elements that are to be defined in the scope in
which the element is |
| 1242 * being resolved |
| 1243 * @return the element to which the expression was resolved |
| 1244 */ |
| 1245 void _resolveNode(AstNode node, [List<Element> definedElements]) { |
| 1246 Scope outerScope = _visitor.nameScope; |
| 1247 try { |
| 1248 EnclosedScope innerScope = new EnclosedScope(outerScope); |
| 1249 if (definedElements != null) { |
| 1250 for (Element element in definedElements) { |
| 1251 innerScope.define(element); |
| 1252 } |
| 1253 } |
| 1254 _visitor.nameScope = innerScope; |
| 1255 node.accept(_resolver); |
| 1256 } finally { |
| 1257 _visitor.nameScope = outerScope; |
| 1258 } |
| 1259 } |
| 1260 |
| 1261 /** |
| 1262 * Return the element associated with the label of the given statement after t
he resolver has |
| 1263 * resolved the statement. |
| 1264 * |
| 1265 * @param statement the statement to be resolved |
| 1266 * @param labelElement the label element to be defined in the statement's labe
l scope |
| 1267 * @return the element to which the statement's label was resolved |
| 1268 */ |
| 1269 void _resolveStatement( |
| 1270 Statement statement, LabelElementImpl labelElement, AstNode labelTarget) { |
| 1271 LabelScope outerScope = _visitor.labelScope; |
| 1272 try { |
| 1273 LabelScope innerScope; |
| 1274 if (labelElement == null) { |
| 1275 innerScope = outerScope; |
| 1276 } else { |
| 1277 innerScope = new LabelScope( |
| 1278 outerScope, labelElement.name, labelTarget, labelElement); |
| 1279 } |
| 1280 _visitor.labelScope = innerScope; |
| 1281 statement.accept(_resolver); |
| 1282 } finally { |
| 1283 _visitor.labelScope = outerScope; |
| 1284 } |
| 1285 } |
| 1286 } |
| OLD | NEW |