| 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_test; | 5 library analyzer.test.src.summary.summary_test; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; | 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:analyzer/src/generated/java_engine_io.dart'; | 9 import 'package:analyzer/src/generated/java_engine_io.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 @override | 54 @override |
| 55 void serializeLibraryText(String text, {bool allowErrors: false}) { | 55 void serializeLibraryText(String text, {bool allowErrors: false}) { |
| 56 Source source = addSource(text); | 56 Source source = addSource(text); |
| 57 LibraryElement library = resolve2(source); | 57 LibraryElement library = resolve2(source); |
| 58 if (!allowErrors) { | 58 if (!allowErrors) { |
| 59 assertNoErrors(source); | 59 assertNoErrors(source); |
| 60 } | 60 } |
| 61 serializeLibraryElement(library); | 61 serializeLibraryElement(library); |
| 62 expect(definingUnit.unlinked.imports.length, lib.importDependencies.length); | 62 expect(definingUnit.unlinked.imports.length, lib.importDependencies.length); |
| 63 expect(unlinked.references.length, lib.references.length); | 63 for (PrelinkedUnit unit in lib.units) { |
| 64 expect(unit.unlinked.references.length, unit.references.length); |
| 65 } |
| 64 } | 66 } |
| 65 | 67 |
| 66 @override | 68 @override |
| 67 void setUp() { | 69 void setUp() { |
| 68 super.setUp(); | 70 super.setUp(); |
| 69 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 71 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
| 70 options.enableGenericMethods = true; | 72 options.enableGenericMethods = true; |
| 71 resetWithOptions(options); | 73 resetWithOptions(options); |
| 72 } | 74 } |
| 73 | 75 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 expect(typeRef.typeArguments, isEmpty); | 202 expect(typeRef.typeArguments, isEmpty); |
| 201 expect(typeRef.paramReference, deBruijnIndex); | 203 expect(typeRef.paramReference, deBruijnIndex); |
| 202 } | 204 } |
| 203 | 205 |
| 204 /** | 206 /** |
| 205 * Verify that the given [typeRef] represents a reference to a type declared | 207 * Verify that the given [typeRef] represents a reference to a type declared |
| 206 * in a file reachable via [absoluteUri] and [relativeUri], having name | 208 * in a file reachable via [absoluteUri] and [relativeUri], having name |
| 207 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is | 209 * [expectedName]. If [expectedPrefix] is supplied, verify that the type is |
| 208 * reached via the given prefix. If [allowTypeParameters] is true, allow the | 210 * reached via the given prefix. If [allowTypeParameters] is true, allow the |
| 209 * type reference to supply type parameters. [expectedKind] is the kind of | 211 * type reference to supply type parameters. [expectedKind] is the kind of |
| 210 * object referenced. | 212 * object referenced. [sourceUnit] is the compilation unit within which the |
| 213 * [typeRef] appears; if not specified it is assumed to be the defining |
| 214 * compilation unit. [expectedTargetUnit] is the index of the compilation |
| 215 * unit in which the target of the [typeRef] is expected to appear; if not |
| 216 * specified it is assumed to be the defining compilation unit. |
| 211 */ | 217 */ |
| 212 void checkTypeRef(UnlinkedTypeRef typeRef, String absoluteUri, | 218 void checkTypeRef(UnlinkedTypeRef typeRef, String absoluteUri, |
| 213 String relativeUri, String expectedName, | 219 String relativeUri, String expectedName, |
| 214 {String expectedPrefix, | 220 {String expectedPrefix, |
| 215 bool allowTypeParameters: false, | 221 bool allowTypeParameters: false, |
| 216 PrelinkedReferenceKind expectedKind: PrelinkedReferenceKind.classOrEnum, | 222 PrelinkedReferenceKind expectedKind: PrelinkedReferenceKind.classOrEnum, |
| 217 int expectedUnit: 0}) { | 223 int expectedTargetUnit: 0, |
| 224 PrelinkedUnit sourceUnit}) { |
| 225 sourceUnit ??= definingUnit; |
| 218 expect(typeRef, new isInstanceOf<UnlinkedTypeRef>()); | 226 expect(typeRef, new isInstanceOf<UnlinkedTypeRef>()); |
| 219 expect(typeRef.paramReference, 0); | 227 expect(typeRef.paramReference, 0); |
| 220 int index = typeRef.reference; | 228 int index = typeRef.reference; |
| 221 UnlinkedReference reference = unlinked.references[index]; | 229 UnlinkedReference reference = sourceUnit.unlinked.references[index]; |
| 222 PrelinkedReference referenceResolution = lib.references[index]; | 230 PrelinkedReference referenceResolution = sourceUnit.references[index]; |
| 223 if (absoluteUri == null) { | 231 if (absoluteUri == null) { |
| 224 expect(referenceResolution.dependency, 0); | 232 expect(referenceResolution.dependency, 0); |
| 225 } else { | 233 } else { |
| 226 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri); | 234 checkDependency(referenceResolution.dependency, absoluteUri, relativeUri); |
| 227 } | 235 } |
| 228 if (!allowTypeParameters) { | 236 if (!allowTypeParameters) { |
| 229 expect(typeRef.typeArguments, isEmpty); | 237 expect(typeRef.typeArguments, isEmpty); |
| 230 } | 238 } |
| 231 if (expectedName == null) { | 239 if (expectedName == null) { |
| 232 expect(reference.name, isEmpty); | 240 expect(reference.name, isEmpty); |
| 233 } else { | 241 } else { |
| 234 expect(reference.name, expectedName); | 242 expect(reference.name, expectedName); |
| 235 } | 243 } |
| 236 if (checkAstDerivedData) { | 244 if (checkAstDerivedData) { |
| 237 if (expectedPrefix == null) { | 245 if (expectedPrefix == null) { |
| 238 expect(reference.prefix, 0); | 246 expect(reference.prefix, 0); |
| 239 expect(unlinked.prefixes[reference.prefix].name, isEmpty); | 247 expect(unlinked.prefixes[reference.prefix].name, isEmpty); |
| 240 } else { | 248 } else { |
| 241 expect(reference.prefix, isNot(0)); | 249 expect(reference.prefix, isNot(0)); |
| 242 expect(unlinked.prefixes[reference.prefix].name, expectedPrefix); | 250 expect(unlinked.prefixes[reference.prefix].name, expectedPrefix); |
| 243 } | 251 } |
| 244 } | 252 } |
| 245 expect(referenceResolution.kind, expectedKind); | 253 expect(referenceResolution.kind, expectedKind); |
| 246 expect(referenceResolution.unit, expectedUnit); | 254 expect(referenceResolution.unit, expectedTargetUnit); |
| 247 } | 255 } |
| 248 | 256 |
| 249 /** | 257 /** |
| 250 * Verify that the given [typeRef] represents a reference to an unresolved | 258 * Verify that the given [typeRef] represents a reference to an unresolved |
| 251 * type. | 259 * type. |
| 252 */ | 260 */ |
| 253 void checkUnresolvedTypeRef( | 261 void checkUnresolvedTypeRef( |
| 254 UnlinkedTypeRef typeRef, String expectedPrefix, String expectedName) { | 262 UnlinkedTypeRef typeRef, String expectedPrefix, String expectedName) { |
| 255 // When serializing from the element model, unresolved type refs lose their | 263 // When serializing from the element model, unresolved type refs lose their |
| 256 // name. | 264 // name. |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 452 |
| 445 /** | 453 /** |
| 446 * Serialize a type declaration using the given [text] as a type name, and | 454 * Serialize a type declaration using the given [text] as a type name, and |
| 447 * return a summary of the corresponding [UnlinkedTypeRef]. If the type | 455 * return a summary of the corresponding [UnlinkedTypeRef]. If the type |
| 448 * declaration needs to refer to types that are not available in core, those | 456 * declaration needs to refer to types that are not available in core, those |
| 449 * types may be declared in [otherDeclarations]. | 457 * types may be declared in [otherDeclarations]. |
| 450 */ | 458 */ |
| 451 UnlinkedTypeRef serializeTypeText(String text, | 459 UnlinkedTypeRef serializeTypeText(String text, |
| 452 {String otherDeclarations: '', bool allowErrors: false}) { | 460 {String otherDeclarations: '', bool allowErrors: false}) { |
| 453 return serializeVariableText('$otherDeclarations\n$text v;', | 461 return serializeVariableText('$otherDeclarations\n$text v;', |
| 454 allowErrors: allowErrors) | 462 allowErrors: allowErrors).type; |
| 455 .type; | |
| 456 } | 463 } |
| 457 | 464 |
| 458 /** | 465 /** |
| 459 * Serialize the given library [text] and return the summary of the variable | 466 * Serialize the given library [text] and return the summary of the variable |
| 460 * with the given [variableName]. | 467 * with the given [variableName]. |
| 461 */ | 468 */ |
| 462 UnlinkedVariable serializeVariableText(String text, | 469 UnlinkedVariable serializeVariableText(String text, |
| 463 {String variableName: 'v', bool allowErrors: false}) { | 470 {String variableName: 'v', bool allowErrors: false}) { |
| 464 serializeLibraryText(text, allowErrors: allowErrors); | 471 serializeLibraryText(text, allowErrors: allowErrors); |
| 465 return findVariable(variableName, failIfAbsent: true); | 472 return findVariable(variableName, failIfAbsent: true); |
| (...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1695 allowTypeParameters: true); | 1702 allowTypeParameters: true); |
| 1696 expect(typeRef.typeArguments, hasLength(2)); | 1703 expect(typeRef.typeArguments, hasLength(2)); |
| 1697 checkTypeRef(typeRef.typeArguments[0], 'dart:core', 'dart:core', 'int'); | 1704 checkTypeRef(typeRef.typeArguments[0], 'dart:core', 'dart:core', 'int'); |
| 1698 checkTypeRef(typeRef.typeArguments[1], 'dart:core', 'dart:core', 'Object'); | 1705 checkTypeRef(typeRef.typeArguments[1], 'dart:core', 'dart:core', 'Object'); |
| 1699 } | 1706 } |
| 1700 | 1707 |
| 1701 test_type_dynamic() { | 1708 test_type_dynamic() { |
| 1702 checkDynamicTypeRef(serializeTypeText('dynamic')); | 1709 checkDynamicTypeRef(serializeTypeText('dynamic')); |
| 1703 } | 1710 } |
| 1704 | 1711 |
| 1712 test_type_reference_from_part() { |
| 1713 addNamedSource('/a.dart', 'part of foo; C v;'); |
| 1714 serializeLibraryText('library foo; part "a.dart"; class C {}'); |
| 1715 checkTypeRef( |
| 1716 findVariable('v', variables: lib.units[1].unlinked.variables).type, |
| 1717 null, |
| 1718 null, |
| 1719 'C', |
| 1720 expectedKind: PrelinkedReferenceKind.classOrEnum, |
| 1721 sourceUnit: lib.units[1]); |
| 1722 } |
| 1723 |
| 1705 test_type_reference_to_class_argument() { | 1724 test_type_reference_to_class_argument() { |
| 1706 UnlinkedClass cls = serializeClassText('class C<T, U> { T t; U u; }'); | 1725 UnlinkedClass cls = serializeClassText('class C<T, U> { T t; U u; }'); |
| 1707 { | 1726 { |
| 1708 UnlinkedTypeRef typeRef = | 1727 UnlinkedTypeRef typeRef = |
| 1709 findVariable('t', variables: cls.fields, failIfAbsent: true).type; | 1728 findVariable('t', variables: cls.fields, failIfAbsent: true).type; |
| 1710 checkParamTypeRef(typeRef, 2); | 1729 checkParamTypeRef(typeRef, 2); |
| 1711 } | 1730 } |
| 1712 { | 1731 { |
| 1713 UnlinkedTypeRef typeRef = | 1732 UnlinkedTypeRef typeRef = |
| 1714 findVariable('u', variables: cls.fields, failIfAbsent: true).type; | 1733 findVariable('u', variables: cls.fields, failIfAbsent: true).type; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1736 | 1755 |
| 1737 test_type_reference_to_imported_part() { | 1756 test_type_reference_to_imported_part() { |
| 1738 addNamedSource('/a.dart', 'library my.lib; part "b.dart";'); | 1757 addNamedSource('/a.dart', 'library my.lib; part "b.dart";'); |
| 1739 addNamedSource('/b.dart', 'part of my.lib; class C {}'); | 1758 addNamedSource('/b.dart', 'part of my.lib; class C {}'); |
| 1740 checkTypeRef( | 1759 checkTypeRef( |
| 1741 serializeTypeText('C', | 1760 serializeTypeText('C', |
| 1742 otherDeclarations: 'library my.lib; import "a.dart";'), | 1761 otherDeclarations: 'library my.lib; import "a.dart";'), |
| 1743 absUri('/a.dart'), | 1762 absUri('/a.dart'), |
| 1744 'a.dart', | 1763 'a.dart', |
| 1745 'C', | 1764 'C', |
| 1746 expectedUnit: 1); | 1765 expectedTargetUnit: 1); |
| 1747 } | 1766 } |
| 1748 | 1767 |
| 1749 test_type_reference_to_imported_part_with_prefix() { | 1768 test_type_reference_to_imported_part_with_prefix() { |
| 1750 addNamedSource('/a.dart', 'library my.lib; part "b.dart";'); | 1769 addNamedSource('/a.dart', 'library my.lib; part "b.dart";'); |
| 1751 addNamedSource('/b.dart', 'part of my.lib; class C {}'); | 1770 addNamedSource('/b.dart', 'part of my.lib; class C {}'); |
| 1752 checkTypeRef( | 1771 checkTypeRef( |
| 1753 serializeTypeText('p.C', | 1772 serializeTypeText('p.C', |
| 1754 otherDeclarations: 'library my.lib; import "a.dart" as p;'), | 1773 otherDeclarations: 'library my.lib; import "a.dart" as p;'), |
| 1755 absUri('/a.dart'), | 1774 absUri('/a.dart'), |
| 1756 'a.dart', | 1775 'a.dart', |
| 1757 'C', | 1776 'C', |
| 1758 expectedPrefix: 'p', | 1777 expectedPrefix: 'p', |
| 1759 expectedUnit: 1); | 1778 expectedTargetUnit: 1); |
| 1760 } | 1779 } |
| 1761 | 1780 |
| 1762 test_type_reference_to_internal_class() { | 1781 test_type_reference_to_internal_class() { |
| 1763 checkTypeRef(serializeTypeText('C', otherDeclarations: 'class C {}'), null, | 1782 checkTypeRef(serializeTypeText('C', otherDeclarations: 'class C {}'), null, |
| 1764 null, 'C'); | 1783 null, 'C'); |
| 1765 } | 1784 } |
| 1766 | 1785 |
| 1767 test_type_reference_to_internal_class_alias() { | 1786 test_type_reference_to_internal_class_alias() { |
| 1768 checkTypeRef( | 1787 checkTypeRef( |
| 1769 serializeTypeText('C', | 1788 serializeTypeText('C', |
| 1770 otherDeclarations: 'class C = D with E; class D {} class E {}'), | 1789 otherDeclarations: 'class C = D with E; class D {} class E {}'), |
| 1771 null, | 1790 null, |
| 1772 null, | 1791 null, |
| 1773 'C'); | 1792 'C'); |
| 1774 } | 1793 } |
| 1775 | 1794 |
| 1776 test_type_reference_to_internal_enum() { | 1795 test_type_reference_to_internal_enum() { |
| 1777 checkTypeRef(serializeTypeText('E', otherDeclarations: 'enum E { value }'), | 1796 checkTypeRef(serializeTypeText('E', otherDeclarations: 'enum E { value }'), |
| 1778 null, null, 'E'); | 1797 null, null, 'E'); |
| 1779 } | 1798 } |
| 1780 | 1799 |
| 1781 test_type_reference_to_local_part() { | 1800 test_type_reference_to_local_part() { |
| 1782 addNamedSource('/a.dart', 'part of my.lib; class C {}'); | 1801 addNamedSource('/a.dart', 'part of my.lib; class C {}'); |
| 1783 checkTypeRef( | 1802 checkTypeRef( |
| 1784 serializeTypeText('C', | 1803 serializeTypeText('C', |
| 1785 otherDeclarations: 'library my.lib; part "a.dart";'), | 1804 otherDeclarations: 'library my.lib; part "a.dart";'), |
| 1786 null, | 1805 null, |
| 1787 null, | 1806 null, |
| 1788 'C', | 1807 'C', |
| 1789 expectedUnit: 1); | 1808 expectedTargetUnit: 1); |
| 1790 } | 1809 } |
| 1791 | 1810 |
| 1792 test_type_reference_to_nonexistent_file_via_prefix() { | 1811 test_type_reference_to_nonexistent_file_via_prefix() { |
| 1793 UnlinkedTypeRef typeRef = serializeTypeText('p.C', | 1812 UnlinkedTypeRef typeRef = serializeTypeText('p.C', |
| 1794 otherDeclarations: 'import "foo.dart" as p;', allowErrors: true); | 1813 otherDeclarations: 'import "foo.dart" as p;', allowErrors: true); |
| 1795 checkUnresolvedTypeRef(typeRef, 'p', 'C'); | 1814 checkUnresolvedTypeRef(typeRef, 'p', 'C'); |
| 1796 } | 1815 } |
| 1797 | 1816 |
| 1817 test_type_reference_to_part() { |
| 1818 addNamedSource('/a.dart', 'part of foo; class C {}'); |
| 1819 checkTypeRef( |
| 1820 serializeTypeText('C', |
| 1821 otherDeclarations: 'library foo; part "a.dart";'), |
| 1822 null, |
| 1823 null, |
| 1824 'C', |
| 1825 expectedKind: PrelinkedReferenceKind.classOrEnum, |
| 1826 expectedTargetUnit: 1); |
| 1827 } |
| 1828 |
| 1798 test_type_reference_to_typedef() { | 1829 test_type_reference_to_typedef() { |
| 1799 checkTypeRef(serializeTypeText('F', otherDeclarations: 'typedef void F();'), | 1830 checkTypeRef(serializeTypeText('F', otherDeclarations: 'typedef void F();'), |
| 1800 null, null, 'F', | 1831 null, null, 'F', |
| 1801 expectedKind: PrelinkedReferenceKind.typedef); | 1832 expectedKind: PrelinkedReferenceKind.typedef); |
| 1802 } | 1833 } |
| 1803 | 1834 |
| 1804 test_type_unit_counts_unreferenced_units() { | 1835 test_type_unit_counts_unreferenced_units() { |
| 1805 addNamedSource('/a.dart', 'library a; part "b.dart"; part "c.dart";'); | 1836 addNamedSource('/a.dart', 'library a; part "b.dart"; part "c.dart";'); |
| 1806 addNamedSource('/b.dart', 'part of a;'); | 1837 addNamedSource('/b.dart', 'part of a;'); |
| 1807 addNamedSource('/c.dart', 'part of a; class C {}'); | 1838 addNamedSource('/c.dart', 'part of a; class C {}'); |
| 1808 UnlinkedTypeRef typeRef = | 1839 UnlinkedTypeRef typeRef = |
| 1809 serializeTypeText('C', otherDeclarations: 'import "a.dart";'); | 1840 serializeTypeText('C', otherDeclarations: 'import "a.dart";'); |
| 1810 // The referenced unit should be 2, since unit 0 is a.dart and unit 1 is | 1841 // The referenced unit should be 2, since unit 0 is a.dart and unit 1 is |
| 1811 // b.dart. a.dart and b.dart are counted even though nothing is imported | 1842 // b.dart. a.dart and b.dart are counted even though nothing is imported |
| 1812 // from them. | 1843 // from them. |
| 1813 checkTypeRef(typeRef, absUri('/a.dart'), 'a.dart', 'C', expectedUnit: 2); | 1844 checkTypeRef(typeRef, absUri('/a.dart'), 'a.dart', 'C', |
| 1845 expectedTargetUnit: 2); |
| 1814 } | 1846 } |
| 1815 | 1847 |
| 1816 test_type_unresolved() { | 1848 test_type_unresolved() { |
| 1817 UnlinkedTypeRef typeRef = serializeTypeText('Foo', allowErrors: true); | 1849 UnlinkedTypeRef typeRef = serializeTypeText('Foo', allowErrors: true); |
| 1818 checkUnresolvedTypeRef(typeRef, null, 'Foo'); | 1850 checkUnresolvedTypeRef(typeRef, null, 'Foo'); |
| 1819 } | 1851 } |
| 1820 | 1852 |
| 1821 test_typedef_name() { | 1853 test_typedef_name() { |
| 1822 UnlinkedTypedef type = serializeTypedefText('typedef F();'); | 1854 UnlinkedTypedef type = serializeTypedefText('typedef F();'); |
| 1823 expect(type.name, 'F'); | 1855 expect(type.name, 'F'); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1936 serializeClassText('class C { static int i; }').fields[0]; | 1968 serializeClassText('class C { static int i; }').fields[0]; |
| 1937 expect(variable.isStatic, isTrue); | 1969 expect(variable.isStatic, isTrue); |
| 1938 } | 1970 } |
| 1939 | 1971 |
| 1940 test_variable_type() { | 1972 test_variable_type() { |
| 1941 UnlinkedVariable variable = | 1973 UnlinkedVariable variable = |
| 1942 serializeVariableText('int i;', variableName: 'i'); | 1974 serializeVariableText('int i;', variableName: 'i'); |
| 1943 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); | 1975 checkTypeRef(variable.type, 'dart:core', 'dart:core', 'int'); |
| 1944 } | 1976 } |
| 1945 } | 1977 } |
| OLD | NEW |