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

Side by Side Diff: pkg/analyzer2dart/test/identifier_semantics_test.dart

Issue 816773002: Add AccessSemantics support for calling typenames. (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
« no previous file with comments | « pkg/analyzer2dart/lib/src/identifier_semantics.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 import 'package:analyzer/analyzer.dart';
6 import 'package:analyzer/file_system/file_system.dart';
7 import 'package:analyzer/file_system/memory_file_system.dart';
8 import 'package:analyzer/src/generated/element.dart';
9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/sdk.dart';
11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:analyzer/src/generated/source_io.dart';
13 import 'package:analyzer2dart/src/identifier_semantics.dart';
5 import 'package:unittest/unittest.dart'; 14 import 'package:unittest/unittest.dart';
6 import 'package:analyzer/file_system/memory_file_system.dart'; 15
7 import 'mock_sdk.dart'; 16 import 'mock_sdk.dart';
8 import 'package:analyzer/src/generated/sdk.dart';
9 import 'package:analyzer/file_system/file_system.dart';
10 import 'package:analyzer/src/generated/source.dart';
11 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/element.dart';
13 import 'package:analyzer/src/generated/source_io.dart';
14 import 'package:analyzer/analyzer.dart';
15 import 'package:analyzer2dart/src/identifier_semantics.dart';
16 17
17 main() { 18 main() {
18 test('Call function defined at top level', () { 19 test('Call function defined at top level', () {
19 Helper helper = new Helper(''' 20 Helper helper = new Helper('''
20 g() {} 21 g() {}
21 22
22 f() { 23 f() {
23 g(); 24 g();
24 } 25 }
25 '''); 26 ''');
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 Helper helper = new Helper(''' 559 Helper helper = new Helper('''
559 h() => null; 560 h() => null;
560 561
561 f() { 562 f() {
562 return h().x(); 563 return h().x();
563 } 564 }
564 '''); 565 ''');
565 helper.checkDynamic('h().x()', 'h()', 'x', isInvoke: true); 566 helper.checkDynamic('h().x()', 'h()', 'x', isInvoke: true);
566 }); 567 });
567 568
569 test('Call class defined at top level', () {
570 Helper helper = new Helper('''
571 class A {}
572
573 f() {
574 A();
575 }
576 ''');
577 helper.checkTypeReference(
578 'A()',
579 'A',
580 AccessKind.TOPLEVEL_TYPE,
581 isInvoke: true);
582 });
583
584 test('Call class defined at top level via prefix', () {
585 Helper helper = new Helper('''
586 import 'lib.dart' as l;
587
588 f() {
589 l.A();
590 }
591 ''');
592 helper.addFile('/lib.dart', '''
593 library lib;
594
595 class A;
596 ''');
597 helper.checkTypeReference(
598 'l.A()',
599 'A',
600 AccessKind.TOPLEVEL_TYPE,
601 isInvoke: true);
602 });
603
604 test('Call dynamic type undefined at toplevel', () {
605 Helper helper = new Helper('''
606 f() {
607 dynamic();
608 }
609 ''');
610 // Since it is legal to define a toplevel function or a class member called
611 // dynamic, "dynamic()" must be treated as a dynamic access to a function
612 // called "dynamic".
613 helper.checkDynamic('dynamic()', null, 'dynamic', isInvoke: true);
614 });
615
616 test('Call function typedef defined at top level', () {
617 Helper helper = new Helper('''
618 typedef F();
619
620 f() {
621 F();
622 }
623 ''');
624 helper.checkTypeReference(
625 'F()',
626 'F',
627 AccessKind.TOPLEVEL_TYPE,
628 isInvoke: true);
629 });
630
631 test('Call function typedef defined at top level via prefix', () {
632 Helper helper = new Helper('''
633 import 'lib.dart' as l;
634
635 f() {
636 l.F();
637 }
638 ''');
639 helper.addFile('/lib.dart', '''
640 library lib;
641
642 typedef F();
643 ''');
644 helper.checkTypeReference(
645 'l.F()',
646 'F',
647 AccessKind.TOPLEVEL_TYPE,
648 isInvoke: true);
649 });
650
651 test('Call mixin application defined at top level', () {
652 Helper helper = new Helper('''
653 class A {}
654 class B {}
655 class C = A with B;
656
657 f() {
658 C();
659 }
660 ''');
661 helper.checkTypeReference(
662 'C()',
663 'C',
664 AccessKind.TOPLEVEL_TYPE,
665 isInvoke: true);
666 });
667
668 test('Call mixin application defined at top level via prefix', () {
669 Helper helper = new Helper('''
670 import 'lib.dart' as l;
671
672 f() {
673 l.C();
674 }
675 ''');
676 helper.addFile('/lib.dart', '''
677 library lib;
678
679 class A;
scheglov 2014/12/18 21:19:35 class A {}
Paul Berry 2014/12/18 21:56:58 Done.
680 class B;
681 class C = A with B;
682 ''');
683 helper.checkTypeReference(
684 'l.C()',
685 'C',
686 AccessKind.TOPLEVEL_TYPE,
687 isInvoke: true);
688 });
689
690 test('Call type parameter of enclosing class', () {
691 Helper helper = new Helper('''
692 class A<T, U> {
693 f() {
694 U();
695 }
696 }
697 ''');
698 helper.checkTypeReference(
699 'U()',
700 'U',
701 AccessKind.TYPE_PARAMETER,
702 isInvoke: true);
703 });
704
568 test('Get function defined at top level', () { 705 test('Get function defined at top level', () {
569 Helper helper = new Helper(''' 706 Helper helper = new Helper('''
570 g() {} 707 g() {}
571 708
572 f() { 709 f() {
573 return g; 710 return g;
574 } 711 }
575 '''); 712 ''');
576 helper.checkStaticMethod('g', null, 'g', true, isRead: true); 713 helper.checkStaticMethod('g', null, 'g', true, isRead: true);
577 }); 714 });
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 } 1227 }
1091 '''); 1228 ''');
1092 helper.checkDynamic('h().x', 'h()', 'x', isRead: true); 1229 helper.checkDynamic('h().x', 'h()', 'x', isRead: true);
1093 }); 1230 });
1094 1231
1095 test('Get class defined at top level', () { 1232 test('Get class defined at top level', () {
1096 Helper helper = new Helper(''' 1233 Helper helper = new Helper('''
1097 class A {} 1234 class A {}
1098 var t = A; 1235 var t = A;
1099 '''); 1236 ''');
1100 helper.checkTypeReference('A', 'A', AccessKind.TOPLEVEL_TYPE); 1237 helper.checkTypeReference('A', 'A', AccessKind.TOPLEVEL_TYPE, isRead: true);
1101 }); 1238 });
1102 1239
1103 test('Get class defined at top level via prefix', () { 1240 test('Get class defined at top level via prefix', () {
1104 Helper helper = new Helper(''' 1241 Helper helper = new Helper('''
1105 import 'lib.dart' as l; 1242 import 'lib.dart' as l;
1106 1243
1107 var t = l.A; 1244 var t = l.A;
1108 '''); 1245 ''');
1109 helper.addFile('/lib.dart', ''' 1246 helper.addFile('/lib.dart', '''
1110 library lib; 1247 library lib;
1111 1248
1112 class A; 1249 class A;
scheglov 2014/12/18 21:19:35 class A {}
Paul Berry 2014/12/18 21:56:58 Done.
1113 '''); 1250 ''');
1114 helper.checkTypeReference('l.A', 'A', AccessKind.TOPLEVEL_TYPE); 1251 helper.checkTypeReference(
1252 'l.A',
1253 'A',
1254 AccessKind.TOPLEVEL_TYPE,
1255 isRead: true);
1115 }); 1256 });
1116 1257
1117 test('Get dynamic type', () { 1258 test('Get dynamic type', () {
1118 Helper helper = new Helper(''' 1259 Helper helper = new Helper('''
1119 var t = dynamic; 1260 var t = dynamic;
1120 '''); 1261 ''');
1121 helper.checkTypeReference('dynamic', 'dynamic', AccessKind.TOPLEVEL_TYPE); 1262 helper.checkTypeReference(
1263 'dynamic',
1264 'dynamic',
1265 AccessKind.TOPLEVEL_TYPE,
1266 isRead: true);
1122 }); 1267 });
1123 1268
1124 test('Get function typedef defined at top level', () { 1269 test('Get function typedef defined at top level', () {
1125 Helper helper = new Helper(''' 1270 Helper helper = new Helper('''
1126 typedef F(); 1271 typedef F();
1127 var t = F; 1272 var t = F;
1128 '''); 1273 ''');
1129 helper.checkTypeReference('F', 'F', AccessKind.TOPLEVEL_TYPE); 1274 helper.checkTypeReference('F', 'F', AccessKind.TOPLEVEL_TYPE, isRead: true);
1130 }); 1275 });
1131 1276
1132 test('Get function typedef defined at top level via prefix', () { 1277 test('Get function typedef defined at top level via prefix', () {
1133 Helper helper = new Helper(''' 1278 Helper helper = new Helper('''
1134 import 'lib.dart' as l; 1279 import 'lib.dart' as l;
1135 1280
1136 var t = l.F; 1281 var t = l.F;
1137 '''); 1282 ''');
1138 helper.addFile('/lib.dart', ''' 1283 helper.addFile('/lib.dart', '''
1139 library lib; 1284 library lib;
1140 1285
1141 typedef F(); 1286 typedef F();
1142 '''); 1287 ''');
1143 helper.checkTypeReference('l.F', 'F', AccessKind.TOPLEVEL_TYPE); 1288 helper.checkTypeReference(
1289 'l.F',
1290 'F',
1291 AccessKind.TOPLEVEL_TYPE,
1292 isRead: true);
1144 }); 1293 });
1145 1294
1146 test('Get mixin application defined at top level', () { 1295 test('Get mixin application defined at top level', () {
1147 Helper helper = new Helper(''' 1296 Helper helper = new Helper('''
1148 class A {} 1297 class A {}
1149 class B {} 1298 class B {}
1150 class C = A with B; 1299 class C = A with B;
1151 var t = C; 1300 var t = C;
1152 '''); 1301 ''');
1153 helper.checkTypeReference('C', 'C', AccessKind.TOPLEVEL_TYPE); 1302 helper.checkTypeReference('C', 'C', AccessKind.TOPLEVEL_TYPE, isRead: true);
1154 }); 1303 });
1155 1304
1156 test('Get mixin application defined at top level via prefix', () { 1305 test('Get mixin application defined at top level via prefix', () {
1157 Helper helper = new Helper(''' 1306 Helper helper = new Helper('''
1158 import 'lib.dart' as l; 1307 import 'lib.dart' as l;
1159 1308
1160 var t = l.C; 1309 var t = l.C;
1161 '''); 1310 ''');
1162 helper.addFile('/lib.dart', ''' 1311 helper.addFile('/lib.dart', '''
1163 library lib; 1312 library lib;
1164 1313
1165 class A; 1314 class A;
1166 class B; 1315 class B;
1167 class C = A with B; 1316 class C = A with B;
1168 '''); 1317 ''');
1169 helper.checkTypeReference('l.C', 'C', AccessKind.TOPLEVEL_TYPE); 1318 helper.checkTypeReference(
1319 'l.C',
1320 'C',
1321 AccessKind.TOPLEVEL_TYPE,
1322 isRead: true);
1170 }); 1323 });
1171 1324
1172 test('Get type parameter of enclosing class', () { 1325 test('Get type parameter of enclosing class', () {
1173 Helper helper = new Helper(''' 1326 Helper helper = new Helper('''
1174 class A<T, U> { 1327 class A<T, U> {
1175 f() { 1328 f() {
1176 var t = U; 1329 var t = U;
1177 } 1330 }
1178 } 1331 }
1179 '''); 1332 ''');
1180 helper.checkTypeReference('U', 'U', AccessKind.TYPE_PARAMETER); 1333 helper.checkTypeReference(
1334 'U',
1335 'U',
1336 AccessKind.TYPE_PARAMETER,
1337 isRead: true);
1181 }); 1338 });
1182 1339
1183 test('Set variable defined at top level', () { 1340 test('Set variable defined at top level', () {
1184 Helper helper = new Helper(''' 1341 Helper helper = new Helper('''
1185 var x; 1342 var x;
1186 1343
1187 f() { 1344 f() {
1188 x = 1; 1345 x = 1;
1189 } 1346 }
1190 '''); 1347 ''');
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 static var x; 1381 static var x;
1225 1382
1226 f() { 1383 f() {
1227 x = 1; 1384 x = 1;
1228 } 1385 }
1229 } 1386 }
1230 '''); 1387 ''');
1231 helper.checkStaticField('x', 'A', 'x', isWrite: true); 1388 helper.checkStaticField('x', 'A', 'x', isWrite: true);
1232 }); 1389 });
1233 1390
1234 test('Set field defined statically in class from inside class in foreach' + 1391 test(
1235 ' loop', () { 1392 'Set field defined statically in class from inside class in foreach' + ' l oop',
1393 () {
1236 Helper helper = new Helper(''' 1394 Helper helper = new Helper('''
1237 class A { 1395 class A {
1238 static var x; 1396 static var x;
1239 1397
1240 f() { 1398 f() {
1241 for (x in []) {} 1399 for (x in []) {}
1242 } 1400 }
1243 } 1401 }
1244 '''); 1402 ''');
1245 helper.checkStaticField('x', 'A', 'x', isWrite: true); 1403 helper.checkStaticField('x', 'A', 'x', isWrite: true);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 var x; 1442 var x;
1285 1443
1286 f() { 1444 f() {
1287 x = 1; 1445 x = 1;
1288 } 1446 }
1289 } 1447 }
1290 '''); 1448 ''');
1291 helper.checkDynamic('x', null, 'x', isWrite: true); 1449 helper.checkDynamic('x', null, 'x', isWrite: true);
1292 }); 1450 });
1293 1451
1294 test('Set field defined dynamically in class from inside class in foreach' + 1452 test(
1295 ' loop', () { 1453 'Set field defined dynamically in class from inside class in foreach' + ' loop',
1454 () {
1296 Helper helper = new Helper(''' 1455 Helper helper = new Helper('''
1297 class A { 1456 class A {
1298 var x; 1457 var x;
1299 1458
1300 f() { 1459 f() {
1301 for (x in []) {} 1460 for (x in []) {}
1302 } 1461 }
1303 } 1462 }
1304 '''); 1463 ''');
1305 helper.checkDynamic('x', null, 'x', isWrite: true); 1464 helper.checkDynamic('x', null, 'x', isWrite: true);
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1419 static set x(value) {} 1578 static set x(value) {}
1420 1579
1421 f() { 1580 f() {
1422 x = 1; 1581 x = 1;
1423 } 1582 }
1424 } 1583 }
1425 '''); 1584 ''');
1426 helper.checkStaticProperty('x', 'A', 'x', true, isWrite: true); 1585 helper.checkStaticProperty('x', 'A', 'x', true, isWrite: true);
1427 }); 1586 });
1428 1587
1429 test('Set accessor defined statically in class from inside class in' + 1588 test(
1430 ' foreach loop', () { 1589 'Set accessor defined statically in class from inside class in' +
1590 ' foreach loop',
1591 () {
1431 Helper helper = new Helper(''' 1592 Helper helper = new Helper('''
1432 class A { 1593 class A {
1433 static set x(value) {} 1594 static set x(value) {}
1434 1595
1435 f() { 1596 f() {
1436 for (x in []) {} 1597 for (x in []) {}
1437 } 1598 }
1438 } 1599 }
1439 '''); 1600 ''');
1440 helper.checkStaticProperty('x', 'A', 'x', true, isWrite: true); 1601 helper.checkStaticProperty('x', 'A', 'x', true, isWrite: true);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 set x(value) {} 1640 set x(value) {}
1480 1641
1481 f() { 1642 f() {
1482 x = 1; 1643 x = 1;
1483 } 1644 }
1484 } 1645 }
1485 '''); 1646 ''');
1486 helper.checkDynamic('x', null, 'x', isWrite: true); 1647 helper.checkDynamic('x', null, 'x', isWrite: true);
1487 }); 1648 });
1488 1649
1489 test('Set accessor defined dynamically in class from inside class in' + 1650 test(
1490 ' foreach loop', () { 1651 'Set accessor defined dynamically in class from inside class in' +
1652 ' foreach loop',
1653 () {
1491 Helper helper = new Helper(''' 1654 Helper helper = new Helper('''
1492 class A { 1655 class A {
1493 set x(value) {} 1656 set x(value) {}
1494 1657
1495 f() { 1658 f() {
1496 for (x in []) {} 1659 for (x in []) {}
1497 } 1660 }
1498 } 1661 }
1499 '''); 1662 ''');
1500 helper.checkDynamic('x', null, 'x', isWrite: true); 1663 helper.checkDynamic('x', null, 'x', isWrite: true);
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1621 Helper helper = new Helper(''' 1784 Helper helper = new Helper('''
1622 class A { 1785 class A {
1623 f() { 1786 f() {
1624 x = 1; 1787 x = 1;
1625 } 1788 }
1626 } 1789 }
1627 '''); 1790 ''');
1628 helper.checkDynamic('x', null, 'x', isWrite: true); 1791 helper.checkDynamic('x', null, 'x', isWrite: true);
1629 }); 1792 });
1630 1793
1631 test('Set accessor undefined dynamically in class from inside class in' + 1794 test(
1632 ' foreach loop', () { 1795 'Set accessor undefined dynamically in class from inside class in' +
1796 ' foreach loop',
1797 () {
1633 Helper helper = new Helper(''' 1798 Helper helper = new Helper('''
1634 class A { 1799 class A {
1635 f() { 1800 f() {
1636 for (x in []) {} 1801 for (x in []) {}
1637 } 1802 }
1638 } 1803 }
1639 '''); 1804 ''');
1640 helper.checkDynamic('x', null, 'x', isWrite: true); 1805 helper.checkDynamic('x', null, 'x', isWrite: true);
1641 }); 1806 });
1642 1807
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2074 A h() => null; 2239 A h() => null;
2075 2240
2076 f() { 2241 f() {
2077 h().x += 1; 2242 h().x += 1;
2078 } 2243 }
2079 '''); 2244 ''');
2080 helper.checkDynamic('h().x', 'h()', 'x', isRead: true, isWrite: true); 2245 helper.checkDynamic('h().x', 'h()', 'x', isRead: true, isWrite: true);
2081 }); 2246 });
2082 } 2247 }
2083 2248
2249 typedef void AccessHandler(Expression node, AccessSemantics semantics);
2250
2084 class Helper { 2251 class Helper {
2085 final MemoryResourceProvider provider = new MemoryResourceProvider(); 2252 final MemoryResourceProvider provider = new MemoryResourceProvider();
2086 Source rootSource; 2253 Source rootSource;
2087 AnalysisContext context; 2254 AnalysisContext context;
2088 2255
2089 Helper(String rootContents) { 2256 Helper(String rootContents) {
2090 DartSdk sdk = new MockSdk(); 2257 DartSdk sdk = new MockSdk();
2091 String rootFile = '/root.dart'; 2258 String rootFile = '/root.dart';
2092 File file = provider.newFile(rootFile, rootContents); 2259 File file = provider.newFile(rootFile, rootContents);
2093 rootSource = file.createSource(); 2260 rootSource = file.createSource();
2094 context = AnalysisEngine.instance.createAnalysisContext(); 2261 context = AnalysisEngine.instance.createAnalysisContext();
2095 // Set up the source factory. 2262 // Set up the source factory.
2096 List<UriResolver> uriResolvers = [ 2263 List<UriResolver> uriResolvers = [
2097 new ResourceUriResolver(provider), 2264 new ResourceUriResolver(provider),
2098 new DartUriResolver(sdk)]; 2265 new DartUriResolver(sdk)];
2099 context.sourceFactory = new SourceFactory(uriResolvers); 2266 context.sourceFactory = new SourceFactory(uriResolvers);
2100 // add the Source 2267 // add the Source
2101 ChangeSet changeSet = new ChangeSet(); 2268 ChangeSet changeSet = new ChangeSet();
2102 changeSet.addedSource(rootSource); 2269 changeSet.addedSource(rootSource);
2103 context.applyChanges(changeSet); 2270 context.applyChanges(changeSet);
2104 } 2271 }
2105 2272
2273 LibraryElement get libraryElement {
2274 return context.computeLibraryElement(rootSource);
2275 }
2276
2106 void addFile(String path, String contents) { 2277 void addFile(String path, String contents) {
2107 provider.newFile(path, contents); 2278 provider.newFile(path, contents);
2108 } 2279 }
2109 2280
2110 LibraryElement get libraryElement {
2111 return context.computeLibraryElement(rootSource);
2112 }
2113
2114 /** 2281 /**
2115 * Verify that the node represented by [expectedSource] is classified as 2282 * Verify that the node represented by [expectedSource] is classified as
2116 * a static field element reference. 2283 * a dynamic method invocation.
2117 */ 2284 */
2118 void checkStaticField(String expectedSource, String expectedClass, 2285 void checkDynamic(String expectedSource, String expectedTarget,
2119 String expectedName, {bool isRead: false, bool isWrite: false, bool isInvo ke: 2286 String expectedName, {bool isRead: false, bool isWrite: false, bool isInvo ke:
2120 false}) { 2287 false}) {
2121 TestVisitor visitor = new TestVisitor(); 2288 TestVisitor visitor = new TestVisitor();
2122 int count = 0; 2289 int count = 0;
2123 visitor.onAccess = (Expression node, AccessSemantics semantics) { 2290 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2124 count++; 2291 count++;
2125 expect(node.toSource(), equals(expectedSource)); 2292 expect(node.toSource(), equals(expectedSource));
2126 expect(semantics.kind, equals(AccessKind.STATIC_FIELD)); 2293 expect(semantics.kind, equals(AccessKind.DYNAMIC));
2294 if (expectedTarget == null) {
2295 expect(semantics.target, isNull);
2296 } else {
2297 expect(semantics.target.toSource(), equals(expectedTarget));
2298 }
2127 expect(semantics.identifier.name, equals(expectedName)); 2299 expect(semantics.identifier.name, equals(expectedName));
2128 expect(semantics.element.displayName, equals(expectedName)); 2300 expect(semantics.element, isNull);
2129 if (expectedClass == null) { 2301 expect(semantics.classElement, isNull);
2130 expect(semantics.classElement, isNull);
2131 } else {
2132 expect(semantics.classElement, isNotNull);
2133 expect(semantics.classElement.displayName, equals(expectedClass));
2134 }
2135 expect(semantics.target, isNull);
2136 expect(semantics.isRead, equals(isRead)); 2302 expect(semantics.isRead, equals(isRead));
2137 expect(semantics.isWrite, equals(isWrite)); 2303 expect(semantics.isWrite, equals(isWrite));
2138 expect(semantics.isInvoke, equals(isInvoke)); 2304 expect(semantics.isInvoke, equals(isInvoke));
2139 }; 2305 };
2140 libraryElement.unit.accept(visitor); 2306 libraryElement.unit.accept(visitor);
2141 expect(count, equals(1)); 2307 expect(count, equals(1));
2142 } 2308 }
2143 2309
2144 /** 2310 /**
2145 * Verify that the node represented by [expectedSource] is classified as 2311 * Verify that the node represented by [expectedSource] is classified as
2146 * a static property access. 2312 * a local function invocation.
2147 */ 2313 */
2148 void checkStaticProperty(String expectedSource, String expectedClass, 2314 void checkLocalFunction(String expectedSource, String expectedName,
2149 String expectedName, bool defined, {bool isRead: false, bool isWrite: fals e, 2315 {bool isRead: false, bool isWrite: false, bool isInvoke: false}) {
2150 bool isInvoke: false}) {
2151 TestVisitor visitor = new TestVisitor(); 2316 TestVisitor visitor = new TestVisitor();
2152 int count = 0; 2317 int count = 0;
2153 visitor.onAccess = (Expression node, AccessSemantics semantics) { 2318 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2154 count++; 2319 count++;
2155 expect(node.toSource(), equals(expectedSource)); 2320 expect(node.toSource(), equals(expectedSource));
2156 expect(semantics.kind, equals(AccessKind.STATIC_PROPERTY)); 2321 expect(semantics.kind, equals(AccessKind.LOCAL_FUNCTION));
2157 expect(semantics.identifier.name, equals(expectedName)); 2322 expect(semantics.identifier.name, equals(expectedName));
2158 if (expectedClass == null) { 2323 expect(semantics.element.displayName, equals(expectedName));
2159 expect(semantics.classElement, isNull); 2324 expect(semantics.classElement, isNull);
2160 } else {
2161 expect(semantics.classElement, isNotNull);
2162 expect(semantics.classElement.displayName, equals(expectedClass));
2163 }
2164 if (defined) {
2165 expect(semantics.element.displayName, equals(expectedName));
2166 } else {
2167 expect(semantics.element, isNull);
2168 }
2169 expect(semantics.target, isNull); 2325 expect(semantics.target, isNull);
2170 expect(semantics.isRead, equals(isRead)); 2326 expect(semantics.isRead, equals(isRead));
2171 expect(semantics.isWrite, equals(isWrite)); 2327 expect(semantics.isWrite, equals(isWrite));
2172 expect(semantics.isInvoke, equals(isInvoke)); 2328 expect(semantics.isInvoke, equals(isInvoke));
2173 }; 2329 };
2174 libraryElement.unit.accept(visitor); 2330 libraryElement.unit.accept(visitor);
2175 expect(count, equals(1)); 2331 expect(count, equals(1));
2176 } 2332 }
2177 2333
2178 /** 2334 /**
2179 * Verify that the node represented by [expectedSource] is classified as 2335 * Verify that the node represented by [expectedSource] is classified as
2180 * a static method. 2336 * a local variable access.
2181 */ 2337 */
2182 void checkStaticMethod(String expectedSource, String expectedClass, 2338 void checkLocalVariable(String expectedSource, String expectedName,
2183 String expectedName, bool defined, {bool isRead: false, bool isWrite: fals e, 2339 {bool isRead: false, bool isWrite: false, bool isInvoke: false}) {
2184 bool isInvoke: false}) {
2185 TestVisitor visitor = new TestVisitor(); 2340 TestVisitor visitor = new TestVisitor();
2186 int count = 0; 2341 int count = 0;
2187 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2342 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2188 count++; 2343 count++;
2189 expect(node.toSource(), equals(expectedSource)); 2344 expect(node.toSource(), equals(expectedSource));
2190 expect(semantics.kind, equals(AccessKind.STATIC_METHOD)); 2345 expect(semantics.kind, equals(AccessKind.LOCAL_VARIABLE));
2191 expect(semantics.identifier.name, equals(expectedName)); 2346 expect(semantics.element.name, equals(expectedName));
2192 if (expectedClass == null) { 2347 expect(semantics.classElement, isNull);
2193 expect(semantics.classElement, isNull);
2194 if (defined) {
2195 expect(semantics.element, new isInstanceOf<FunctionElement>());
2196 }
2197 } else {
2198 expect(semantics.classElement, isNotNull);
2199 expect(semantics.classElement.displayName, equals(expectedClass));
2200 if (defined) {
2201 expect(semantics.element, new isInstanceOf<MethodElement>());
2202 }
2203 }
2204 if (defined) {
2205 expect(semantics.element.displayName, equals(expectedName));
2206 } else {
2207 expect(semantics.element, isNull);
2208 }
2209 expect(semantics.target, isNull); 2348 expect(semantics.target, isNull);
2210 expect(semantics.isRead, equals(isRead)); 2349 expect(semantics.isRead, equals(isRead));
2211 expect(semantics.isWrite, equals(isWrite)); 2350 expect(semantics.isWrite, equals(isWrite));
2212 expect(semantics.isInvoke, equals(isInvoke)); 2351 expect(semantics.isInvoke, equals(isInvoke));
2213 }; 2352 };
2214 libraryElement.unit.accept(visitor); 2353 libraryElement.unit.accept(visitor);
2215 expect(count, equals(1)); 2354 expect(count, equals(1));
2216 } 2355 }
2217 2356
2218 /** 2357 /**
2219 * Verify that the node represented by [expectedSource] is classified as 2358 * Verify that the node represented by [expectedSource] is classified as a
2220 * a dynamic method invocation. 2359 * parameter access.
2221 */ 2360 */
2222 void checkDynamic(String expectedSource, String expectedTarget, 2361 void checkParameter(String expectedSource, String expectedName, {bool isRead:
2223 String expectedName, {bool isRead: false, bool isWrite: false, bool isInvo ke: 2362 false, bool isWrite: false, bool isInvoke: false}) {
2224 false}) {
2225 TestVisitor visitor = new TestVisitor(); 2363 TestVisitor visitor = new TestVisitor();
2226 int count = 0; 2364 int count = 0;
2227 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2365 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2228 count++; 2366 count++;
2229 expect(node.toSource(), equals(expectedSource)); 2367 expect(node.toSource(), equals(expectedSource));
2230 expect(semantics.kind, equals(AccessKind.DYNAMIC)); 2368 expect(semantics.kind, equals(AccessKind.PARAMETER));
2231 if (expectedTarget == null) { 2369 expect(semantics.element.name, equals(expectedName));
2232 expect(semantics.target, isNull);
2233 } else {
2234 expect(semantics.target.toSource(), equals(expectedTarget));
2235 }
2236 expect(semantics.identifier.name, equals(expectedName));
2237 expect(semantics.element, isNull);
2238 expect(semantics.classElement, isNull); 2370 expect(semantics.classElement, isNull);
2371 expect(semantics.target, isNull);
2239 expect(semantics.isRead, equals(isRead)); 2372 expect(semantics.isRead, equals(isRead));
2240 expect(semantics.isWrite, equals(isWrite)); 2373 expect(semantics.isWrite, equals(isWrite));
2241 expect(semantics.isInvoke, equals(isInvoke)); 2374 expect(semantics.isInvoke, equals(isInvoke));
2242 }; 2375 };
2243 libraryElement.unit.accept(visitor); 2376 libraryElement.unit.accept(visitor);
2244 expect(count, equals(1)); 2377 expect(count, equals(1));
2245 } 2378 }
2246 2379
2247 /** 2380 /**
2248 * Verify that the node represented by [expectedSource] is classified as 2381 * Verify that the node represented by [expectedSource] is classified as
2249 * a local function invocation. 2382 * a static field element reference.
2250 */ 2383 */
2251 void checkLocalFunction(String expectedSource, String expectedName, 2384 void checkStaticField(String expectedSource, String expectedClass,
2252 {bool isRead: false, bool isWrite: false, bool isInvoke: false}) { 2385 String expectedName, {bool isRead: false, bool isWrite: false, bool isInvo ke:
2386 false}) {
2253 TestVisitor visitor = new TestVisitor(); 2387 TestVisitor visitor = new TestVisitor();
2254 int count = 0; 2388 int count = 0;
2255 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2389 visitor.onAccess = (Expression node, AccessSemantics semantics) {
2256 count++; 2390 count++;
2257 expect(node.toSource(), equals(expectedSource)); 2391 expect(node.toSource(), equals(expectedSource));
2258 expect(semantics.kind, equals(AccessKind.LOCAL_FUNCTION)); 2392 expect(semantics.kind, equals(AccessKind.STATIC_FIELD));
2259 expect(semantics.identifier.name, equals(expectedName)); 2393 expect(semantics.identifier.name, equals(expectedName));
2260 expect(semantics.element.displayName, equals(expectedName)); 2394 expect(semantics.element.displayName, equals(expectedName));
2261 expect(semantics.classElement, isNull); 2395 if (expectedClass == null) {
2396 expect(semantics.classElement, isNull);
2397 } else {
2398 expect(semantics.classElement, isNotNull);
2399 expect(semantics.classElement.displayName, equals(expectedClass));
2400 }
2262 expect(semantics.target, isNull); 2401 expect(semantics.target, isNull);
2263 expect(semantics.isRead, equals(isRead)); 2402 expect(semantics.isRead, equals(isRead));
2264 expect(semantics.isWrite, equals(isWrite)); 2403 expect(semantics.isWrite, equals(isWrite));
2265 expect(semantics.isInvoke, equals(isInvoke)); 2404 expect(semantics.isInvoke, equals(isInvoke));
2266 }; 2405 };
2267 libraryElement.unit.accept(visitor); 2406 libraryElement.unit.accept(visitor);
2268 expect(count, equals(1)); 2407 expect(count, equals(1));
2269 } 2408 }
2270 2409
2271 /** 2410 /**
2272 * Verify that the node represented by [expectedSource] is classified as 2411 * Verify that the node represented by [expectedSource] is classified as
2273 * a local variable access. 2412 * a static method.
2274 */ 2413 */
2275 void checkLocalVariable(String expectedSource, String expectedName, 2414 void checkStaticMethod(String expectedSource, String expectedClass,
2276 {bool isRead: false, bool isWrite: false, bool isInvoke: false}) { 2415 String expectedName, bool defined, {bool isRead: false, bool isWrite: fals e,
2416 bool isInvoke: false}) {
2277 TestVisitor visitor = new TestVisitor(); 2417 TestVisitor visitor = new TestVisitor();
2278 int count = 0; 2418 int count = 0;
2279 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2419 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2280 count++; 2420 count++;
2281 expect(node.toSource(), equals(expectedSource)); 2421 expect(node.toSource(), equals(expectedSource));
2282 expect(semantics.kind, equals(AccessKind.LOCAL_VARIABLE)); 2422 expect(semantics.kind, equals(AccessKind.STATIC_METHOD));
2283 expect(semantics.element.name, equals(expectedName)); 2423 expect(semantics.identifier.name, equals(expectedName));
2284 expect(semantics.classElement, isNull); 2424 if (expectedClass == null) {
2425 expect(semantics.classElement, isNull);
2426 if (defined) {
2427 expect(semantics.element, new isInstanceOf<FunctionElement>());
2428 }
2429 } else {
2430 expect(semantics.classElement, isNotNull);
2431 expect(semantics.classElement.displayName, equals(expectedClass));
2432 if (defined) {
2433 expect(semantics.element, new isInstanceOf<MethodElement>());
2434 }
2435 }
2436 if (defined) {
2437 expect(semantics.element.displayName, equals(expectedName));
2438 } else {
2439 expect(semantics.element, isNull);
2440 }
2285 expect(semantics.target, isNull); 2441 expect(semantics.target, isNull);
2286 expect(semantics.isRead, equals(isRead)); 2442 expect(semantics.isRead, equals(isRead));
2287 expect(semantics.isWrite, equals(isWrite)); 2443 expect(semantics.isWrite, equals(isWrite));
2288 expect(semantics.isInvoke, equals(isInvoke)); 2444 expect(semantics.isInvoke, equals(isInvoke));
2289 }; 2445 };
2290 libraryElement.unit.accept(visitor); 2446 libraryElement.unit.accept(visitor);
2291 expect(count, equals(1)); 2447 expect(count, equals(1));
2292 } 2448 }
2293 2449
2294 /** 2450 /**
2295 * Verify that the node represented by [expectedSource] is classified as a 2451 * Verify that the node represented by [expectedSource] is classified as
2296 * parameter access. 2452 * a static property access.
2297 */ 2453 */
2298 void checkParameter(String expectedSource, String expectedName, {bool isRead: 2454 void checkStaticProperty(String expectedSource, String expectedClass,
2299 false, bool isWrite: false, bool isInvoke: false}) { 2455 String expectedName, bool defined, {bool isRead: false, bool isWrite: fals e,
2456 bool isInvoke: false}) {
2300 TestVisitor visitor = new TestVisitor(); 2457 TestVisitor visitor = new TestVisitor();
2301 int count = 0; 2458 int count = 0;
2302 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2459 visitor.onAccess = (Expression node, AccessSemantics semantics) {
2303 count++; 2460 count++;
2304 expect(node.toSource(), equals(expectedSource)); 2461 expect(node.toSource(), equals(expectedSource));
2305 expect(semantics.kind, equals(AccessKind.PARAMETER)); 2462 expect(semantics.kind, equals(AccessKind.STATIC_PROPERTY));
2306 expect(semantics.element.name, equals(expectedName)); 2463 expect(semantics.identifier.name, equals(expectedName));
2307 expect(semantics.classElement, isNull); 2464 if (expectedClass == null) {
2465 expect(semantics.classElement, isNull);
2466 } else {
2467 expect(semantics.classElement, isNotNull);
2468 expect(semantics.classElement.displayName, equals(expectedClass));
2469 }
2470 if (defined) {
2471 expect(semantics.element.displayName, equals(expectedName));
2472 } else {
2473 expect(semantics.element, isNull);
2474 }
2308 expect(semantics.target, isNull); 2475 expect(semantics.target, isNull);
2309 expect(semantics.isRead, equals(isRead)); 2476 expect(semantics.isRead, equals(isRead));
2310 expect(semantics.isWrite, equals(isWrite)); 2477 expect(semantics.isWrite, equals(isWrite));
2311 expect(semantics.isInvoke, equals(isInvoke)); 2478 expect(semantics.isInvoke, equals(isInvoke));
2312 }; 2479 };
2313 libraryElement.unit.accept(visitor); 2480 libraryElement.unit.accept(visitor);
2314 expect(count, equals(1)); 2481 expect(count, equals(1));
2315 } 2482 }
2316 2483
2317 /** 2484 /**
2318 * Verify that the node represented by [expectedSource] is classified as a 2485 * Verify that the node represented by [expectedSource] is classified as a
2319 * reference to a toplevel class or a type parameter. 2486 * reference to a toplevel class or a type parameter.
2320 */ 2487 */
2321 void checkTypeReference( 2488 void checkTypeReference(String expectedSource, String expectedName,
2322 String expectedSource, String expectedName, AccessKind expectedKind) { 2489 AccessKind expectedKind, {bool isRead: false, bool isInvoke: false}) {
2323 TestVisitor visitor = new TestVisitor(); 2490 TestVisitor visitor = new TestVisitor();
2324 int count = 0; 2491 int count = 0;
2325 visitor.onAccess = (AstNode node, AccessSemantics semantics) { 2492 visitor.onAccess = (AstNode node, AccessSemantics semantics) {
2326 count++; 2493 count++;
2327 expect(node.toSource(), equals(expectedSource)); 2494 expect(node.toSource(), equals(expectedSource));
2328 expect(semantics.kind, equals(expectedKind)); 2495 expect(semantics.kind, equals(expectedKind));
2329 expect(semantics.element.name, equals(expectedName)); 2496 expect(semantics.element.name, equals(expectedName));
2330 expect(semantics.classElement, isNull); 2497 expect(semantics.classElement, isNull);
2331 expect(semantics.target, isNull); 2498 expect(semantics.target, isNull);
2332 expect(semantics.isRead, isTrue); 2499 expect(semantics.isRead, equals(isRead));
2333 expect(semantics.isWrite, isFalse); 2500 expect(semantics.isWrite, isFalse);
2334 expect(semantics.isInvoke, isFalse); 2501 expect(semantics.isInvoke, equals(isInvoke));
2335 }; 2502 };
2336 libraryElement.unit.accept(visitor); 2503 libraryElement.unit.accept(visitor);
2337 expect(count, equals(1)); 2504 expect(count, equals(1));
2338 } 2505 }
2339 } 2506 }
2340 2507
2341 typedef void AccessHandler(Expression node, AccessSemantics semantics);
2342
2343 /** 2508 /**
2344 * Visitor class used to run the tests. 2509 * Visitor class used to run the tests.
2345 */ 2510 */
2346 class TestVisitor extends RecursiveAstVisitor { 2511 class TestVisitor extends RecursiveAstVisitor {
2347 AccessHandler onAccess; 2512 AccessHandler onAccess;
2348 2513
2349 @override 2514 @override
2350 visitMethodInvocation(MethodInvocation node) { 2515 visitMethodInvocation(MethodInvocation node) {
2351 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR)); 2516 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR));
2352 } 2517 }
2353 2518
2354 @override 2519 @override
2355 visitPrefixedIdentifier(PrefixedIdentifier node) { 2520 visitPrefixedIdentifier(PrefixedIdentifier node) {
2356 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR)); 2521 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR));
2357 } 2522 }
2358 2523
2359 @override 2524 @override
2360 visitPropertyAccess(PropertyAccess node) { 2525 visitPropertyAccess(PropertyAccess node) {
2361 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR)); 2526 onAccess(node, node.accept(ACCESS_SEMANTICS_VISITOR));
2362 } 2527 }
2363 2528
2364 @override 2529 @override
2365 visitSimpleIdentifier(SimpleIdentifier node) { 2530 visitSimpleIdentifier(SimpleIdentifier node) {
2366 AccessSemantics semantics = node.accept(ACCESS_SEMANTICS_VISITOR); 2531 AccessSemantics semantics = node.accept(ACCESS_SEMANTICS_VISITOR);
2367 if (semantics != null) { 2532 if (semantics != null) {
2368 onAccess(node, semantics); 2533 onAccess(node, semantics);
2369 } 2534 }
2370 } 2535 }
2371 } 2536 }
OLDNEW
« no previous file with comments | « pkg/analyzer2dart/lib/src/identifier_semantics.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698