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 import 'package:unittest/unittest.dart'; | 5 import 'package:unittest/unittest.dart'; |
6 import 'package:analyzer/file_system/memory_file_system.dart'; | 6 import 'package:analyzer/file_system/memory_file_system.dart'; |
7 import 'mock_sdk.dart'; | 7 import 'mock_sdk.dart'; |
8 import 'package:analyzer/src/generated/sdk.dart'; | 8 import 'package:analyzer/src/generated/sdk.dart'; |
9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
(...skipping 1074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1085 | 1085 |
1086 A h() => null; | 1086 A h() => null; |
1087 | 1087 |
1088 f() { | 1088 f() { |
1089 return h().x; | 1089 return h().x; |
1090 } | 1090 } |
1091 '''); | 1091 '''); |
1092 helper.checkDynamic('h().x', 'h()', 'x', isRead: true); | 1092 helper.checkDynamic('h().x', 'h()', 'x', isRead: true); |
1093 }); | 1093 }); |
1094 | 1094 |
| 1095 test('Get class defined at top level', () { |
| 1096 Helper helper = new Helper(''' |
| 1097 class A {} |
| 1098 var t = A; |
| 1099 '''); |
| 1100 helper.checkTypeReference('A', 'A', AccessKind.TOPLEVEL_CLASS); |
| 1101 }); |
| 1102 |
| 1103 test('Get class defined at top level via prefix', () { |
| 1104 Helper helper = new Helper(''' |
| 1105 import 'lib.dart' as l; |
| 1106 |
| 1107 var t = l.A; |
| 1108 '''); |
| 1109 helper.addFile('/lib.dart', ''' |
| 1110 library lib; |
| 1111 |
| 1112 class A; |
| 1113 '''); |
| 1114 helper.checkTypeReference('l.A', 'A', AccessKind.TOPLEVEL_CLASS); |
| 1115 }); |
| 1116 |
| 1117 test('Get type parameter of enclosing class', () { |
| 1118 Helper helper = new Helper(''' |
| 1119 class A<T, U> { |
| 1120 f() { |
| 1121 var t = U; |
| 1122 } |
| 1123 } |
| 1124 '''); |
| 1125 helper.checkTypeReference('U', 'U', AccessKind.TYPE_PARAMETER); |
| 1126 }); |
| 1127 |
1095 test('Set variable defined at top level', () { | 1128 test('Set variable defined at top level', () { |
1096 Helper helper = new Helper(''' | 1129 Helper helper = new Helper(''' |
1097 var x; | 1130 var x; |
1098 | 1131 |
1099 f() { | 1132 f() { |
1100 x = 1; | 1133 x = 1; |
1101 } | 1134 } |
1102 '''); | 1135 '''); |
1103 helper.checkStaticField('x', null, 'x', isWrite: true); | 1136 helper.checkStaticField('x', null, 'x', isWrite: true); |
1104 }); | 1137 }); |
(...skipping 1113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2218 expect(semantics.element.name, equals(expectedName)); | 2251 expect(semantics.element.name, equals(expectedName)); |
2219 expect(semantics.classElement, isNull); | 2252 expect(semantics.classElement, isNull); |
2220 expect(semantics.target, isNull); | 2253 expect(semantics.target, isNull); |
2221 expect(semantics.isRead, equals(isRead)); | 2254 expect(semantics.isRead, equals(isRead)); |
2222 expect(semantics.isWrite, equals(isWrite)); | 2255 expect(semantics.isWrite, equals(isWrite)); |
2223 expect(semantics.isInvoke, equals(isInvoke)); | 2256 expect(semantics.isInvoke, equals(isInvoke)); |
2224 }; | 2257 }; |
2225 libraryElement.unit.accept(visitor); | 2258 libraryElement.unit.accept(visitor); |
2226 expect(count, equals(1)); | 2259 expect(count, equals(1)); |
2227 } | 2260 } |
| 2261 |
| 2262 /** |
| 2263 * Verify that the node represented by [expectedSource] is classified as a |
| 2264 * reference to a toplevel class or a type parameter. |
| 2265 */ |
| 2266 void checkTypeReference( |
| 2267 String expectedSource, String expectedName, AccessKind expectedKind) { |
| 2268 TestVisitor visitor = new TestVisitor(); |
| 2269 int count = 0; |
| 2270 visitor.onAccess = (AstNode node, AccessSemantics semantics) { |
| 2271 count++; |
| 2272 expect(node.toSource(), equals(expectedSource)); |
| 2273 expect(semantics.kind, equals(expectedKind)); |
| 2274 expect(semantics.element.name, equals(expectedName)); |
| 2275 expect(semantics.classElement, isNull); |
| 2276 expect(semantics.target, isNull); |
| 2277 expect(semantics.isRead, isTrue); |
| 2278 expect(semantics.isWrite, isFalse); |
| 2279 expect(semantics.isInvoke, isFalse); |
| 2280 }; |
| 2281 libraryElement.unit.accept(visitor); |
| 2282 expect(count, equals(1)); |
| 2283 } |
2228 } | 2284 } |
2229 | 2285 |
2230 typedef void AccessHandler(Expression node, AccessSemantics semantics); | 2286 typedef void AccessHandler(Expression node, AccessSemantics semantics); |
2231 | 2287 |
2232 /** | 2288 /** |
2233 * Visitor class used to run the tests. | 2289 * Visitor class used to run the tests. |
2234 */ | 2290 */ |
2235 class TestVisitor extends RecursiveAstVisitor { | 2291 class TestVisitor extends RecursiveAstVisitor { |
2236 AccessHandler onAccess; | 2292 AccessHandler onAccess; |
2237 | 2293 |
(...skipping 13 matching lines...) Expand all Loading... |
2251 } | 2307 } |
2252 | 2308 |
2253 @override | 2309 @override |
2254 visitSimpleIdentifier(SimpleIdentifier node) { | 2310 visitSimpleIdentifier(SimpleIdentifier node) { |
2255 AccessSemantics semantics = node.accept(ACCESS_SEMANTICS_VISITOR); | 2311 AccessSemantics semantics = node.accept(ACCESS_SEMANTICS_VISITOR); |
2256 if (semantics != null) { | 2312 if (semantics != null) { |
2257 onAccess(node, semantics); | 2313 onAccess(node, semantics); |
2258 } | 2314 } |
2259 } | 2315 } |
2260 } | 2316 } |
OLD | NEW |