| 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 import 'package:analyzer/dart/element/element.dart'; |
| 5 import 'package:analyzer/src/generated/source.dart'; | 6 import 'package:analyzer/src/generated/source.dart'; |
| 6 import 'package:analyzer/src/summary/idl.dart'; | |
| 7 import 'package:analyzer/src/summary/incremental_cache.dart'; | 7 import 'package:analyzer/src/summary/incremental_cache.dart'; |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 |
| 10 import '../../reflective_tests.dart'; | 10 import '../../reflective_tests.dart'; |
| 11 import '../abstract_single_unit.dart'; | 11 import '../abstract_single_unit.dart'; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 groupSep = ' | '; | 14 groupSep = ' | '; |
| 15 runReflectiveTests(IncrementalCacheTest); | 15 runReflectiveTests(IncrementalCacheTest); |
| 16 } | 16 } |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * TODO(scheglov) write more tests for invalidation. | 19 * TODO(scheglov) write more tests for invalidation. |
| 20 */ | 20 */ |
| 21 @reflectiveTest | 21 @reflectiveTest |
| 22 class IncrementalCacheTest extends AbstractSingleUnitTest { | 22 class IncrementalCacheTest extends AbstractSingleUnitTest { |
| 23 _TestCacheStorage storage = new _TestCacheStorage(); | 23 _TestCacheStorage storage = new _TestCacheStorage(); |
| 24 IncrementalCache cache; | 24 IncrementalCache cache; |
| 25 | 25 |
| 26 Source putLibrary(String path, String code) { |
| 27 Source source = addSource(path, code); |
| 28 LibraryElement libraryElement = context.computeLibraryElement(source); |
| 29 cache.putLibrary(libraryElement); |
| 30 return source; |
| 31 } |
| 32 |
| 33 void putTestLibrary(String code) { |
| 34 resolveTestUnit(code); |
| 35 cache.putLibrary(testLibraryElement); |
| 36 } |
| 37 |
| 26 @override | 38 @override |
| 27 void setUp() { | 39 void setUp() { |
| 28 super.setUp(); | 40 super.setUp(); |
| 29 cache = new IncrementalCache(storage, context, <int>[]); | 41 cache = new IncrementalCache(storage, context, <int>[]); |
| 30 } | 42 } |
| 31 | 43 |
| 32 void test_getSourceKind_library() { | 44 void test_getLibraryClosureBundles_emptyCache() { |
| 33 resolveTestUnit(r''' | 45 resolveTestUnit('main() {}'); |
| 46 // the cache is empty, no bundles |
| 47 List<LibraryBundleWithId> bundles = |
| 48 cache.getLibraryClosureBundles(testSource); |
| 49 expect(bundles, isNull); |
| 50 } |
| 51 |
| 52 void test_getLibraryClosureBundles_exportLib() { |
| 53 Source aSource = putLibrary('/a.dart', ''); |
| 54 putTestLibrary(r''' |
| 55 import 'a.dart'; |
| 34 main() {} | 56 main() {} |
| 35 '''); | 57 '''); |
| 36 cache.putLibrary(testLibraryElement); | 58 List<LibraryBundleWithId> bundles = |
| 59 cache.getLibraryClosureBundles(testSource); |
| 60 expect(bundles, isNotNull); |
| 61 expect(_getBundleSources(bundles), [testSource, aSource].toSet()); |
| 62 // remove the 'a.dart' bundle, 'test.dart' loading fails |
| 63 cache.clearInternalCaches(); |
| 64 storage.map.remove(_findBundleForSource(bundles, aSource).id); |
| 65 expect(cache.getLibraryClosureBundles(testSource), isNull); |
| 66 } |
| 67 |
| 68 void test_getLibraryClosureBundles_importLib() { |
| 69 Source aSource = putLibrary('/a.dart', ''); |
| 70 putTestLibrary(r''' |
| 71 import 'a.dart'; |
| 72 main() {} |
| 73 '''); |
| 74 List<LibraryBundleWithId> bundles = |
| 75 cache.getLibraryClosureBundles(testSource); |
| 76 expect(bundles, isNotNull); |
| 77 expect(_getBundleSources(bundles), [testSource, aSource].toSet()); |
| 78 // remove the 'a.dart' bundle, 'test.dart' loading fails |
| 79 cache.clearInternalCaches(); |
| 80 storage.map.remove(_findBundleForSource(bundles, aSource).id); |
| 81 expect(cache.getLibraryClosureBundles(testSource), isNull); |
| 82 } |
| 83 |
| 84 void test_getLibraryClosureBundles_importLib2() { |
| 85 Source aSource = putLibrary('/a.dart', ''); |
| 86 Source bSource = putLibrary('/b.dart', "import 'a.dart';"); |
| 87 putTestLibrary(r''' |
| 88 import 'b.dart'; |
| 89 main() {} |
| 90 '''); |
| 91 List<LibraryBundleWithId> bundles = |
| 92 cache.getLibraryClosureBundles(testSource); |
| 93 expect(bundles, isNotNull); |
| 94 expect(_getBundleSources(bundles), [testSource, aSource, bSource].toSet()); |
| 95 // remove the 'a.dart' bundle, 'test.dart' loading fails |
| 96 cache.clearInternalCaches(); |
| 97 storage.map.remove(_findBundleForSource(bundles, aSource).id); |
| 98 expect(cache.getLibraryClosureBundles(testSource), isNull); |
| 99 } |
| 100 |
| 101 void test_getLibraryClosureBundles_importSdk() { |
| 102 putTestLibrary(r''' |
| 103 import 'dart:async'; |
| 104 main() {} |
| 105 '''); |
| 106 List<LibraryBundleWithId> bundles = |
| 107 cache.getLibraryClosureBundles(testSource); |
| 108 expect(bundles, isNotNull); |
| 109 expect(_getBundleSources(bundles), [testSource].toSet()); |
| 110 } |
| 111 |
| 112 void test_getLibraryClosureBundles_onlyLibrary() { |
| 113 putTestLibrary(r''' |
| 114 main() {} |
| 115 '''); |
| 116 // the cache is empty, no bundles |
| 117 List<LibraryBundleWithId> bundles = |
| 118 cache.getLibraryClosureBundles(testSource); |
| 119 expect(bundles, isNotNull); |
| 120 } |
| 121 |
| 122 void test_getSourceKind_library() { |
| 123 putTestLibrary(r''' |
| 124 main() {} |
| 125 '''); |
| 37 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); | 126 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); |
| 38 } | 127 } |
| 39 | 128 |
| 40 void test_getSourceKind_library_usedAsPart() { | 129 void test_getSourceKind_library_usedAsPart() { |
| 41 verifyNoTestUnitErrors = false; | 130 verifyNoTestUnitErrors = false; |
| 42 Source fooSource = addSource( | 131 Source fooSource = addSource( |
| 43 '/foo.dart', | 132 '/foo.dart', |
| 44 r''' | 133 r''' |
| 45 import 'dart:math'; | 134 import 'dart:math'; |
| 46 '''); | 135 '''); |
| 47 resolveTestUnit(r''' | 136 putTestLibrary(r''' |
| 48 part 'foo.dart'; | 137 part 'foo.dart'; |
| 49 main() {} | 138 main() {} |
| 50 '''); | 139 '''); |
| 51 cache.putLibrary(testLibraryElement); | |
| 52 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); | 140 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); |
| 53 // not a part, but also not enough information to write it as a library | 141 // not a part, but also not enough information to write it as a library |
| 54 expect(cache.getSourceKind(fooSource), isNull); | 142 expect(cache.getSourceKind(fooSource), isNull); |
| 55 } | 143 } |
| 56 | 144 |
| 57 void test_getSourceKind_notCached() { | 145 void test_getSourceKind_notCached() { |
| 58 resolveTestUnit(r''' | 146 resolveTestUnit(r''' |
| 59 main() {} | 147 main() {} |
| 60 '''); | 148 '''); |
| 61 expect(cache.getSourceKind(testSource), isNull); | 149 expect(cache.getSourceKind(testSource), isNull); |
| 62 } | 150 } |
| 63 | 151 |
| 64 void test_getSourceKind_part() { | 152 void test_getSourceKind_part() { |
| 65 Source partSource = addSource('/foo.dart', 'part of lib;'); | 153 Source partSource = addSource('/foo.dart', 'part of lib;'); |
| 66 resolveTestUnit(r''' | 154 putTestLibrary(r''' |
| 67 library lib; | 155 library lib; |
| 68 part 'foo.dart'; | 156 part 'foo.dart'; |
| 69 '''); | 157 '''); |
| 70 cache.putLibrary(testLibraryElement); | |
| 71 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); | 158 expect(cache.getSourceKind(testSource), SourceKind.LIBRARY); |
| 72 expect(cache.getSourceKind(partSource), SourceKind.PART); | 159 expect(cache.getSourceKind(partSource), SourceKind.PART); |
| 73 } | 160 } |
| 74 | 161 |
| 75 void test_readBundle_emptyCache() { | 162 LibraryBundleWithId _findBundleForSource( |
| 76 resolveTestUnit('main() {}'); | 163 List<LibraryBundleWithId> bundles, Source source) { |
| 77 // the cache is empty, no bundle | 164 return bundles.singleWhere((b) => b.source == source); |
| 78 PackageBundle bundle = cache.readBundle(testSource); | |
| 79 expect(bundle, isNull); | |
| 80 } | 165 } |
| 81 | 166 |
| 82 void test_readBundle_exportLib() { | 167 Set<Source> _getBundleSources(List<LibraryBundleWithId> bundles) { |
| 83 addSource('/a.dart', '// 1'); | 168 return bundles.map((b) => b.source).toSet(); |
| 84 resolveTestUnit(r''' | |
| 85 export 'a.dart'; | |
| 86 main() {} | |
| 87 '''); | |
| 88 cache.putLibrary(testLibraryElement); | |
| 89 // has bundle | |
| 90 expect(cache.readBundle(testSource), isNotNull); | |
| 91 // update a.dart, no bundle | |
| 92 cache.clearInternalCaches(); | |
| 93 resourceProvider.updateFile('/a.dart', '// 2'); | |
| 94 expect(cache.readBundle(testSource), isNull); | |
| 95 } | |
| 96 | |
| 97 void test_readBundle_importLib() { | |
| 98 addSource('/a.dart', '// 1'); | |
| 99 resolveTestUnit(r''' | |
| 100 import 'a.dart'; | |
| 101 main() {} | |
| 102 '''); | |
| 103 cache.putLibrary(testLibraryElement); | |
| 104 // has bundle | |
| 105 expect(cache.readBundle(testSource), isNotNull); | |
| 106 // update a.dart, no bundle | |
| 107 cache.clearInternalCaches(); | |
| 108 resourceProvider.updateFile('/a.dart', '// 2'); | |
| 109 expect(cache.readBundle(testSource), isNull); | |
| 110 } | |
| 111 | |
| 112 void test_readBundle_importSdk() { | |
| 113 resolveTestUnit(r''' | |
| 114 import 'dart:async'; | |
| 115 main() {} | |
| 116 '''); | |
| 117 cache.putLibrary(testLibraryElement); | |
| 118 // has bundle | |
| 119 PackageBundle bundle = cache.readBundle(testSource); | |
| 120 expect(bundle, isNotNull); | |
| 121 } | |
| 122 | |
| 123 void test_readBundle_withoutDependencies() { | |
| 124 resolveTestUnit('main() {}'); | |
| 125 cache.putLibrary(testLibraryElement); | |
| 126 // has bundle | |
| 127 PackageBundle bundle = cache.readBundle(testSource); | |
| 128 expect(bundle, isNotNull); | |
| 129 } | 169 } |
| 130 } | 170 } |
| 131 | 171 |
| 132 /** | 172 /** |
| 133 * A [Map] based [CacheStorage]. | 173 * A [Map] based [CacheStorage]. |
| 134 */ | 174 */ |
| 135 class _TestCacheStorage implements CacheStorage { | 175 class _TestCacheStorage implements CacheStorage { |
| 136 final Map<String, List<int>> map = <String, List<int>>{}; | 176 final Map<String, List<int>> map = <String, List<int>>{}; |
| 137 | 177 |
| 138 @override | 178 @override |
| 139 List<int> get(String key) { | 179 List<int> get(String key) { |
| 140 return map[key]; | 180 return map[key]; |
| 141 } | 181 } |
| 142 | 182 |
| 143 @override | 183 @override |
| 144 void put(String key, List<int> bytes) { | 184 void put(String key, List<int> bytes) { |
| 145 map[key] = bytes; | 185 map[key] = bytes; |
| 146 } | 186 } |
| 147 } | 187 } |
| OLD | NEW |