OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // TODO(jmesserly): this file needs to be refactored, it's a port from | 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
6 // package:dev_compiler's tests | 6 // package:dev_compiler's tests |
7 library analyzer.test.src.task.strong.strong_test_helper; | 7 library analyzer.test.src.task.strong.strong_test_helper; |
8 | 8 |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 | 10 |
11 import 'package:analyzer/dart/ast/ast.dart'; | 11 import 'package:analyzer/dart/ast/ast.dart'; |
12 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 12 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
13 import 'package:analyzer/dart/ast/token.dart'; | 13 import 'package:analyzer/dart/ast/token.dart'; |
14 import 'package:analyzer/dart/element/element.dart'; | 14 import 'package:analyzer/dart/element/element.dart'; |
15 import 'package:analyzer/error/error.dart'; | 15 import 'package:analyzer/error/error.dart'; |
16 import 'package:analyzer/error/listener.dart'; | 16 import 'package:analyzer/error/listener.dart'; |
17 import 'package:analyzer/file_system/file_system.dart'; | 17 import 'package:analyzer/file_system/file_system.dart'; |
18 import 'package:analyzer/file_system/memory_file_system.dart'; | 18 import 'package:analyzer/file_system/memory_file_system.dart'; |
19 import 'package:analyzer/source/error_processor.dart'; | 19 import 'package:analyzer/source/error_processor.dart'; |
| 20 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
20 import 'package:analyzer/src/dart/analysis/driver.dart'; | 21 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 22 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
21 import 'package:analyzer/src/dart/ast/token.dart'; | 23 import 'package:analyzer/src/dart/ast/token.dart'; |
22 import 'package:analyzer/src/error/codes.dart'; | 24 import 'package:analyzer/src/error/codes.dart'; |
23 import 'package:analyzer/src/generated/engine.dart'; | 25 import 'package:analyzer/src/generated/engine.dart'; |
24 import 'package:analyzer/src/generated/source.dart'; | 26 import 'package:analyzer/src/generated/source.dart'; |
25 import 'package:source_span/source_span.dart'; | 27 import 'package:source_span/source_span.dart'; |
26 import 'package:test/test.dart'; | 28 import 'package:test/test.dart'; |
27 | 29 |
28 import '../../context/mock_sdk.dart'; | 30 import '../../context/mock_sdk.dart'; |
29 | 31 |
30 SourceSpanWithContext _createSpanHelper( | 32 SourceSpanWithContext _createSpanHelper( |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 }); | 221 }); |
220 message.writeln(); | 222 message.writeln(); |
221 } | 223 } |
222 fail('Checker errors do not match expected errors:\n\n$message'); | 224 fail('Checker errors do not match expected errors:\n\n$message'); |
223 } | 225 } |
224 | 226 |
225 class AbstractStrongTest { | 227 class AbstractStrongTest { |
226 MemoryResourceProvider _resourceProvider = new MemoryResourceProvider(); | 228 MemoryResourceProvider _resourceProvider = new MemoryResourceProvider(); |
227 bool _checkCalled = false; | 229 bool _checkCalled = false; |
228 | 230 |
| 231 AnalysisContext _context = null; |
| 232 AnalysisDriver _driver = null; |
| 233 |
229 bool get enableNewAnalysisDriver => false; | 234 bool get enableNewAnalysisDriver => false; |
230 | 235 |
231 /// Adds a file to check. The file should contain: | 236 /// Adds a file to check. The file should contain: |
232 /// | 237 /// |
233 /// * all expected failures are listed in the source code using comments | 238 /// * all expected failures are listed in the source code using comments |
234 /// immediately in front of the AST node that should contain the error. | 239 /// immediately in front of the AST node that should contain the error. |
235 /// | 240 /// |
236 /// * errors are formatted as a token `severity:ErrorCode`, where | 241 /// * errors are formatted as a token `severity:ErrorCode`, where |
237 /// `severity` is the ErrorSeverity the error would be reported at, and | 242 /// `severity` is the ErrorSeverity the error would be reported at, and |
238 /// `ErrorCode` is the error code's name. | 243 /// `ErrorCode` is the error code's name. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 mockSdk.context.analysisOptions = analysisOptions; | 285 mockSdk.context.analysisOptions = analysisOptions; |
281 | 286 |
282 SourceFactory sourceFactory; | 287 SourceFactory sourceFactory; |
283 { | 288 { |
284 var uriResolver = new _TestUriResolver(_resourceProvider); | 289 var uriResolver = new _TestUriResolver(_resourceProvider); |
285 sourceFactory = | 290 sourceFactory = |
286 new SourceFactory([new DartUriResolver(mockSdk), uriResolver]); | 291 new SourceFactory([new DartUriResolver(mockSdk), uriResolver]); |
287 } | 292 } |
288 | 293 |
289 CompilationUnit mainUnit; | 294 CompilationUnit mainUnit; |
290 AnalysisContext context = null; | |
291 AnalysisDriver driver = null; | |
292 if (enableNewAnalysisDriver) { | 295 if (enableNewAnalysisDriver) { |
293 // TODO(scheglov) | 296 StringBuffer logBuffer = new StringBuffer(); |
| 297 FileContentOverlay fileContentOverlay = new FileContentOverlay(); |
| 298 PerformanceLog log = new PerformanceLog(logBuffer); |
| 299 AnalysisDriverScheduler scheduler = new AnalysisDriverScheduler(log); |
| 300 _driver = new AnalysisDriver( |
| 301 scheduler, |
| 302 log, |
| 303 _resourceProvider, |
| 304 new MemoryByteStore(), |
| 305 fileContentOverlay, |
| 306 'test', |
| 307 sourceFactory, |
| 308 analysisOptions); |
| 309 scheduler.start(); |
| 310 |
| 311 mainUnit = (await _driver.getResult(mainFile.path)).unit; |
294 } else { | 312 } else { |
295 context = AnalysisEngine.instance.createAnalysisContext(); | 313 _context = AnalysisEngine.instance.createAnalysisContext(); |
296 context.analysisOptions = analysisOptions; | 314 _context.analysisOptions = analysisOptions; |
297 context.sourceFactory = sourceFactory; | 315 _context.sourceFactory = sourceFactory; |
298 | 316 |
299 // Run the checker on /main.dart. | 317 // Run the checker on /main.dart. |
300 Source mainSource = sourceFactory.forUri2(mainFile.toUri()); | 318 Source mainSource = sourceFactory.forUri2(mainFile.toUri()); |
301 mainUnit = context.resolveCompilationUnit2(mainSource, mainSource); | 319 mainUnit = _context.resolveCompilationUnit2(mainSource, mainSource); |
302 } | 320 } |
303 | 321 |
304 var collector = new _ErrorCollector(analysisOptions); | 322 var collector = new _ErrorCollector(analysisOptions); |
305 | 323 |
306 // Extract expectations from the comments in the test files, and | 324 // Extract expectations from the comments in the test files, and |
307 // check that all errors we emit are included in the expected map. | 325 // check that all errors we emit are included in the expected map. |
308 LibraryElement mainLibrary = | 326 LibraryElement mainLibrary = |
309 resolutionMap.elementDeclaredByCompilationUnit(mainUnit).library; | 327 resolutionMap.elementDeclaredByCompilationUnit(mainUnit).library; |
310 Set<LibraryElement> allLibraries = _reachableLibraries(mainLibrary); | 328 Set<LibraryElement> allLibraries = _reachableLibraries(mainLibrary); |
311 for (LibraryElement library in allLibraries) { | 329 for (LibraryElement library in allLibraries) { |
312 for (CompilationUnitElement unit in library.units) { | 330 for (CompilationUnitElement unit in library.units) { |
313 var errors = <AnalysisError>[]; | 331 var errors = <AnalysisError>[]; |
314 collector.errors = errors; | 332 collector.errors = errors; |
315 | 333 |
316 var source = unit.source; | 334 var source = unit.source; |
317 if (source.uri.scheme == 'dart') { | 335 if (source.uri.scheme == 'dart') { |
318 continue; | 336 continue; |
319 } | 337 } |
320 | 338 |
321 var analysisResult = await _resolve(context, driver, source); | 339 var analysisResult = await _resolve(source); |
322 | 340 |
323 errors.addAll(analysisResult.errors.where((e) => | 341 errors.addAll(analysisResult.errors.where((e) => |
324 // TODO(jmesserly): these are usually intentional dynamic calls. | 342 // TODO(jmesserly): these are usually intentional dynamic calls. |
325 e.errorCode.name != 'UNDEFINED_METHOD' && | 343 e.errorCode.name != 'UNDEFINED_METHOD' && |
326 // We don't care about any of these: | 344 // We don't care about any of these: |
327 e.errorCode != HintCode.UNUSED_ELEMENT && | 345 e.errorCode != HintCode.UNUSED_ELEMENT && |
328 e.errorCode != HintCode.UNUSED_FIELD && | 346 e.errorCode != HintCode.UNUSED_FIELD && |
329 e.errorCode != HintCode.UNUSED_IMPORT && | 347 e.errorCode != HintCode.UNUSED_IMPORT && |
330 e.errorCode != HintCode.UNUSED_LOCAL_VARIABLE && | 348 e.errorCode != HintCode.UNUSED_LOCAL_VARIABLE && |
331 e.errorCode != TodoCode.TODO)); | 349 e.errorCode != TodoCode.TODO)); |
(...skipping 12 matching lines...) Expand all Loading... |
344 return check(); | 362 return check(); |
345 } | 363 } |
346 | 364 |
347 void setUp() { | 365 void setUp() { |
348 AnalysisEngine.instance.processRequiredPlugins(); | 366 AnalysisEngine.instance.processRequiredPlugins(); |
349 } | 367 } |
350 | 368 |
351 void tearDown() { | 369 void tearDown() { |
352 // This is a sanity check, in case only addFile is called. | 370 // This is a sanity check, in case only addFile is called. |
353 expect(_checkCalled, true, reason: 'must call check() method in test case'); | 371 expect(_checkCalled, true, reason: 'must call check() method in test case'); |
| 372 _context?.dispose(); |
| 373 _driver?.dispose(); |
354 AnalysisEngine.instance.clearCaches(); | 374 AnalysisEngine.instance.clearCaches(); |
355 } | 375 } |
356 | 376 |
357 Future<_TestAnalysisResult> _resolve( | 377 Future<_TestAnalysisResult> _resolve(Source source) async { |
358 AnalysisContext context, AnalysisDriver driver, Source source) async { | |
359 if (enableNewAnalysisDriver) { | 378 if (enableNewAnalysisDriver) { |
360 // TODO(scheglov) implement | 379 var result = await _driver.getResult(source.fullName); |
361 throw new UnimplementedError(); | 380 return new _TestAnalysisResult(source, result.unit, result.errors); |
362 } else { | 381 } else { |
363 List<Source> libraries = context.getLibrariesContaining(source); | 382 List<Source> libraries = _context.getLibrariesContaining(source); |
364 var unit = context.resolveCompilationUnit2(source, libraries.single); | 383 var unit = _context.resolveCompilationUnit2(source, libraries.single); |
365 var errors = context.computeErrors(source); | 384 var errors = _context.computeErrors(source); |
366 return new _TestAnalysisResult(source, unit, errors); | 385 return new _TestAnalysisResult(source, unit, errors); |
367 } | 386 } |
368 } | 387 } |
369 } | 388 } |
370 | 389 |
371 class _ErrorCollector implements AnalysisErrorListener { | 390 class _ErrorCollector implements AnalysisErrorListener { |
372 final AnalysisOptions analysisOptions; | 391 final AnalysisOptions analysisOptions; |
373 List<AnalysisError> errors; | 392 List<AnalysisError> errors; |
374 final bool hints; | 393 final bool hints; |
375 | 394 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 @override | 468 @override |
450 Source resolveAbsolute(Uri uri, [Uri actualUri]) { | 469 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
451 if (uri.scheme == 'package') { | 470 if (uri.scheme == 'package') { |
452 return (provider.getResource( | 471 return (provider.getResource( |
453 provider.convertPath('/packages/' + uri.path)) as File) | 472 provider.convertPath('/packages/' + uri.path)) as File) |
454 .createSource(uri); | 473 .createSource(uri); |
455 } | 474 } |
456 return super.resolveAbsolute(uri, actualUri); | 475 return super.resolveAbsolute(uri, actualUri); |
457 } | 476 } |
458 } | 477 } |
OLD | NEW |