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

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

Issue 814533005: Incrementally resolve users of a changed method. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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';
11 import 'package:analyzer/src/generated/incremental_logger.dart' as log; 11 import 'package:analyzer/src/generated/incremental_logger.dart' as log;
12 import 'package:analyzer/src/generated/incremental_resolver.dart'; 12 import 'package:analyzer/src/generated/incremental_resolver.dart';
13 import 'package:analyzer/src/generated/java_engine.dart'; 13 import 'package:analyzer/src/generated/java_engine.dart';
14 import 'package:analyzer/src/generated/parser.dart'; 14 import 'package:analyzer/src/generated/parser.dart';
15 import 'package:analyzer/src/generated/resolver.dart'; 15 import 'package:analyzer/src/generated/resolver.dart';
16 import 'package:analyzer/src/generated/scanner.dart'; 16 import 'package:analyzer/src/generated/scanner.dart';
17 import 'package:analyzer/src/generated/source_io.dart'; 17 import 'package:analyzer/src/generated/source_io.dart';
18 import 'package:analyzer/src/generated/testing/ast_factory.dart'; 18 import 'package:analyzer/src/generated/testing/ast_factory.dart';
19 import 'package:analyzer/src/generated/testing/element_factory.dart'; 19 import 'package:analyzer/src/generated/testing/element_factory.dart';
20 import 'package:unittest/unittest.dart'; 20 import 'package:unittest/unittest.dart';
21 21
22 import '../reflective_tests.dart'; 22 import '../reflective_tests.dart';
23 import 'parser_test.dart'; 23 import 'parser_test.dart';
24 import 'resolver_test.dart'; 24 import 'resolver_test.dart';
25 import 'test_support.dart'; 25 import 'test_support.dart';
26 26
27 27
28
28 main() { 29 main() {
29 groupSep = ' | '; 30 groupSep = ' | ';
30 runReflectiveTests(DeclarationMatcherTest); 31 runReflectiveTests(DeclarationMatcherTest);
31 runReflectiveTests(IncrementalResolverTest); 32 runReflectiveTests(IncrementalResolverTest);
32 runReflectiveTests(PoorMansIncrementalResolutionTest); 33 runReflectiveTests(PoorMansIncrementalResolutionTest);
33 runReflectiveTests(ResolutionContextBuilderTest); 34 runReflectiveTests(ResolutionContextBuilderTest);
34 } 35 }
35 36
36 37
38 void _assertEqualError(AnalysisError incrError, AnalysisError fullError) {
39 expect(incrError.errorCode, same(fullError.errorCode));
40 expect(incrError.source, fullError.source);
41 expect(incrError.offset, fullError.offset);
42 expect(incrError.length, fullError.length);
43 expect(incrError.message, fullError.message);
44 }
45
46
47 void _assertEqualErrors(List<AnalysisError> incrErrors,
48 List<AnalysisError> fullErrors) {
49 expect(incrErrors, hasLength(fullErrors.length));
50 if (incrErrors.isNotEmpty) {
51 incrErrors.sort((a, b) => a.offset - b.offset);
52 }
53 if (fullErrors.isNotEmpty) {
54 fullErrors.sort((a, b) => a.offset - b.offset);
55 }
56 int length = incrErrors.length;
57 for (int i = 0; i < length; i++) {
58 AnalysisError incrError = incrErrors[i];
59 AnalysisError fullError = fullErrors[i];
60 _assertEqualError(incrError, fullError);
61 }
62 }
63
64
37 @ReflectiveTestCase() 65 @ReflectiveTestCase()
38 class DeclarationMatcherTest extends ResolverTestCase { 66 class DeclarationMatcherTest extends ResolverTestCase {
39 void setUp() { 67 void setUp() {
40 super.setUp(); 68 super.setUp();
41 test_resolveApiChanges = true; 69 test_resolveApiChanges = true;
42 } 70 }
43 71
44 void test_false_class_list_add() { 72 void test_false_class_list_add() {
45 _assertDoesNotMatch(r''' 73 _assertDoesNotMatch(r'''
46 class A {} 74 class A {}
(...skipping 761 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 } 836 }
809 ''', r''' 837 ''', r'''
810 class A { 838 class A {
811 a() {} 839 a() {}
812 b() {} 840 b() {}
813 } 841 }
814 '''); 842 ''');
815 } 843 }
816 844
817 void test_false_method_parameters_type_edit() { 845 void test_false_method_parameters_type_edit() {
818 // TODO
819 _assertDoesNotMatchOK(r''' 846 _assertDoesNotMatchOK(r'''
820 class A { 847 class A {
821 m(int p) { 848 m(int p) {
822 } 849 }
823 } 850 }
824 ''', r''' 851 ''', r'''
825 class A { 852 class A {
826 m(String p) { 853 m(String p) {
827 } 854 }
828 } 855 }
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after
1892 @ReflectiveTestCase() 1919 @ReflectiveTestCase()
1893 class IncrementalResolverTest extends ResolverTestCase { 1920 class IncrementalResolverTest extends ResolverTestCase {
1894 Source source; 1921 Source source;
1895 String code; 1922 String code;
1896 LibraryElement library; 1923 LibraryElement library;
1897 CompilationUnit unit; 1924 CompilationUnit unit;
1898 1925
1899 void setUp() { 1926 void setUp() {
1900 super.setUp(); 1927 super.setUp();
1901 test_resolveApiChanges = true; 1928 test_resolveApiChanges = true;
1929 log.logger = log.NULL_LOGGER;
1902 } 1930 }
1903 1931
1904 void test_api_method_edit_returnType() { 1932 void test_api_method_edit_returnType() {
1905 _resolveUnit(r''' 1933 _resolveUnit(r'''
1906 class A { 1934 class A {
1907 int m() { 1935 int m() {
1908 return null; 1936 return null;
1909 } 1937 }
1910 } 1938 }
1911 main() { 1939 main() {
1912 A a = new A(); 1940 A a = new A();
1913 var v = a.m(); 1941 int v = a.m();
1942 print(v);
1914 } 1943 }
1915 '''); 1944 ''');
1916 _resolve(_editString('int m', 'String m'), _isDeclaration); 1945 _resolve(_editString('int m', 'String m'), _isDeclaration);
1917 // We don't add or fix an error, but we verify that type of "v" 1946 // We don't add or fix an error, but we verify that type of "v"
1918 // is updated from "int" to "String". 1947 // is updated from "int" to "String".
1919 } 1948 }
1920 1949
1921 void test_classMemberAccessor_body() { 1950 void test_classMemberAccessor_body() {
1922 _resolveUnit(r''' 1951 _resolveUnit(r'''
1923 class A { 1952 class A {
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
2233 { 2262 {
2234 bool success = NodeReplacer.replace(oldNode, newNode); 2263 bool success = NodeReplacer.replace(oldNode, newNode);
2235 expect(success, isTrue); 2264 expect(success, isTrue);
2236 } 2265 }
2237 // update tokens 2266 // update tokens
2238 { 2267 {
2239 int delta = edit.replacement.length - edit.length; 2268 int delta = edit.replacement.length - edit.length;
2240 _shiftTokens(unit.beginToken, offset, delta); 2269 _shiftTokens(unit.beginToken, offset, delta);
2241 } 2270 }
2242 // do incremental resolution 2271 // do incremental resolution
2272 int updateOffset = edit.offset;
2273 int updateEndOld = updateOffset + edit.length;
2274 int updateOldNew = updateOffset + edit.replacement.length;
2243 IncrementalResolver resolver = new IncrementalResolver( 2275 IncrementalResolver resolver = new IncrementalResolver(
2244 typeProvider,
2245 unit.element, 2276 unit.element,
2246 edit.offset, 2277 updateOffset,
2247 edit.length, 2278 updateEndOld,
2248 edit.replacement.length); 2279 updateOldNew);
2249 bool success = resolver.resolve(newNode); 2280 bool success = resolver.resolve(newNode);
2250 expect(success, isTrue); 2281 expect(success, isTrue);
2282 List<AnalysisError> newErrors = analysisContext.getErrors(source).errors;
2251 // resolve "newCode" from scratch 2283 // resolve "newCode" from scratch
2252 CompilationUnit fullNewUnit; 2284 CompilationUnit fullNewUnit;
2253 { 2285 {
2254 source = addSource(newCode); 2286 source = addSource(newCode);
2287 _runTasks();
2255 LibraryElement library = resolve(source); 2288 LibraryElement library = resolve(source);
2256 fullNewUnit = resolveCompilationUnit(source, library); 2289 fullNewUnit = resolveCompilationUnit(source, library);
2257 } 2290 }
2258 _SameResolutionValidator.assertSameResolution(unit, fullNewUnit); 2291 _SameResolutionValidator.assertSameResolution(unit, fullNewUnit);
2292 // errors
2293 List<AnalysisError> newFullErrors =
2294 analysisContext.getErrors(source).errors;
2295 _assertEqualErrors(newErrors, newFullErrors);
2296 // prepare for the next cycle
2297 code = newCode;
2259 } 2298 }
2260 2299
2261 void _resolveUnit(String code) { 2300 void _resolveUnit(String code) {
2262 this.code = code; 2301 this.code = code;
2263 source = addSource(code); 2302 source = addSource(code);
2264 library = resolve(source); 2303 library = resolve(source);
2265 unit = resolveCompilationUnit(source, library); 2304 unit = resolveCompilationUnit(source, library);
2305 _runTasks();
2306 }
2307
2308 void _runTasks() {
2309 AnalysisResult result = analysisContext.performAnalysisTask();
2310 while (result.changeNotices != null) {
2311 result = analysisContext.performAnalysisTask();
2312 }
2266 } 2313 }
2267 2314
2268 static AstNode _findNodeAt(CompilationUnit oldUnit, int offset, 2315 static AstNode _findNodeAt(CompilationUnit oldUnit, int offset,
2269 Predicate<AstNode> predicate) { 2316 Predicate<AstNode> predicate) {
2270 NodeLocator locator = new NodeLocator.con1(offset); 2317 NodeLocator locator = new NodeLocator.con1(offset);
2271 AstNode node = locator.searchWithin(oldUnit); 2318 AstNode node = locator.searchWithin(oldUnit);
2272 return node.getAncestor(predicate); 2319 return node.getAncestor(predicate);
2273 } 2320 }
2274 2321
2275 static bool _isBlock(AstNode node) => node is Block; 2322 static bool _isBlock(AstNode node) => node is Block;
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
2682 2729
2683 void test_true_emptyLine_betweenCompilationUnitMembers_remove() { 2730 void test_true_emptyLine_betweenCompilationUnitMembers_remove() {
2684 _resolveUnit(r''' 2731 _resolveUnit(r'''
2685 a() { 2732 a() {
2686 print(1) 2733 print(1)
2687 } 2734 }
2688 2735
2689 b() { 2736 b() {
2690 foo(42); 2737 foo(42);
2691 } 2738 }
2692 foo(String p) {}; 2739 foo(String p) {}
2693 '''); 2740 ''');
2694 _updateAndValidate(r''' 2741 _updateAndValidate(r'''
2695 a() { 2742 a() {
2696 print(1) 2743 print(1)
2697 } 2744 }
2698 b() { 2745 b() {
2699 foo(42); 2746 foo(42);
2700 } 2747 }
2701 foo(String p) {}; 2748 foo(String p) {}
2702 '''); 2749 ''');
2703 } 2750 }
2704 2751
2705 void test_true_wholeConstructor() { 2752 void test_true_wholeConstructor() {
2706 _resolveUnit(r''' 2753 _resolveUnit(r'''
2707 class A { 2754 class A {
2708 A(int a) { 2755 A(int a) {
2709 print(a); 2756 print(a);
2710 } 2757 }
2711 } 2758 }
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
2982 // Validate that "incremental" and "full" units have the same resolution. 3029 // Validate that "incremental" and "full" units have the same resolution.
2983 _SameResolutionValidator.assertSameResolution(newUnit, fullNewUnit); 3030 _SameResolutionValidator.assertSameResolution(newUnit, fullNewUnit);
2984 _assertEqualTokens(newUnit, fullNewUnit); 3031 _assertEqualTokens(newUnit, fullNewUnit);
2985 List<AnalysisError> newFullErrors = 3032 List<AnalysisError> newFullErrors =
2986 analysisContext.getErrors(source).errors; 3033 analysisContext.getErrors(source).errors;
2987 _assertEqualErrors(newErrors, newFullErrors); 3034 _assertEqualErrors(newErrors, newFullErrors);
2988 // TODO(scheglov) check line info 3035 // TODO(scheglov) check line info
2989 } 3036 }
2990 } 3037 }
2991 3038
2992 static void _assertEqualError(AnalysisError incrError,
2993 AnalysisError fullError) {
2994 expect(incrError.errorCode, same(fullError.errorCode));
2995 expect(incrError.source, fullError.source);
2996 expect(incrError.offset, fullError.offset);
2997 expect(incrError.length, fullError.length);
2998 expect(incrError.message, fullError.message);
2999 }
3000
3001 static void _assertEqualErrors(List<AnalysisError> incrErrors,
3002 List<AnalysisError> fullErrors) {
3003 expect(incrErrors, hasLength(fullErrors.length));
3004 if (incrErrors.isNotEmpty) {
3005 incrErrors.sort((a, b) => a.offset - b.offset);
3006 }
3007 if (fullErrors.isNotEmpty) {
3008 fullErrors.sort((a, b) => a.offset - b.offset);
3009 }
3010 int length = incrErrors.length;
3011 for (int i = 0; i < length; i++) {
3012 AnalysisError incrError = incrErrors[i];
3013 AnalysisError fullError = fullErrors[i];
3014 _assertEqualError(incrError, fullError);
3015 }
3016 }
3017
3018 static void _assertEqualToken(Token incrToken, Token fullToken) { 3039 static void _assertEqualToken(Token incrToken, Token fullToken) {
3019 expect(incrToken.type, fullToken.type); 3040 expect(incrToken.type, fullToken.type);
3020 expect(incrToken.offset, fullToken.offset); 3041 expect(incrToken.offset, fullToken.offset);
3021 expect(incrToken.length, fullToken.length); 3042 expect(incrToken.length, fullToken.length);
3022 expect(incrToken.lexeme, fullToken.lexeme); 3043 expect(incrToken.lexeme, fullToken.lexeme);
3023 } 3044 }
3024 3045
3025 static void _assertEqualTokens(CompilationUnit incrUnit, 3046 static void _assertEqualTokens(CompilationUnit incrUnit,
3026 CompilationUnit fullUnit) { 3047 CompilationUnit fullUnit) {
3027 Token incrToken = incrUnit.beginToken; 3048 Token incrToken = incrUnit.beginToken;
3028 Token fullToken = fullUnit.beginToken; 3049 Token fullToken = fullUnit.beginToken;
3029 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) { 3050 while (incrToken.type != TokenType.EOF && fullToken.type != TokenType.EOF) {
3030 // print('$incrToken @ ${incrToken.offset}');
3031 // print('$fullToken @ ${fullToken.offset}');
3032 _assertEqualToken(incrToken, fullToken); 3051 _assertEqualToken(incrToken, fullToken);
3033 // comments 3052 // comments
3034 { 3053 {
3035 Token incrComment = incrToken.precedingComments; 3054 Token incrComment = incrToken.precedingComments;
3036 Token fullComment = fullToken.precedingComments; 3055 Token fullComment = fullToken.precedingComments;
3037 while (true) { 3056 while (true) {
3038 if (fullComment == null) { 3057 if (fullComment == null) {
3039 expect(incrComment, isNull); 3058 expect(incrComment, isNull);
3040 break; 3059 break;
3041 } 3060 }
(...skipping 1077 matching lines...) Expand 10 before | Expand all | Expand 10 after
4119 _visitList(node.metadata, other.metadata); 4138 _visitList(node.metadata, other.metadata);
4120 _visitNode(node.identifier, other.identifier); 4139 _visitNode(node.identifier, other.identifier);
4121 } 4140 }
4122 4141
4123 static void assertSameResolution(CompilationUnit actual, 4142 static void assertSameResolution(CompilationUnit actual,
4124 CompilationUnit expected) { 4143 CompilationUnit expected) {
4125 _SameResolutionValidator validator = new _SameResolutionValidator(expected); 4144 _SameResolutionValidator validator = new _SameResolutionValidator(expected);
4126 actual.accept(validator); 4145 actual.accept(validator);
4127 } 4146 }
4128 } 4147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698