| 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 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 } | 456 } |
| 457 | 457 |
| 458 void test_functionBody_statement() { | 458 void test_functionBody_statement() { |
| 459 _resolveUnit(r''' | 459 _resolveUnit(r''' |
| 460 main(int a, int b) { | 460 main(int a, int b) { |
| 461 return a + b; | 461 return a + b; |
| 462 }'''); | 462 }'''); |
| 463 _resolve(_editString('+', '*'), _isStatement); | 463 _resolve(_editString('+', '*'), _isStatement); |
| 464 } | 464 } |
| 465 | 465 |
| 466 void test_updateElementOffset() { |
| 467 _resolveUnit(r''' |
| 468 class A { |
| 469 int am(String ap) { |
| 470 int av = 1; |
| 471 return av; |
| 472 } |
| 473 } |
| 474 main(int a, int b) { |
| 475 return a + b; |
| 476 } |
| 477 class B { |
| 478 int bm(String bp) { |
| 479 int bv = 1; |
| 480 return bv; |
| 481 } |
| 482 } |
| 483 '''); |
| 484 _resolve(_editString('+', ' + '), _isStatement); |
| 485 } |
| 486 |
| 466 _Edit _editString(String search, String replacement, [int length]) { | 487 _Edit _editString(String search, String replacement, [int length]) { |
| 467 int offset = code.indexOf(search); | 488 int offset = code.indexOf(search); |
| 468 expect(offset, isNot(-1)); | 489 expect(offset, isNot(-1)); |
| 469 if (length == null) { | 490 if (length == null) { |
| 470 length = search.length; | 491 length = search.length; |
| 471 } | 492 } |
| 472 return new _Edit(offset, length, replacement); | 493 return new _Edit(offset, length, replacement); |
| 473 } | 494 } |
| 474 | 495 |
| 475 /** | 496 /** |
| (...skipping 11 matching lines...) Expand all Loading... |
| 487 edit.replacement + | 508 edit.replacement + |
| 488 code.substring(offset + edit.length); | 509 code.substring(offset + edit.length); |
| 489 CompilationUnit newUnit = _parseUnit(newCode); | 510 CompilationUnit newUnit = _parseUnit(newCode); |
| 490 // replace the node | 511 // replace the node |
| 491 AstNode oldNode = _findNodeAt(unit, offset, predicate); | 512 AstNode oldNode = _findNodeAt(unit, offset, predicate); |
| 492 AstNode newNode = _findNodeAt(newUnit, offset, predicate); | 513 AstNode newNode = _findNodeAt(newUnit, offset, predicate); |
| 493 bool success = NodeReplacer.replace(oldNode, newNode); | 514 bool success = NodeReplacer.replace(oldNode, newNode); |
| 494 expect(success, isTrue); | 515 expect(success, isTrue); |
| 495 // do incremental resolution | 516 // do incremental resolution |
| 496 GatheringErrorListener errorListener = new GatheringErrorListener(); | 517 GatheringErrorListener errorListener = new GatheringErrorListener(); |
| 497 // TODO(scheglov) use 'replacement' to update elements offsets | 518 IncrementalResolver resolver = new IncrementalResolver( |
| 498 IncrementalResolver resolver = | 519 errorListener, |
| 499 new IncrementalResolver(library, source, typeProvider, errorListener); | 520 typeProvider, |
| 521 library, |
| 522 unit.element, |
| 523 source, |
| 524 edit.offset, |
| 525 edit.length, |
| 526 edit.replacement.length); |
| 500 resolver.resolve(newNode); | 527 resolver.resolve(newNode); |
| 501 // resolve "newCode" from scratch | 528 // resolve "newCode" from scratch |
| 502 CompilationUnit fullNewUnit; | 529 CompilationUnit fullNewUnit; |
| 503 { | 530 { |
| 504 source = addSource(newCode); | 531 source = addSource(newCode); |
| 505 LibraryElement library = resolve(source); | 532 LibraryElement library = resolve(source); |
| 506 fullNewUnit = resolveCompilationUnit(source, library); | 533 fullNewUnit = resolveCompilationUnit(source, library); |
| 507 } | 534 } |
| 508 _SameResolutionValidator.assertSameResolution(unit, fullNewUnit); | 535 _SameResolutionValidator.assertSameResolution(unit, fullNewUnit); |
| 509 } | 536 } |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 CatchClause other = this.other; | 935 CatchClause other = this.other; |
| 909 _visitNode(node.exceptionType, other.exceptionType); | 936 _visitNode(node.exceptionType, other.exceptionType); |
| 910 _visitNode(node.exceptionParameter, other.exceptionParameter); | 937 _visitNode(node.exceptionParameter, other.exceptionParameter); |
| 911 _visitNode(node.stackTraceParameter, other.stackTraceParameter); | 938 _visitNode(node.stackTraceParameter, other.stackTraceParameter); |
| 912 _visitNode(node.body, other.body); | 939 _visitNode(node.body, other.body); |
| 913 } | 940 } |
| 914 | 941 |
| 915 @override | 942 @override |
| 916 visitClassDeclaration(ClassDeclaration node) { | 943 visitClassDeclaration(ClassDeclaration node) { |
| 917 ClassDeclaration other = this.other; | 944 ClassDeclaration other = this.other; |
| 945 _visitDeclaration(node, other); |
| 918 _visitNode(node.name, other.name); | 946 _visitNode(node.name, other.name); |
| 919 _visitNode(node.typeParameters, other.typeParameters); | 947 _visitNode(node.typeParameters, other.typeParameters); |
| 920 _visitNode(node.extendsClause, other.extendsClause); | 948 _visitNode(node.extendsClause, other.extendsClause); |
| 921 _visitNode(node.implementsClause, other.implementsClause); | 949 _visitNode(node.implementsClause, other.implementsClause); |
| 922 _visitNode(node.withClause, other.withClause); | 950 _visitNode(node.withClause, other.withClause); |
| 923 _visitList(node.members, other.members); | 951 _visitList(node.members, other.members); |
| 924 } | 952 } |
| 925 | 953 |
| 926 @override | 954 @override |
| 927 visitClassTypeAlias(ClassTypeAlias node) { | 955 visitClassTypeAlias(ClassTypeAlias node) { |
| 928 ClassTypeAlias other = this.other; | 956 ClassTypeAlias other = this.other; |
| 929 _verifyElement(node.element, other.element); | 957 _visitDeclaration(node, other); |
| 930 _visitNode(node.name, other.name); | 958 _visitNode(node.name, other.name); |
| 931 _visitNode(node.typeParameters, other.typeParameters); | 959 _visitNode(node.typeParameters, other.typeParameters); |
| 932 _visitNode(node.superclass, other.superclass); | 960 _visitNode(node.superclass, other.superclass); |
| 933 _visitNode(node.withClause, other.withClause); | 961 _visitNode(node.withClause, other.withClause); |
| 934 } | 962 } |
| 935 | 963 |
| 936 @override | 964 @override |
| 937 visitComment(Comment node) { | 965 visitComment(Comment node) { |
| 938 Comment other = this.other; | 966 Comment other = this.other; |
| 939 _visitList(node.references, other.references); | 967 _visitList(node.references, other.references); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 958 ConditionalExpression other = this.other; | 986 ConditionalExpression other = this.other; |
| 959 _visitExpression(node, other); | 987 _visitExpression(node, other); |
| 960 _visitNode(node.condition, other.condition); | 988 _visitNode(node.condition, other.condition); |
| 961 _visitNode(node.thenExpression, other.thenExpression); | 989 _visitNode(node.thenExpression, other.thenExpression); |
| 962 _visitNode(node.elseExpression, other.elseExpression); | 990 _visitNode(node.elseExpression, other.elseExpression); |
| 963 } | 991 } |
| 964 | 992 |
| 965 @override | 993 @override |
| 966 visitConstructorDeclaration(ConstructorDeclaration node) { | 994 visitConstructorDeclaration(ConstructorDeclaration node) { |
| 967 ConstructorDeclaration other = this.other; | 995 ConstructorDeclaration other = this.other; |
| 968 _verifyElement(node.element, other.element); | 996 _visitDeclaration(node, other); |
| 969 _visitNode(node.returnType, other.returnType); | 997 _visitNode(node.returnType, other.returnType); |
| 970 _visitNode(node.name, other.name); | 998 _visitNode(node.name, other.name); |
| 971 _visitNode(node.parameters, other.parameters); | 999 _visitNode(node.parameters, other.parameters); |
| 972 _visitNode(node.redirectedConstructor, other.redirectedConstructor); | 1000 _visitNode(node.redirectedConstructor, other.redirectedConstructor); |
| 973 _visitList(node.initializers, other.initializers); | 1001 _visitList(node.initializers, other.initializers); |
| 974 } | 1002 } |
| 975 | 1003 |
| 976 @override | 1004 @override |
| 977 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { | 1005 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { |
| 978 ConstructorFieldInitializer other = this.other; | 1006 ConstructorFieldInitializer other = this.other; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1064 | 1092 |
| 1065 @override | 1093 @override |
| 1066 visitExtendsClause(ExtendsClause node) { | 1094 visitExtendsClause(ExtendsClause node) { |
| 1067 ExtendsClause other = this.other; | 1095 ExtendsClause other = this.other; |
| 1068 _visitNode(node.superclass, other.superclass); | 1096 _visitNode(node.superclass, other.superclass); |
| 1069 } | 1097 } |
| 1070 | 1098 |
| 1071 @override | 1099 @override |
| 1072 visitFieldDeclaration(FieldDeclaration node) { | 1100 visitFieldDeclaration(FieldDeclaration node) { |
| 1073 FieldDeclaration other = this.other; | 1101 FieldDeclaration other = this.other; |
| 1102 _visitDeclaration(node, other); |
| 1074 _visitNode(node.fields, other.fields); | 1103 _visitNode(node.fields, other.fields); |
| 1075 } | 1104 } |
| 1076 | 1105 |
| 1077 @override | 1106 @override |
| 1078 visitFieldFormalParameter(FieldFormalParameter node) { | 1107 visitFieldFormalParameter(FieldFormalParameter node) { |
| 1079 FieldFormalParameter other = this.other; | 1108 FieldFormalParameter other = this.other; |
| 1080 _visitNormalFormalParameter(node, other); | 1109 _visitNormalFormalParameter(node, other); |
| 1081 _visitNode(node.type, other.type); | 1110 _visitNode(node.type, other.type); |
| 1082 _visitNode(node.parameters, other.parameters); | 1111 _visitNode(node.parameters, other.parameters); |
| 1083 } | 1112 } |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1272 @override | 1301 @override |
| 1273 visitMapLiteralEntry(MapLiteralEntry node) { | 1302 visitMapLiteralEntry(MapLiteralEntry node) { |
| 1274 MapLiteralEntry other = this.other; | 1303 MapLiteralEntry other = this.other; |
| 1275 _visitNode(node.key, other.key); | 1304 _visitNode(node.key, other.key); |
| 1276 _visitNode(node.value, other.value); | 1305 _visitNode(node.value, other.value); |
| 1277 } | 1306 } |
| 1278 | 1307 |
| 1279 @override | 1308 @override |
| 1280 visitMethodDeclaration(MethodDeclaration node) { | 1309 visitMethodDeclaration(MethodDeclaration node) { |
| 1281 MethodDeclaration other = this.other; | 1310 MethodDeclaration other = this.other; |
| 1311 _visitDeclaration(node, other); |
| 1282 _visitNode(node.name, other.name); | 1312 _visitNode(node.name, other.name); |
| 1283 _visitNode(node.parameters, other.parameters); | 1313 _visitNode(node.parameters, other.parameters); |
| 1284 _visitNode(node.body, other.body); | 1314 _visitNode(node.body, other.body); |
| 1285 } | 1315 } |
| 1286 | 1316 |
| 1287 @override | 1317 @override |
| 1288 visitMethodInvocation(MethodInvocation node) { | 1318 visitMethodInvocation(MethodInvocation node) { |
| 1289 MethodInvocation other = this.other; | 1319 MethodInvocation other = this.other; |
| 1290 _visitNode(node.target, other.target); | 1320 _visitNode(node.target, other.target); |
| 1291 _visitNode(node.methodName, other.methodName); | 1321 _visitNode(node.methodName, other.methodName); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1509 | 1539 |
| 1510 @override | 1540 @override |
| 1511 visitTypeParameterList(TypeParameterList node) { | 1541 visitTypeParameterList(TypeParameterList node) { |
| 1512 TypeParameterList other = this.other; | 1542 TypeParameterList other = this.other; |
| 1513 _visitList(node.typeParameters, other.typeParameters); | 1543 _visitList(node.typeParameters, other.typeParameters); |
| 1514 } | 1544 } |
| 1515 | 1545 |
| 1516 @override | 1546 @override |
| 1517 visitVariableDeclaration(VariableDeclaration node) { | 1547 visitVariableDeclaration(VariableDeclaration node) { |
| 1518 VariableDeclaration other = this.other; | 1548 VariableDeclaration other = this.other; |
| 1549 _visitDeclaration(node, other); |
| 1519 _visitNode(node.name, other.name); | 1550 _visitNode(node.name, other.name); |
| 1520 _visitNode(node.initializer, other.initializer); | 1551 _visitNode(node.initializer, other.initializer); |
| 1521 } | 1552 } |
| 1522 | 1553 |
| 1523 @override | 1554 @override |
| 1524 visitVariableDeclarationList(VariableDeclarationList node) { | 1555 visitVariableDeclarationList(VariableDeclarationList node) { |
| 1525 VariableDeclarationList other = this.other; | 1556 VariableDeclarationList other = this.other; |
| 1526 _visitNode(node.type, other.type); | 1557 _visitNode(node.type, other.type); |
| 1527 _visitList(node.variables, other.variables); | 1558 _visitList(node.variables, other.variables); |
| 1528 } | 1559 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1552 _visitNode(node.expression, other.expression); | 1583 _visitNode(node.expression, other.expression); |
| 1553 } | 1584 } |
| 1554 | 1585 |
| 1555 void _verifyElement(Element a, Element b) { | 1586 void _verifyElement(Element a, Element b) { |
| 1556 if (a != b) { | 1587 if (a != b) { |
| 1557 fail('Expected: $b\n Actual: $a'); | 1588 fail('Expected: $b\n Actual: $a'); |
| 1558 } | 1589 } |
| 1559 if (a == null && b == null) { | 1590 if (a == null && b == null) { |
| 1560 return; | 1591 return; |
| 1561 } | 1592 } |
| 1562 // TODO(scheglov) uncomment when implement elements shifting | 1593 expect(a.nameOffset, b.nameOffset); |
| 1563 // expect(a.nameOffset, b.nameOffset); | |
| 1564 } | 1594 } |
| 1565 | 1595 |
| 1566 void _verifyType(DartType a, DartType b) { | 1596 void _verifyType(DartType a, DartType b) { |
| 1567 expect(a, equals(b)); | 1597 expect(a, equals(b)); |
| 1568 } | 1598 } |
| 1569 | 1599 |
| 1570 void _visitAnnotatedNode(AnnotatedNode node, AnnotatedNode other) { | 1600 void _visitAnnotatedNode(AnnotatedNode node, AnnotatedNode other) { |
| 1571 _visitNode(node.documentationComment, other.documentationComment); | 1601 _visitNode(node.documentationComment, other.documentationComment); |
| 1572 _visitList(node.metadata, other.metadata); | 1602 _visitList(node.metadata, other.metadata); |
| 1573 } | 1603 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1601 if (node == null) { | 1631 if (node == null) { |
| 1602 expect(other, isNull); | 1632 expect(other, isNull); |
| 1603 } else { | 1633 } else { |
| 1604 this.other = other; | 1634 this.other = other; |
| 1605 node.accept(this); | 1635 node.accept(this); |
| 1606 } | 1636 } |
| 1607 } | 1637 } |
| 1608 | 1638 |
| 1609 void _visitNormalFormalParameter(NormalFormalParameter node, | 1639 void _visitNormalFormalParameter(NormalFormalParameter node, |
| 1610 NormalFormalParameter other) { | 1640 NormalFormalParameter other) { |
| 1641 _verifyElement(node.element, other.element); |
| 1611 _visitNode(node.documentationComment, other.documentationComment); | 1642 _visitNode(node.documentationComment, other.documentationComment); |
| 1612 _visitList(node.metadata, other.metadata); | 1643 _visitList(node.metadata, other.metadata); |
| 1613 _visitNode(node.identifier, other.identifier); | 1644 _visitNode(node.identifier, other.identifier); |
| 1614 } | 1645 } |
| 1615 | 1646 |
| 1616 static void assertSameResolution(CompilationUnit actual, | 1647 static void assertSameResolution(CompilationUnit actual, |
| 1617 CompilationUnit expected) { | 1648 CompilationUnit expected) { |
| 1618 _SameResolutionValidator validator = new _SameResolutionValidator(expected); | 1649 _SameResolutionValidator validator = new _SameResolutionValidator(expected); |
| 1619 actual.accept(validator); | 1650 actual.accept(validator); |
| 1620 } | 1651 } |
| 1621 } | 1652 } |
| OLD | NEW |