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.task.incremental_element_builder_test; | 5 library analyzer.test.src.task.incremental_element_builder_test; |
6 | 6 |
7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/visitor.dart'; |
9 import 'package:analyzer/src/dart/ast/utilities.dart'; | 10 import 'package:analyzer/src/dart/ast/utilities.dart'; |
10 import 'package:analyzer/src/dart/element/element.dart'; | 11 import 'package:analyzer/src/dart/element/element.dart'; |
11 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
12 import 'package:analyzer/src/generated/source.dart'; | 13 import 'package:analyzer/src/generated/source.dart'; |
13 import 'package:analyzer/src/task/dart.dart'; | 14 import 'package:analyzer/src/task/dart.dart'; |
14 import 'package:analyzer/src/task/incremental_element_builder.dart'; | 15 import 'package:analyzer/src/task/incremental_element_builder.dart'; |
15 import 'package:analyzer/task/dart.dart'; | 16 import 'package:analyzer/task/dart.dart'; |
16 import 'package:unittest/unittest.dart'; | 17 import 'package:unittest/unittest.dart'; |
17 | 18 |
18 import '../../reflective_tests.dart'; | 19 import '../../reflective_tests.dart'; |
(...skipping 1749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1768 /// [c) | 1769 /// [c) |
1769 class A {} | 1770 class A {} |
1770 '''); | 1771 '''); |
1771 _buildNewUnit(r''' | 1772 _buildNewUnit(r''' |
1772 int a; | 1773 int a; |
1773 /// [c) | 1774 /// [c) |
1774 class A {} | 1775 class A {} |
1775 '''); | 1776 '''); |
1776 } | 1777 } |
1777 | 1778 |
| 1779 test_update_element_implicitAccessors_classField() { |
| 1780 _buildOldUnit(r''' |
| 1781 // 0 |
| 1782 class A { |
| 1783 var F = 0; |
| 1784 } |
| 1785 '''); |
| 1786 _materializeLazyElements(unitElement); |
| 1787 _buildNewUnit(r''' |
| 1788 // 012 |
| 1789 class A { |
| 1790 var F = 0; |
| 1791 } |
| 1792 '''); |
| 1793 } |
| 1794 |
| 1795 test_update_element_implicitAccessors_topLevelVariable() { |
| 1796 _buildOldUnit(r''' |
| 1797 var A = 0; |
| 1798 var B = 1; |
| 1799 '''); |
| 1800 _materializeLazyElements(unitElement); |
| 1801 _buildNewUnit(r''' |
| 1802 var B = 1; |
| 1803 '''); |
| 1804 } |
| 1805 |
1778 test_update_rewrittenConstructorName() { | 1806 test_update_rewrittenConstructorName() { |
1779 _buildOldUnit(r''' | 1807 _buildOldUnit(r''' |
1780 class A { | 1808 class A { |
1781 A(); | 1809 A(); |
1782 A.named(); | 1810 A.named(); |
1783 } | 1811 } |
1784 | 1812 |
1785 foo() {} | 1813 foo() {} |
1786 | 1814 |
1787 main() { | 1815 main() { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1837 this.oldCode = oldCode; | 1865 this.oldCode = oldCode; |
1838 source = newSource('/test.dart', oldCode); | 1866 source = newSource('/test.dart', oldCode); |
1839 if (libSource == null) { | 1867 if (libSource == null) { |
1840 libSource = source; | 1868 libSource = source; |
1841 } | 1869 } |
1842 oldUnit = context.resolveCompilationUnit2(source, libSource); | 1870 oldUnit = context.resolveCompilationUnit2(source, libSource); |
1843 unitElement = oldUnit.element; | 1871 unitElement = oldUnit.element; |
1844 expect(unitElement, isNotNull); | 1872 expect(unitElement, isNotNull); |
1845 } | 1873 } |
1846 | 1874 |
| 1875 void _materializeLazyElements(CompilationUnitElement unitElement) { |
| 1876 unitElement.accept(new _MaterializeLazyElementsVisitor()); |
| 1877 } |
| 1878 |
1847 void _verifyNoClassDeltaForTheLast(String oldCode, String newCode) { | 1879 void _verifyNoClassDeltaForTheLast(String oldCode, String newCode) { |
1848 _buildOldUnit(oldCode); | 1880 _buildOldUnit(oldCode); |
1849 List<CompilationUnitMember> oldMembers = oldUnit.declarations.toList(); | 1881 List<CompilationUnitMember> oldMembers = oldUnit.declarations.toList(); |
1850 Element oldElementLast = oldMembers.last.element; | 1882 Element oldElementLast = oldMembers.last.element; |
1851 _buildNewUnit(newCode); | 1883 _buildNewUnit(newCode); |
1852 List<CompilationUnitMember> newMembers = newUnit.declarations; | 1884 List<CompilationUnitMember> newMembers = newUnit.declarations; |
1853 Element newElementLast = newMembers.last.element; | 1885 Element newElementLast = newMembers.last.element; |
1854 expect(newElementLast, isNot(same(oldElementLast))); | 1886 expect(newElementLast, isNot(same(oldElementLast))); |
1855 expect(unitDelta.classDeltas, isEmpty); | 1887 expect(unitDelta.classDeltas, isEmpty); |
1856 expect(unitDelta.removedDeclarations, unorderedEquals([oldElementLast])); | 1888 expect(unitDelta.removedDeclarations, unorderedEquals([oldElementLast])); |
1857 expect(unitDelta.addedDeclarations, unorderedEquals([newElementLast])); | 1889 expect(unitDelta.addedDeclarations, unorderedEquals([newElementLast])); |
1858 } | 1890 } |
1859 } | 1891 } |
1860 | 1892 |
1861 /** | 1893 /** |
1862 * Compares tokens and ASTs, and built elements of declared identifiers. | 1894 * Compares tokens and ASTs, and built elements of declared identifiers. |
1863 */ | 1895 */ |
1864 class _BuiltElementsValidator extends AstComparator { | 1896 class _BuiltElementsValidator extends AstComparator { |
| 1897 final Set visited = new Set.identity(); |
| 1898 |
1865 @override | 1899 @override |
1866 bool isEqualNodes(AstNode expected, AstNode actual) { | 1900 bool isEqualNodes(AstNode expected, AstNode actual) { |
1867 // Elements of nodes which are children of ClassDeclaration(s) must be | 1901 // Elements of nodes which are children of ClassDeclaration(s) must be |
1868 // linked to the corresponding ClassElement(s). | 1902 // linked to the corresponding ClassElement(s). |
1869 if (actual is TypeParameter) { | 1903 if (actual is TypeParameter) { |
1870 TypeParameterElement element = actual.element; | 1904 TypeParameterElement element = actual.element; |
1871 ClassDeclaration classNode = actual.parent.parent; | 1905 ClassDeclaration classNode = actual.parent.parent; |
1872 expect(element.enclosingElement, same(classNode.element)); | 1906 expect(element.enclosingElement, same(classNode.element)); |
1873 } else if (actual is FieldDeclaration) { | 1907 } else if (actual is FieldDeclaration) { |
1874 for (VariableDeclaration field in actual.fields.variables) { | 1908 for (VariableDeclaration field in actual.fields.variables) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1916 bool result = super.isEqualNodes(expected, actual); | 1950 bool result = super.isEqualNodes(expected, actual); |
1917 if (!result) { | 1951 if (!result) { |
1918 fail('|$actual| != expected |$expected|'); | 1952 fail('|$actual| != expected |$expected|'); |
1919 } | 1953 } |
1920 // Verify that declared identifiers have equal elements. | 1954 // Verify that declared identifiers have equal elements. |
1921 if (expected is SimpleIdentifier && actual is SimpleIdentifier) { | 1955 if (expected is SimpleIdentifier && actual is SimpleIdentifier) { |
1922 if (expected.inDeclarationContext()) { | 1956 if (expected.inDeclarationContext()) { |
1923 expect(actual.inDeclarationContext(), isTrue); | 1957 expect(actual.inDeclarationContext(), isTrue); |
1924 Element expectedElement = expected.staticElement; | 1958 Element expectedElement = expected.staticElement; |
1925 Element actualElement = actual.staticElement; | 1959 Element actualElement = actual.staticElement; |
1926 _verifyElement(expectedElement, actualElement, 'staticElement'); | 1960 _verifyElement( |
| 1961 expectedElement, actualElement, 'staticElement ($expectedElement)'); |
1927 } | 1962 } |
1928 } | 1963 } |
1929 return true; | 1964 return true; |
1930 } | 1965 } |
1931 | 1966 |
1932 void _verifyElement(Element expected, Element actual, String desc) { | 1967 void _verifyElement(Element expected, Element actual, String desc) { |
| 1968 if (!visited.add(expected)) { |
| 1969 return; |
| 1970 } |
1933 if (expected == null && actual == null) { | 1971 if (expected == null && actual == null) { |
1934 return; | 1972 return; |
1935 } | 1973 } |
1936 // Prefixes are built later. | 1974 // Prefixes are built later. |
1937 if (actual is PrefixElement) { | 1975 if (actual is PrefixElement) { |
1938 return; | 1976 return; |
1939 } | 1977 } |
1940 // Compare properties. | 1978 // Compare properties. |
1941 _verifyEqual('$desc name', expected.name, actual.name); | 1979 _verifyEqual('$desc name', expected.name, actual.name); |
1942 _verifyEqual('$desc nameOffset', expected.nameOffset, actual.nameOffset); | 1980 _verifyEqual('$desc nameOffset', expected.nameOffset, actual.nameOffset); |
1943 if (expected is ElementImpl && actual is ElementImpl) { | 1981 if (expected is ElementImpl && actual is ElementImpl) { |
1944 _verifyEqual('$desc codeOffset', expected.codeOffset, actual.codeOffset); | 1982 _verifyEqual('$desc codeOffset', expected.codeOffset, actual.codeOffset); |
1945 _verifyEqual('$desc codeLength', expected.codeLength, actual.codeLength); | 1983 _verifyEqual('$desc codeLength', expected.codeLength, actual.codeLength); |
1946 } | 1984 } |
1947 if (expected is LocalElement && actual is LocalElement) { | 1985 if (expected is LocalElement && actual is LocalElement) { |
1948 _verifyEqual( | 1986 _verifyEqual( |
1949 '$desc visibleRange', expected.visibleRange, actual.visibleRange); | 1987 '$desc visibleRange', expected.visibleRange, actual.visibleRange); |
1950 } | 1988 } |
1951 _verifyEqual('$desc documentationComment', expected.documentationComment, | 1989 _verifyEqual('$desc documentationComment', expected.documentationComment, |
1952 actual.documentationComment); | 1990 actual.documentationComment); |
1953 { | 1991 { |
1954 var expectedEnclosing = expected.enclosingElement; | 1992 var expectedEnclosing = expected.enclosingElement; |
1955 var actualEnclosing = actual.enclosingElement; | 1993 var actualEnclosing = actual.enclosingElement; |
1956 if (expectedEnclosing != null) { | 1994 if (expectedEnclosing != null) { |
1957 expect(actualEnclosing, isNotNull, reason: '$desc enclosingElement'); | 1995 expect(actualEnclosing, isNotNull, reason: '$desc enclosingElement'); |
1958 _verifyElement(expectedEnclosing, actualEnclosing, | 1996 _verifyElement(expectedEnclosing, actualEnclosing, |
1959 '${expectedEnclosing.name}.$desc'); | 1997 '${expectedEnclosing.name}.$desc'); |
1960 } | 1998 } |
1961 } | 1999 } |
| 2000 // Compare implicit accessors. |
| 2001 if (expected is PropertyInducingElement && |
| 2002 actual is PropertyInducingElement && |
| 2003 !expected.isSynthetic) { |
| 2004 _verifyElement(expected.getter, actual.getter, '$desc getter'); |
| 2005 _verifyElement(expected.setter, actual.setter, '$desc setter'); |
| 2006 } |
| 2007 // Compare parameters. |
| 2008 if (expected is ExecutableElement && actual is ExecutableElement) { |
| 2009 List<ParameterElement> actualParameters = actual.parameters; |
| 2010 List<ParameterElement> expectedParameters = expected.parameters; |
| 2011 expect(actualParameters, hasLength(expectedParameters.length)); |
| 2012 for (int i = 0; i < expectedParameters.length; i++) { |
| 2013 _verifyElement( |
| 2014 expectedParameters[i], actualParameters[i], '$desc parameters[$i]'); |
| 2015 } |
| 2016 } |
1962 } | 2017 } |
1963 | 2018 |
1964 void _verifyEqual(String name, expected, actual) { | 2019 void _verifyEqual(String name, expected, actual) { |
1965 if (actual != expected) { | 2020 if (actual != expected) { |
1966 fail('$name\nExpected: $expected\n Actual: $actual'); | 2021 fail('$name\nExpected: $expected\n Actual: $actual'); |
1967 } | 2022 } |
1968 } | 2023 } |
1969 } | 2024 } |
1970 | 2025 |
1971 class _ClassDeltaHelper { | 2026 class _ClassDeltaHelper { |
(...skipping 21 matching lines...) Expand all Loading... |
1993 expect(oldClass, isNotNull); | 2048 expect(oldClass, isNotNull); |
1994 element = oldClass.element; | 2049 element = oldClass.element; |
1995 oldVersion = element.version; | 2050 oldVersion = element.version; |
1996 oldMembers = oldClass.members.toList(); | 2051 oldMembers = oldClass.members.toList(); |
1997 } | 2052 } |
1998 | 2053 |
1999 ClassDeclaration _findClassNode(CompilationUnit unit, String name) => | 2054 ClassDeclaration _findClassNode(CompilationUnit unit, String name) => |
2000 unit.declarations.singleWhere((unitMember) => | 2055 unit.declarations.singleWhere((unitMember) => |
2001 unitMember is ClassDeclaration && unitMember.name.name == name); | 2056 unitMember is ClassDeclaration && unitMember.name.name == name); |
2002 } | 2057 } |
| 2058 |
| 2059 class _MaterializeLazyElementsVisitor extends GeneralizingElementVisitor { |
| 2060 @override |
| 2061 visitExecutableElement(ExecutableElement element) { |
| 2062 element.parameters; |
| 2063 super.visitExecutableElement(element); |
| 2064 } |
| 2065 } |
OLD | NEW |