Chromium Code Reviews| Index: pkg/analyzer/test/src/dart/analysis/file_byte_store_test.dart |
| diff --git a/pkg/analyzer/test/src/dart/analysis/file_byte_store_test.dart b/pkg/analyzer/test/src/dart/analysis/file_byte_store_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d61645b4cd92cd56aa2f1b3a1aa4dc628f0f948 |
| --- /dev/null |
| +++ b/pkg/analyzer/test/src/dart/analysis/file_byte_store_test.dart |
| @@ -0,0 +1,110 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'package:analyzer/file_system/file_system.dart'; |
| +import 'package:analyzer/file_system/memory_file_system.dart'; |
| +import 'package:analyzer/src/dart/analysis/file_byte_store.dart'; |
| +import 'package:test/test.dart'; |
| +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| + |
| +main() { |
| + defineReflectiveSuite(() { |
| + defineReflectiveTests(FileByteStoreTest); |
| + }); |
| +} |
| + |
| +@reflectiveTest |
| +class FileByteStoreTest { |
| + final MemoryResourceProvider provider = new MemoryResourceProvider(); |
| + Folder folder; |
| + FileByteStore store; |
| + |
| + void setUp() { |
| + folder = provider.newFolder('/my/cache'); |
| + } |
| + |
| + test_evict_afterGet() { |
| + store = new FileByteStore(folder, '__temp', 100); |
| + |
| + store.put('1', _b(40)); |
| + store.put('2', _b(50)); |
| + store.get('1'); |
| + |
| + store.put('3', _b(30)); |
| + _assertExists('1', 40); |
| + _assertNotExists('2'); |
| + _assertExists('3', 30); |
| + } |
| + |
| + test_evict_first() { |
| + store = new FileByteStore(folder, '__temp', 100); |
| + |
| + store.put('1', _b(40)); |
| + store.put('2', _b(50)); |
| + _assertExists('1', 40); |
| + _assertExists('2', 50); |
| + |
| + store.put('3', _b(30)); |
| + _assertNotExists('1'); |
| + _assertExists('2', 50); |
| + _assertExists('3', 30); |
| + } |
| + |
| + test_evict_firstAndSecond() { |
| + store = new FileByteStore(folder, '__temp', 100); |
| + |
| + store.put('1', _b(10)); |
| + store.put('2', _b(80)); |
| + _assertExists('1', 10); |
| + _assertExists('2', 80); |
| + |
| + store.put('3', _b(30)); |
| + _assertNotExists('1'); |
| + _assertNotExists('2'); |
| + _assertExists('3', 30); |
| + } |
| + |
| + test_evict_preExisting() { |
| + folder.getChildAssumingFile('1').writeAsBytesSync(_b(40)); |
| + folder.getChildAssumingFile('2').writeAsBytesSync(_b(50)); |
| + store = new FileByteStore(folder, '__temp', 100); |
| + |
| + // Access '1', so now the queue looks [2, 1]. |
| + store.get('1'); |
| + |
| + // 40 + 50 + 30 > 100 |
| + // So, '2' is removed. |
| + store.put('3', _b(30)); |
| + _assertExists('1', 40); |
| + _assertNotExists('2'); |
| + _assertExists('3', 30); |
| + } |
| + |
| + test_get_null() { |
| + store = new FileByteStore(folder, '__temp', 1 << 10); |
| + List<int> bytes = store.get('foo'); |
| + expect(bytes, isNull); |
| + } |
| + |
| + test_put_get() { |
| + store = new FileByteStore(folder, '__temp', 1 << 10); |
| + List<int> bytes = [0, 1, 2, 3, 4]; |
| + store.put('foo', bytes); |
| + expect(store.get('foo'), bytes); |
| + } |
| + |
| + void _assertExists(String key, int length) { |
| + expect(folder.getChildAssumingFile(key).exists, isTrue); |
| + expect(store.get(key), hasLength(length)); |
| + } |
| + |
| + void _assertNotExists(String key) { |
| + expect(folder.getChildAssumingFile(key).exists, isFalse); |
| + expect(store.get(key), isNull); |
| + } |
| + |
| + static List<int> _b(int length) { |
|
Brian Wilkerson
2016/11/10 22:12:04
Really ?!? If we can't just write "new List<int>",
|
| + return new List<int>(length); |
| + } |
| +} |