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 test.src.context.context_test; | 5 library test.src.context.context_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 10 import 'package:analyzer/source/package_map_resolver.dart'; |
10 import 'package:analyzer/src/cancelable_future.dart'; | 11 import 'package:analyzer/src/cancelable_future.dart'; |
11 import 'package:analyzer/src/context/cache.dart'; | 12 import 'package:analyzer/src/context/cache.dart'; |
12 import 'package:analyzer/src/context/context.dart'; | 13 import 'package:analyzer/src/context/context.dart'; |
13 import 'package:analyzer/src/generated/ast.dart'; | 14 import 'package:analyzer/src/generated/ast.dart'; |
14 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
15 import 'package:analyzer/src/generated/engine.dart' | 16 import 'package:analyzer/src/generated/engine.dart' |
16 show | 17 show |
17 AnalysisContext, | 18 AnalysisContext, |
18 AnalysisContextStatistics, | 19 AnalysisContextStatistics, |
19 AnalysisDelta, | 20 AnalysisDelta, |
20 AnalysisEngine, | 21 AnalysisEngine, |
21 AnalysisErrorInfo, | 22 AnalysisErrorInfo, |
22 AnalysisLevel, | 23 AnalysisLevel, |
23 AnalysisNotScheduledError, | 24 AnalysisNotScheduledError, |
24 AnalysisOptions, | 25 AnalysisOptions, |
25 AnalysisOptionsImpl, | 26 AnalysisOptionsImpl, |
26 AnalysisResult, | 27 AnalysisResult, |
27 CacheState, | 28 CacheState, |
28 ChangeNotice, | 29 ChangeNotice, |
29 ChangeSet, | 30 ChangeSet, |
30 IncrementalAnalysisCache, | 31 IncrementalAnalysisCache, |
31 TimestampedData; | 32 TimestampedData; |
32 import 'package:analyzer/src/generated/error.dart'; | 33 import 'package:analyzer/src/generated/error.dart'; |
33 import 'package:analyzer/src/generated/java_engine.dart'; | 34 import 'package:analyzer/src/generated/java_engine.dart'; |
34 import 'package:analyzer/src/generated/resolver.dart'; | 35 import 'package:analyzer/src/generated/resolver.dart'; |
35 import 'package:analyzer/src/generated/scanner.dart'; | 36 import 'package:analyzer/src/generated/scanner.dart'; |
36 import 'package:analyzer/src/generated/source.dart'; | 37 import 'package:analyzer/src/generated/source.dart'; |
| 38 import 'package:analyzer/src/generated/utilities_collection.dart'; |
37 import 'package:analyzer/src/task/dart.dart'; | 39 import 'package:analyzer/src/task/dart.dart'; |
38 import 'package:analyzer/src/task/html.dart'; | 40 import 'package:analyzer/src/task/html.dart'; |
39 import 'package:analyzer/task/dart.dart'; | 41 import 'package:analyzer/task/dart.dart'; |
40 import 'package:analyzer/task/model.dart'; | 42 import 'package:analyzer/task/model.dart'; |
41 import 'package:html/dom.dart' show Document; | 43 import 'package:html/dom.dart' show Document; |
42 import 'package:unittest/unittest.dart'; | 44 import 'package:unittest/unittest.dart'; |
43 import 'package:watcher/src/utils.dart'; | 45 import 'package:watcher/src/utils.dart'; |
44 | 46 |
45 import '../../generated/engine_test.dart'; | 47 import '../../generated/engine_test.dart'; |
46 import '../../generated/test_support.dart'; | 48 import '../../generated/test_support.dart'; |
(...skipping 1913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1960 _analyzeAll_assertFinished(); | 1962 _analyzeAll_assertFinished(); |
1961 // verify | 1963 // verify |
1962 expect(libraryElementUris, contains('dart:core')); | 1964 expect(libraryElementUris, contains('dart:core')); |
1963 expect(libraryElementUris, contains('file:///test.dart')); | 1965 expect(libraryElementUris, contains('file:///test.dart')); |
1964 expect(parsedUnitUris, contains('dart:core')); | 1966 expect(parsedUnitUris, contains('dart:core')); |
1965 expect(parsedUnitUris, contains('file:///test.dart')); | 1967 expect(parsedUnitUris, contains('file:///test.dart')); |
1966 expect(resolvedUnitUris, contains('dart:core')); | 1968 expect(resolvedUnitUris, contains('dart:core')); |
1967 expect(resolvedUnitUris, contains('file:///test.dart')); | 1969 expect(resolvedUnitUris, contains('file:///test.dart')); |
1968 } | 1970 } |
1969 | 1971 |
| 1972 void test_performAnalysisTask_switchPackageVersion() { |
| 1973 // version 1 |
| 1974 resourceProvider.newFile( |
| 1975 '/pkgs/crypto-1/lib/crypto.dart', |
| 1976 r''' |
| 1977 library crypto; |
| 1978 part 'src/hash_utils.dart'; |
| 1979 '''); |
| 1980 resourceProvider.newFile( |
| 1981 '/pkgs/crypto-1/lib/src/hash_utils.dart', |
| 1982 r''' |
| 1983 part of crypto; |
| 1984 const _MASK_8 = 0xff; |
| 1985 '''); |
| 1986 // version 2 |
| 1987 resourceProvider.newFile( |
| 1988 '/pkgs/crypto-2/lib/crypto.dart', |
| 1989 r''' |
| 1990 library crypto; |
| 1991 part 'src/hash_utils.dart'; |
| 1992 '''); |
| 1993 resourceProvider.newFile( |
| 1994 '/pkgs/crypto-2/lib/src/hash_utils.dart', |
| 1995 r''' |
| 1996 part of crypto; |
| 1997 const _MASK_8 = 0xff; |
| 1998 '''); |
| 1999 // use version 1 |
| 2000 context.sourceFactory = new SourceFactory(<UriResolver>[ |
| 2001 sdkResolver, |
| 2002 resourceResolver, |
| 2003 new PackageMapUriResolver(resourceProvider, { |
| 2004 'crypto': [resourceProvider.getFolder('/pkgs/crypto-1/lib')] |
| 2005 }) |
| 2006 ]); |
| 2007 // analyze |
| 2008 addSource( |
| 2009 "/test.dart", |
| 2010 r''' |
| 2011 import 'package:crypto/crypto.dart'; |
| 2012 '''); |
| 2013 _analyzeAll_assertFinished(); |
| 2014 // use version 2 |
| 2015 context.sourceFactory = new SourceFactory(<UriResolver>[ |
| 2016 sdkResolver, |
| 2017 resourceResolver, |
| 2018 new PackageMapUriResolver(resourceProvider, { |
| 2019 'crypto': [resourceProvider.getFolder('/pkgs/crypto-2/lib')] |
| 2020 }) |
| 2021 ]); |
| 2022 _analyzeAll_assertFinished(); |
| 2023 _assertNoExceptions(); |
| 2024 } |
| 2025 |
1970 void test_resolveCompilationUnit_import_relative() { | 2026 void test_resolveCompilationUnit_import_relative() { |
1971 Source sourceA = | 2027 Source sourceA = |
1972 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); | 2028 addSource("/libA.dart", "library libA; import 'libB.dart'; class A{}"); |
1973 addSource("/libB.dart", "library libB; class B{}"); | 2029 addSource("/libB.dart", "library libB; class B{}"); |
1974 CompilationUnit compilationUnit = | 2030 CompilationUnit compilationUnit = |
1975 context.resolveCompilationUnit2(sourceA, sourceA); | 2031 context.resolveCompilationUnit2(sourceA, sourceA); |
1976 expect(compilationUnit, isNotNull); | 2032 expect(compilationUnit, isNotNull); |
1977 LibraryElement library = compilationUnit.element.library; | 2033 LibraryElement library = compilationUnit.element.library; |
1978 List<LibraryElement> importedLibraries = library.importedLibraries; | 2034 List<LibraryElement> importedLibraries = library.importedLibraries; |
1979 assertNamedElements(importedLibraries, ["dart.core", "libB"]); | 2035 assertNamedElements(importedLibraries, ["dart.core", "libB"]); |
(...skipping 19 matching lines...) Expand all Loading... |
1999 | 2055 |
2000 void test_resolveCompilationUnit_library() { | 2056 void test_resolveCompilationUnit_library() { |
2001 Source source = addSource("/lib.dart", "library lib;"); | 2057 Source source = addSource("/lib.dart", "library lib;"); |
2002 LibraryElement library = context.computeLibraryElement(source); | 2058 LibraryElement library = context.computeLibraryElement(source); |
2003 CompilationUnit compilationUnit = | 2059 CompilationUnit compilationUnit = |
2004 context.resolveCompilationUnit(source, library); | 2060 context.resolveCompilationUnit(source, library); |
2005 expect(compilationUnit, isNotNull); | 2061 expect(compilationUnit, isNotNull); |
2006 expect(compilationUnit.element, isNotNull); | 2062 expect(compilationUnit.element, isNotNull); |
2007 } | 2063 } |
2008 | 2064 |
| 2065 void test_resolveCompilationUnit_source() { |
| 2066 Source source = addSource("/lib.dart", "library lib;"); |
| 2067 CompilationUnit compilationUnit = |
| 2068 context.resolveCompilationUnit2(source, source); |
| 2069 expect(compilationUnit, isNotNull); |
| 2070 } |
| 2071 |
2009 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { | 2072 // void test_resolveCompilationUnit_sourceChangeDuringResolution() { |
2010 // _context = new _AnalysisContext_sourceChangeDuringResolution(); | 2073 // _context = new _AnalysisContext_sourceChangeDuringResolution(); |
2011 // AnalysisContextFactory.initContextWithCore(_context); | 2074 // AnalysisContextFactory.initContextWithCore(_context); |
2012 // _sourceFactory = _context.sourceFactory; | 2075 // _sourceFactory = _context.sourceFactory; |
2013 // Source source = _addSource("/lib.dart", "library lib;"); | 2076 // Source source = _addSource("/lib.dart", "library lib;"); |
2014 // CompilationUnit compilationUnit = | 2077 // CompilationUnit compilationUnit = |
2015 // _context.resolveCompilationUnit2(source, source); | 2078 // _context.resolveCompilationUnit2(source, source); |
2016 // expect(compilationUnit, isNotNull); | 2079 // expect(compilationUnit, isNotNull); |
2017 // expect(_context.getLineInfo(source), isNotNull); | 2080 // expect(_context.getLineInfo(source), isNotNull); |
2018 // } | 2081 // } |
2019 | 2082 |
2020 void test_resolveCompilationUnit_source() { | |
2021 Source source = addSource("/lib.dart", "library lib;"); | |
2022 CompilationUnit compilationUnit = | |
2023 context.resolveCompilationUnit2(source, source); | |
2024 expect(compilationUnit, isNotNull); | |
2025 } | |
2026 | |
2027 void test_setAnalysisOptions() { | 2083 void test_setAnalysisOptions() { |
2028 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | 2084 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); |
2029 options.cacheSize = 42; | 2085 options.cacheSize = 42; |
2030 options.dart2jsHint = false; | 2086 options.dart2jsHint = false; |
2031 options.hint = false; | 2087 options.hint = false; |
2032 context.analysisOptions = options; | 2088 context.analysisOptions = options; |
2033 AnalysisOptions result = context.analysisOptions; | 2089 AnalysisOptions result = context.analysisOptions; |
2034 expect(result.cacheSize, options.cacheSize); | 2090 expect(result.cacheSize, options.cacheSize); |
2035 expect(result.dart2jsHint, options.dart2jsHint); | 2091 expect(result.dart2jsHint, options.dart2jsHint); |
2036 expect(result.hint, options.hint); | 2092 expect(result.hint, options.hint); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2269 if (notice == null) { | 2325 if (notice == null) { |
2270 bool inconsistent = context.validateCacheConsistency(); | 2326 bool inconsistent = context.validateCacheConsistency(); |
2271 if (!inconsistent) { | 2327 if (!inconsistent) { |
2272 return; | 2328 return; |
2273 } | 2329 } |
2274 } | 2330 } |
2275 } | 2331 } |
2276 fail("performAnalysisTask failed to terminate after analyzing all sources"); | 2332 fail("performAnalysisTask failed to terminate after analyzing all sources"); |
2277 } | 2333 } |
2278 | 2334 |
| 2335 void _assertNoExceptions() { |
| 2336 MapIterator<AnalysisTarget, CacheEntry> iterator = analysisCache.iterator(); |
| 2337 String exceptionsStr = ''; |
| 2338 while (iterator.moveNext()) { |
| 2339 CaughtException exception = iterator.value.exception; |
| 2340 if (exception != null) { |
| 2341 AnalysisTarget target = iterator.key; |
| 2342 exceptionsStr += |
| 2343 '============= key: $target source: ${target.source}\n'; |
| 2344 exceptionsStr += exception.toString(); |
| 2345 exceptionsStr += '\n'; |
| 2346 } |
| 2347 } |
| 2348 if (exceptionsStr.isNotEmpty) { |
| 2349 fail(exceptionsStr); |
| 2350 } |
| 2351 } |
| 2352 |
2279 void _changeSource(TestSource source, String contents) { | 2353 void _changeSource(TestSource source, String contents) { |
2280 source.setContents(contents); | 2354 source.setContents(contents); |
2281 ChangeSet changeSet = new ChangeSet(); | 2355 ChangeSet changeSet = new ChangeSet(); |
2282 changeSet.changedSource(source); | 2356 changeSet.changedSource(source); |
2283 context.applyChanges(changeSet); | 2357 context.applyChanges(changeSet); |
2284 } | 2358 } |
2285 | 2359 |
2286 /** | 2360 /** |
2287 * Search the given compilation unit for a class with the given name. Return t
he class with the | 2361 * Search the given compilation unit for a class with the given name. Return t
he class with the |
2288 * given name, or `null` if the class cannot be found. | 2362 * given name, or `null` if the class cannot be found. |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2650 } | 2724 } |
2651 } | 2725 } |
2652 | 2726 |
2653 class _AnalysisContextImplTest_test_applyChanges_removeContainer | 2727 class _AnalysisContextImplTest_test_applyChanges_removeContainer |
2654 implements SourceContainer { | 2728 implements SourceContainer { |
2655 Source libB; | 2729 Source libB; |
2656 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB); | 2730 _AnalysisContextImplTest_test_applyChanges_removeContainer(this.libB); |
2657 @override | 2731 @override |
2658 bool contains(Source source) => source == libB; | 2732 bool contains(Source source) => source == libB; |
2659 } | 2733 } |
OLD | NEW |