Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(973)

Side by Side Diff: pkg/analyzer/test/generated/incremental_resolver_test.dart

Issue 753803002: Use a poor man's incremental parser to perform incremental resolution. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes for review comments. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/analyzer/lib/src/generated/incremental_resolver.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
21 import '../reflective_tests.dart'; 21 import '../reflective_tests.dart';
22 import 'parser_test.dart'; 22 import 'parser_test.dart';
23 import 'resolver_test.dart'; 23 import 'resolver_test.dart';
24 import 'test_support.dart'; 24 import 'test_support.dart';
25 25
26 26
27 main() { 27 main() {
28 groupSep = ' | '; 28 groupSep = ' | ';
29 runReflectiveTests(DeclarationMatcherTest); 29 runReflectiveTests(DeclarationMatcherTest);
30 runReflectiveTests(IncrementalResolverTest); 30 runReflectiveTests(IncrementalResolverTest);
31 runReflectiveTests(PoorMansIncrementalResolutionTest);
31 runReflectiveTests(ResolutionContextBuilderTest); 32 runReflectiveTests(ResolutionContextBuilderTest);
32 } 33 }
33 34
34 35
35 class DeclarationMatcherTest extends ResolverTestCase { 36 class DeclarationMatcherTest extends ResolverTestCase {
36 void test_false_class_list_add() { 37 void test_false_class_list_add() {
37 _assertCompilationUnitMatches(false, r''' 38 _assertCompilationUnitMatches(false, r'''
38 class A {} 39 class A {}
39 class B {} 40 class B {}
40 ''', r''' 41 ''', r'''
(...skipping 1665 matching lines...) Expand 10 before | Expand all | Expand 10 after
1706 while (token.type != TokenType.EOF) { 1707 while (token.type != TokenType.EOF) {
1707 if (token.offset >= afterOffset) { 1708 if (token.offset >= afterOffset) {
1708 token.applyDelta(delta); 1709 token.applyDelta(delta);
1709 } 1710 }
1710 token = token.next; 1711 token = token.next;
1711 } 1712 }
1712 } 1713 }
1713 } 1714 }
1714 1715
1715 1716
1717 /**
1718 * The test for [poorMansIncrementalResolution] function and its integration
1719 * into [AnalysisContext].
1720 */
1721 class PoorMansIncrementalResolutionTest extends ResolverTestCase {
1722 Source source;
1723 String code;
1724 LibraryElement oldLibrary;
1725 CompilationUnit oldUnit;
1726 CompilationUnitElement oldUnitElement;
1727
1728 void setUp() {
1729 super.setUp();
1730 _resetWithIncremental(true);
1731 }
1732
1733 void test_inBody_expression() {
1734 _resolveUnit(r'''
1735 class A {
1736 m() {
1737 print(1);
1738 }
1739 }
1740 ''');
1741 _updateAndValidate(r'''
1742 class A {
1743 m() {
1744 print(2 + 3);
1745 }
1746 }
1747 ''');
1748 }
1749
1750 void test_inBody_insertStatement() {
1751 _resolveUnit(r'''
1752 main() {
1753 print(1);
1754 }
1755 ''');
1756 _updateAndValidate(r'''
1757 main() {
1758 print(0);
1759 print(1);
1760 }
1761 ''');
1762 }
1763
1764 void test_inBody_tokenToNode() {
1765 _resolveUnit(r'''
1766 main() {
1767 var v = 42;
1768 }
1769 ''');
1770 _updateAndValidate(r'''
1771 main() {
1772 int v = 42;
1773 }
1774 ''');
1775 }
1776
1777 void test_twice() {
1778 _resolveUnit(r'''
1779 main() {
1780 print(1);
1781 }''');
1782 _updateAndValidate(r'''
1783 main() {
1784 print(12);
1785 }''', false);
1786 _updateAndValidate(r'''
1787 main() {
1788 print(1);
1789 }''', false);
1790 }
1791
1792 void test_twice2() {
1793 _resolveUnit(r'''
1794 class A {
1795 m() {
1796 int vvvvvvvv = 42;
1797 print(vvvvvvvv);
1798 }
1799 }''');
1800 _updateAndValidate(r'''
1801 class A {
1802 m() {
1803 int vvvvvvvv2 = 42;
1804 print(vvvvvvvv2);
1805 }
1806 }''', false);
1807 _updateAndValidate(r'''
1808 class A {
1809 m() {
1810 int vvvvvvvv = 42;
1811 print(vvvvvvvv);
1812 }
1813 }''', false);
1814 }
1815
1816 void test_withImport() {
1817 _resolveUnit(r'''
1818 import 'dart:async';
1819 import 'dart:math';
1820 main() {
1821 print(1);
1822 }
1823 ''');
1824 _updateAndValidate(r'''
1825 import 'dart:async';
1826 import 'dart:math';
1827 main() {
1828 print(2 + 3);
1829 }
1830 ''');
1831 }
1832
1833 /**
1834 * Reset the analysis context to have the 'incremental' option set to the
1835 * given value.
1836 */
1837 void _resetWithIncremental(bool enable) {
1838 AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl();
1839 analysisOptions.incremental = enable;
1840 analysisContext2.analysisOptions = analysisOptions;
1841 }
1842
1843 void _resolveUnit(String code) {
1844 this.code = code;
1845 source = addSource(code);
1846 oldLibrary = resolve(source);
1847 oldUnit = resolveCompilationUnit(source, oldLibrary);
1848 oldUnitElement = oldUnit.element;
1849 }
1850
1851 void _runTasks() {
1852 AnalysisResult result = analysisContext.performAnalysisTask();
1853 while (result.changeNotices != null) {
1854 result = analysisContext.performAnalysisTask();
1855 }
1856 }
1857
1858 void _updateAndValidate(String newCode, [bool compareWithFull = true]) {
1859 // Run any pending tasks tasks.
1860 _runTasks();
1861 // Update the source - currently this may cause incremental resolution.
1862 // Then request the updated resolved unit.
1863 _resetWithIncremental(true);
1864 analysisContext2.setContents(source, newCode);
1865 CompilationUnit newUnit = resolveCompilationUnit(source, oldLibrary);
1866 // The existing CompilationUnitElement should be updated.
1867 expect(newUnit.element, same(oldUnitElement));
1868 // The only expected pending task should return the same resolved
1869 // "newUnit", so all clients will get it using the usual way.
1870 AnalysisResult analysisResult = analysisContext.performAnalysisTask();
1871 ChangeNotice notice = analysisResult.changeNotices[0];
1872 expect(notice.compilationUnit, same(newUnit));
1873 // Resolve "newCode" from scratch.
1874 if (compareWithFull) {
1875 _resetWithIncremental(false);
1876 source = addSource(newCode);
1877 LibraryElement library = resolve(source);
1878 CompilationUnit fullNewUnit = resolveCompilationUnit(source, library);
1879 // Validate that "incremental" and "full" units have the same resolution.
1880 _SameResolutionValidator.assertSameResolution(newUnit, fullNewUnit);
1881 _assertEqualsTokens(newUnit, fullNewUnit);
1882 }
1883 }
1884
1885 static void _assertEqualsTokens(CompilationUnit incrUnit,
1886 CompilationUnit fullUnit) {
1887 Token incrToken = incrUnit.beginToken;
1888 Token fullToken = fullUnit.beginToken;
1889 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) {
1890 // print('$incrToken @ ${incrToken.offset}');
1891 // print('$fullToken @ ${fullToken.offset}');
1892 _assertEqualsToken(incrToken, fullToken);
1893 incrToken = incrToken.next;
1894 fullToken = fullToken.next;
1895 }
1896 }
1897
1898 static void _assertEqualsToken(Token incrToken, Token fullToken) {
1899 expect(incrToken.type, fullToken.type);
1900 expect(incrToken.offset, fullToken.offset);
1901 expect(incrToken.length, fullToken.length);
1902 expect(incrToken.lexeme, fullToken.lexeme);
1903 }
1904 }
1905
1906
1716 class ResolutionContextBuilderTest extends EngineTestCase { 1907 class ResolutionContextBuilderTest extends EngineTestCase {
1717 GatheringErrorListener listener = new GatheringErrorListener(); 1908 GatheringErrorListener listener = new GatheringErrorListener();
1718 1909
1719 void test_scopeFor_ClassDeclaration() { 1910 void test_scopeFor_ClassDeclaration() {
1720 Scope scope = _scopeFor(_createResolvedClassDeclaration()); 1911 Scope scope = _scopeFor(_createResolvedClassDeclaration());
1721 EngineTestCase.assertInstanceOf( 1912 EngineTestCase.assertInstanceOf(
1722 (obj) => obj is LibraryScope, 1913 (obj) => obj is LibraryScope,
1723 LibraryScope, 1914 LibraryScope,
1724 scope); 1915 scope);
1725 } 1916 }
(...skipping 1046 matching lines...) Expand 10 before | Expand all | Expand 10 after
2772 _visitList(node.metadata, other.metadata); 2963 _visitList(node.metadata, other.metadata);
2773 _visitNode(node.identifier, other.identifier); 2964 _visitNode(node.identifier, other.identifier);
2774 } 2965 }
2775 2966
2776 static void assertSameResolution(CompilationUnit actual, 2967 static void assertSameResolution(CompilationUnit actual,
2777 CompilationUnit expected) { 2968 CompilationUnit expected) {
2778 _SameResolutionValidator validator = new _SameResolutionValidator(expected); 2969 _SameResolutionValidator validator = new _SameResolutionValidator(expected);
2779 actual.accept(validator); 2970 actual.accept(validator);
2780 } 2971 }
2781 } 2972 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/incremental_resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698