| 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 engine.incremental_resolver_test; | 5 library engine.incremental_resolver_test; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 10 import 'package:analyzer/src/generated/error.dart'; |
| (...skipping 1725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1736 { | 1736 { |
| 1737 int delta = edit.replacement.length - edit.length; | 1737 int delta = edit.replacement.length - edit.length; |
| 1738 _shiftTokens(unit.beginToken, offset, delta); | 1738 _shiftTokens(unit.beginToken, offset, delta); |
| 1739 } | 1739 } |
| 1740 // replace the node | 1740 // replace the node |
| 1741 AstNode oldNode = _findNodeAt(unit, offset, predicate); | 1741 AstNode oldNode = _findNodeAt(unit, offset, predicate); |
| 1742 AstNode newNode = _findNodeAt(newUnit, offset, predicate); | 1742 AstNode newNode = _findNodeAt(newUnit, offset, predicate); |
| 1743 bool success = NodeReplacer.replace(oldNode, newNode); | 1743 bool success = NodeReplacer.replace(oldNode, newNode); |
| 1744 expect(success, isTrue); | 1744 expect(success, isTrue); |
| 1745 // do incremental resolution | 1745 // do incremental resolution |
| 1746 GatheringErrorListener errorListener = new GatheringErrorListener(); | |
| 1747 IncrementalResolver resolver = new IncrementalResolver( | 1746 IncrementalResolver resolver = new IncrementalResolver( |
| 1748 errorListener, | |
| 1749 typeProvider, | 1747 typeProvider, |
| 1750 library, | 1748 library, |
| 1751 unit.element, | 1749 unit.element, |
| 1752 source, | 1750 source, |
| 1753 edit.offset, | 1751 edit.offset, |
| 1754 edit.length, | 1752 edit.length, |
| 1755 edit.replacement.length); | 1753 edit.replacement.length); |
| 1756 resolver.resolve(newNode); | 1754 resolver.resolve(newNode); |
| 1757 // resolve "newCode" from scratch | 1755 // resolve "newCode" from scratch |
| 1758 CompilationUnit fullNewUnit; | 1756 CompilationUnit fullNewUnit; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1854 print(0); | 1852 print(0); |
| 1855 print(1); | 1853 print(1); |
| 1856 } | 1854 } |
| 1857 '''); | 1855 '''); |
| 1858 } | 1856 } |
| 1859 | 1857 |
| 1860 void test_inBody_tokenToNode() { | 1858 void test_inBody_tokenToNode() { |
| 1861 _resolveUnit(r''' | 1859 _resolveUnit(r''' |
| 1862 main() { | 1860 main() { |
| 1863 var v = 42; | 1861 var v = 42; |
| 1862 print(v); |
| 1864 } | 1863 } |
| 1865 '''); | 1864 '''); |
| 1866 _updateAndValidate(r''' | 1865 _updateAndValidate(r''' |
| 1867 main() { | 1866 main() { |
| 1868 int v = 42; | 1867 int v = 42; |
| 1868 print(v); |
| 1869 } | 1869 } |
| 1870 '''); | 1870 '''); |
| 1871 } | 1871 } |
| 1872 | 1872 |
| 1873 void test_twice() { | 1873 void test_twice() { |
| 1874 _resolveUnit(r''' | 1874 _resolveUnit(r''' |
| 1875 main() { | 1875 main() { |
| 1876 print(1); | 1876 print(1); |
| 1877 }'''); | 1877 }'''); |
| 1878 _updateAndValidate(r''' | 1878 _updateAndValidate(r''' |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1902 }''', false); | 1902 }''', false); |
| 1903 _updateAndValidate(r''' | 1903 _updateAndValidate(r''' |
| 1904 class A { | 1904 class A { |
| 1905 m() { | 1905 m() { |
| 1906 int vvvvvvvv = 42; | 1906 int vvvvvvvv = 42; |
| 1907 print(vvvvvvvv); | 1907 print(vvvvvvvv); |
| 1908 } | 1908 } |
| 1909 }''', false); | 1909 }''', false); |
| 1910 } | 1910 } |
| 1911 | 1911 |
| 1912 void test_withImport() { | 1912 void test_updateErrors_addNew_hints() { |
| 1913 _resolveUnit(r''' | 1913 _resolveUnit(r''' |
| 1914 import 'dart:async'; | |
| 1915 import 'dart:math'; | |
| 1916 main() { | 1914 main() { |
| 1917 print(1); | 1915 int v = 0; |
| 1916 print(v); |
| 1917 print(42); |
| 1918 } |
| 1919 '''); |
| 1920 // TODO(scheglov) "print(42)" is here because it seems we have bug with |
| 1921 // incremental analysis and the end of a function body. |
| 1922 _updateAndValidate(r''' |
| 1923 main() { |
| 1924 int v = 0; |
| 1925 print(42); |
| 1926 } |
| 1927 '''); |
| 1928 } |
| 1929 |
| 1930 void test_updateErrors_addNew_parse() { |
| 1931 _resolveUnit(r''' |
| 1932 main() { |
| 1933 print(42); |
| 1918 } | 1934 } |
| 1919 '''); | 1935 '''); |
| 1920 _updateAndValidate(r''' | 1936 _updateAndValidate(r''' |
| 1921 import 'dart:async'; | |
| 1922 import 'dart:math'; | |
| 1923 main() { | 1937 main() { |
| 1924 print(2 + 3); | 1938 print(42) |
| 1925 } | 1939 } |
| 1926 '''); | 1940 '''); |
| 1927 } | 1941 } |
| 1942 |
| 1943 void test_updateErrors_addNew_resolve() { |
| 1944 _resolveUnit(r''' |
| 1945 main() { |
| 1946 foo(); |
| 1947 } |
| 1948 foo() {} |
| 1949 '''); |
| 1950 _updateAndValidate(r''' |
| 1951 main() { |
| 1952 bar(); |
| 1953 } |
| 1954 foo() {} |
| 1955 '''); |
| 1956 } |
| 1957 |
| 1958 void test_updateErrors_addNew_scan() { |
| 1959 _resolveUnit(r''' |
| 1960 main() { |
| 1961 1; |
| 1962 } |
| 1963 '''); |
| 1964 _updateAndValidate(r''' |
| 1965 main() { |
| 1966 1e; |
| 1967 } |
| 1968 '''); |
| 1969 } |
| 1970 |
| 1971 void test_updateErrors_addNew_verify() { |
| 1972 _resolveUnit(r''' |
| 1973 main() { |
| 1974 foo(0); |
| 1975 } |
| 1976 foo(int p) {} |
| 1977 '''); |
| 1978 _updateAndValidate(r''' |
| 1979 main() { |
| 1980 foo('abc'); |
| 1981 } |
| 1982 foo(int p) {} |
| 1983 '''); |
| 1984 } |
| 1985 |
| 1986 void test_updateErrors_removeExisting() { |
| 1987 _resolveUnit(r''' |
| 1988 f1() { |
| 1989 print(1) |
| 1990 } |
| 1991 f2() { |
| 1992 print(22) |
| 1993 } |
| 1994 f3() { |
| 1995 print(333) |
| 1996 } |
| 1997 '''); |
| 1998 _updateAndValidate(r''' |
| 1999 f1() { |
| 2000 print(1) |
| 2001 } |
| 2002 f2() { |
| 2003 print(22); |
| 2004 } |
| 2005 f3() { |
| 2006 print(333) |
| 2007 } |
| 2008 '''); |
| 2009 } |
| 2010 |
| 2011 void test_updateErrors_shiftExisting() { |
| 2012 _resolveUnit(r''' |
| 2013 f1() { |
| 2014 print(1) |
| 2015 } |
| 2016 f2() { |
| 2017 print(2); |
| 2018 } |
| 2019 f3() { |
| 2020 print(333) |
| 2021 } |
| 2022 '''); |
| 2023 _updateAndValidate(r''' |
| 2024 f1() { |
| 2025 print(1) |
| 2026 } |
| 2027 f2() { |
| 2028 print(22); |
| 2029 } |
| 2030 f3() { |
| 2031 print(333) |
| 2032 } |
| 2033 '''); |
| 2034 } |
| 1928 | 2035 |
| 1929 /** | 2036 /** |
| 1930 * Reset the analysis context to have the 'incremental' option set to the | 2037 * Reset the analysis context to have the 'incremental' option set to the |
| 1931 * given value. | 2038 * given value. |
| 1932 */ | 2039 */ |
| 1933 void _resetWithIncremental(bool enable) { | 2040 void _resetWithIncremental(bool enable) { |
| 1934 AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl(); | 2041 AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl(); |
| 1935 analysisOptions.incremental = enable; | 2042 analysisOptions.incremental = enable; |
| 1936 analysisContext2.analysisOptions = analysisOptions; | 2043 analysisContext2.analysisOptions = analysisOptions; |
| 1937 } | 2044 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1952 } | 2059 } |
| 1953 | 2060 |
| 1954 void _updateAndValidate(String newCode, [bool compareWithFull = true]) { | 2061 void _updateAndValidate(String newCode, [bool compareWithFull = true]) { |
| 1955 // Run any pending tasks tasks. | 2062 // Run any pending tasks tasks. |
| 1956 _runTasks(); | 2063 _runTasks(); |
| 1957 // Update the source - currently this may cause incremental resolution. | 2064 // Update the source - currently this may cause incremental resolution. |
| 1958 // Then request the updated resolved unit. | 2065 // Then request the updated resolved unit. |
| 1959 _resetWithIncremental(true); | 2066 _resetWithIncremental(true); |
| 1960 analysisContext2.setContents(source, newCode); | 2067 analysisContext2.setContents(source, newCode); |
| 1961 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary); | 2068 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary); |
| 2069 List<AnalysisError> newErrors = analysisContext.getErrors(source).errors; |
| 1962 // The existing CompilationUnitElement should be updated. | 2070 // The existing CompilationUnitElement should be updated. |
| 1963 expect(newUnit.element, same(oldUnitElement)); | 2071 expect(newUnit.element, same(oldUnitElement)); |
| 1964 // The only expected pending task should return the same resolved | 2072 // The only expected pending task should return the same resolved |
| 1965 // "newUnit", so all clients will get it using the usual way. | 2073 // "newUnit", so all clients will get it using the usual way. |
| 1966 AnalysisResult analysisResult = analysisContext.performAnalysisTask(); | 2074 AnalysisResult analysisResult = analysisContext.performAnalysisTask(); |
| 1967 ChangeNotice notice = analysisResult.changeNotices[0]; | 2075 ChangeNotice notice = analysisResult.changeNotices[0]; |
| 1968 expect(notice.compilationUnit, same(newUnit)); | 2076 expect(notice.compilationUnit, same(newUnit)); |
| 1969 // Resolve "newCode" from scratch. | 2077 // Resolve "newCode" from scratch. |
| 1970 if (compareWithFull) { | 2078 if (compareWithFull) { |
| 1971 _resetWithIncremental(false); | 2079 _resetWithIncremental(false); |
| 1972 source = addSource(newCode); | 2080 source = addSource(newCode); |
| 2081 _runTasks(); |
| 1973 LibraryElement library = resolve(source); | 2082 LibraryElement library = resolve(source); |
| 1974 CompilationUnit fullNewUnit = resolveCompilationUnit(source, library); | 2083 CompilationUnit fullNewUnit = resolveCompilationUnit(source, library); |
| 1975 // Validate that "incremental" and "full" units have the same resolution. | 2084 // Validate that "incremental" and "full" units have the same resolution. |
| 1976 _SameResolutionValidator.assertSameResolution(newUnit, fullNewUnit); | 2085 _SameResolutionValidator.assertSameResolution(newUnit, fullNewUnit); |
| 1977 _assertEqualsTokens(newUnit, fullNewUnit); | 2086 _assertEqualTokens(newUnit, fullNewUnit); |
| 2087 List<AnalysisError> newFullErrors = |
| 2088 analysisContext.getErrors(source).errors; |
| 2089 _assertEqualErrors(newErrors, newFullErrors); |
| 2090 // TODO(scheglov) check line info |
| 1978 } | 2091 } |
| 1979 } | 2092 } |
| 1980 | 2093 |
| 1981 static void _assertEqualsToken(Token incrToken, Token fullToken) { | 2094 static void _assertEqualError(AnalysisError incrError, |
| 2095 AnalysisError fullError) { |
| 2096 expect(incrError.errorCode, same(fullError.errorCode)); |
| 2097 expect(incrError.source, fullError.source); |
| 2098 expect(incrError.offset, fullError.offset); |
| 2099 expect(incrError.length, fullError.length); |
| 2100 expect(incrError.message, fullError.message); |
| 2101 } |
| 2102 |
| 2103 static void _assertEqualErrors(List<AnalysisError> incrErrors, |
| 2104 List<AnalysisError> fullErrors) { |
| 2105 expect(incrErrors, hasLength(fullErrors.length)); |
| 2106 if (incrErrors.isNotEmpty) { |
| 2107 incrErrors.sort((a, b) => a.offset - b.offset); |
| 2108 } |
| 2109 if (fullErrors.isNotEmpty) { |
| 2110 fullErrors.sort((a, b) => a.offset - b.offset); |
| 2111 } |
| 2112 int length = incrErrors.length; |
| 2113 for (int i = 0; i < length; i++) { |
| 2114 AnalysisError incrError = incrErrors[i]; |
| 2115 AnalysisError fullError = fullErrors[i]; |
| 2116 _assertEqualError(incrError, fullError); |
| 2117 } |
| 2118 } |
| 2119 |
| 2120 static void _assertEqualToken(Token incrToken, Token fullToken) { |
| 1982 expect(incrToken.type, fullToken.type); | 2121 expect(incrToken.type, fullToken.type); |
| 1983 expect(incrToken.offset, fullToken.offset); | 2122 expect(incrToken.offset, fullToken.offset); |
| 1984 expect(incrToken.length, fullToken.length); | 2123 expect(incrToken.length, fullToken.length); |
| 1985 expect(incrToken.lexeme, fullToken.lexeme); | 2124 expect(incrToken.lexeme, fullToken.lexeme); |
| 1986 } | 2125 } |
| 1987 | 2126 |
| 1988 static void _assertEqualsTokens(CompilationUnit incrUnit, | 2127 static void _assertEqualTokens(CompilationUnit incrUnit, |
| 1989 CompilationUnit fullUnit) { | 2128 CompilationUnit fullUnit) { |
| 1990 Token incrToken = incrUnit.beginToken; | 2129 Token incrToken = incrUnit.beginToken; |
| 1991 Token fullToken = fullUnit.beginToken; | 2130 Token fullToken = fullUnit.beginToken; |
| 1992 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) { | 2131 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) { |
| 1993 // print('$incrToken @ ${incrToken.offset}'); | 2132 // print('$incrToken @ ${incrToken.offset}'); |
| 1994 // print('$fullToken @ ${fullToken.offset}'); | 2133 // print('$fullToken @ ${fullToken.offset}'); |
| 1995 _assertEqualsToken(incrToken, fullToken); | 2134 _assertEqualToken(incrToken, fullToken); |
| 1996 incrToken = incrToken.next; | 2135 incrToken = incrToken.next; |
| 1997 fullToken = fullToken.next; | 2136 fullToken = fullToken.next; |
| 1998 } | 2137 } |
| 1999 } | 2138 } |
| 2000 } | 2139 } |
| 2001 | 2140 |
| 2002 | 2141 |
| 2003 class ResolutionContextBuilderTest extends EngineTestCase { | 2142 class ResolutionContextBuilderTest extends EngineTestCase { |
| 2004 GatheringErrorListener listener = new GatheringErrorListener(); | 2143 GatheringErrorListener listener = new GatheringErrorListener(); |
| 2005 | 2144 |
| (...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3059 _visitList(node.metadata, other.metadata); | 3198 _visitList(node.metadata, other.metadata); |
| 3060 _visitNode(node.identifier, other.identifier); | 3199 _visitNode(node.identifier, other.identifier); |
| 3061 } | 3200 } |
| 3062 | 3201 |
| 3063 static void assertSameResolution(CompilationUnit actual, | 3202 static void assertSameResolution(CompilationUnit actual, |
| 3064 CompilationUnit expected) { | 3203 CompilationUnit expected) { |
| 3065 _SameResolutionValidator validator = new _SameResolutionValidator(expected); | 3204 _SameResolutionValidator validator = new _SameResolutionValidator(expected); |
| 3066 actual.accept(validator); | 3205 actual.accept(validator); |
| 3067 } | 3206 } |
| 3068 } | 3207 } |
| OLD | NEW |