| 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 10 matching lines...) Expand all Loading... |
| 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(ScopeBuilderTest); | 31 runReflectiveTests(ResolutionContextBuilderTest); |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 class DeclarationMatcherTest extends ResolverTestCase { | 35 class DeclarationMatcherTest extends ResolverTestCase { |
| 36 void fail_test_methodDeclarationMatches_false_localVariable() { | 36 void fail_test_methodDeclarationMatches_false_localVariable() { |
| 37 // TODO(scheglov) as I understand DeclarationMatcher, we care only | 37 // TODO(scheglov) as I understand DeclarationMatcher, we care only |
| 38 // about externally visible model changes. So, because we analyze (at least | 38 // about externally visible model changes. So, because we analyze (at least |
| 39 // right now) incremental changes on method level, local variable can be | 39 // right now) incremental changes on method level, local variable can be |
| 40 // ignored. | 40 // ignored. |
| 41 _assertMethodMatches(false, r''' | 41 _assertMethodMatches(false, r''' |
| (...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1093 void fail_test_constructor_fieldInitializer_add() { | 1093 void fail_test_constructor_fieldInitializer_add() { |
| 1094 // TODO(scheglov) resolver uses "enclosingClass", which we don't set yet | 1094 // TODO(scheglov) resolver uses "enclosingClass", which we don't set yet |
| 1095 _resolveUnit(r''' | 1095 _resolveUnit(r''' |
| 1096 class A { | 1096 class A { |
| 1097 int f; | 1097 int f; |
| 1098 A(int a, int b); | 1098 A(int a, int b); |
| 1099 }'''); | 1099 }'''); |
| 1100 _resolve(_editString(');', ') : f = a + b;'), _isClassMember); | 1100 _resolve(_editString(');', ') : f = a + b;'), _isClassMember); |
| 1101 } | 1101 } |
| 1102 | 1102 |
| 1103 void fail_test_constructor_fieldInitializer_edit() { | |
| 1104 // TODO(scheglov) resolver uses "enclosingClass", which we don't set yet | |
| 1105 _resolveUnit(r''' | |
| 1106 class A { | |
| 1107 int f; | |
| 1108 A(int a, int b) : f = a + b { | |
| 1109 int a = 42; | |
| 1110 } | |
| 1111 }'''); | |
| 1112 _resolve(_editString('+', '*'), _isExpression); | |
| 1113 } | |
| 1114 | |
| 1115 void fail_test_topLevelFunction_parameter_rename() { | 1103 void fail_test_topLevelFunction_parameter_rename() { |
| 1116 // TODO(scheglov) Decide if incremental parser keeps the element | 1104 // TODO(scheglov) Decide if incremental parser keeps the element |
| 1117 // of the function. If so, we can resolve parameter renames. | 1105 // of the function. If so, we can resolve parameter renames. |
| 1118 _resolveUnit(r''' | 1106 _resolveUnit(r''' |
| 1119 int main(int a, int b) { | 1107 int main(int a, int b) { |
| 1120 return a + b; | 1108 return a + b; |
| 1121 } | 1109 } |
| 1122 '''); | 1110 '''); |
| 1123 _resolve(_editString(r'''(int a, int b) { | 1111 _resolve(_editString(r'''(int a, int b) { |
| 1124 return a + b;''', r'''(int first, int b) { | 1112 return a + b;''', r'''(int first, int b) { |
| 1125 return first + b;'''), _isDeclaration); | 1113 return first + b;'''), _isDeclaration); |
| 1126 } | 1114 } |
| 1127 | 1115 |
| 1128 void test_constructor_body() { | 1116 void test_constructor_body() { |
| 1129 _resolveUnit(r''' | 1117 _resolveUnit(r''' |
| 1130 class A { | 1118 class A { |
| 1131 int f; | 1119 int f; |
| 1132 A(int a, int b) { | 1120 A(int a, int b) { |
| 1133 f = a + b; | 1121 f = a + b; |
| 1134 } | 1122 } |
| 1135 }'''); | 1123 }'''); |
| 1136 _resolve(_editString('+', '*'), _isFunctionBody); | 1124 _resolve(_editString('+', '*'), _isFunctionBody); |
| 1137 } | 1125 } |
| 1138 | 1126 |
| 1127 void test_constructor_fieldInitializer_edit() { |
| 1128 // TODO(scheglov) resolver uses "enclosingClass", which we don't set yet |
| 1129 _resolveUnit(r''' |
| 1130 class A { |
| 1131 int f; |
| 1132 A(int a, int b) : f = a + b { |
| 1133 int a = 42; |
| 1134 } |
| 1135 }'''); |
| 1136 _resolve(_editString('+', '*'), _isExpression); |
| 1137 } |
| 1138 |
| 1139 void test_constructor_superConstructorInvocation() { | 1139 void test_constructor_superConstructorInvocation() { |
| 1140 _resolveUnit(r''' | 1140 _resolveUnit(r''' |
| 1141 class A { | 1141 class A { |
| 1142 A(int p); | 1142 A(int p); |
| 1143 } | 1143 } |
| 1144 class A { | 1144 class A { |
| 1145 A(int a, int b) : super(a + b); | 1145 A(int a, int b) : super(a + b); |
| 1146 } | 1146 } |
| 1147 '''); | 1147 '''); |
| 1148 _resolve(_editString('+', '*'), _isExpression); | 1148 _resolve(_editString('+', '*'), _isExpression); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1355 var errorListener = new BooleanErrorListener(); | 1355 var errorListener = new BooleanErrorListener(); |
| 1356 var reader = new CharSequenceReader(code); | 1356 var reader = new CharSequenceReader(code); |
| 1357 var scanner = new Scanner(null, reader, errorListener); | 1357 var scanner = new Scanner(null, reader, errorListener); |
| 1358 var token = scanner.tokenize(); | 1358 var token = scanner.tokenize(); |
| 1359 var parser = new Parser(null, errorListener); | 1359 var parser = new Parser(null, errorListener); |
| 1360 return parser.parseCompilationUnit(token); | 1360 return parser.parseCompilationUnit(token); |
| 1361 } | 1361 } |
| 1362 } | 1362 } |
| 1363 | 1363 |
| 1364 | 1364 |
| 1365 class ScopeBuilderTest extends EngineTestCase { | 1365 class ResolutionContextBuilderTest extends EngineTestCase { |
| 1366 GatheringErrorListener listener = new GatheringErrorListener(); |
| 1367 |
| 1366 void test_scopeFor_ClassDeclaration() { | 1368 void test_scopeFor_ClassDeclaration() { |
| 1367 GatheringErrorListener listener = new GatheringErrorListener(); | 1369 Scope scope = _scopeFor(_createResolvedClassDeclaration()); |
| 1368 Scope scope = | |
| 1369 ScopeBuilder.scopeFor(_createResolvedClassDeclaration(), listener); | |
| 1370 EngineTestCase.assertInstanceOf( | 1370 EngineTestCase.assertInstanceOf( |
| 1371 (obj) => obj is LibraryScope, | 1371 (obj) => obj is LibraryScope, |
| 1372 LibraryScope, | 1372 LibraryScope, |
| 1373 scope); | 1373 scope); |
| 1374 } | 1374 } |
| 1375 | 1375 |
| 1376 void test_scopeFor_ClassTypeAlias() { | 1376 void test_scopeFor_ClassTypeAlias() { |
| 1377 GatheringErrorListener listener = new GatheringErrorListener(); | 1377 Scope scope = _scopeFor(_createResolvedClassTypeAlias()); |
| 1378 Scope scope = | |
| 1379 ScopeBuilder.scopeFor(_createResolvedClassTypeAlias(), listener); | |
| 1380 EngineTestCase.assertInstanceOf( | 1378 EngineTestCase.assertInstanceOf( |
| 1381 (obj) => obj is LibraryScope, | 1379 (obj) => obj is LibraryScope, |
| 1382 LibraryScope, | 1380 LibraryScope, |
| 1383 scope); | 1381 scope); |
| 1384 } | 1382 } |
| 1385 | 1383 |
| 1386 void test_scopeFor_CompilationUnit() { | 1384 void test_scopeFor_CompilationUnit() { |
| 1387 GatheringErrorListener listener = new GatheringErrorListener(); | 1385 Scope scope = _scopeFor(_createResolvedCompilationUnit()); |
| 1388 Scope scope = | |
| 1389 ScopeBuilder.scopeFor(_createResolvedCompilationUnit(), listener); | |
| 1390 EngineTestCase.assertInstanceOf( | 1386 EngineTestCase.assertInstanceOf( |
| 1391 (obj) => obj is LibraryScope, | 1387 (obj) => obj is LibraryScope, |
| 1392 LibraryScope, | 1388 LibraryScope, |
| 1393 scope); | 1389 scope); |
| 1394 } | 1390 } |
| 1395 | 1391 |
| 1396 void test_scopeFor_ConstructorDeclaration() { | 1392 void test_scopeFor_ConstructorDeclaration() { |
| 1397 GatheringErrorListener listener = new GatheringErrorListener(); | 1393 Scope scope = _scopeFor(_createResolvedConstructorDeclaration()); |
| 1398 Scope scope = | |
| 1399 ScopeBuilder.scopeFor(_createResolvedConstructorDeclaration(), listener)
; | |
| 1400 EngineTestCase.assertInstanceOf( | 1394 EngineTestCase.assertInstanceOf( |
| 1401 (obj) => obj is ClassScope, | 1395 (obj) => obj is ClassScope, |
| 1402 ClassScope, | 1396 ClassScope, |
| 1403 scope); | 1397 scope); |
| 1404 } | 1398 } |
| 1405 | 1399 |
| 1406 void test_scopeFor_ConstructorDeclaration_parameters() { | 1400 void test_scopeFor_ConstructorDeclaration_parameters() { |
| 1407 GatheringErrorListener listener = new GatheringErrorListener(); | 1401 Scope scope = _scopeFor(_createResolvedConstructorDeclaration().parameters); |
| 1408 Scope scope = ScopeBuilder.scopeFor( | |
| 1409 _createResolvedConstructorDeclaration().parameters, | |
| 1410 listener); | |
| 1411 EngineTestCase.assertInstanceOf( | 1402 EngineTestCase.assertInstanceOf( |
| 1412 (obj) => obj is FunctionScope, | 1403 (obj) => obj is FunctionScope, |
| 1413 FunctionScope, | 1404 FunctionScope, |
| 1414 scope); | 1405 scope); |
| 1415 } | 1406 } |
| 1416 | 1407 |
| 1417 void test_scopeFor_FunctionDeclaration() { | 1408 void test_scopeFor_FunctionDeclaration() { |
| 1418 GatheringErrorListener listener = new GatheringErrorListener(); | 1409 Scope scope = _scopeFor(_createResolvedFunctionDeclaration()); |
| 1419 Scope scope = | |
| 1420 ScopeBuilder.scopeFor(_createResolvedFunctionDeclaration(), listener); | |
| 1421 EngineTestCase.assertInstanceOf( | 1410 EngineTestCase.assertInstanceOf( |
| 1422 (obj) => obj is LibraryScope, | 1411 (obj) => obj is LibraryScope, |
| 1423 LibraryScope, | 1412 LibraryScope, |
| 1424 scope); | 1413 scope); |
| 1425 } | 1414 } |
| 1426 | 1415 |
| 1427 void test_scopeFor_FunctionDeclaration_parameters() { | 1416 void test_scopeFor_FunctionDeclaration_parameters() { |
| 1428 GatheringErrorListener listener = new GatheringErrorListener(); | 1417 Scope scope = |
| 1429 Scope scope = ScopeBuilder.scopeFor( | 1418 _scopeFor(_createResolvedFunctionDeclaration().functionExpression.parame
ters); |
| 1430 _createResolvedFunctionDeclaration().functionExpression.parameters, | |
| 1431 listener); | |
| 1432 EngineTestCase.assertInstanceOf( | 1419 EngineTestCase.assertInstanceOf( |
| 1433 (obj) => obj is FunctionScope, | 1420 (obj) => obj is FunctionScope, |
| 1434 FunctionScope, | 1421 FunctionScope, |
| 1435 scope); | 1422 scope); |
| 1436 } | 1423 } |
| 1437 | 1424 |
| 1438 void test_scopeFor_FunctionTypeAlias() { | 1425 void test_scopeFor_FunctionTypeAlias() { |
| 1439 GatheringErrorListener listener = new GatheringErrorListener(); | 1426 Scope scope = _scopeFor(_createResolvedFunctionTypeAlias()); |
| 1440 Scope scope = | |
| 1441 ScopeBuilder.scopeFor(_createResolvedFunctionTypeAlias(), listener); | |
| 1442 EngineTestCase.assertInstanceOf( | 1427 EngineTestCase.assertInstanceOf( |
| 1443 (obj) => obj is LibraryScope, | 1428 (obj) => obj is LibraryScope, |
| 1444 LibraryScope, | 1429 LibraryScope, |
| 1445 scope); | 1430 scope); |
| 1446 } | 1431 } |
| 1447 | 1432 |
| 1448 void test_scopeFor_FunctionTypeAlias_parameters() { | 1433 void test_scopeFor_FunctionTypeAlias_parameters() { |
| 1449 GatheringErrorListener listener = new GatheringErrorListener(); | 1434 Scope scope = _scopeFor(_createResolvedFunctionTypeAlias().parameters); |
| 1450 Scope scope = | |
| 1451 ScopeBuilder.scopeFor(_createResolvedFunctionTypeAlias().parameters, lis
tener); | |
| 1452 EngineTestCase.assertInstanceOf( | 1435 EngineTestCase.assertInstanceOf( |
| 1453 (obj) => obj is FunctionTypeScope, | 1436 (obj) => obj is FunctionTypeScope, |
| 1454 FunctionTypeScope, | 1437 FunctionTypeScope, |
| 1455 scope); | 1438 scope); |
| 1456 } | 1439 } |
| 1457 | 1440 |
| 1458 void test_scopeFor_MethodDeclaration() { | 1441 void test_scopeFor_MethodDeclaration() { |
| 1459 GatheringErrorListener listener = new GatheringErrorListener(); | 1442 Scope scope = _scopeFor(_createResolvedMethodDeclaration()); |
| 1460 Scope scope = | |
| 1461 ScopeBuilder.scopeFor(_createResolvedMethodDeclaration(), listener); | |
| 1462 EngineTestCase.assertInstanceOf( | 1443 EngineTestCase.assertInstanceOf( |
| 1463 (obj) => obj is ClassScope, | 1444 (obj) => obj is ClassScope, |
| 1464 ClassScope, | 1445 ClassScope, |
| 1465 scope); | 1446 scope); |
| 1466 } | 1447 } |
| 1467 | 1448 |
| 1468 void test_scopeFor_MethodDeclaration_body() { | 1449 void test_scopeFor_MethodDeclaration_body() { |
| 1469 GatheringErrorListener listener = new GatheringErrorListener(); | 1450 Scope scope = _scopeFor(_createResolvedMethodDeclaration().body); |
| 1470 Scope scope = | |
| 1471 ScopeBuilder.scopeFor(_createResolvedMethodDeclaration().body, listener)
; | |
| 1472 EngineTestCase.assertInstanceOf( | 1451 EngineTestCase.assertInstanceOf( |
| 1473 (obj) => obj is FunctionScope, | 1452 (obj) => obj is FunctionScope, |
| 1474 FunctionScope, | 1453 FunctionScope, |
| 1475 scope); | 1454 scope); |
| 1476 } | 1455 } |
| 1477 | 1456 |
| 1478 void test_scopeFor_notInCompilationUnit() { | 1457 void test_scopeFor_notInCompilationUnit() { |
| 1479 GatheringErrorListener listener = new GatheringErrorListener(); | |
| 1480 try { | 1458 try { |
| 1481 ScopeBuilder.scopeFor(AstFactory.identifier3("x"), listener); | 1459 _scopeFor(AstFactory.identifier3("x")); |
| 1482 fail("Expected AnalysisException"); | 1460 fail("Expected AnalysisException"); |
| 1483 } on AnalysisException catch (exception) { | 1461 } on AnalysisException catch (exception) { |
| 1484 // Expected | 1462 // Expected |
| 1485 } | 1463 } |
| 1486 } | 1464 } |
| 1487 | 1465 |
| 1488 void test_scopeFor_null() { | 1466 void test_scopeFor_null() { |
| 1489 GatheringErrorListener listener = new GatheringErrorListener(); | |
| 1490 try { | 1467 try { |
| 1491 ScopeBuilder.scopeFor(null, listener); | 1468 _scopeFor(null); |
| 1492 fail("Expected AnalysisException"); | 1469 fail("Expected AnalysisException"); |
| 1493 } on AnalysisException catch (exception) { | 1470 } on AnalysisException catch (exception) { |
| 1494 // Expected | 1471 // Expected |
| 1495 } | 1472 } |
| 1496 } | 1473 } |
| 1497 | 1474 |
| 1498 void test_scopeFor_unresolved() { | 1475 void test_scopeFor_unresolved() { |
| 1499 GatheringErrorListener listener = new GatheringErrorListener(); | |
| 1500 try { | 1476 try { |
| 1501 ScopeBuilder.scopeFor(AstFactory.compilationUnit(), listener); | 1477 _scopeFor(AstFactory.compilationUnit()); |
| 1502 fail("Expected AnalysisException"); | 1478 fail("Expected AnalysisException"); |
| 1503 } on AnalysisException catch (exception) { | 1479 } on AnalysisException catch (exception) { |
| 1504 // Expected | 1480 // Expected |
| 1505 } | 1481 } |
| 1506 } | 1482 } |
| 1507 | 1483 |
| 1508 ClassDeclaration _createResolvedClassDeclaration() { | 1484 ClassDeclaration _createResolvedClassDeclaration() { |
| 1509 CompilationUnit unit = _createResolvedCompilationUnit(); | 1485 CompilationUnit unit = _createResolvedCompilationUnit(); |
| 1510 String className = "C"; | 1486 String className = "C"; |
| 1511 ClassDeclaration classNode = AstFactory.classDeclaration( | 1487 ClassDeclaration classNode = AstFactory.classDeclaration( |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1611 AstFactory.identifier3(methodName), | 1587 AstFactory.identifier3(methodName), |
| 1612 AstFactory.formalParameterList()); | 1588 AstFactory.formalParameterList()); |
| 1613 classNode.members.add(methodNode); | 1589 classNode.members.add(methodNode); |
| 1614 MethodElement methodElement = | 1590 MethodElement methodElement = |
| 1615 ElementFactory.methodElement(methodName, null); | 1591 ElementFactory.methodElement(methodName, null); |
| 1616 methodNode.name.staticElement = methodElement; | 1592 methodNode.name.staticElement = methodElement; |
| 1617 (classNode.element as ClassElementImpl).methods = | 1593 (classNode.element as ClassElementImpl).methods = |
| 1618 <MethodElement>[methodElement]; | 1594 <MethodElement>[methodElement]; |
| 1619 return methodNode; | 1595 return methodNode; |
| 1620 } | 1596 } |
| 1597 |
| 1598 Scope _scopeFor(AstNode node) { |
| 1599 return ResolutionContextBuilder.contextFor(node, listener).scope; |
| 1600 } |
| 1621 } | 1601 } |
| 1622 | 1602 |
| 1623 | 1603 |
| 1624 class _Edit { | 1604 class _Edit { |
| 1625 final int offset; | 1605 final int offset; |
| 1626 final int length; | 1606 final int length; |
| 1627 final String replacement; | 1607 final String replacement; |
| 1628 _Edit(this.offset, this.length, this.replacement); | 1608 _Edit(this.offset, this.length, this.replacement); |
| 1629 } | 1609 } |
| 1630 | 1610 |
| (...skipping 808 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2439 _visitList(node.metadata, other.metadata); | 2419 _visitList(node.metadata, other.metadata); |
| 2440 _visitNode(node.identifier, other.identifier); | 2420 _visitNode(node.identifier, other.identifier); |
| 2441 } | 2421 } |
| 2442 | 2422 |
| 2443 static void assertSameResolution(CompilationUnit actual, | 2423 static void assertSameResolution(CompilationUnit actual, |
| 2444 CompilationUnit expected) { | 2424 CompilationUnit expected) { |
| 2445 _SameResolutionValidator validator = new _SameResolutionValidator(expected); | 2425 _SameResolutionValidator validator = new _SameResolutionValidator(expected); |
| 2446 actual.accept(validator); | 2426 actual.accept(validator); |
| 2447 } | 2427 } |
| 2448 } | 2428 } |
| OLD | NEW |