| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 analyzer.test.driver; | 5 library analyzer.test.driver; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| 11 import 'package:analyzer/dart/ast/resolution_accessors.dart'; |
| 11 import 'package:analyzer/dart/element/element.dart'; | 12 import 'package:analyzer/dart/element/element.dart'; |
| 12 import 'package:analyzer/error/error.dart'; | 13 import 'package:analyzer/error/error.dart'; |
| 13 import 'package:analyzer/file_system/file_system.dart'; | 14 import 'package:analyzer/file_system/file_system.dart'; |
| 14 import 'package:analyzer/file_system/memory_file_system.dart'; | 15 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 15 import 'package:analyzer/src/dart/analysis/byte_store.dart'; | 16 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 16 import 'package:analyzer/src/dart/analysis/driver.dart'; | 17 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 17 import 'package:analyzer/src/dart/analysis/file_state.dart'; | 18 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 18 import 'package:analyzer/src/dart/analysis/status.dart'; | 19 import 'package:analyzer/src/dart/analysis/status.dart'; |
| 19 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; | 20 import 'package:analyzer/src/dart/analysis/top_level_declaration.dart'; |
| 20 import 'package:analyzer/src/error/codes.dart'; | 21 import 'package:analyzer/src/error/codes.dart'; |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 String content = r''' | 531 String content = r''' |
| 531 export 'dart:async'; | 532 export 'dart:async'; |
| 532 export 'dart:noSuchLib'; | 533 export 'dart:noSuchLib'; |
| 533 export 'dart:math'; | 534 export 'dart:math'; |
| 534 '''; | 535 '''; |
| 535 addTestFile(content, priority: true); | 536 addTestFile(content, priority: true); |
| 536 | 537 |
| 537 AnalysisResult result = await driver.getResult(testFile); | 538 AnalysisResult result = await driver.getResult(testFile); |
| 538 expect(result.path, testFile); | 539 expect(result.path, testFile); |
| 539 // Has only exports for valid URIs. | 540 // Has only exports for valid URIs. |
| 540 List<ExportElement> imports = result.unit.element.library.exports; | 541 List<ExportElement> imports = |
| 542 elementForCompilationUnit(result.unit).library.exports; |
| 541 expect( | 543 expect( |
| 542 imports.map((import) => import.exportedLibrary.source.uri.toString()), | 544 imports.map((import) => import.exportedLibrary.source.uri.toString()), |
| 543 unorderedEquals(['dart:async', 'dart:math'])); | 545 unorderedEquals(['dart:async', 'dart:math'])); |
| 544 } | 546 } |
| 545 | 547 |
| 546 test_getResult_invalidUri_imports_dart() async { | 548 test_getResult_invalidUri_imports_dart() async { |
| 547 String content = r''' | 549 String content = r''' |
| 548 import 'dart:async'; | 550 import 'dart:async'; |
| 549 import 'dart:noSuchLib'; | 551 import 'dart:noSuchLib'; |
| 550 import 'dart:math'; | 552 import 'dart:math'; |
| 551 '''; | 553 '''; |
| 552 addTestFile(content, priority: true); | 554 addTestFile(content, priority: true); |
| 553 | 555 |
| 554 AnalysisResult result = await driver.getResult(testFile); | 556 AnalysisResult result = await driver.getResult(testFile); |
| 555 expect(result.path, testFile); | 557 expect(result.path, testFile); |
| 556 // Has only imports for valid URIs. | 558 // Has only imports for valid URIs. |
| 557 List<ImportElement> imports = result.unit.element.library.imports; | 559 List<ImportElement> imports = |
| 560 elementForCompilationUnit(result.unit).library.imports; |
| 558 expect( | 561 expect( |
| 559 imports.map((import) => import.importedLibrary.source.uri.toString()), | 562 imports.map((import) => import.importedLibrary.source.uri.toString()), |
| 560 unorderedEquals(['dart:async', 'dart:math', 'dart:core'])); | 563 unorderedEquals(['dart:async', 'dart:math', 'dart:core'])); |
| 561 } | 564 } |
| 562 | 565 |
| 563 test_getResult_mix_fileAndPackageUris() async { | 566 test_getResult_mix_fileAndPackageUris() async { |
| 564 var a = _p('/test/bin/a.dart'); | 567 var a = _p('/test/bin/a.dart'); |
| 565 var b = _p('/test/bin/b.dart'); | 568 var b = _p('/test/bin/b.dart'); |
| 566 var c = _p('/test/lib/c.dart'); | 569 var c = _p('/test/lib/c.dart'); |
| 567 var d = _p('/test/test/d.dart'); | 570 var d = _p('/test/test/d.dart'); |
| (...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 } | 1197 } |
| 1195 } | 1198 } |
| 1196 } | 1199 } |
| 1197 } | 1200 } |
| 1198 fail('Cannot find the field $fieldName in the class $className in\n$unit'); | 1201 fail('Cannot find the field $fieldName in the class $className in\n$unit'); |
| 1199 return null; | 1202 return null; |
| 1200 } | 1203 } |
| 1201 | 1204 |
| 1202 String _getClassFieldType( | 1205 String _getClassFieldType( |
| 1203 CompilationUnit unit, String className, String fieldName) { | 1206 CompilationUnit unit, String className, String fieldName) { |
| 1204 return _getClassField(unit, className, fieldName).element.type.toString(); | 1207 return elementForVariableDeclaration( |
| 1208 _getClassField(unit, className, fieldName)) |
| 1209 .type |
| 1210 .toString(); |
| 1205 } | 1211 } |
| 1206 | 1212 |
| 1207 MethodDeclaration _getClassMethod( | 1213 MethodDeclaration _getClassMethod( |
| 1208 CompilationUnit unit, String className, String methodName) { | 1214 CompilationUnit unit, String className, String methodName) { |
| 1209 ClassDeclaration classDeclaration = _getClass(unit, className); | 1215 ClassDeclaration classDeclaration = _getClass(unit, className); |
| 1210 for (ClassMember declaration in classDeclaration.members) { | 1216 for (ClassMember declaration in classDeclaration.members) { |
| 1211 if (declaration is MethodDeclaration && | 1217 if (declaration is MethodDeclaration && |
| 1212 declaration.name.name == methodName) { | 1218 declaration.name.name == methodName) { |
| 1213 return declaration; | 1219 return declaration; |
| 1214 } | 1220 } |
| 1215 } | 1221 } |
| 1216 fail('Cannot find the method $methodName in the class $className in\n' | 1222 fail('Cannot find the method $methodName in the class $className in\n' |
| 1217 '$unit'); | 1223 '$unit'); |
| 1218 return null; | 1224 return null; |
| 1219 } | 1225 } |
| 1220 | 1226 |
| 1221 String _getClassMethodReturnType( | 1227 String _getClassMethodReturnType( |
| 1222 CompilationUnit unit, String className, String fieldName) { | 1228 CompilationUnit unit, String className, String fieldName) { |
| 1223 return _getClassMethod(unit, className, fieldName) | 1229 return elementForMethodDeclaration( |
| 1224 .element | 1230 _getClassMethod(unit, className, fieldName)) |
| 1225 .type | 1231 .type |
| 1226 .returnType | 1232 .returnType |
| 1227 .toString(); | 1233 .toString(); |
| 1228 } | 1234 } |
| 1229 | 1235 |
| 1230 ImportElement _getImportElement(CompilationUnit unit, int directiveIndex) { | 1236 ImportElement _getImportElement(CompilationUnit unit, int directiveIndex) { |
| 1231 var import = unit.directives[directiveIndex] as ImportDirective; | 1237 var import = unit.directives[directiveIndex] as ImportDirective; |
| 1232 return import.element as ImportElement; | 1238 return import.element as ImportElement; |
| 1233 } | 1239 } |
| 1234 | 1240 |
| 1235 Source _getImportSource(CompilationUnit unit, int directiveIndex) { | 1241 Source _getImportSource(CompilationUnit unit, int directiveIndex) { |
| 1236 return _getImportElement(unit, directiveIndex).importedLibrary.source; | 1242 return _getImportElement(unit, directiveIndex).importedLibrary.source; |
| 1237 } | 1243 } |
| 1238 | 1244 |
| 1239 VariableDeclaration _getTopLevelVar(CompilationUnit unit, String name) { | 1245 VariableDeclaration _getTopLevelVar(CompilationUnit unit, String name) { |
| 1240 for (CompilationUnitMember declaration in unit.declarations) { | 1246 for (CompilationUnitMember declaration in unit.declarations) { |
| 1241 if (declaration is TopLevelVariableDeclaration) { | 1247 if (declaration is TopLevelVariableDeclaration) { |
| 1242 for (VariableDeclaration variable in declaration.variables.variables) { | 1248 for (VariableDeclaration variable in declaration.variables.variables) { |
| 1243 if (variable.name.name == name) { | 1249 if (variable.name.name == name) { |
| 1244 return variable; | 1250 return variable; |
| 1245 } | 1251 } |
| 1246 } | 1252 } |
| 1247 } | 1253 } |
| 1248 } | 1254 } |
| 1249 fail('Cannot find the top-level variable $name in\n$unit'); | 1255 fail('Cannot find the top-level variable $name in\n$unit'); |
| 1250 return null; | 1256 return null; |
| 1251 } | 1257 } |
| 1252 | 1258 |
| 1253 String _getTopLevelVarType(CompilationUnit unit, String name) { | 1259 String _getTopLevelVarType(CompilationUnit unit, String name) { |
| 1254 return _getTopLevelVar(unit, name).element.type.toString(); | 1260 return elementForVariableDeclaration(_getTopLevelVar(unit, name)) |
| 1261 .type |
| 1262 .toString(); |
| 1255 } | 1263 } |
| 1256 | 1264 |
| 1257 /** | 1265 /** |
| 1258 * Return the [provider] specific path for the given Posix [path]. | 1266 * Return the [provider] specific path for the given Posix [path]. |
| 1259 */ | 1267 */ |
| 1260 String _p(String path) => provider.convertPath(path); | 1268 String _p(String path) => provider.convertPath(path); |
| 1261 | 1269 |
| 1262 Future<Null> _waitForIdle() async { | 1270 Future<Null> _waitForIdle() async { |
| 1263 await idleStatusMonitor.signal; | 1271 await idleStatusMonitor.signal; |
| 1264 } | 1272 } |
| 1265 | 1273 |
| 1266 static String _md5(String content) { | 1274 static String _md5(String content) { |
| 1267 return hex.encode(md5.convert(UTF8.encode(content)).bytes); | 1275 return hex.encode(md5.convert(UTF8.encode(content)).bytes); |
| 1268 } | 1276 } |
| 1269 } | 1277 } |
| OLD | NEW |