| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.src.summary.summary_common; | 5 library analyzer.test.src.summary.summary_common; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/dart/ast/ast.dart'; | 8 import 'package:analyzer/dart/ast/ast.dart'; |
| 9 import 'package:analyzer/dart/element/element.dart'; | 9 import 'package:analyzer/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 return FileUtilities2.createFile(path).toURI().toString(); | 179 return FileUtilities2.createFile(path).toURI().toString(); |
| 180 } | 180 } |
| 181 | 181 |
| 182 /** | 182 /** |
| 183 * Add the given source file so that it may be referenced by the file under | 183 * Add the given source file so that it may be referenced by the file under |
| 184 * test. | 184 * test. |
| 185 */ | 185 */ |
| 186 Source addNamedSource(String filePath, String contents); | 186 Source addNamedSource(String filePath, String contents); |
| 187 | 187 |
| 188 /** | 188 /** |
| 189 * Check that [annotations] contains a single entry which is a reference to |
| 190 * a top level variable called `a` in the current library. |
| 191 */ |
| 192 void checkAnnotationA(List<UnlinkedConst> annotations) { |
| 193 expect(annotations, hasLength(1)); |
| 194 _assertUnlinkedConst(annotations[0], operators: [ |
| 195 UnlinkedConstOperation.pushReference |
| 196 ], referenceValidators: [ |
| 197 (EntityRef r) => checkTypeRef(r, null, null, 'a', |
| 198 expectedKind: ReferenceKind.topLevelPropertyAccessor) |
| 199 ]); |
| 200 } |
| 201 |
| 202 /** |
| 189 * Verify that the [dependency]th element of the dependency table represents | 203 * Verify that the [dependency]th element of the dependency table represents |
| 190 * a file reachable via the given [absoluteUri] and [relativeUri]. | 204 * a file reachable via the given [absoluteUri] and [relativeUri]. |
| 191 */ | 205 */ |
| 192 void checkDependency(int dependency, String absoluteUri, String relativeUri) { | 206 void checkDependency(int dependency, String absoluteUri, String relativeUri) { |
| 193 if (expectAbsoluteUrisInDependencies) { | 207 if (expectAbsoluteUrisInDependencies) { |
| 194 // The element model doesn't (yet) store enough information to recover | 208 // The element model doesn't (yet) store enough information to recover |
| 195 // relative URIs, so we have to use the absolute URI. | 209 // relative URIs, so we have to use the absolute URI. |
| 196 // TODO(paulberry): fix this. | 210 // TODO(paulberry): fix this. |
| 197 relativeUri = absoluteUri; | 211 relativeUri = absoluteUri; |
| 198 } | 212 } |
| (...skipping 4364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4563 addNamedSource('/a.dart', 'class C {} class D {}'); | 4577 addNamedSource('/a.dart', 'class C {} class D {}'); |
| 4564 serializeLibraryText(''' | 4578 serializeLibraryText(''' |
| 4565 import 'a.dart'; | 4579 import 'a.dart'; |
| 4566 class C {} | 4580 class C {} |
| 4567 C c; | 4581 C c; |
| 4568 D d;'''); | 4582 D d;'''); |
| 4569 checkTypeRef(findVariable('c').type, null, null, 'C'); | 4583 checkTypeRef(findVariable('c').type, null, null, 'C'); |
| 4570 checkTypeRef(findVariable('d').type, absUri('/a.dart'), 'a.dart', 'D'); | 4584 checkTypeRef(findVariable('d').type, absUri('/a.dart'), 'a.dart', 'D'); |
| 4571 } | 4585 } |
| 4572 | 4586 |
| 4587 test_metadata_classDeclaration() { |
| 4588 checkAnnotationA( |
| 4589 serializeClassText('const a = null; @a class C {}').annotations); |
| 4590 } |
| 4591 |
| 4592 test_metadata_classTypeAlias() { |
| 4593 checkAnnotationA(serializeClassText( |
| 4594 'const a = null; @a class C = D with E; class D {} class E {}', |
| 4595 className: 'C') |
| 4596 .annotations); |
| 4597 } |
| 4598 |
| 4599 test_metadata_constructor_call_named() { |
| 4600 UnlinkedClass cls = serializeClassText( |
| 4601 'class A { const A.named(); } @A.named() class C {}'); |
| 4602 expect(cls.annotations, hasLength(1)); |
| 4603 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4604 UnlinkedConstOperation.invokeConstructor, |
| 4605 ], ints: [ |
| 4606 0, |
| 4607 0 |
| 4608 ], referenceValidators: [ |
| 4609 (EntityRef r) => checkTypeRef(r, null, null, 'named', |
| 4610 expectedKind: ReferenceKind.constructor, |
| 4611 prefixExpectations: [ |
| 4612 new _PrefixExpectation(ReferenceKind.classOrEnum, 'A') |
| 4613 ]) |
| 4614 ]); |
| 4615 } |
| 4616 |
| 4617 test_metadata_constructor_call_named_prefixed() { |
| 4618 addNamedSource('/foo.dart', 'class A { const A.named(); }'); |
| 4619 UnlinkedClass cls = serializeClassText( |
| 4620 'import "foo.dart" as foo; @foo.A.named() class C {}'); |
| 4621 expect(cls.annotations, hasLength(1)); |
| 4622 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4623 UnlinkedConstOperation.invokeConstructor, |
| 4624 ], ints: [ |
| 4625 0, |
| 4626 0 |
| 4627 ], referenceValidators: [ |
| 4628 (EntityRef r) => checkTypeRef(r, null, null, 'named', |
| 4629 expectedKind: ReferenceKind.constructor, |
| 4630 prefixExpectations: [ |
| 4631 new _PrefixExpectation(ReferenceKind.classOrEnum, 'A', |
| 4632 absoluteUri: absUri('/foo.dart'), relativeUri: 'foo.dart'), |
| 4633 new _PrefixExpectation(ReferenceKind.prefix, 'foo') |
| 4634 ]) |
| 4635 ]); |
| 4636 } |
| 4637 |
| 4638 test_metadata_constructor_call_unnamed() { |
| 4639 UnlinkedClass cls = |
| 4640 serializeClassText('class A { const A(); } @A() class C {}'); |
| 4641 expect(cls.annotations, hasLength(1)); |
| 4642 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4643 UnlinkedConstOperation.invokeConstructor, |
| 4644 ], ints: [ |
| 4645 0, |
| 4646 0 |
| 4647 ], referenceValidators: [ |
| 4648 (EntityRef r) => checkTypeRef(r, null, null, 'A', |
| 4649 expectedKind: ReferenceKind.classOrEnum) |
| 4650 ]); |
| 4651 } |
| 4652 |
| 4653 test_metadata_constructor_call_unnamed_prefixed() { |
| 4654 addNamedSource('/foo.dart', 'class A { const A(); }'); |
| 4655 UnlinkedClass cls = |
| 4656 serializeClassText('import "foo.dart" as foo; @foo.A() class C {}'); |
| 4657 expect(cls.annotations, hasLength(1)); |
| 4658 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4659 UnlinkedConstOperation.invokeConstructor, |
| 4660 ], ints: [ |
| 4661 0, |
| 4662 0 |
| 4663 ], referenceValidators: [ |
| 4664 (EntityRef r) => checkTypeRef(r, absUri('/foo.dart'), 'foo.dart', 'A', |
| 4665 expectedKind: ReferenceKind.classOrEnum, expectedPrefix: 'foo') |
| 4666 ]); |
| 4667 } |
| 4668 |
| 4669 test_metadata_constructor_call_with_args() { |
| 4670 UnlinkedClass cls = |
| 4671 serializeClassText('class A { const A(x); } @A(null) class C {}'); |
| 4672 expect(cls.annotations, hasLength(1)); |
| 4673 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4674 UnlinkedConstOperation.pushNull, |
| 4675 UnlinkedConstOperation.invokeConstructor, |
| 4676 ], ints: [ |
| 4677 0, |
| 4678 1 |
| 4679 ], referenceValidators: [ |
| 4680 (EntityRef r) => checkTypeRef(r, null, null, 'A', |
| 4681 expectedKind: ReferenceKind.classOrEnum) |
| 4682 ]); |
| 4683 } |
| 4684 |
| 4685 test_metadata_constructorDeclaration_named() { |
| 4686 checkAnnotationA( |
| 4687 serializeClassText('const a = null; class C { @a C.named(); }') |
| 4688 .executables[0] |
| 4689 .annotations); |
| 4690 } |
| 4691 |
| 4692 test_metadata_constructorDeclaration_unnamed() { |
| 4693 checkAnnotationA(serializeClassText('const a = null; class C { @a C(); }') |
| 4694 .executables[0] |
| 4695 .annotations); |
| 4696 } |
| 4697 |
| 4698 test_metadata_enumDeclaration() { |
| 4699 checkAnnotationA( |
| 4700 serializeEnumText('const a = null; @a enum E { v }').annotations); |
| 4701 } |
| 4702 |
| 4703 test_metadata_exportDirective() { |
| 4704 addNamedSource('/foo.dart', ''); |
| 4705 serializeLibraryText('@a export "foo.dart"; const a = null;'); |
| 4706 checkAnnotationA(unlinkedUnits[0].exports[0].annotations); |
| 4707 } |
| 4708 |
| 4709 test_metadata_fieldDeclaration() { |
| 4710 checkAnnotationA(serializeClassText('const a = null; class C { @a int x; }') |
| 4711 .fields[0] |
| 4712 .annotations); |
| 4713 } |
| 4714 |
| 4715 test_metadata_fieldFormalParameter() { |
| 4716 checkAnnotationA( |
| 4717 serializeClassText('const a = null; class C { var x; C(@a this.x); }') |
| 4718 .executables[0] |
| 4719 .parameters[0] |
| 4720 .annotations); |
| 4721 } |
| 4722 |
| 4723 test_metadata_fieldFormalParameter_withDefault() { |
| 4724 checkAnnotationA(serializeClassText( |
| 4725 'const a = null; class C { var x; C([@a this.x = null]); }') |
| 4726 .executables[0] |
| 4727 .parameters[0] |
| 4728 .annotations); |
| 4729 } |
| 4730 |
| 4731 test_metadata_functionDeclaration_function() { |
| 4732 checkAnnotationA( |
| 4733 serializeExecutableText('const a = null; @a f() {}').annotations); |
| 4734 } |
| 4735 |
| 4736 test_metadata_functionDeclaration_getter() { |
| 4737 checkAnnotationA( |
| 4738 serializeExecutableText('const a = null; @a get f => null;') |
| 4739 .annotations); |
| 4740 } |
| 4741 |
| 4742 test_metadata_functionDeclaration_setter() { |
| 4743 checkAnnotationA( |
| 4744 serializeExecutableText('const a = null; @a set f(value) {}', 'f=') |
| 4745 .annotations); |
| 4746 } |
| 4747 |
| 4748 test_metadata_functionTypeAlias() { |
| 4749 checkAnnotationA( |
| 4750 serializeTypedefText('const a = null; @a typedef F();').annotations); |
| 4751 } |
| 4752 |
| 4753 test_metadata_functionTypedFormalParameter() { |
| 4754 checkAnnotationA(serializeExecutableText('const a = null; f(@a g()) {}') |
| 4755 .parameters[0] |
| 4756 .annotations); |
| 4757 } |
| 4758 |
| 4759 test_metadata_functionTypedFormalParameter_withDefault() { |
| 4760 checkAnnotationA( |
| 4761 serializeExecutableText('const a = null; f([@a g() = null]) {}') |
| 4762 .parameters[0] |
| 4763 .annotations); |
| 4764 } |
| 4765 |
| 4766 test_metadata_importDirective() { |
| 4767 addNamedSource('/foo.dart', 'const b = null;'); |
| 4768 serializeLibraryText('@a import "foo.dart"; const a = b;'); |
| 4769 checkAnnotationA(unlinkedUnits[0].imports[0].annotations); |
| 4770 } |
| 4771 |
| 4772 test_metadata_libraryDirective() { |
| 4773 serializeLibraryText('@a library L; const a = null;'); |
| 4774 checkAnnotationA(unlinkedUnits[0].libraryAnnotations); |
| 4775 } |
| 4776 |
| 4777 test_metadata_methodDeclaration_getter() { |
| 4778 checkAnnotationA( |
| 4779 serializeClassText('const a = null; class C { @a get m => null; }') |
| 4780 .executables[0] |
| 4781 .annotations); |
| 4782 } |
| 4783 |
| 4784 test_metadata_methodDeclaration_method() { |
| 4785 checkAnnotationA(serializeClassText('const a = null; class C { @a m() {} }') |
| 4786 .executables[0] |
| 4787 .annotations); |
| 4788 } |
| 4789 |
| 4790 test_metadata_methodDeclaration_setter() { |
| 4791 checkAnnotationA( |
| 4792 serializeClassText('const a = null; class C { @a set m(value) {} }') |
| 4793 .executables[0] |
| 4794 .annotations); |
| 4795 } |
| 4796 |
| 4797 test_metadata_multiple_annotations() { |
| 4798 UnlinkedClass cls = |
| 4799 serializeClassText('const a = null, b = null; @a @b class C {}'); |
| 4800 List<UnlinkedConst> annotations = cls.annotations; |
| 4801 expect(annotations, hasLength(2)); |
| 4802 _assertUnlinkedConst(annotations[0], operators: [ |
| 4803 UnlinkedConstOperation.pushReference |
| 4804 ], referenceValidators: [ |
| 4805 (EntityRef r) => checkTypeRef(r, null, null, 'a', |
| 4806 expectedKind: ReferenceKind.topLevelPropertyAccessor) |
| 4807 ]); |
| 4808 _assertUnlinkedConst(annotations[1], operators: [ |
| 4809 UnlinkedConstOperation.pushReference |
| 4810 ], referenceValidators: [ |
| 4811 (EntityRef r) => checkTypeRef(r, null, null, 'b', |
| 4812 expectedKind: ReferenceKind.topLevelPropertyAccessor) |
| 4813 ]); |
| 4814 } |
| 4815 |
| 4816 test_metadata_partDirective() { |
| 4817 addNamedSource('/foo.dart', 'part of L;'); |
| 4818 serializeLibraryText('library L; @a part "foo.dart"; const a = null;'); |
| 4819 checkAnnotationA(unlinkedUnits[0].parts[0].annotations); |
| 4820 } |
| 4821 |
| 4822 test_metadata_prefixed_variable() { |
| 4823 addNamedSource('/a.dart', 'const b = null;'); |
| 4824 UnlinkedClass cls = |
| 4825 serializeClassText('import "a.dart" as a; @a.b class C {}'); |
| 4826 expect(cls.annotations, hasLength(1)); |
| 4827 _assertUnlinkedConst(cls.annotations[0], operators: [ |
| 4828 UnlinkedConstOperation.pushReference |
| 4829 ], referenceValidators: [ |
| 4830 (EntityRef r) => checkTypeRef(r, absUri('/a.dart'), 'a.dart', 'b', |
| 4831 expectedKind: ReferenceKind.topLevelPropertyAccessor, |
| 4832 expectedPrefix: 'a') |
| 4833 ]); |
| 4834 } |
| 4835 |
| 4836 test_metadata_simpleFormalParameter() { |
| 4837 checkAnnotationA(serializeExecutableText('const a = null; f(@a x) {}') |
| 4838 .parameters[0] |
| 4839 .annotations); |
| 4840 } |
| 4841 |
| 4842 test_metadata_simpleFormalParameter_withDefault() { |
| 4843 checkAnnotationA( |
| 4844 serializeExecutableText('const a = null; f([@a x = null]) {}') |
| 4845 .parameters[0] |
| 4846 .annotations); |
| 4847 } |
| 4848 |
| 4849 test_metadata_topLevelVariableDeclaration() { |
| 4850 checkAnnotationA( |
| 4851 serializeVariableText('const a = null; @a int v;').annotations); |
| 4852 } |
| 4853 |
| 4854 test_metadata_typeParameter_ofClass() { |
| 4855 checkAnnotationA(serializeClassText('const a = null; class C<@a T> {}') |
| 4856 .typeParameters[0] |
| 4857 .annotations); |
| 4858 } |
| 4859 |
| 4860 test_metadata_typeParameter_ofClassTypeAlias() { |
| 4861 checkAnnotationA(serializeClassText( |
| 4862 'const a = null; class C<@a T> = D with E; class D {} class E {}', |
| 4863 className: 'C') |
| 4864 .typeParameters[0] |
| 4865 .annotations); |
| 4866 } |
| 4867 |
| 4868 test_metadata_typeParameter_ofFunction() { |
| 4869 checkAnnotationA( |
| 4870 serializeExecutableText('const a = null; f<@a T>() {}') |
| 4871 .typeParameters[0] |
| 4872 .annotations); |
| 4873 } |
| 4874 |
| 4875 test_metadata_typeParameter_ofTypedef() { |
| 4876 checkAnnotationA(serializeTypedefText('const a = null; typedef F<@a T>();') |
| 4877 .typeParameters[0] |
| 4878 .annotations); |
| 4879 } |
| 4880 |
| 4573 test_method_documented() { | 4881 test_method_documented() { |
| 4574 String text = ''' | 4882 String text = ''' |
| 4575 class C { | 4883 class C { |
| 4576 /** | 4884 /** |
| 4577 * Docs | 4885 * Docs |
| 4578 */ | 4886 */ |
| 4579 f() {} | 4887 f() {} |
| 4580 }'''; | 4888 }'''; |
| 4581 UnlinkedExecutable executable = serializeClassText(text).executables[0]; | 4889 UnlinkedExecutable executable = serializeClassText(text).executables[0]; |
| 4582 expect(executable.documentationComment, isNotNull); | 4890 expect(executable.documentationComment, isNotNull); |
| (...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5453 final String absoluteUri; | 5761 final String absoluteUri; |
| 5454 final String relativeUri; | 5762 final String relativeUri; |
| 5455 final int numTypeParameters; | 5763 final int numTypeParameters; |
| 5456 | 5764 |
| 5457 _PrefixExpectation(this.kind, this.name, | 5765 _PrefixExpectation(this.kind, this.name, |
| 5458 {this.inLibraryDefiningUnit: false, | 5766 {this.inLibraryDefiningUnit: false, |
| 5459 this.absoluteUri, | 5767 this.absoluteUri, |
| 5460 this.relativeUri, | 5768 this.relativeUri, |
| 5461 this.numTypeParameters: 0}); | 5769 this.numTypeParameters: 0}); |
| 5462 } | 5770 } |
| OLD | NEW |