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 analyzer.test.generated.all_the_rest_test; | 5 library analyzer.test.generated.all_the_rest_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
10 import 'package:analyzer/dart/element/type.dart'; | 10 import 'package:analyzer/dart/element/type.dart'; |
11 import 'package:analyzer/error/error.dart'; | 11 import 'package:analyzer/error/error.dart'; |
12 import 'package:analyzer/error/listener.dart'; | 12 import 'package:analyzer/error/listener.dart'; |
13 import 'package:analyzer/file_system/file_system.dart'; | 13 import 'package:analyzer/file_system/file_system.dart'; |
14 import 'package:analyzer/file_system/memory_file_system.dart'; | 14 import 'package:analyzer/file_system/memory_file_system.dart'; |
15 import 'package:analyzer/file_system/physical_file_system.dart'; | 15 import 'package:analyzer/file_system/physical_file_system.dart'; |
16 import 'package:analyzer/src/dart/ast/ast.dart'; | |
17 import 'package:analyzer/src/dart/ast/utilities.dart' hide ConstantEvaluator; | 16 import 'package:analyzer/src/dart/ast/utilities.dart' hide ConstantEvaluator; |
18 import 'package:analyzer/src/dart/element/builder.dart'; | 17 import 'package:analyzer/src/dart/element/builder.dart'; |
19 import 'package:analyzer/src/dart/element/element.dart'; | 18 import 'package:analyzer/src/dart/element/element.dart'; |
20 import 'package:analyzer/src/dart/sdk/sdk.dart' hide SdkLibrariesReader; | 19 import 'package:analyzer/src/dart/sdk/sdk.dart' hide SdkLibrariesReader; |
21 import 'package:analyzer/src/error/codes.dart'; | 20 import 'package:analyzer/src/error/codes.dart'; |
22 import 'package:analyzer/src/generated/engine.dart'; | 21 import 'package:analyzer/src/generated/engine.dart'; |
23 import 'package:analyzer/src/generated/java_engine_io.dart'; | 22 import 'package:analyzer/src/generated/java_engine_io.dart'; |
24 import 'package:analyzer/src/generated/java_io.dart'; | 23 import 'package:analyzer/src/generated/java_io.dart'; |
25 import 'package:analyzer/src/generated/resolver.dart'; | 24 import 'package:analyzer/src/generated/resolver.dart'; |
26 import 'package:analyzer/src/generated/sdk.dart'; | 25 import 'package:analyzer/src/generated/sdk.dart'; |
(...skipping 18 matching lines...) Expand all Loading... |
45 main() { | 44 main() { |
46 defineReflectiveSuite(() { | 45 defineReflectiveSuite(() { |
47 defineReflectiveTests(ContentCacheTest); | 46 defineReflectiveTests(ContentCacheTest); |
48 // ignore: deprecated_member_use | 47 // ignore: deprecated_member_use |
49 defineReflectiveTests(CustomUriResolverTest); | 48 defineReflectiveTests(CustomUriResolverTest); |
50 defineReflectiveTests(DartUriResolverTest); | 49 defineReflectiveTests(DartUriResolverTest); |
51 // ignore: deprecated_member_use | 50 // ignore: deprecated_member_use |
52 defineReflectiveTests(DirectoryBasedDartSdkTest); | 51 defineReflectiveTests(DirectoryBasedDartSdkTest); |
53 // ignore: deprecated_member_use | 52 // ignore: deprecated_member_use |
54 defineReflectiveTests(DirectoryBasedSourceContainerTest); | 53 defineReflectiveTests(DirectoryBasedSourceContainerTest); |
55 defineReflectiveTests(ElementBuilderTest); | |
56 defineReflectiveTests(ElementLocatorTest); | 54 defineReflectiveTests(ElementLocatorTest); |
57 defineReflectiveTests(EnumMemberBuilderTest); | 55 defineReflectiveTests(EnumMemberBuilderTest); |
58 defineReflectiveTests(ErrorReporterTest); | 56 defineReflectiveTests(ErrorReporterTest); |
59 defineReflectiveTests(ErrorSeverityTest); | 57 defineReflectiveTests(ErrorSeverityTest); |
60 defineReflectiveTests(ExitDetectorTest); | 58 defineReflectiveTests(ExitDetectorTest); |
61 defineReflectiveTests(ExitDetectorTest2); | 59 defineReflectiveTests(ExitDetectorTest2); |
62 defineReflectiveTests(FileBasedSourceTest); | 60 defineReflectiveTests(FileBasedSourceTest); |
63 defineReflectiveTests(ResolveRelativeUriTest); | 61 defineReflectiveTests(ResolveRelativeUriTest); |
64 // ignore: deprecated_member_use | 62 // ignore: deprecated_member_use |
65 defineReflectiveTests(SDKLibrariesReaderTest); | 63 defineReflectiveTests(SDKLibrariesReaderTest); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 Source source3 = new FileSource(file3); | 343 Source source3 = new FileSource(file3); |
346 DirectoryBasedSourceContainer container = | 344 DirectoryBasedSourceContainer container = |
347 new DirectoryBasedSourceContainer.con2('/does/not/exist'); | 345 new DirectoryBasedSourceContainer.con2('/does/not/exist'); |
348 expect(container.contains(source1), isTrue); | 346 expect(container.contains(source1), isTrue); |
349 expect(container.contains(source2), isTrue); | 347 expect(container.contains(source2), isTrue); |
350 expect(container.contains(source3), isFalse); | 348 expect(container.contains(source3), isFalse); |
351 } | 349 } |
352 } | 350 } |
353 | 351 |
354 @reflectiveTest | 352 @reflectiveTest |
355 class ElementBuilderTest extends ParserTestCase { | |
356 CompilationUnitElement compilationUnitElement; | |
357 CompilationUnit compilationUnit; | |
358 | |
359 /** | |
360 * Parse the given [code], pass it through [ElementBuilder], and return the | |
361 * resulting [ElementHolder]. | |
362 */ | |
363 ElementHolder buildElementsForText(String code) { | |
364 TestLogger logger = new TestLogger(); | |
365 AnalysisEngine.instance.logger = logger; | |
366 try { | |
367 compilationUnit = ParserTestCase.parseCompilationUnit(code); | |
368 ElementHolder holder = new ElementHolder(); | |
369 ElementBuilder builder = | |
370 new ElementBuilder(holder, compilationUnitElement); | |
371 compilationUnit.accept(builder); | |
372 return holder; | |
373 } finally { | |
374 expect(logger.log, hasLength(0)); | |
375 AnalysisEngine.instance.logger = Logger.NULL; | |
376 } | |
377 } | |
378 | |
379 /** | |
380 * Verify that the given [metadata] has exactly one annotation, and that its | |
381 * [ElementAnnotationImpl] is unresolved. | |
382 */ | |
383 void checkAnnotation(NodeList<Annotation> metadata) { | |
384 expect(metadata, hasLength(1)); | |
385 expect(metadata[0], new isInstanceOf<AnnotationImpl>()); | |
386 AnnotationImpl annotation = metadata[0]; | |
387 expect(annotation.elementAnnotation, | |
388 new isInstanceOf<ElementAnnotationImpl>()); | |
389 ElementAnnotationImpl elementAnnotation = annotation.elementAnnotation; | |
390 expect(elementAnnotation.element, isNull); // Not yet resolved | |
391 expect(elementAnnotation.compilationUnit, isNotNull); | |
392 expect(elementAnnotation.compilationUnit, compilationUnitElement); | |
393 } | |
394 | |
395 /** | |
396 * Verify that the given [element] has exactly one annotation, and that its | |
397 * [ElementAnnotationImpl] is unresolved. | |
398 */ | |
399 void checkMetadata(Element element) { | |
400 expect(element.metadata, hasLength(1)); | |
401 expect(element.metadata[0], new isInstanceOf<ElementAnnotationImpl>()); | |
402 ElementAnnotationImpl elementAnnotation = element.metadata[0]; | |
403 expect(elementAnnotation.element, isNull); // Not yet resolved | |
404 expect(elementAnnotation.compilationUnit, isNotNull); | |
405 expect(elementAnnotation.compilationUnit, compilationUnitElement); | |
406 } | |
407 | |
408 void fail_visitMethodDeclaration_setter_duplicate() { | |
409 // https://github.com/dart-lang/sdk/issues/25601 | |
410 String code = r''' | |
411 class C { | |
412 set zzz(x) {} | |
413 set zzz(y) {} | |
414 } | |
415 '''; | |
416 ClassElement classElement = buildElementsForText(code).types[0]; | |
417 for (PropertyAccessorElement accessor in classElement.accessors) { | |
418 expect(accessor.variable.setter, same(accessor)); | |
419 } | |
420 } | |
421 | |
422 @override | |
423 void setUp() { | |
424 super.setUp(); | |
425 compilationUnitElement = new CompilationUnitElementImpl('test.dart'); | |
426 } | |
427 | |
428 void test_metadata_fieldDeclaration() { | |
429 List<FieldElement> fields = | |
430 buildElementsForText('class C { @a int x, y; }').types[0].fields; | |
431 checkMetadata(fields[0]); | |
432 checkMetadata(fields[1]); | |
433 expect(fields[0].metadata, same(fields[1].metadata)); | |
434 } | |
435 | |
436 void test_metadata_localVariableDeclaration() { | |
437 List<LocalVariableElement> localVariables = | |
438 buildElementsForText('f() { @a int x, y; }') | |
439 .functions[0] | |
440 .localVariables; | |
441 checkMetadata(localVariables[0]); | |
442 checkMetadata(localVariables[1]); | |
443 expect(localVariables[0].metadata, same(localVariables[1].metadata)); | |
444 } | |
445 | |
446 void test_metadata_topLevelVariableDeclaration() { | |
447 List<TopLevelVariableElement> topLevelVariables = | |
448 buildElementsForText('@a int x, y;').topLevelVariables; | |
449 checkMetadata(topLevelVariables[0]); | |
450 checkMetadata(topLevelVariables[1]); | |
451 expect(topLevelVariables[0].metadata, same(topLevelVariables[1].metadata)); | |
452 } | |
453 | |
454 void test_metadata_visitClassDeclaration() { | |
455 ClassElement classElement = buildElementsForText('@a class C {}').types[0]; | |
456 checkMetadata(classElement); | |
457 } | |
458 | |
459 void test_metadata_visitClassTypeAlias() { | |
460 ClassElement classElement = | |
461 buildElementsForText('@a class C = D with E;').types[0]; | |
462 checkMetadata(classElement); | |
463 } | |
464 | |
465 void test_metadata_visitConstructorDeclaration() { | |
466 ConstructorElement constructorElement = | |
467 buildElementsForText('class C { @a C(); }').types[0].constructors[0]; | |
468 checkMetadata(constructorElement); | |
469 } | |
470 | |
471 void test_metadata_visitDeclaredIdentifier() { | |
472 LocalVariableElement localVariableElement = | |
473 buildElementsForText('f() { for (@a var x in y) {} }') | |
474 .functions[0] | |
475 .localVariables[0]; | |
476 checkMetadata(localVariableElement); | |
477 } | |
478 | |
479 void test_metadata_visitDefaultFormalParameter_fieldFormalParameter() { | |
480 ParameterElement parameterElement = | |
481 buildElementsForText('class C { var x; C([@a this.x = null]); }') | |
482 .types[0] | |
483 .constructors[0] | |
484 .parameters[0]; | |
485 checkMetadata(parameterElement); | |
486 } | |
487 | |
488 void | |
489 test_metadata_visitDefaultFormalParameter_functionTypedFormalParameter() { | |
490 ParameterElement parameterElement = | |
491 buildElementsForText('f([@a g() = null]) {}').functions[0].parameters[ | |
492 0]; | |
493 checkMetadata(parameterElement); | |
494 } | |
495 | |
496 void test_metadata_visitDefaultFormalParameter_simpleFormalParameter() { | |
497 ParameterElement parameterElement = | |
498 buildElementsForText('f([@a gx = null]) {}').functions[0].parameters[0]; | |
499 checkMetadata(parameterElement); | |
500 } | |
501 | |
502 void test_metadata_visitEnumDeclaration() { | |
503 ClassElement classElement = | |
504 buildElementsForText('@a enum E { v }').enums[0]; | |
505 checkMetadata(classElement); | |
506 } | |
507 | |
508 void test_metadata_visitExportDirective() { | |
509 buildElementsForText('@a export "foo.dart";'); | |
510 expect(compilationUnit.directives[0], new isInstanceOf<ExportDirective>()); | |
511 ExportDirective exportDirective = compilationUnit.directives[0]; | |
512 checkAnnotation(exportDirective.metadata); | |
513 } | |
514 | |
515 void test_metadata_visitFieldFormalParameter() { | |
516 ParameterElement parameterElement = | |
517 buildElementsForText('class C { var x; C(@a this.x); }') | |
518 .types[0] | |
519 .constructors[0] | |
520 .parameters[0]; | |
521 checkMetadata(parameterElement); | |
522 } | |
523 | |
524 void test_metadata_visitFunctionDeclaration_function() { | |
525 FunctionElement functionElement = | |
526 buildElementsForText('@a f() {}').functions[0]; | |
527 checkMetadata(functionElement); | |
528 } | |
529 | |
530 void test_metadata_visitFunctionDeclaration_getter() { | |
531 PropertyAccessorElement propertyAccessorElement = | |
532 buildElementsForText('@a get f => null;').accessors[0]; | |
533 checkMetadata(propertyAccessorElement); | |
534 } | |
535 | |
536 void test_metadata_visitFunctionDeclaration_setter() { | |
537 PropertyAccessorElement propertyAccessorElement = | |
538 buildElementsForText('@a set f(value) {}').accessors[0]; | |
539 checkMetadata(propertyAccessorElement); | |
540 } | |
541 | |
542 void test_metadata_visitFunctionTypeAlias() { | |
543 FunctionTypeAliasElement functionTypeAliasElement = | |
544 buildElementsForText('@a typedef F();').typeAliases[0]; | |
545 checkMetadata(functionTypeAliasElement); | |
546 } | |
547 | |
548 void test_metadata_visitFunctionTypedFormalParameter() { | |
549 ParameterElement parameterElement = | |
550 buildElementsForText('f(@a g()) {}').functions[0].parameters[0]; | |
551 checkMetadata(parameterElement); | |
552 } | |
553 | |
554 void test_metadata_visitImportDirective() { | |
555 buildElementsForText('@a import "foo.dart";'); | |
556 expect(compilationUnit.directives[0], new isInstanceOf<ImportDirective>()); | |
557 ImportDirective importDirective = compilationUnit.directives[0]; | |
558 checkAnnotation(importDirective.metadata); | |
559 } | |
560 | |
561 void test_metadata_visitLibraryDirective() { | |
562 buildElementsForText('@a library L;'); | |
563 expect(compilationUnit.directives[0], new isInstanceOf<LibraryDirective>()); | |
564 LibraryDirective libraryDirective = compilationUnit.directives[0]; | |
565 checkAnnotation(libraryDirective.metadata); | |
566 } | |
567 | |
568 void test_metadata_visitMethodDeclaration_getter() { | |
569 PropertyAccessorElement propertyAccessorElement = | |
570 buildElementsForText('class C { @a get m => null; }') | |
571 .types[0] | |
572 .accessors[0]; | |
573 checkMetadata(propertyAccessorElement); | |
574 } | |
575 | |
576 void test_metadata_visitMethodDeclaration_method() { | |
577 MethodElement methodElement = | |
578 buildElementsForText('class C { @a m() {} }').types[0].methods[0]; | |
579 checkMetadata(methodElement); | |
580 } | |
581 | |
582 void test_metadata_visitMethodDeclaration_setter() { | |
583 PropertyAccessorElement propertyAccessorElement = | |
584 buildElementsForText('class C { @a set f(value) {} }') | |
585 .types[0] | |
586 .accessors[0]; | |
587 checkMetadata(propertyAccessorElement); | |
588 } | |
589 | |
590 void test_metadata_visitPartDirective() { | |
591 buildElementsForText('@a part "foo.dart";'); | |
592 expect(compilationUnit.directives[0], new isInstanceOf<PartDirective>()); | |
593 PartDirective partDirective = compilationUnit.directives[0]; | |
594 checkAnnotation(partDirective.metadata); | |
595 } | |
596 | |
597 void test_metadata_visitPartOfDirective() { | |
598 // We don't build ElementAnnotation objects for `part of` directives, since | |
599 // analyzer ignores them in favor of annotations on the library directive. | |
600 buildElementsForText('@a part of L;'); | |
601 expect(compilationUnit.directives[0], new isInstanceOf<PartOfDirective>()); | |
602 PartOfDirective partOfDirective = compilationUnit.directives[0]; | |
603 expect(partOfDirective.metadata, hasLength(1)); | |
604 expect(partOfDirective.metadata[0].elementAnnotation, isNull); | |
605 } | |
606 | |
607 void test_metadata_visitSimpleFormalParameter() { | |
608 ParameterElement parameterElement = | |
609 buildElementsForText('f(@a x) {}').functions[0].parameters[0]; | |
610 checkMetadata(parameterElement); | |
611 } | |
612 | |
613 void test_metadata_visitTypeParameter() { | |
614 TypeParameterElement typeParameterElement = | |
615 buildElementsForText('class C<@a T> {}').types[0].typeParameters[0]; | |
616 checkMetadata(typeParameterElement); | |
617 } | |
618 | |
619 void test_visitCatchClause() { | |
620 List<LocalVariableElement> variables = | |
621 buildElementsForText('f() { try {} catch (e, s) {} }') | |
622 .functions[0] | |
623 .localVariables; | |
624 String exceptionParameterName = "e"; | |
625 String stackParameterName = "s"; | |
626 expect(variables, hasLength(2)); | |
627 | |
628 LocalVariableElement exceptionVariable = variables[0]; | |
629 expect(exceptionVariable, isNotNull); | |
630 expect(exceptionVariable.name, exceptionParameterName); | |
631 expect(exceptionVariable.hasImplicitType, isTrue); | |
632 expect(exceptionVariable.isSynthetic, isFalse); | |
633 expect(exceptionVariable.isConst, isFalse); | |
634 expect(exceptionVariable.isFinal, isFalse); | |
635 expect(exceptionVariable.initializer, isNull); | |
636 _assertVisibleRange(exceptionVariable, 13, 28); | |
637 | |
638 LocalVariableElement stackVariable = variables[1]; | |
639 expect(stackVariable, isNotNull); | |
640 expect(stackVariable.name, stackParameterName); | |
641 expect(stackVariable.isSynthetic, isFalse); | |
642 expect(stackVariable.isConst, isFalse); | |
643 expect(stackVariable.isFinal, isFalse); | |
644 expect(stackVariable.initializer, isNull); | |
645 _assertVisibleRange(stackVariable, 13, 28); | |
646 } | |
647 | |
648 void test_visitCatchClause_withType() { | |
649 List<LocalVariableElement> variables = | |
650 buildElementsForText('f() { try {} on E catch (e) {} }') | |
651 .functions[0] | |
652 .localVariables; | |
653 String exceptionParameterName = "e"; | |
654 expect(variables, hasLength(1)); | |
655 VariableElement exceptionVariable = variables[0]; | |
656 expect(exceptionVariable, isNotNull); | |
657 expect(exceptionVariable.name, exceptionParameterName); | |
658 expect(exceptionVariable.hasImplicitType, isFalse); | |
659 } | |
660 | |
661 void test_visitClassDeclaration_abstract() { | |
662 ElementHolder holder = new ElementHolder(); | |
663 ElementBuilder builder = _makeBuilder(holder); | |
664 String className = "C"; | |
665 ClassDeclaration classDeclaration = AstFactory.classDeclaration( | |
666 Keyword.ABSTRACT, className, null, null, null, null); | |
667 classDeclaration.accept(builder); | |
668 List<ClassElement> types = holder.types; | |
669 expect(types, hasLength(1)); | |
670 ClassElement type = types[0]; | |
671 expect(type, isNotNull); | |
672 expect(type.name, className); | |
673 List<TypeParameterElement> typeParameters = type.typeParameters; | |
674 expect(typeParameters, hasLength(0)); | |
675 expect(type.isAbstract, isTrue); | |
676 expect(type.isMixinApplication, isFalse); | |
677 expect(type.isSynthetic, isFalse); | |
678 } | |
679 | |
680 void test_visitClassDeclaration_invalidFunctionInAnnotation_class() { | |
681 // https://github.com/dart-lang/sdk/issues/25696 | |
682 String code = r''' | |
683 class A { | |
684 const A({f}); | |
685 } | |
686 | |
687 @A(f: () {}) | |
688 class C {} | |
689 '''; | |
690 buildElementsForText(code); | |
691 } | |
692 | |
693 void test_visitClassDeclaration_invalidFunctionInAnnotation_method() { | |
694 String code = r''' | |
695 class A { | |
696 const A({f}); | |
697 } | |
698 | |
699 class C { | |
700 @A(f: () {}) | |
701 void m() {} | |
702 } | |
703 '''; | |
704 ElementHolder holder = buildElementsForText(code); | |
705 ClassElement elementC = holder.types[1]; | |
706 expect(elementC, isNotNull); | |
707 MethodElement methodM = elementC.methods[0]; | |
708 expect(methodM, isNotNull); | |
709 expect(methodM.functions, isEmpty); | |
710 } | |
711 | |
712 void test_visitClassDeclaration_minimal() { | |
713 ElementHolder holder = new ElementHolder(); | |
714 ElementBuilder builder = _makeBuilder(holder); | |
715 String className = "C"; | |
716 ClassDeclaration classDeclaration = | |
717 AstFactory.classDeclaration(null, className, null, null, null, null); | |
718 classDeclaration.documentationComment = AstFactory.documentationComment( | |
719 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
720 classDeclaration.endToken.offset = 80; | |
721 classDeclaration.accept(builder); | |
722 List<ClassElement> types = holder.types; | |
723 expect(types, hasLength(1)); | |
724 ClassElement type = types[0]; | |
725 expect(type, isNotNull); | |
726 expect(type.name, className); | |
727 List<TypeParameterElement> typeParameters = type.typeParameters; | |
728 expect(typeParameters, hasLength(0)); | |
729 expect(type.isAbstract, isFalse); | |
730 expect(type.isMixinApplication, isFalse); | |
731 expect(type.isSynthetic, isFalse); | |
732 expect(type.documentationComment, '/// aaa'); | |
733 _assertHasCodeRange(type, 50, 31); | |
734 } | |
735 | |
736 void test_visitClassDeclaration_parameterized() { | |
737 ElementHolder holder = new ElementHolder(); | |
738 ElementBuilder builder = _makeBuilder(holder); | |
739 String className = "C"; | |
740 String firstVariableName = "E"; | |
741 String secondVariableName = "F"; | |
742 ClassDeclaration classDeclaration = AstFactory.classDeclaration( | |
743 null, | |
744 className, | |
745 AstFactory.typeParameterList([firstVariableName, secondVariableName]), | |
746 null, | |
747 null, | |
748 null); | |
749 classDeclaration.accept(builder); | |
750 List<ClassElement> types = holder.types; | |
751 expect(types, hasLength(1)); | |
752 ClassElement type = types[0]; | |
753 expect(type, isNotNull); | |
754 expect(type.name, className); | |
755 List<TypeParameterElement> typeParameters = type.typeParameters; | |
756 expect(typeParameters, hasLength(2)); | |
757 expect(typeParameters[0].name, firstVariableName); | |
758 expect(typeParameters[1].name, secondVariableName); | |
759 expect(type.isAbstract, isFalse); | |
760 expect(type.isMixinApplication, isFalse); | |
761 expect(type.isSynthetic, isFalse); | |
762 } | |
763 | |
764 void test_visitClassDeclaration_withMembers() { | |
765 ElementHolder holder = new ElementHolder(); | |
766 ElementBuilder builder = _makeBuilder(holder); | |
767 String className = "C"; | |
768 String typeParameterName = "E"; | |
769 String fieldName = "f"; | |
770 String methodName = "m"; | |
771 ClassDeclaration classDeclaration = AstFactory.classDeclaration( | |
772 null, | |
773 className, | |
774 AstFactory.typeParameterList([typeParameterName]), | |
775 null, | |
776 null, | |
777 null, [ | |
778 AstFactory.fieldDeclaration2( | |
779 false, null, [AstFactory.variableDeclaration(fieldName)]), | |
780 AstFactory.methodDeclaration2( | |
781 null, | |
782 null, | |
783 null, | |
784 null, | |
785 AstFactory.identifier3(methodName), | |
786 AstFactory.formalParameterList(), | |
787 AstFactory.blockFunctionBody2()) | |
788 ]); | |
789 classDeclaration.accept(builder); | |
790 List<ClassElement> types = holder.types; | |
791 expect(types, hasLength(1)); | |
792 ClassElement type = types[0]; | |
793 expect(type, isNotNull); | |
794 expect(type.name, className); | |
795 expect(type.isAbstract, isFalse); | |
796 expect(type.isMixinApplication, isFalse); | |
797 expect(type.isSynthetic, isFalse); | |
798 List<TypeParameterElement> typeParameters = type.typeParameters; | |
799 expect(typeParameters, hasLength(1)); | |
800 TypeParameterElement typeParameter = typeParameters[0]; | |
801 expect(typeParameter, isNotNull); | |
802 expect(typeParameter.name, typeParameterName); | |
803 List<FieldElement> fields = type.fields; | |
804 expect(fields, hasLength(1)); | |
805 FieldElement field = fields[0]; | |
806 expect(field, isNotNull); | |
807 expect(field.name, fieldName); | |
808 List<MethodElement> methods = type.methods; | |
809 expect(methods, hasLength(1)); | |
810 MethodElement method = methods[0]; | |
811 expect(method, isNotNull); | |
812 expect(method.name, methodName); | |
813 } | |
814 | |
815 void test_visitClassTypeAlias() { | |
816 // class B {} | |
817 // class M {} | |
818 // class C = B with M | |
819 ElementHolder holder = new ElementHolder(); | |
820 ElementBuilder builder = _makeBuilder(holder); | |
821 ClassElementImpl classB = ElementFactory.classElement2('B', []); | |
822 ConstructorElementImpl constructorB = | |
823 ElementFactory.constructorElement2(classB, '', []); | |
824 constructorB.setModifier(Modifier.SYNTHETIC, true); | |
825 classB.constructors = [constructorB]; | |
826 ClassElement classM = ElementFactory.classElement2('M', []); | |
827 WithClause withClause = | |
828 AstFactory.withClause([AstFactory.typeName(classM, [])]); | |
829 ClassTypeAlias alias = AstFactory.classTypeAlias( | |
830 'C', null, null, AstFactory.typeName(classB, []), withClause, null); | |
831 alias.accept(builder); | |
832 List<ClassElement> types = holder.types; | |
833 expect(types, hasLength(1)); | |
834 ClassElement type = types[0]; | |
835 expect(alias.element, same(type)); | |
836 expect(type.name, equals('C')); | |
837 expect(type.isAbstract, isFalse); | |
838 expect(type.isMixinApplication, isTrue); | |
839 expect(type.isSynthetic, isFalse); | |
840 expect(type.typeParameters, isEmpty); | |
841 expect(type.fields, isEmpty); | |
842 expect(type.methods, isEmpty); | |
843 } | |
844 | |
845 void test_visitClassTypeAlias_abstract() { | |
846 // class B {} | |
847 // class M {} | |
848 // abstract class C = B with M | |
849 ElementHolder holder = new ElementHolder(); | |
850 ElementBuilder builder = _makeBuilder(holder); | |
851 ClassElementImpl classB = ElementFactory.classElement2('B', []); | |
852 ConstructorElementImpl constructorB = | |
853 ElementFactory.constructorElement2(classB, '', []); | |
854 constructorB.setModifier(Modifier.SYNTHETIC, true); | |
855 classB.constructors = [constructorB]; | |
856 ClassElement classM = ElementFactory.classElement2('M', []); | |
857 WithClause withClause = | |
858 AstFactory.withClause([AstFactory.typeName(classM, [])]); | |
859 ClassTypeAlias classCAst = AstFactory.classTypeAlias('C', null, | |
860 Keyword.ABSTRACT, AstFactory.typeName(classB, []), withClause, null); | |
861 classCAst.accept(builder); | |
862 List<ClassElement> types = holder.types; | |
863 expect(types, hasLength(1)); | |
864 ClassElement type = types[0]; | |
865 expect(type.isAbstract, isTrue); | |
866 expect(type.isMixinApplication, isTrue); | |
867 } | |
868 | |
869 void test_visitClassTypeAlias_typeParams() { | |
870 // class B {} | |
871 // class M {} | |
872 // class C<T> = B with M | |
873 ElementHolder holder = new ElementHolder(); | |
874 ElementBuilder builder = _makeBuilder(holder); | |
875 ClassElementImpl classB = ElementFactory.classElement2('B', []); | |
876 ConstructorElementImpl constructorB = | |
877 ElementFactory.constructorElement2(classB, '', []); | |
878 constructorB.setModifier(Modifier.SYNTHETIC, true); | |
879 classB.constructors = [constructorB]; | |
880 ClassElementImpl classM = ElementFactory.classElement2('M', []); | |
881 WithClause withClause = | |
882 AstFactory.withClause([AstFactory.typeName(classM, [])]); | |
883 ClassTypeAlias classCAst = AstFactory.classTypeAlias( | |
884 'C', | |
885 AstFactory.typeParameterList(['T']), | |
886 null, | |
887 AstFactory.typeName(classB, []), | |
888 withClause, | |
889 null); | |
890 classCAst.accept(builder); | |
891 List<ClassElement> types = holder.types; | |
892 expect(types, hasLength(1)); | |
893 ClassElement type = types[0]; | |
894 expect(type.typeParameters, hasLength(1)); | |
895 expect(type.typeParameters[0].name, equals('T')); | |
896 } | |
897 | |
898 void test_visitCompilationUnit_codeRange() { | |
899 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory | |
900 .topLevelVariableDeclaration(null, AstFactory.typeName4('int'), | |
901 [AstFactory.variableDeclaration('V')]); | |
902 CompilationUnit unit = new CompilationUnit( | |
903 topLevelVariableDeclaration.beginToken, | |
904 null, | |
905 [], | |
906 [topLevelVariableDeclaration], | |
907 topLevelVariableDeclaration.endToken); | |
908 ElementHolder holder = new ElementHolder(); | |
909 ElementBuilder builder = _makeBuilder(holder); | |
910 unit.beginToken.offset = 10; | |
911 unit.endToken.offset = 40; | |
912 unit.accept(builder); | |
913 | |
914 CompilationUnitElement element = builder.compilationUnitElement; | |
915 _assertHasCodeRange(element, 0, 41); | |
916 } | |
917 | |
918 void test_visitConstructorDeclaration_external() { | |
919 ElementHolder holder = new ElementHolder(); | |
920 ElementBuilder builder = _makeBuilder(holder); | |
921 String className = "A"; | |
922 ConstructorDeclaration constructorDeclaration = | |
923 AstFactory.constructorDeclaration2( | |
924 null, | |
925 null, | |
926 AstFactory.identifier3(className), | |
927 null, | |
928 AstFactory.formalParameterList(), | |
929 null, | |
930 AstFactory.blockFunctionBody2()); | |
931 constructorDeclaration.externalKeyword = | |
932 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
933 constructorDeclaration.accept(builder); | |
934 List<ConstructorElement> constructors = holder.constructors; | |
935 expect(constructors, hasLength(1)); | |
936 ConstructorElement constructor = constructors[0]; | |
937 expect(constructor, isNotNull); | |
938 expect(constructor.isExternal, isTrue); | |
939 expect(constructor.isFactory, isFalse); | |
940 expect(constructor.name, ""); | |
941 expect(constructor.functions, hasLength(0)); | |
942 expect(constructor.labels, hasLength(0)); | |
943 expect(constructor.localVariables, hasLength(0)); | |
944 expect(constructor.parameters, hasLength(0)); | |
945 } | |
946 | |
947 void test_visitConstructorDeclaration_factory() { | |
948 ElementHolder holder = new ElementHolder(); | |
949 ElementBuilder builder = _makeBuilder(holder); | |
950 String className = "A"; | |
951 ConstructorDeclaration constructorDeclaration = | |
952 AstFactory.constructorDeclaration2( | |
953 null, | |
954 Keyword.FACTORY, | |
955 AstFactory.identifier3(className), | |
956 null, | |
957 AstFactory.formalParameterList(), | |
958 null, | |
959 AstFactory.blockFunctionBody2()); | |
960 constructorDeclaration.accept(builder); | |
961 List<ConstructorElement> constructors = holder.constructors; | |
962 expect(constructors, hasLength(1)); | |
963 ConstructorElement constructor = constructors[0]; | |
964 expect(constructor, isNotNull); | |
965 expect(constructor.isExternal, isFalse); | |
966 expect(constructor.isFactory, isTrue); | |
967 expect(constructor.name, ""); | |
968 expect(constructor.functions, hasLength(0)); | |
969 expect(constructor.labels, hasLength(0)); | |
970 expect(constructor.localVariables, hasLength(0)); | |
971 expect(constructor.parameters, hasLength(0)); | |
972 } | |
973 | |
974 void test_visitConstructorDeclaration_minimal() { | |
975 ElementHolder holder = new ElementHolder(); | |
976 ElementBuilder builder = _makeBuilder(holder); | |
977 String className = "A"; | |
978 ConstructorDeclaration constructorDeclaration = | |
979 AstFactory.constructorDeclaration2( | |
980 null, | |
981 null, | |
982 AstFactory.identifier3(className), | |
983 null, | |
984 AstFactory.formalParameterList(), | |
985 null, | |
986 AstFactory.blockFunctionBody2()); | |
987 constructorDeclaration.documentationComment = AstFactory | |
988 .documentationComment( | |
989 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
990 constructorDeclaration.endToken.offset = 80; | |
991 constructorDeclaration.accept(builder); | |
992 | |
993 List<ConstructorElement> constructors = holder.constructors; | |
994 expect(constructors, hasLength(1)); | |
995 ConstructorElement constructor = constructors[0]; | |
996 expect(constructor, isNotNull); | |
997 _assertHasCodeRange(constructor, 50, 31); | |
998 expect(constructor.documentationComment, '/// aaa'); | |
999 expect(constructor.isExternal, isFalse); | |
1000 expect(constructor.isFactory, isFalse); | |
1001 expect(constructor.name, ""); | |
1002 expect(constructor.functions, hasLength(0)); | |
1003 expect(constructor.labels, hasLength(0)); | |
1004 expect(constructor.localVariables, hasLength(0)); | |
1005 expect(constructor.parameters, hasLength(0)); | |
1006 } | |
1007 | |
1008 void test_visitConstructorDeclaration_named() { | |
1009 ElementHolder holder = new ElementHolder(); | |
1010 ElementBuilder builder = _makeBuilder(holder); | |
1011 String className = "A"; | |
1012 String constructorName = "c"; | |
1013 ConstructorDeclaration constructorDeclaration = | |
1014 AstFactory.constructorDeclaration2( | |
1015 null, | |
1016 null, | |
1017 AstFactory.identifier3(className), | |
1018 constructorName, | |
1019 AstFactory.formalParameterList(), | |
1020 null, | |
1021 AstFactory.blockFunctionBody2()); | |
1022 constructorDeclaration.accept(builder); | |
1023 List<ConstructorElement> constructors = holder.constructors; | |
1024 expect(constructors, hasLength(1)); | |
1025 ConstructorElement constructor = constructors[0]; | |
1026 expect(constructor, isNotNull); | |
1027 expect(constructor.isExternal, isFalse); | |
1028 expect(constructor.isFactory, isFalse); | |
1029 expect(constructor.name, constructorName); | |
1030 expect(constructor.functions, hasLength(0)); | |
1031 expect(constructor.labels, hasLength(0)); | |
1032 expect(constructor.localVariables, hasLength(0)); | |
1033 expect(constructor.parameters, hasLength(0)); | |
1034 expect(constructorDeclaration.name.staticElement, same(constructor)); | |
1035 expect(constructorDeclaration.element, same(constructor)); | |
1036 } | |
1037 | |
1038 void test_visitConstructorDeclaration_unnamed() { | |
1039 ElementHolder holder = new ElementHolder(); | |
1040 ElementBuilder builder = _makeBuilder(holder); | |
1041 String className = "A"; | |
1042 ConstructorDeclaration constructorDeclaration = | |
1043 AstFactory.constructorDeclaration2( | |
1044 null, | |
1045 null, | |
1046 AstFactory.identifier3(className), | |
1047 null, | |
1048 AstFactory.formalParameterList(), | |
1049 null, | |
1050 AstFactory.blockFunctionBody2()); | |
1051 constructorDeclaration.accept(builder); | |
1052 List<ConstructorElement> constructors = holder.constructors; | |
1053 expect(constructors, hasLength(1)); | |
1054 ConstructorElement constructor = constructors[0]; | |
1055 expect(constructor, isNotNull); | |
1056 expect(constructor.isExternal, isFalse); | |
1057 expect(constructor.isFactory, isFalse); | |
1058 expect(constructor.name, ""); | |
1059 expect(constructor.functions, hasLength(0)); | |
1060 expect(constructor.labels, hasLength(0)); | |
1061 expect(constructor.localVariables, hasLength(0)); | |
1062 expect(constructor.parameters, hasLength(0)); | |
1063 expect(constructorDeclaration.element, same(constructor)); | |
1064 } | |
1065 | |
1066 void test_visitDeclaredIdentifier_noType() { | |
1067 LocalVariableElement variable = | |
1068 buildElementsForText('f() { for (var i in []) {} }') | |
1069 .functions[0] | |
1070 .localVariables[0]; | |
1071 _assertHasCodeRange(variable, 11, 5); | |
1072 expect(variable, isNotNull); | |
1073 expect(variable.hasImplicitType, isTrue); | |
1074 expect(variable.isConst, isFalse); | |
1075 expect(variable.isDeprecated, isFalse); | |
1076 expect(variable.isFinal, isFalse); | |
1077 expect(variable.isOverride, isFalse); | |
1078 expect(variable.isPrivate, isFalse); | |
1079 expect(variable.isPublic, isTrue); | |
1080 expect(variable.isSynthetic, isFalse); | |
1081 expect(variable.name, 'i'); | |
1082 } | |
1083 | |
1084 void test_visitDeclaredIdentifier_type() { | |
1085 LocalVariableElement variable = | |
1086 buildElementsForText('f() { for (int i in []) {} }') | |
1087 .functions[0] | |
1088 .localVariables[0]; | |
1089 _assertHasCodeRange(variable, 11, 5); | |
1090 expect(variable.hasImplicitType, isFalse); | |
1091 expect(variable.isConst, isFalse); | |
1092 expect(variable.isDeprecated, isFalse); | |
1093 expect(variable.isFinal, isFalse); | |
1094 expect(variable.isOverride, isFalse); | |
1095 expect(variable.isPrivate, isFalse); | |
1096 expect(variable.isPublic, isTrue); | |
1097 expect(variable.isSynthetic, isFalse); | |
1098 expect(variable.name, 'i'); | |
1099 } | |
1100 | |
1101 void test_visitDefaultFormalParameter_noType() { | |
1102 // p = 0 | |
1103 ElementHolder holder = new ElementHolder(); | |
1104 ElementBuilder builder = _makeBuilder(holder); | |
1105 String parameterName = 'p'; | |
1106 DefaultFormalParameter formalParameter = | |
1107 AstFactory.positionalFormalParameter( | |
1108 AstFactory.simpleFormalParameter3(parameterName), | |
1109 AstFactory.integer(0)); | |
1110 formalParameter.beginToken.offset = 50; | |
1111 formalParameter.endToken.offset = 80; | |
1112 formalParameter.accept(builder); | |
1113 | |
1114 List<ParameterElement> parameters = holder.parameters; | |
1115 expect(parameters, hasLength(1)); | |
1116 ParameterElement parameter = parameters[0]; | |
1117 _assertHasCodeRange(parameter, 50, 31); | |
1118 expect(parameter.hasImplicitType, isTrue); | |
1119 expect(parameter.initializer, isNotNull); | |
1120 expect(parameter.initializer.type, isNotNull); | |
1121 expect(parameter.initializer.hasImplicitReturnType, isTrue); | |
1122 expect(parameter.isConst, isFalse); | |
1123 expect(parameter.isDeprecated, isFalse); | |
1124 expect(parameter.isFinal, isFalse); | |
1125 expect(parameter.isInitializingFormal, isFalse); | |
1126 expect(parameter.isOverride, isFalse); | |
1127 expect(parameter.isPrivate, isFalse); | |
1128 expect(parameter.isPublic, isTrue); | |
1129 expect(parameter.isSynthetic, isFalse); | |
1130 expect(parameter.name, parameterName); | |
1131 } | |
1132 | |
1133 void test_visitDefaultFormalParameter_type() { | |
1134 // E p = 0 | |
1135 ElementHolder holder = new ElementHolder(); | |
1136 ElementBuilder builder = _makeBuilder(holder); | |
1137 String parameterName = 'p'; | |
1138 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter( | |
1139 AstFactory.simpleFormalParameter4( | |
1140 AstFactory.typeName4('E'), parameterName), | |
1141 AstFactory.integer(0)); | |
1142 formalParameter.accept(builder); | |
1143 | |
1144 List<ParameterElement> parameters = holder.parameters; | |
1145 expect(parameters, hasLength(1)); | |
1146 ParameterElement parameter = parameters[0]; | |
1147 expect(parameter.hasImplicitType, isFalse); | |
1148 expect(parameter.initializer, isNotNull); | |
1149 expect(parameter.initializer.type, isNotNull); | |
1150 expect(parameter.initializer.hasImplicitReturnType, isTrue); | |
1151 expect(parameter.isConst, isFalse); | |
1152 expect(parameter.isDeprecated, isFalse); | |
1153 expect(parameter.isFinal, isFalse); | |
1154 expect(parameter.isInitializingFormal, isFalse); | |
1155 expect(parameter.isOverride, isFalse); | |
1156 expect(parameter.isPrivate, isFalse); | |
1157 expect(parameter.isPublic, isTrue); | |
1158 expect(parameter.isSynthetic, isFalse); | |
1159 expect(parameter.name, parameterName); | |
1160 } | |
1161 | |
1162 void test_visitEnumDeclaration() { | |
1163 ElementHolder holder = new ElementHolder(); | |
1164 ElementBuilder builder = _makeBuilder(holder); | |
1165 String enumName = "E"; | |
1166 EnumDeclaration enumDeclaration = | |
1167 AstFactory.enumDeclaration2(enumName, ["ONE"]); | |
1168 enumDeclaration.documentationComment = AstFactory.documentationComment( | |
1169 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1170 enumDeclaration.endToken.offset = 80; | |
1171 enumDeclaration.accept(builder); | |
1172 List<ClassElement> enums = holder.enums; | |
1173 expect(enums, hasLength(1)); | |
1174 ClassElement enumElement = enums[0]; | |
1175 expect(enumElement, isNotNull); | |
1176 _assertHasCodeRange(enumElement, 50, 31); | |
1177 expect(enumElement.documentationComment, '/// aaa'); | |
1178 expect(enumElement.name, enumName); | |
1179 } | |
1180 | |
1181 void test_visitFieldDeclaration() { | |
1182 ElementHolder holder = new ElementHolder(); | |
1183 ElementBuilder builder = _makeBuilder(holder); | |
1184 String firstFieldName = "x"; | |
1185 String secondFieldName = "y"; | |
1186 FieldDeclaration fieldDeclaration = | |
1187 AstFactory.fieldDeclaration2(false, null, [ | |
1188 AstFactory.variableDeclaration(firstFieldName), | |
1189 AstFactory.variableDeclaration(secondFieldName) | |
1190 ]); | |
1191 fieldDeclaration.documentationComment = AstFactory.documentationComment( | |
1192 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1193 fieldDeclaration.endToken.offset = 110; | |
1194 fieldDeclaration.accept(builder); | |
1195 | |
1196 List<FieldElement> fields = holder.fields; | |
1197 expect(fields, hasLength(2)); | |
1198 | |
1199 FieldElement firstField = fields[0]; | |
1200 expect(firstField, isNotNull); | |
1201 _assertHasCodeRange(firstField, 50, 61); | |
1202 expect(firstField.documentationComment, '/// aaa'); | |
1203 expect(firstField.name, firstFieldName); | |
1204 expect(firstField.initializer, isNull); | |
1205 expect(firstField.isConst, isFalse); | |
1206 expect(firstField.isFinal, isFalse); | |
1207 expect(firstField.isSynthetic, isFalse); | |
1208 | |
1209 FieldElement secondField = fields[1]; | |
1210 expect(secondField, isNotNull); | |
1211 _assertHasCodeRange(secondField, 50, 61); | |
1212 expect(secondField.documentationComment, '/// aaa'); | |
1213 expect(secondField.name, secondFieldName); | |
1214 expect(secondField.initializer, isNull); | |
1215 expect(secondField.isConst, isFalse); | |
1216 expect(secondField.isFinal, isFalse); | |
1217 expect(secondField.isSynthetic, isFalse); | |
1218 } | |
1219 | |
1220 void test_visitFieldFormalParameter() { | |
1221 ElementHolder holder = new ElementHolder(); | |
1222 ElementBuilder builder = _makeBuilder(holder); | |
1223 String parameterName = "p"; | |
1224 FieldFormalParameter formalParameter = | |
1225 AstFactory.fieldFormalParameter(null, null, parameterName); | |
1226 formalParameter.beginToken.offset = 50; | |
1227 formalParameter.endToken.offset = 80; | |
1228 formalParameter.accept(builder); | |
1229 List<ParameterElement> parameters = holder.parameters; | |
1230 expect(parameters, hasLength(1)); | |
1231 ParameterElement parameter = parameters[0]; | |
1232 expect(parameter, isNotNull); | |
1233 _assertHasCodeRange(parameter, 50, 31); | |
1234 expect(parameter.name, parameterName); | |
1235 expect(parameter.initializer, isNull); | |
1236 expect(parameter.isConst, isFalse); | |
1237 expect(parameter.isFinal, isFalse); | |
1238 expect(parameter.isSynthetic, isFalse); | |
1239 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
1240 expect(parameter.parameters, hasLength(0)); | |
1241 } | |
1242 | |
1243 void test_visitFieldFormalParameter_functionTyped() { | |
1244 ElementHolder holder = new ElementHolder(); | |
1245 ElementBuilder builder = _makeBuilder(holder); | |
1246 String parameterName = "p"; | |
1247 FieldFormalParameter formalParameter = AstFactory.fieldFormalParameter( | |
1248 null, | |
1249 null, | |
1250 parameterName, | |
1251 AstFactory | |
1252 .formalParameterList([AstFactory.simpleFormalParameter3("a")])); | |
1253 formalParameter.accept(builder); | |
1254 List<ParameterElement> parameters = holder.parameters; | |
1255 expect(parameters, hasLength(1)); | |
1256 ParameterElement parameter = parameters[0]; | |
1257 expect(parameter, isNotNull); | |
1258 expect(parameter.name, parameterName); | |
1259 expect(parameter.initializer, isNull); | |
1260 expect(parameter.isConst, isFalse); | |
1261 expect(parameter.isFinal, isFalse); | |
1262 expect(parameter.isSynthetic, isFalse); | |
1263 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
1264 expect(parameter.parameters, hasLength(1)); | |
1265 } | |
1266 | |
1267 void test_visitFormalParameterList() { | |
1268 ElementHolder holder = new ElementHolder(); | |
1269 ElementBuilder builder = _makeBuilder(holder); | |
1270 String firstParameterName = "a"; | |
1271 String secondParameterName = "b"; | |
1272 FormalParameterList parameterList = AstFactory.formalParameterList([ | |
1273 AstFactory.simpleFormalParameter3(firstParameterName), | |
1274 AstFactory.simpleFormalParameter3(secondParameterName) | |
1275 ]); | |
1276 parameterList.accept(builder); | |
1277 List<ParameterElement> parameters = holder.parameters; | |
1278 expect(parameters, hasLength(2)); | |
1279 expect(parameters[0].name, firstParameterName); | |
1280 expect(parameters[1].name, secondParameterName); | |
1281 } | |
1282 | |
1283 void test_visitFunctionDeclaration_external() { | |
1284 // external f(); | |
1285 ElementHolder holder = new ElementHolder(); | |
1286 ElementBuilder builder = _makeBuilder(holder); | |
1287 String functionName = "f"; | |
1288 FunctionDeclaration declaration = AstFactory.functionDeclaration( | |
1289 null, | |
1290 null, | |
1291 functionName, | |
1292 AstFactory.functionExpression2( | |
1293 AstFactory.formalParameterList(), AstFactory.emptyFunctionBody())); | |
1294 declaration.externalKeyword = | |
1295 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
1296 declaration.accept(builder); | |
1297 | |
1298 List<FunctionElement> functions = holder.functions; | |
1299 expect(functions, hasLength(1)); | |
1300 FunctionElement function = functions[0]; | |
1301 expect(function, isNotNull); | |
1302 expect(function.name, functionName); | |
1303 expect(declaration.element, same(function)); | |
1304 expect(declaration.functionExpression.element, same(function)); | |
1305 expect(function.hasImplicitReturnType, isTrue); | |
1306 expect(function.isExternal, isTrue); | |
1307 expect(function.isSynthetic, isFalse); | |
1308 expect(function.typeParameters, hasLength(0)); | |
1309 } | |
1310 | |
1311 void test_visitFunctionDeclaration_getter() { | |
1312 // get f() {} | |
1313 ElementHolder holder = new ElementHolder(); | |
1314 ElementBuilder builder = _makeBuilder(holder); | |
1315 String functionName = "f"; | |
1316 FunctionDeclaration declaration = AstFactory.functionDeclaration( | |
1317 null, | |
1318 Keyword.GET, | |
1319 functionName, | |
1320 AstFactory.functionExpression2( | |
1321 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); | |
1322 declaration.documentationComment = AstFactory.documentationComment( | |
1323 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1324 declaration.endToken.offset = 80; | |
1325 declaration.accept(builder); | |
1326 | |
1327 List<PropertyAccessorElement> accessors = holder.accessors; | |
1328 expect(accessors, hasLength(1)); | |
1329 PropertyAccessorElement accessor = accessors[0]; | |
1330 expect(accessor, isNotNull); | |
1331 _assertHasCodeRange(accessor, 50, 31); | |
1332 expect(accessor.documentationComment, '/// aaa'); | |
1333 expect(accessor.name, functionName); | |
1334 expect(declaration.element, same(accessor)); | |
1335 expect(declaration.functionExpression.element, same(accessor)); | |
1336 expect(accessor.hasImplicitReturnType, isTrue); | |
1337 expect(accessor.isGetter, isTrue); | |
1338 expect(accessor.isExternal, isFalse); | |
1339 expect(accessor.isSetter, isFalse); | |
1340 expect(accessor.isSynthetic, isFalse); | |
1341 expect(accessor.typeParameters, hasLength(0)); | |
1342 PropertyInducingElement variable = accessor.variable; | |
1343 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement, | |
1344 TopLevelVariableElement, variable); | |
1345 expect(variable.isSynthetic, isTrue); | |
1346 } | |
1347 | |
1348 void test_visitFunctionDeclaration_plain() { | |
1349 // T f() {} | |
1350 ElementHolder holder = new ElementHolder(); | |
1351 ElementBuilder builder = _makeBuilder(holder); | |
1352 String functionName = "f"; | |
1353 FunctionDeclaration declaration = AstFactory.functionDeclaration( | |
1354 AstFactory.typeName4('T'), | |
1355 null, | |
1356 functionName, | |
1357 AstFactory.functionExpression2( | |
1358 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); | |
1359 declaration.documentationComment = AstFactory.documentationComment( | |
1360 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1361 declaration.endToken.offset = 80; | |
1362 declaration.accept(builder); | |
1363 | |
1364 List<FunctionElement> functions = holder.functions; | |
1365 expect(functions, hasLength(1)); | |
1366 FunctionElement function = functions[0]; | |
1367 expect(function, isNotNull); | |
1368 _assertHasCodeRange(function, 50, 31); | |
1369 expect(function.documentationComment, '/// aaa'); | |
1370 expect(function.hasImplicitReturnType, isFalse); | |
1371 expect(function.name, functionName); | |
1372 expect(declaration.element, same(function)); | |
1373 expect(declaration.functionExpression.element, same(function)); | |
1374 expect(function.isExternal, isFalse); | |
1375 expect(function.isSynthetic, isFalse); | |
1376 expect(function.typeParameters, hasLength(0)); | |
1377 } | |
1378 | |
1379 void test_visitFunctionDeclaration_setter() { | |
1380 // set f() {} | |
1381 ElementHolder holder = new ElementHolder(); | |
1382 ElementBuilder builder = _makeBuilder(holder); | |
1383 String functionName = "f"; | |
1384 FunctionDeclaration declaration = AstFactory.functionDeclaration( | |
1385 null, | |
1386 Keyword.SET, | |
1387 functionName, | |
1388 AstFactory.functionExpression2( | |
1389 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2())); | |
1390 declaration.documentationComment = AstFactory.documentationComment( | |
1391 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1392 declaration.endToken.offset = 80; | |
1393 declaration.accept(builder); | |
1394 | |
1395 List<PropertyAccessorElement> accessors = holder.accessors; | |
1396 expect(accessors, hasLength(1)); | |
1397 PropertyAccessorElement accessor = accessors[0]; | |
1398 expect(accessor, isNotNull); | |
1399 _assertHasCodeRange(accessor, 50, 31); | |
1400 expect(accessor.documentationComment, '/// aaa'); | |
1401 expect(accessor.hasImplicitReturnType, isTrue); | |
1402 expect(accessor.name, "$functionName="); | |
1403 expect(declaration.element, same(accessor)); | |
1404 expect(declaration.functionExpression.element, same(accessor)); | |
1405 expect(accessor.isGetter, isFalse); | |
1406 expect(accessor.isExternal, isFalse); | |
1407 expect(accessor.isSetter, isTrue); | |
1408 expect(accessor.isSynthetic, isFalse); | |
1409 expect(accessor.typeParameters, hasLength(0)); | |
1410 PropertyInducingElement variable = accessor.variable; | |
1411 EngineTestCase.assertInstanceOf((obj) => obj is TopLevelVariableElement, | |
1412 TopLevelVariableElement, variable); | |
1413 expect(variable.isSynthetic, isTrue); | |
1414 } | |
1415 | |
1416 void test_visitFunctionDeclaration_typeParameters() { | |
1417 // f<E>() {} | |
1418 ElementHolder holder = new ElementHolder(); | |
1419 ElementBuilder builder = _makeBuilder(holder); | |
1420 String functionName = 'f'; | |
1421 String typeParameterName = 'E'; | |
1422 FunctionExpression expression = AstFactory.functionExpression3( | |
1423 AstFactory.typeParameterList([typeParameterName]), | |
1424 AstFactory.formalParameterList(), | |
1425 AstFactory.blockFunctionBody2()); | |
1426 FunctionDeclaration declaration = | |
1427 AstFactory.functionDeclaration(null, null, functionName, expression); | |
1428 declaration.accept(builder); | |
1429 | |
1430 List<FunctionElement> functions = holder.functions; | |
1431 expect(functions, hasLength(1)); | |
1432 FunctionElement function = functions[0]; | |
1433 expect(function, isNotNull); | |
1434 expect(function.hasImplicitReturnType, isTrue); | |
1435 expect(function.name, functionName); | |
1436 expect(function.isExternal, isFalse); | |
1437 expect(function.isSynthetic, isFalse); | |
1438 expect(declaration.element, same(function)); | |
1439 expect(expression.element, same(function)); | |
1440 List<TypeParameterElement> typeParameters = function.typeParameters; | |
1441 expect(typeParameters, hasLength(1)); | |
1442 TypeParameterElement typeParameter = typeParameters[0]; | |
1443 expect(typeParameter, isNotNull); | |
1444 expect(typeParameter.name, typeParameterName); | |
1445 } | |
1446 | |
1447 void test_visitFunctionExpression() { | |
1448 ElementHolder holder = new ElementHolder(); | |
1449 ElementBuilder builder = _makeBuilder(holder); | |
1450 FunctionExpression expression = AstFactory.functionExpression2( | |
1451 AstFactory.formalParameterList(), AstFactory.blockFunctionBody2()); | |
1452 expression.accept(builder); | |
1453 List<FunctionElement> functions = holder.functions; | |
1454 expect(functions, hasLength(1)); | |
1455 FunctionElement function = functions[0]; | |
1456 expect(function, isNotNull); | |
1457 expect(expression.element, same(function)); | |
1458 expect(function.hasImplicitReturnType, isTrue); | |
1459 expect(function.isSynthetic, isFalse); | |
1460 expect(function.typeParameters, hasLength(0)); | |
1461 } | |
1462 | |
1463 void test_visitFunctionExpression_inBlockBody() { | |
1464 List<FunctionElement> functions = | |
1465 buildElementsForText('f() { return () => 42; }').functions[0].functions; | |
1466 expect(functions, hasLength(1)); | |
1467 FunctionElement function = functions[0]; | |
1468 expect(function, isNotNull); | |
1469 expect(function.hasImplicitReturnType, isTrue); | |
1470 expect(function.isSynthetic, isFalse); | |
1471 expect(function.typeParameters, hasLength(0)); | |
1472 } | |
1473 | |
1474 void test_visitFunctionExpression_inExpressionBody() { | |
1475 List<FunctionElement> functions = | |
1476 buildElementsForText('f() => () => 42;').functions[0].functions; | |
1477 expect(functions, hasLength(1)); | |
1478 FunctionElement function = functions[0]; | |
1479 expect(function, isNotNull); | |
1480 expect(function.hasImplicitReturnType, isTrue); | |
1481 expect(function.isSynthetic, isFalse); | |
1482 expect(function.typeParameters, hasLength(0)); | |
1483 } | |
1484 | |
1485 void test_visitFunctionTypeAlias() { | |
1486 ElementHolder holder = new ElementHolder(); | |
1487 ElementBuilder builder = _makeBuilder(holder); | |
1488 String aliasName = "F"; | |
1489 String parameterName = "E"; | |
1490 FunctionTypeAlias aliasNode = AstFactory.typeAlias( | |
1491 null, aliasName, AstFactory.typeParameterList([parameterName]), null); | |
1492 aliasNode.documentationComment = AstFactory.documentationComment( | |
1493 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1494 aliasNode.endToken.offset = 80; | |
1495 aliasNode.accept(builder); | |
1496 | |
1497 List<FunctionTypeAliasElement> aliases = holder.typeAliases; | |
1498 expect(aliases, hasLength(1)); | |
1499 FunctionTypeAliasElement alias = aliases[0]; | |
1500 expect(alias, isNotNull); | |
1501 _assertHasCodeRange(alias, 50, 31); | |
1502 expect(alias.documentationComment, '/// aaa'); | |
1503 expect(alias.name, aliasName); | |
1504 expect(alias.parameters, hasLength(0)); | |
1505 List<TypeParameterElement> typeParameters = alias.typeParameters; | |
1506 expect(typeParameters, hasLength(1)); | |
1507 TypeParameterElement typeParameter = typeParameters[0]; | |
1508 expect(typeParameter, isNotNull); | |
1509 expect(typeParameter.name, parameterName); | |
1510 } | |
1511 | |
1512 void test_visitFunctionTypedFormalParameter() { | |
1513 ElementHolder holder = new ElementHolder(); | |
1514 ElementBuilder builder = _makeBuilder(holder); | |
1515 String parameterName = "p"; | |
1516 FunctionTypedFormalParameter formalParameter = | |
1517 AstFactory.functionTypedFormalParameter(null, parameterName); | |
1518 _useParameterInMethod(formalParameter, 100, 110); | |
1519 formalParameter.accept(builder); | |
1520 List<ParameterElement> parameters = holder.parameters; | |
1521 expect(parameters, hasLength(1)); | |
1522 ParameterElement parameter = parameters[0]; | |
1523 expect(parameter, isNotNull); | |
1524 expect(parameter.name, parameterName); | |
1525 expect(parameter.initializer, isNull); | |
1526 expect(parameter.isConst, isFalse); | |
1527 expect(parameter.isFinal, isFalse); | |
1528 expect(parameter.isSynthetic, isFalse); | |
1529 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
1530 _assertVisibleRange(parameter, 100, 110); | |
1531 } | |
1532 | |
1533 void test_visitFunctionTypedFormalParameter_withTypeParameters() { | |
1534 ElementHolder holder = new ElementHolder(); | |
1535 ElementBuilder builder = _makeBuilder(holder); | |
1536 String parameterName = "p"; | |
1537 FunctionTypedFormalParameter formalParameter = | |
1538 AstFactory.functionTypedFormalParameter(null, parameterName); | |
1539 formalParameter.typeParameters = AstFactory.typeParameterList(['F']); | |
1540 _useParameterInMethod(formalParameter, 100, 110); | |
1541 formalParameter.accept(builder); | |
1542 List<ParameterElement> parameters = holder.parameters; | |
1543 expect(parameters, hasLength(1)); | |
1544 ParameterElement parameter = parameters[0]; | |
1545 expect(parameter, isNotNull); | |
1546 expect(parameter.name, parameterName); | |
1547 expect(parameter.initializer, isNull); | |
1548 expect(parameter.isConst, isFalse); | |
1549 expect(parameter.isFinal, isFalse); | |
1550 expect(parameter.isSynthetic, isFalse); | |
1551 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
1552 expect(parameter.typeParameters, hasLength(1)); | |
1553 _assertVisibleRange(parameter, 100, 110); | |
1554 } | |
1555 | |
1556 void test_visitLabeledStatement() { | |
1557 List<LabelElement> labels = | |
1558 buildElementsForText('f() { l: print(42); }').functions[0].labels; | |
1559 expect(labels, hasLength(1)); | |
1560 LabelElement label = labels[0]; | |
1561 expect(label, isNotNull); | |
1562 expect(label.name, 'l'); | |
1563 expect(label.isSynthetic, isFalse); | |
1564 } | |
1565 | |
1566 void test_visitMethodDeclaration_abstract() { | |
1567 // m(); | |
1568 ElementHolder holder = new ElementHolder(); | |
1569 ElementBuilder builder = _makeBuilder(holder); | |
1570 String methodName = "m"; | |
1571 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1572 null, | |
1573 null, | |
1574 null, | |
1575 null, | |
1576 AstFactory.identifier3(methodName), | |
1577 AstFactory.formalParameterList(), | |
1578 AstFactory.emptyFunctionBody()); | |
1579 methodDeclaration.accept(builder); | |
1580 | |
1581 List<MethodElement> methods = holder.methods; | |
1582 expect(methods, hasLength(1)); | |
1583 MethodElement method = methods[0]; | |
1584 expect(method, isNotNull); | |
1585 expect(method.hasImplicitReturnType, isTrue); | |
1586 expect(method.name, methodName); | |
1587 expect(method.functions, hasLength(0)); | |
1588 expect(method.labels, hasLength(0)); | |
1589 expect(method.localVariables, hasLength(0)); | |
1590 expect(method.parameters, hasLength(0)); | |
1591 expect(method.typeParameters, hasLength(0)); | |
1592 expect(method.isAbstract, isTrue); | |
1593 expect(method.isExternal, isFalse); | |
1594 expect(method.isStatic, isFalse); | |
1595 expect(method.isSynthetic, isFalse); | |
1596 } | |
1597 | |
1598 void test_visitMethodDeclaration_duplicateField_synthetic() { | |
1599 buildElementsForText(r''' | |
1600 class A { | |
1601 int f; | |
1602 int get f => 42; | |
1603 } | |
1604 '''); | |
1605 ClassDeclaration classNode = compilationUnit.declarations.single; | |
1606 // ClassElement | |
1607 ClassElement classElement = classNode.element; | |
1608 expect(classElement.fields, hasLength(2)); | |
1609 expect(classElement.accessors, hasLength(3)); | |
1610 FieldElement notSyntheticFieldElement = classElement.fields | |
1611 .singleWhere((f) => f.displayName == 'f' && !f.isSynthetic); | |
1612 FieldElement syntheticFieldElement = classElement.fields | |
1613 .singleWhere((f) => f.displayName == 'f' && f.isSynthetic); | |
1614 PropertyAccessorElement syntheticGetterElement = classElement.accessors | |
1615 .singleWhere( | |
1616 (a) => a.displayName == 'f' && a.isGetter && a.isSynthetic); | |
1617 PropertyAccessorElement syntheticSetterElement = classElement.accessors | |
1618 .singleWhere( | |
1619 (a) => a.displayName == 'f' && a.isSetter && a.isSynthetic); | |
1620 PropertyAccessorElement notSyntheticGetterElement = classElement.accessors | |
1621 .singleWhere( | |
1622 (a) => a.displayName == 'f' && a.isGetter && !a.isSynthetic); | |
1623 expect(notSyntheticFieldElement.getter, same(syntheticGetterElement)); | |
1624 expect(notSyntheticFieldElement.setter, same(syntheticSetterElement)); | |
1625 expect(syntheticFieldElement.getter, same(notSyntheticGetterElement)); | |
1626 expect(syntheticFieldElement.setter, isNull); | |
1627 // class members nodes and their elements | |
1628 FieldDeclaration fieldDeclNode = classNode.members[0]; | |
1629 VariableDeclaration fieldNode = fieldDeclNode.fields.variables.single; | |
1630 MethodDeclaration getterNode = classNode.members[1]; | |
1631 expect(fieldNode.element, notSyntheticFieldElement); | |
1632 expect(getterNode.element, notSyntheticGetterElement); | |
1633 } | |
1634 | |
1635 void test_visitMethodDeclaration_external() { | |
1636 // external m(); | |
1637 ElementHolder holder = new ElementHolder(); | |
1638 ElementBuilder builder = _makeBuilder(holder); | |
1639 String methodName = "m"; | |
1640 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1641 null, | |
1642 null, | |
1643 null, | |
1644 null, | |
1645 AstFactory.identifier3(methodName), | |
1646 AstFactory.formalParameterList(), | |
1647 AstFactory.emptyFunctionBody()); | |
1648 methodDeclaration.externalKeyword = | |
1649 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
1650 methodDeclaration.accept(builder); | |
1651 | |
1652 List<MethodElement> methods = holder.methods; | |
1653 expect(methods, hasLength(1)); | |
1654 MethodElement method = methods[0]; | |
1655 expect(method, isNotNull); | |
1656 expect(method.hasImplicitReturnType, isTrue); | |
1657 expect(method.name, methodName); | |
1658 expect(method.functions, hasLength(0)); | |
1659 expect(method.labels, hasLength(0)); | |
1660 expect(method.localVariables, hasLength(0)); | |
1661 expect(method.parameters, hasLength(0)); | |
1662 expect(method.typeParameters, hasLength(0)); | |
1663 expect(method.isAbstract, isFalse); | |
1664 expect(method.isExternal, isTrue); | |
1665 expect(method.isStatic, isFalse); | |
1666 expect(method.isSynthetic, isFalse); | |
1667 } | |
1668 | |
1669 void test_visitMethodDeclaration_getter() { | |
1670 // get m() {} | |
1671 ElementHolder holder = new ElementHolder(); | |
1672 ElementBuilder builder = _makeBuilder(holder); | |
1673 String methodName = "m"; | |
1674 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1675 null, | |
1676 null, | |
1677 Keyword.GET, | |
1678 null, | |
1679 AstFactory.identifier3(methodName), | |
1680 AstFactory.formalParameterList(), | |
1681 AstFactory.blockFunctionBody2()); | |
1682 methodDeclaration.documentationComment = AstFactory.documentationComment( | |
1683 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1684 methodDeclaration.endToken.offset = 80; | |
1685 methodDeclaration.accept(builder); | |
1686 | |
1687 List<FieldElement> fields = holder.fields; | |
1688 expect(fields, hasLength(1)); | |
1689 FieldElement field = fields[0]; | |
1690 expect(field, isNotNull); | |
1691 expect(field.name, methodName); | |
1692 expect(field.isSynthetic, isTrue); | |
1693 expect(field.setter, isNull); | |
1694 PropertyAccessorElement getter = field.getter; | |
1695 expect(getter, isNotNull); | |
1696 _assertHasCodeRange(getter, 50, 31); | |
1697 expect(getter.documentationComment, '/// aaa'); | |
1698 expect(getter.hasImplicitReturnType, isTrue); | |
1699 expect(getter.isAbstract, isFalse); | |
1700 expect(getter.isExternal, isFalse); | |
1701 expect(getter.isGetter, isTrue); | |
1702 expect(getter.isSynthetic, isFalse); | |
1703 expect(getter.name, methodName); | |
1704 expect(getter.variable, field); | |
1705 expect(getter.functions, hasLength(0)); | |
1706 expect(getter.labels, hasLength(0)); | |
1707 expect(getter.localVariables, hasLength(0)); | |
1708 expect(getter.parameters, hasLength(0)); | |
1709 } | |
1710 | |
1711 void test_visitMethodDeclaration_getter_abstract() { | |
1712 // get m(); | |
1713 ElementHolder holder = new ElementHolder(); | |
1714 ElementBuilder builder = _makeBuilder(holder); | |
1715 String methodName = "m"; | |
1716 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1717 null, | |
1718 null, | |
1719 Keyword.GET, | |
1720 null, | |
1721 AstFactory.identifier3(methodName), | |
1722 AstFactory.formalParameterList(), | |
1723 AstFactory.emptyFunctionBody()); | |
1724 methodDeclaration.accept(builder); | |
1725 | |
1726 List<FieldElement> fields = holder.fields; | |
1727 expect(fields, hasLength(1)); | |
1728 FieldElement field = fields[0]; | |
1729 expect(field, isNotNull); | |
1730 expect(field.name, methodName); | |
1731 expect(field.isSynthetic, isTrue); | |
1732 expect(field.setter, isNull); | |
1733 PropertyAccessorElement getter = field.getter; | |
1734 expect(getter, isNotNull); | |
1735 expect(getter.hasImplicitReturnType, isTrue); | |
1736 expect(getter.isAbstract, isTrue); | |
1737 expect(getter.isExternal, isFalse); | |
1738 expect(getter.isGetter, isTrue); | |
1739 expect(getter.isSynthetic, isFalse); | |
1740 expect(getter.name, methodName); | |
1741 expect(getter.variable, field); | |
1742 expect(getter.functions, hasLength(0)); | |
1743 expect(getter.labels, hasLength(0)); | |
1744 expect(getter.localVariables, hasLength(0)); | |
1745 expect(getter.parameters, hasLength(0)); | |
1746 } | |
1747 | |
1748 void test_visitMethodDeclaration_getter_external() { | |
1749 // external get m(); | |
1750 ElementHolder holder = new ElementHolder(); | |
1751 ElementBuilder builder = _makeBuilder(holder); | |
1752 String methodName = "m"; | |
1753 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration( | |
1754 null, | |
1755 null, | |
1756 Keyword.GET, | |
1757 null, | |
1758 AstFactory.identifier3(methodName), | |
1759 AstFactory.formalParameterList()); | |
1760 methodDeclaration.externalKeyword = | |
1761 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
1762 methodDeclaration.accept(builder); | |
1763 | |
1764 List<FieldElement> fields = holder.fields; | |
1765 expect(fields, hasLength(1)); | |
1766 FieldElement field = fields[0]; | |
1767 expect(field, isNotNull); | |
1768 expect(field.name, methodName); | |
1769 expect(field.isSynthetic, isTrue); | |
1770 expect(field.setter, isNull); | |
1771 PropertyAccessorElement getter = field.getter; | |
1772 expect(getter, isNotNull); | |
1773 expect(getter.hasImplicitReturnType, isTrue); | |
1774 expect(getter.isAbstract, isFalse); | |
1775 expect(getter.isExternal, isTrue); | |
1776 expect(getter.isGetter, isTrue); | |
1777 expect(getter.isSynthetic, isFalse); | |
1778 expect(getter.name, methodName); | |
1779 expect(getter.variable, field); | |
1780 expect(getter.functions, hasLength(0)); | |
1781 expect(getter.labels, hasLength(0)); | |
1782 expect(getter.localVariables, hasLength(0)); | |
1783 expect(getter.parameters, hasLength(0)); | |
1784 } | |
1785 | |
1786 void test_visitMethodDeclaration_minimal() { | |
1787 // T m() {} | |
1788 ElementHolder holder = new ElementHolder(); | |
1789 ElementBuilder builder = _makeBuilder(holder); | |
1790 String methodName = "m"; | |
1791 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1792 null, | |
1793 AstFactory.typeName4('T'), | |
1794 null, | |
1795 null, | |
1796 AstFactory.identifier3(methodName), | |
1797 AstFactory.formalParameterList(), | |
1798 AstFactory.blockFunctionBody2()); | |
1799 methodDeclaration.documentationComment = AstFactory.documentationComment( | |
1800 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1801 methodDeclaration.endToken.offset = 80; | |
1802 methodDeclaration.accept(builder); | |
1803 | |
1804 List<MethodElement> methods = holder.methods; | |
1805 expect(methods, hasLength(1)); | |
1806 MethodElement method = methods[0]; | |
1807 expect(method, isNotNull); | |
1808 _assertHasCodeRange(method, 50, 31); | |
1809 expect(method.documentationComment, '/// aaa'); | |
1810 expect(method.hasImplicitReturnType, isFalse); | |
1811 expect(method.name, methodName); | |
1812 expect(method.functions, hasLength(0)); | |
1813 expect(method.labels, hasLength(0)); | |
1814 expect(method.localVariables, hasLength(0)); | |
1815 expect(method.parameters, hasLength(0)); | |
1816 expect(method.typeParameters, hasLength(0)); | |
1817 expect(method.isAbstract, isFalse); | |
1818 expect(method.isExternal, isFalse); | |
1819 expect(method.isStatic, isFalse); | |
1820 expect(method.isSynthetic, isFalse); | |
1821 } | |
1822 | |
1823 void test_visitMethodDeclaration_operator() { | |
1824 // operator +(addend) {} | |
1825 ElementHolder holder = new ElementHolder(); | |
1826 ElementBuilder builder = _makeBuilder(holder); | |
1827 String methodName = "+"; | |
1828 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1829 null, | |
1830 null, | |
1831 null, | |
1832 Keyword.OPERATOR, | |
1833 AstFactory.identifier3(methodName), | |
1834 AstFactory | |
1835 .formalParameterList([AstFactory.simpleFormalParameter3("addend")]), | |
1836 AstFactory.blockFunctionBody2()); | |
1837 methodDeclaration.accept(builder); | |
1838 | |
1839 List<MethodElement> methods = holder.methods; | |
1840 expect(methods, hasLength(1)); | |
1841 MethodElement method = methods[0]; | |
1842 expect(method, isNotNull); | |
1843 expect(method.hasImplicitReturnType, isTrue); | |
1844 expect(method.name, methodName); | |
1845 expect(method.functions, hasLength(0)); | |
1846 expect(method.labels, hasLength(0)); | |
1847 expect(method.localVariables, hasLength(0)); | |
1848 expect(method.parameters, hasLength(1)); | |
1849 expect(method.typeParameters, hasLength(0)); | |
1850 expect(method.isAbstract, isFalse); | |
1851 expect(method.isExternal, isFalse); | |
1852 expect(method.isStatic, isFalse); | |
1853 expect(method.isSynthetic, isFalse); | |
1854 } | |
1855 | |
1856 void test_visitMethodDeclaration_setter() { | |
1857 // set m() {} | |
1858 ElementHolder holder = new ElementHolder(); | |
1859 ElementBuilder builder = _makeBuilder(holder); | |
1860 String methodName = "m"; | |
1861 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1862 null, | |
1863 null, | |
1864 Keyword.SET, | |
1865 null, | |
1866 AstFactory.identifier3(methodName), | |
1867 AstFactory.formalParameterList(), | |
1868 AstFactory.blockFunctionBody2()); | |
1869 methodDeclaration.documentationComment = AstFactory.documentationComment( | |
1870 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
1871 methodDeclaration.endToken.offset = 80; | |
1872 methodDeclaration.accept(builder); | |
1873 | |
1874 List<FieldElement> fields = holder.fields; | |
1875 expect(fields, hasLength(1)); | |
1876 FieldElement field = fields[0]; | |
1877 expect(field, isNotNull); | |
1878 expect(field.name, methodName); | |
1879 expect(field.isSynthetic, isTrue); | |
1880 expect(field.getter, isNull); | |
1881 | |
1882 PropertyAccessorElement setter = field.setter; | |
1883 expect(setter, isNotNull); | |
1884 _assertHasCodeRange(setter, 50, 31); | |
1885 expect(setter.documentationComment, '/// aaa'); | |
1886 expect(setter.hasImplicitReturnType, isTrue); | |
1887 expect(setter.isAbstract, isFalse); | |
1888 expect(setter.isExternal, isFalse); | |
1889 expect(setter.isSetter, isTrue); | |
1890 expect(setter.isSynthetic, isFalse); | |
1891 expect(setter.name, "$methodName="); | |
1892 expect(setter.displayName, methodName); | |
1893 expect(setter.variable, field); | |
1894 expect(setter.functions, hasLength(0)); | |
1895 expect(setter.labels, hasLength(0)); | |
1896 expect(setter.localVariables, hasLength(0)); | |
1897 expect(setter.parameters, hasLength(0)); | |
1898 } | |
1899 | |
1900 void test_visitMethodDeclaration_setter_abstract() { | |
1901 // set m(); | |
1902 ElementHolder holder = new ElementHolder(); | |
1903 ElementBuilder builder = _makeBuilder(holder); | |
1904 String methodName = "m"; | |
1905 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1906 null, | |
1907 null, | |
1908 Keyword.SET, | |
1909 null, | |
1910 AstFactory.identifier3(methodName), | |
1911 AstFactory.formalParameterList(), | |
1912 AstFactory.emptyFunctionBody()); | |
1913 methodDeclaration.accept(builder); | |
1914 | |
1915 List<FieldElement> fields = holder.fields; | |
1916 expect(fields, hasLength(1)); | |
1917 FieldElement field = fields[0]; | |
1918 expect(field, isNotNull); | |
1919 expect(field.name, methodName); | |
1920 expect(field.isSynthetic, isTrue); | |
1921 expect(field.getter, isNull); | |
1922 PropertyAccessorElement setter = field.setter; | |
1923 expect(setter, isNotNull); | |
1924 expect(setter.hasImplicitReturnType, isTrue); | |
1925 expect(setter.isAbstract, isTrue); | |
1926 expect(setter.isExternal, isFalse); | |
1927 expect(setter.isSetter, isTrue); | |
1928 expect(setter.isSynthetic, isFalse); | |
1929 expect(setter.name, "$methodName="); | |
1930 expect(setter.displayName, methodName); | |
1931 expect(setter.variable, field); | |
1932 expect(setter.functions, hasLength(0)); | |
1933 expect(setter.labels, hasLength(0)); | |
1934 expect(setter.localVariables, hasLength(0)); | |
1935 expect(setter.parameters, hasLength(0)); | |
1936 } | |
1937 | |
1938 void test_visitMethodDeclaration_setter_external() { | |
1939 // external m(); | |
1940 ElementHolder holder = new ElementHolder(); | |
1941 ElementBuilder builder = _makeBuilder(holder); | |
1942 String methodName = "m"; | |
1943 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration( | |
1944 null, | |
1945 null, | |
1946 Keyword.SET, | |
1947 null, | |
1948 AstFactory.identifier3(methodName), | |
1949 AstFactory.formalParameterList()); | |
1950 methodDeclaration.externalKeyword = | |
1951 TokenFactory.tokenFromKeyword(Keyword.EXTERNAL); | |
1952 methodDeclaration.accept(builder); | |
1953 | |
1954 List<FieldElement> fields = holder.fields; | |
1955 expect(fields, hasLength(1)); | |
1956 FieldElement field = fields[0]; | |
1957 expect(field, isNotNull); | |
1958 expect(field.name, methodName); | |
1959 expect(field.isSynthetic, isTrue); | |
1960 expect(field.getter, isNull); | |
1961 PropertyAccessorElement setter = field.setter; | |
1962 expect(setter, isNotNull); | |
1963 expect(setter.hasImplicitReturnType, isTrue); | |
1964 expect(setter.isAbstract, isFalse); | |
1965 expect(setter.isExternal, isTrue); | |
1966 expect(setter.isSetter, isTrue); | |
1967 expect(setter.isSynthetic, isFalse); | |
1968 expect(setter.name, "$methodName="); | |
1969 expect(setter.displayName, methodName); | |
1970 expect(setter.variable, field); | |
1971 expect(setter.functions, hasLength(0)); | |
1972 expect(setter.labels, hasLength(0)); | |
1973 expect(setter.localVariables, hasLength(0)); | |
1974 expect(setter.parameters, hasLength(0)); | |
1975 } | |
1976 | |
1977 void test_visitMethodDeclaration_static() { | |
1978 // static m() {} | |
1979 ElementHolder holder = new ElementHolder(); | |
1980 ElementBuilder builder = _makeBuilder(holder); | |
1981 String methodName = "m"; | |
1982 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
1983 Keyword.STATIC, | |
1984 null, | |
1985 null, | |
1986 null, | |
1987 AstFactory.identifier3(methodName), | |
1988 AstFactory.formalParameterList(), | |
1989 AstFactory.blockFunctionBody2()); | |
1990 methodDeclaration.accept(builder); | |
1991 List<MethodElement> methods = holder.methods; | |
1992 expect(methods, hasLength(1)); | |
1993 MethodElement method = methods[0]; | |
1994 expect(method, isNotNull); | |
1995 expect(method.hasImplicitReturnType, isTrue); | |
1996 expect(method.name, methodName); | |
1997 expect(method.functions, hasLength(0)); | |
1998 expect(method.labels, hasLength(0)); | |
1999 expect(method.localVariables, hasLength(0)); | |
2000 expect(method.parameters, hasLength(0)); | |
2001 expect(method.typeParameters, hasLength(0)); | |
2002 expect(method.isAbstract, isFalse); | |
2003 expect(method.isExternal, isFalse); | |
2004 expect(method.isStatic, isTrue); | |
2005 expect(method.isSynthetic, isFalse); | |
2006 } | |
2007 | |
2008 void test_visitMethodDeclaration_typeParameters() { | |
2009 // m<E>() {} | |
2010 ElementHolder holder = new ElementHolder(); | |
2011 ElementBuilder builder = _makeBuilder(holder); | |
2012 String methodName = "m"; | |
2013 MethodDeclaration methodDeclaration = AstFactory.methodDeclaration2( | |
2014 null, | |
2015 null, | |
2016 null, | |
2017 null, | |
2018 AstFactory.identifier3(methodName), | |
2019 AstFactory.formalParameterList(), | |
2020 AstFactory.blockFunctionBody2()); | |
2021 methodDeclaration.typeParameters = AstFactory.typeParameterList(['E']); | |
2022 methodDeclaration.accept(builder); | |
2023 | |
2024 List<MethodElement> methods = holder.methods; | |
2025 expect(methods, hasLength(1)); | |
2026 MethodElement method = methods[0]; | |
2027 expect(method, isNotNull); | |
2028 expect(method.hasImplicitReturnType, isTrue); | |
2029 expect(method.name, methodName); | |
2030 expect(method.functions, hasLength(0)); | |
2031 expect(method.labels, hasLength(0)); | |
2032 expect(method.localVariables, hasLength(0)); | |
2033 expect(method.parameters, hasLength(0)); | |
2034 expect(method.typeParameters, hasLength(1)); | |
2035 expect(method.isAbstract, isFalse); | |
2036 expect(method.isExternal, isFalse); | |
2037 expect(method.isStatic, isFalse); | |
2038 expect(method.isSynthetic, isFalse); | |
2039 } | |
2040 | |
2041 void test_visitMethodDeclaration_withMembers() { | |
2042 MethodElement method = buildElementsForText( | |
2043 'class C { m(p) { var v; try { l: return; } catch (e) {} } }') | |
2044 .types[0] | |
2045 .methods[0]; | |
2046 String methodName = "m"; | |
2047 String parameterName = "p"; | |
2048 String localVariableName = "v"; | |
2049 String labelName = "l"; | |
2050 String exceptionParameterName = "e"; | |
2051 expect(method, isNotNull); | |
2052 expect(method.hasImplicitReturnType, isTrue); | |
2053 expect(method.name, methodName); | |
2054 expect(method.typeParameters, hasLength(0)); | |
2055 expect(method.isAbstract, isFalse); | |
2056 expect(method.isExternal, isFalse); | |
2057 expect(method.isStatic, isFalse); | |
2058 expect(method.isSynthetic, isFalse); | |
2059 List<VariableElement> parameters = method.parameters; | |
2060 expect(parameters, hasLength(1)); | |
2061 VariableElement parameter = parameters[0]; | |
2062 expect(parameter, isNotNull); | |
2063 expect(parameter.name, parameterName); | |
2064 List<VariableElement> localVariables = method.localVariables; | |
2065 expect(localVariables, hasLength(2)); | |
2066 VariableElement firstVariable = localVariables[0]; | |
2067 VariableElement secondVariable = localVariables[1]; | |
2068 expect(firstVariable, isNotNull); | |
2069 expect(secondVariable, isNotNull); | |
2070 expect( | |
2071 (firstVariable.name == localVariableName && | |
2072 secondVariable.name == exceptionParameterName) || | |
2073 (firstVariable.name == exceptionParameterName && | |
2074 secondVariable.name == localVariableName), | |
2075 isTrue); | |
2076 List<LabelElement> labels = method.labels; | |
2077 expect(labels, hasLength(1)); | |
2078 LabelElement label = labels[0]; | |
2079 expect(label, isNotNull); | |
2080 expect(label.name, labelName); | |
2081 } | |
2082 | |
2083 void test_visitNamedFormalParameter() { | |
2084 ElementHolder holder = new ElementHolder(); | |
2085 ElementBuilder builder = _makeBuilder(holder); | |
2086 String parameterName = "p"; | |
2087 DefaultFormalParameter formalParameter = AstFactory.namedFormalParameter( | |
2088 AstFactory.simpleFormalParameter3(parameterName), | |
2089 AstFactory.identifier3("42")); | |
2090 _useParameterInMethod(formalParameter, 100, 110); | |
2091 formalParameter.beginToken.offset = 50; | |
2092 formalParameter.endToken.offset = 80; | |
2093 formalParameter.accept(builder); | |
2094 List<ParameterElement> parameters = holder.parameters; | |
2095 expect(parameters, hasLength(1)); | |
2096 ParameterElement parameter = parameters[0]; | |
2097 expect(parameter, isNotNull); | |
2098 _assertHasCodeRange(parameter, 50, 32); | |
2099 expect(parameter.name, parameterName); | |
2100 expect(parameter.isConst, isFalse); | |
2101 expect(parameter.isFinal, isFalse); | |
2102 expect(parameter.isSynthetic, isFalse); | |
2103 expect(parameter.parameterKind, ParameterKind.NAMED); | |
2104 _assertVisibleRange(parameter, 100, 110); | |
2105 expect(parameter.defaultValueCode, "42"); | |
2106 FunctionElement initializer = parameter.initializer; | |
2107 expect(initializer, isNotNull); | |
2108 expect(initializer.isSynthetic, isTrue); | |
2109 expect(initializer.hasImplicitReturnType, isTrue); | |
2110 } | |
2111 | |
2112 void test_visitSimpleFormalParameter_noType() { | |
2113 // p | |
2114 ElementHolder holder = new ElementHolder(); | |
2115 ElementBuilder builder = _makeBuilder(holder); | |
2116 String parameterName = "p"; | |
2117 SimpleFormalParameter formalParameter = | |
2118 AstFactory.simpleFormalParameter3(parameterName); | |
2119 _useParameterInMethod(formalParameter, 100, 110); | |
2120 formalParameter.accept(builder); | |
2121 List<ParameterElement> parameters = holder.parameters; | |
2122 expect(parameters, hasLength(1)); | |
2123 ParameterElement parameter = parameters[0]; | |
2124 expect(parameter, isNotNull); | |
2125 expect(parameter.hasImplicitType, isTrue); | |
2126 expect(parameter.initializer, isNull); | |
2127 expect(parameter.isConst, isFalse); | |
2128 expect(parameter.isFinal, isFalse); | |
2129 expect(parameter.isSynthetic, isFalse); | |
2130 expect(parameter.name, parameterName); | |
2131 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
2132 _assertVisibleRange(parameter, 100, 110); | |
2133 } | |
2134 | |
2135 void test_visitSimpleFormalParameter_type() { | |
2136 // T p | |
2137 ElementHolder holder = new ElementHolder(); | |
2138 ElementBuilder builder = _makeBuilder(holder); | |
2139 String parameterName = "p"; | |
2140 SimpleFormalParameter formalParameter = AstFactory.simpleFormalParameter4( | |
2141 AstFactory.typeName4('T'), parameterName); | |
2142 _useParameterInMethod(formalParameter, 100, 110); | |
2143 formalParameter.accept(builder); | |
2144 List<ParameterElement> parameters = holder.parameters; | |
2145 expect(parameters, hasLength(1)); | |
2146 ParameterElement parameter = parameters[0]; | |
2147 expect(parameter, isNotNull); | |
2148 expect(parameter.hasImplicitType, isFalse); | |
2149 expect(parameter.initializer, isNull); | |
2150 expect(parameter.isConst, isFalse); | |
2151 expect(parameter.isFinal, isFalse); | |
2152 expect(parameter.isSynthetic, isFalse); | |
2153 expect(parameter.name, parameterName); | |
2154 expect(parameter.parameterKind, ParameterKind.REQUIRED); | |
2155 _assertVisibleRange(parameter, 100, 110); | |
2156 } | |
2157 | |
2158 void test_visitTypeAlias_minimal() { | |
2159 ElementHolder holder = new ElementHolder(); | |
2160 ElementBuilder builder = _makeBuilder(holder); | |
2161 String aliasName = "F"; | |
2162 TypeAlias typeAlias = AstFactory.typeAlias(null, aliasName, null, null); | |
2163 typeAlias.accept(builder); | |
2164 List<FunctionTypeAliasElement> aliases = holder.typeAliases; | |
2165 expect(aliases, hasLength(1)); | |
2166 FunctionTypeAliasElement alias = aliases[0]; | |
2167 expect(alias, isNotNull); | |
2168 expect(alias.name, aliasName); | |
2169 expect(alias.type, isNotNull); | |
2170 expect(alias.isSynthetic, isFalse); | |
2171 } | |
2172 | |
2173 void test_visitTypeAlias_withFormalParameters() { | |
2174 ElementHolder holder = new ElementHolder(); | |
2175 ElementBuilder builder = _makeBuilder(holder); | |
2176 String aliasName = "F"; | |
2177 String firstParameterName = "x"; | |
2178 String secondParameterName = "y"; | |
2179 TypeAlias typeAlias = AstFactory.typeAlias( | |
2180 null, | |
2181 aliasName, | |
2182 AstFactory.typeParameterList(), | |
2183 AstFactory.formalParameterList([ | |
2184 AstFactory.simpleFormalParameter3(firstParameterName), | |
2185 AstFactory.simpleFormalParameter3(secondParameterName) | |
2186 ])); | |
2187 typeAlias.beginToken.offset = 50; | |
2188 typeAlias.endToken.offset = 80; | |
2189 typeAlias.accept(builder); | |
2190 List<FunctionTypeAliasElement> aliases = holder.typeAliases; | |
2191 expect(aliases, hasLength(1)); | |
2192 FunctionTypeAliasElement alias = aliases[0]; | |
2193 expect(alias, isNotNull); | |
2194 _assertHasCodeRange(alias, 50, 31); | |
2195 expect(alias.name, aliasName); | |
2196 expect(alias.type, isNotNull); | |
2197 expect(alias.isSynthetic, isFalse); | |
2198 List<VariableElement> parameters = alias.parameters; | |
2199 expect(parameters, hasLength(2)); | |
2200 expect(parameters[0].name, firstParameterName); | |
2201 expect(parameters[1].name, secondParameterName); | |
2202 List<TypeParameterElement> typeParameters = alias.typeParameters; | |
2203 expect(typeParameters, isNotNull); | |
2204 expect(typeParameters, hasLength(0)); | |
2205 } | |
2206 | |
2207 void test_visitTypeAlias_withTypeParameters() { | |
2208 ElementHolder holder = new ElementHolder(); | |
2209 ElementBuilder builder = _makeBuilder(holder); | |
2210 String aliasName = "F"; | |
2211 String firstTypeParameterName = "A"; | |
2212 String secondTypeParameterName = "B"; | |
2213 TypeAlias typeAlias = AstFactory.typeAlias( | |
2214 null, | |
2215 aliasName, | |
2216 AstFactory.typeParameterList( | |
2217 [firstTypeParameterName, secondTypeParameterName]), | |
2218 AstFactory.formalParameterList()); | |
2219 typeAlias.accept(builder); | |
2220 List<FunctionTypeAliasElement> aliases = holder.typeAliases; | |
2221 expect(aliases, hasLength(1)); | |
2222 FunctionTypeAliasElement alias = aliases[0]; | |
2223 expect(alias, isNotNull); | |
2224 expect(alias.name, aliasName); | |
2225 expect(alias.type, isNotNull); | |
2226 expect(alias.isSynthetic, isFalse); | |
2227 List<VariableElement> parameters = alias.parameters; | |
2228 expect(parameters, isNotNull); | |
2229 expect(parameters, hasLength(0)); | |
2230 List<TypeParameterElement> typeParameters = alias.typeParameters; | |
2231 expect(typeParameters, hasLength(2)); | |
2232 expect(typeParameters[0].name, firstTypeParameterName); | |
2233 expect(typeParameters[1].name, secondTypeParameterName); | |
2234 } | |
2235 | |
2236 void test_visitTypeParameter() { | |
2237 ElementHolder holder = new ElementHolder(); | |
2238 ElementBuilder builder = _makeBuilder(holder); | |
2239 String parameterName = "E"; | |
2240 TypeParameter typeParameter = AstFactory.typeParameter(parameterName); | |
2241 typeParameter.beginToken.offset = 50; | |
2242 typeParameter.accept(builder); | |
2243 List<TypeParameterElement> typeParameters = holder.typeParameters; | |
2244 expect(typeParameters, hasLength(1)); | |
2245 TypeParameterElement typeParameterElement = typeParameters[0]; | |
2246 expect(typeParameterElement, isNotNull); | |
2247 _assertHasCodeRange(typeParameterElement, 50, 1); | |
2248 expect(typeParameterElement.name, parameterName); | |
2249 expect(typeParameterElement.bound, isNull); | |
2250 expect(typeParameterElement.isSynthetic, isFalse); | |
2251 } | |
2252 | |
2253 void test_visitVariableDeclaration_inConstructor() { | |
2254 List<ConstructorElement> constructors = | |
2255 buildElementsForText('class C { C() { var v = 1; } }') | |
2256 .types[0] | |
2257 .constructors; | |
2258 expect(constructors, hasLength(1)); | |
2259 List<LocalVariableElement> variableElements = | |
2260 constructors[0].localVariables; | |
2261 expect(variableElements, hasLength(1)); | |
2262 LocalVariableElement variableElement = variableElements[0]; | |
2263 _assertHasCodeRange(variableElement, 16, 10); | |
2264 expect(variableElement.hasImplicitType, isTrue); | |
2265 expect(variableElement.name, 'v'); | |
2266 _assertVisibleRange(variableElement, 14, 28); | |
2267 } | |
2268 | |
2269 void test_visitVariableDeclaration_inForEachStatement() { | |
2270 ElementHolder holder = new ElementHolder(); | |
2271 ElementBuilder builder = _makeBuilder(holder); | |
2272 // | |
2273 // m() { for (var v in []) } | |
2274 // | |
2275 String variableName = "v"; | |
2276 Statement statement = AstFactory.forEachStatement( | |
2277 AstFactory.declaredIdentifier3('v'), | |
2278 AstFactory.listLiteral(), | |
2279 AstFactory.block()); | |
2280 _setNodeSourceRange(statement, 100, 110); | |
2281 MethodDeclaration method = AstFactory.methodDeclaration2( | |
2282 null, | |
2283 null, | |
2284 null, | |
2285 null, | |
2286 AstFactory.identifier3("m"), | |
2287 AstFactory.formalParameterList(), | |
2288 AstFactory.blockFunctionBody2([statement])); | |
2289 _setBlockBodySourceRange(method.body, 200, 220); | |
2290 method.accept(builder); | |
2291 | |
2292 List<MethodElement> methods = holder.methods; | |
2293 expect(methods, hasLength(1)); | |
2294 List<LocalVariableElement> variableElements = methods[0].localVariables; | |
2295 expect(variableElements, hasLength(1)); | |
2296 LocalVariableElement variableElement = variableElements[0]; | |
2297 expect(variableElement.name, variableName); | |
2298 _assertVisibleRange(variableElement, 100, 110); | |
2299 } | |
2300 | |
2301 void test_visitVariableDeclaration_inForStatement() { | |
2302 ElementHolder holder = new ElementHolder(); | |
2303 ElementBuilder builder = _makeBuilder(holder); | |
2304 // | |
2305 // m() { for (T v;;) } | |
2306 // | |
2307 String variableName = "v"; | |
2308 ForStatement statement = AstFactory.forStatement2( | |
2309 AstFactory.variableDeclarationList(null, AstFactory.typeName4('T'), | |
2310 [AstFactory.variableDeclaration('v')]), | |
2311 null, | |
2312 null, | |
2313 AstFactory.block()); | |
2314 _setNodeSourceRange(statement, 100, 110); | |
2315 MethodDeclaration method = AstFactory.methodDeclaration2( | |
2316 null, | |
2317 null, | |
2318 null, | |
2319 null, | |
2320 AstFactory.identifier3("m"), | |
2321 AstFactory.formalParameterList(), | |
2322 AstFactory.blockFunctionBody2([statement])); | |
2323 _setBlockBodySourceRange(method.body, 200, 220); | |
2324 method.accept(builder); | |
2325 | |
2326 List<MethodElement> methods = holder.methods; | |
2327 expect(methods, hasLength(1)); | |
2328 List<LocalVariableElement> variableElements = methods[0].localVariables; | |
2329 expect(variableElements, hasLength(1)); | |
2330 LocalVariableElement variableElement = variableElements[0]; | |
2331 expect(variableElement.name, variableName); | |
2332 _assertVisibleRange(variableElement, 100, 110); | |
2333 } | |
2334 | |
2335 void test_visitVariableDeclaration_inMethod() { | |
2336 ElementHolder holder = new ElementHolder(); | |
2337 ElementBuilder builder = _makeBuilder(holder); | |
2338 // | |
2339 // m() {T v;} | |
2340 // | |
2341 String variableName = "v"; | |
2342 VariableDeclaration variable = | |
2343 AstFactory.variableDeclaration2(variableName, null); | |
2344 Statement statement = AstFactory.variableDeclarationStatement( | |
2345 null, AstFactory.typeName4('T'), [variable]); | |
2346 MethodDeclaration method = AstFactory.methodDeclaration2( | |
2347 null, | |
2348 null, | |
2349 null, | |
2350 null, | |
2351 AstFactory.identifier3("m"), | |
2352 AstFactory.formalParameterList(), | |
2353 AstFactory.blockFunctionBody2([statement])); | |
2354 _setBlockBodySourceRange(method.body, 100, 110); | |
2355 method.accept(builder); | |
2356 | |
2357 List<MethodElement> methods = holder.methods; | |
2358 expect(methods, hasLength(1)); | |
2359 List<LocalVariableElement> variableElements = methods[0].localVariables; | |
2360 expect(variableElements, hasLength(1)); | |
2361 LocalVariableElement variableElement = variableElements[0]; | |
2362 expect(variableElement.hasImplicitType, isFalse); | |
2363 expect(variableElement.name, variableName); | |
2364 _assertVisibleRange(variableElement, 100, 110); | |
2365 } | |
2366 | |
2367 void test_visitVariableDeclaration_localNestedInFunction() { | |
2368 ElementHolder holder = new ElementHolder(); | |
2369 ElementBuilder builder = _makeBuilder(holder); | |
2370 // | |
2371 // var f = () {var v;}; | |
2372 // | |
2373 String variableName = "v"; | |
2374 VariableDeclaration variable = | |
2375 AstFactory.variableDeclaration2(variableName, null); | |
2376 Statement statement = | |
2377 AstFactory.variableDeclarationStatement2(null, [variable]); | |
2378 Expression initializer = AstFactory.functionExpression2( | |
2379 AstFactory.formalParameterList(), | |
2380 AstFactory.blockFunctionBody2([statement])); | |
2381 String fieldName = "f"; | |
2382 VariableDeclaration field = | |
2383 AstFactory.variableDeclaration2(fieldName, initializer); | |
2384 FieldDeclaration fieldDeclaration = | |
2385 AstFactory.fieldDeclaration2(false, null, [field]); | |
2386 fieldDeclaration.accept(builder); | |
2387 | |
2388 List<FieldElement> variables = holder.fields; | |
2389 expect(variables, hasLength(1)); | |
2390 FieldElement fieldElement = variables[0]; | |
2391 expect(fieldElement, isNotNull); | |
2392 FunctionElement initializerElement = fieldElement.initializer; | |
2393 expect(initializerElement, isNotNull); | |
2394 expect(initializerElement.hasImplicitReturnType, isTrue); | |
2395 List<FunctionElement> functionElements = initializerElement.functions; | |
2396 expect(functionElements, hasLength(1)); | |
2397 List<LocalVariableElement> variableElements = | |
2398 functionElements[0].localVariables; | |
2399 expect(variableElements, hasLength(1)); | |
2400 LocalVariableElement variableElement = variableElements[0]; | |
2401 expect(variableElement.hasImplicitType, isTrue); | |
2402 expect(variableElement.isConst, isFalse); | |
2403 expect(variableElement.isFinal, isFalse); | |
2404 expect(variableElement.isSynthetic, isFalse); | |
2405 expect(variableElement.name, variableName); | |
2406 } | |
2407 | |
2408 void test_visitVariableDeclaration_noInitializer() { | |
2409 // var v; | |
2410 ElementHolder holder = new ElementHolder(); | |
2411 ElementBuilder builder = _makeBuilder(holder); | |
2412 String variableName = "v"; | |
2413 VariableDeclaration variableDeclaration = | |
2414 AstFactory.variableDeclaration2(variableName, null); | |
2415 AstFactory.variableDeclarationList2(null, [variableDeclaration]); | |
2416 variableDeclaration.accept(builder); | |
2417 | |
2418 List<TopLevelVariableElement> variables = holder.topLevelVariables; | |
2419 expect(variables, hasLength(1)); | |
2420 TopLevelVariableElement variable = variables[0]; | |
2421 expect(variable, isNotNull); | |
2422 expect(variable.hasImplicitType, isTrue); | |
2423 expect(variable.initializer, isNull); | |
2424 expect(variable.name, variableName); | |
2425 expect(variable.isConst, isFalse); | |
2426 expect(variable.isFinal, isFalse); | |
2427 expect(variable.isSynthetic, isFalse); | |
2428 expect(variable.getter, isNotNull); | |
2429 expect(variable.setter, isNotNull); | |
2430 } | |
2431 | |
2432 void test_visitVariableDeclaration_top() { | |
2433 // final a, b; | |
2434 ElementHolder holder = new ElementHolder(); | |
2435 ElementBuilder builder = _makeBuilder(holder); | |
2436 VariableDeclaration variableDeclaration1 = | |
2437 AstFactory.variableDeclaration('a'); | |
2438 VariableDeclaration variableDeclaration2 = | |
2439 AstFactory.variableDeclaration('b'); | |
2440 TopLevelVariableDeclaration topLevelVariableDeclaration = AstFactory | |
2441 .topLevelVariableDeclaration( | |
2442 Keyword.FINAL, null, [variableDeclaration1, variableDeclaration2]); | |
2443 topLevelVariableDeclaration.documentationComment = AstFactory | |
2444 .documentationComment( | |
2445 [TokenFactory.tokenFromString('/// aaa')..offset = 50], []); | |
2446 | |
2447 topLevelVariableDeclaration.accept(builder); | |
2448 List<TopLevelVariableElement> variables = holder.topLevelVariables; | |
2449 expect(variables, hasLength(2)); | |
2450 | |
2451 TopLevelVariableElement variable1 = variables[0]; | |
2452 expect(variable1, isNotNull); | |
2453 expect(variable1.documentationComment, '/// aaa'); | |
2454 | |
2455 TopLevelVariableElement variable2 = variables[1]; | |
2456 expect(variable2, isNotNull); | |
2457 expect(variable2.documentationComment, '/// aaa'); | |
2458 } | |
2459 | |
2460 void test_visitVariableDeclaration_top_const_hasInitializer() { | |
2461 // const v = 42; | |
2462 ElementHolder holder = new ElementHolder(); | |
2463 ElementBuilder builder = _makeBuilder(holder); | |
2464 String variableName = "v"; | |
2465 VariableDeclaration variableDeclaration = | |
2466 AstFactory.variableDeclaration2(variableName, AstFactory.integer(42)); | |
2467 AstFactory.variableDeclarationList2(Keyword.CONST, [variableDeclaration]); | |
2468 variableDeclaration.accept(builder); | |
2469 | |
2470 List<TopLevelVariableElement> variables = holder.topLevelVariables; | |
2471 expect(variables, hasLength(1)); | |
2472 TopLevelVariableElement variable = variables[0]; | |
2473 expect(variable, new isInstanceOf<ConstTopLevelVariableElementImpl>()); | |
2474 expect(variable.initializer, isNotNull); | |
2475 expect(variable.initializer.type, isNotNull); | |
2476 expect(variable.initializer.hasImplicitReturnType, isTrue); | |
2477 expect(variable.name, variableName); | |
2478 expect(variable.hasImplicitType, isTrue); | |
2479 expect(variable.isConst, isTrue); | |
2480 expect(variable.isFinal, isFalse); | |
2481 expect(variable.isSynthetic, isFalse); | |
2482 expect(variable.getter, isNotNull); | |
2483 expect(variable.setter, isNull); | |
2484 } | |
2485 | |
2486 void test_visitVariableDeclaration_top_final() { | |
2487 // final v; | |
2488 ElementHolder holder = new ElementHolder(); | |
2489 ElementBuilder builder = _makeBuilder(holder); | |
2490 String variableName = "v"; | |
2491 VariableDeclaration variableDeclaration = | |
2492 AstFactory.variableDeclaration2(variableName, null); | |
2493 AstFactory.variableDeclarationList2(Keyword.FINAL, [variableDeclaration]); | |
2494 variableDeclaration.accept(builder); | |
2495 List<TopLevelVariableElement> variables = holder.topLevelVariables; | |
2496 expect(variables, hasLength(1)); | |
2497 TopLevelVariableElement variable = variables[0]; | |
2498 expect(variable, isNotNull); | |
2499 expect(variable.hasImplicitType, isTrue); | |
2500 expect(variable.initializer, isNull); | |
2501 expect(variable.name, variableName); | |
2502 expect(variable.isConst, isFalse); | |
2503 expect(variable.isFinal, isTrue); | |
2504 expect(variable.isSynthetic, isFalse); | |
2505 expect(variable.getter, isNotNull); | |
2506 expect(variable.setter, isNull); | |
2507 } | |
2508 | |
2509 void _assertHasCodeRange(Element element, int offset, int length) { | |
2510 ElementImpl elementImpl = element; | |
2511 expect(elementImpl.codeOffset, offset); | |
2512 expect(elementImpl.codeLength, length); | |
2513 } | |
2514 | |
2515 void _assertVisibleRange(LocalElement element, int offset, int end) { | |
2516 SourceRange visibleRange = element.visibleRange; | |
2517 expect(visibleRange.offset, offset); | |
2518 expect(visibleRange.end, end); | |
2519 } | |
2520 | |
2521 ElementBuilder _makeBuilder(ElementHolder holder) => | |
2522 new ElementBuilder(holder, new CompilationUnitElementImpl('test.dart')); | |
2523 | |
2524 void _setBlockBodySourceRange(BlockFunctionBody body, int offset, int end) { | |
2525 _setNodeSourceRange(body.block, offset, end); | |
2526 } | |
2527 | |
2528 void _setNodeSourceRange(AstNode node, int offset, int end) { | |
2529 node.beginToken.offset = offset; | |
2530 Token endToken = node.endToken; | |
2531 endToken.offset = end - endToken.length; | |
2532 } | |
2533 | |
2534 void _useParameterInMethod( | |
2535 FormalParameter formalParameter, int blockOffset, int blockEnd) { | |
2536 Block block = AstFactory.block(); | |
2537 block.leftBracket.offset = blockOffset; | |
2538 block.rightBracket.offset = blockEnd - 1; | |
2539 BlockFunctionBody body = AstFactory.blockFunctionBody(block); | |
2540 AstFactory.methodDeclaration2( | |
2541 null, | |
2542 null, | |
2543 null, | |
2544 null, | |
2545 AstFactory.identifier3("main"), | |
2546 AstFactory.formalParameterList([formalParameter]), | |
2547 body); | |
2548 } | |
2549 } | |
2550 | |
2551 @reflectiveTest | |
2552 class ElementLocatorTest extends ResolverTestCase { | 353 class ElementLocatorTest extends ResolverTestCase { |
2553 void fail_locate_Identifier_partOfDirective() { | 354 void fail_locate_Identifier_partOfDirective() { |
2554 // Can't resolve the library element without the library declaration. | 355 // Can't resolve the library element without the library declaration. |
2555 // AstNode id = findNodeIn("foo", "part of foo.bar;"); | 356 // AstNode id = findNodeIn("foo", "part of foo.bar;"); |
2556 // Element element = ElementLocator.locate(id); | 357 // Element element = ElementLocator.locate(id); |
2557 // assertInstanceOf(LibraryElement.class, element); | 358 // assertInstanceOf(LibraryElement.class, element); |
2558 fail("Test this case"); | 359 fail("Test this case"); |
2559 } | 360 } |
2560 | 361 |
2561 @override | 362 @override |
(...skipping 1906 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4468 expect(UriKind.fromEncoding(0x70), same(UriKind.PACKAGE_URI)); | 2269 expect(UriKind.fromEncoding(0x70), same(UriKind.PACKAGE_URI)); |
4469 expect(UriKind.fromEncoding(0x58), same(null)); | 2270 expect(UriKind.fromEncoding(0x58), same(null)); |
4470 } | 2271 } |
4471 | 2272 |
4472 void test_getEncoding() { | 2273 void test_getEncoding() { |
4473 expect(UriKind.DART_URI.encoding, 0x64); | 2274 expect(UriKind.DART_URI.encoding, 0x64); |
4474 expect(UriKind.FILE_URI.encoding, 0x66); | 2275 expect(UriKind.FILE_URI.encoding, 0x66); |
4475 expect(UriKind.PACKAGE_URI.encoding, 0x70); | 2276 expect(UriKind.PACKAGE_URI.encoding, 0x70); |
4476 } | 2277 } |
4477 } | 2278 } |
OLD | NEW |