diff --git a/lib/src/json_cache.dart b/lib/src/json_cache.dart index ffc66eb..42cf242 100644 --- a/lib/src/json_cache.dart +++ b/lib/src/json_cache.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + /// Represents cached data in json format. /// ///> Cache is a hardware or software component that stores data so that future @@ -25,4 +27,9 @@ abstract class JsonCache { /// /// Returns `true` if there is cached data at [key]; `false` otherwise. Future contains(String key); + + /// Lists all keys. + /// + /// Returns an **unmodifiable** list of cache keys without duplicates. + Future> keys(); } diff --git a/lib/src/json_cache_exception.dart b/lib/src/json_cache_exception.dart index fa3270d..ac6110c 100644 --- a/lib/src/json_cache_exception.dart +++ b/lib/src/json_cache_exception.dart @@ -11,7 +11,7 @@ class JsonCacheException implements Exception { /// The original exception that indicated the failure of the caching /// operation. - final Exception? exception; + final Object? exception; /// Returns [extra] along with the original exception message. @override diff --git a/lib/src/json_cache_fake.dart b/lib/src/json_cache_fake.dart index 9cb4dd5..e293f5e 100644 --- a/lib/src/json_cache_fake.dart +++ b/lib/src/json_cache_fake.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + import 'package:json_cache/json_cache.dart'; /// In-memory cache without synchronization. @@ -50,4 +52,9 @@ class JsonCacheFake implements JsonCache { Future contains(String key) async { return _memory.containsKey(key); } + + @override + Future> keys() async { + return UnmodifiableListView(_memory.keys); + } } diff --git a/lib/src/json_cache_flutter_secure_storage.dart b/lib/src/json_cache_flutter_secure_storage.dart index 46e733c..3dad0fa 100644 --- a/lib/src/json_cache_flutter_secure_storage.dart +++ b/lib/src/json_cache_flutter_secure_storage.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; @@ -39,6 +40,11 @@ class JsonCacheFlutterSecureStorage implements JsonCache { @override Future contains(String key) async { - return _storage.containsKey(key: key); + return await _storage.containsKey(key: key); + } + + @override + Future> keys() async { + return UnmodifiableListView((await _storage.readAll()).keys); } } diff --git a/lib/src/json_cache_hive.dart b/lib/src/json_cache_hive.dart index 5911364..1b501fb 100644 --- a/lib/src/json_cache_hive.dart +++ b/lib/src/json_cache_hive.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'package:hive/hive.dart'; @@ -37,4 +38,9 @@ class JsonCacheHive implements JsonCache { Future contains(String key) async { return _box.containsKey(key); } + + @override + Future> keys() async { + return UnmodifiableListView(_box.keys.map((k) => k as String)); + } } diff --git a/lib/src/json_cache_hollow.dart b/lib/src/json_cache_hollow.dart index f1fcfe8..5b96f69 100644 --- a/lib/src/json_cache_hollow.dart +++ b/lib/src/json_cache_hollow.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + import 'package:json_cache/json_cache.dart'; /// Hollow (empty) [JsonCache] — It is intended to serve as a placeholder for @@ -38,4 +40,9 @@ class JsonCacheHollow implements JsonCache { /// Always returns `false`. @override Future contains(String key) async => false; + + /// An empty list of keys. + @override + Future> keys() async => + UnmodifiableListView(const []); } diff --git a/lib/src/json_cache_local_storage.dart b/lib/src/json_cache_local_storage.dart index 8b0325a..4d8e73e 100644 --- a/lib/src/json_cache_local_storage.dart +++ b/lib/src/json_cache_local_storage.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'package:json_cache/json_cache.dart'; @@ -44,4 +45,15 @@ final class JsonCacheLocalStorage implements JsonCache { @override Future contains(String key) async => _storage.getItem(key) != null; + + @override + Future> keys() async { + return UnmodifiableListView( + List.generate( + _storage.length, + (int i) => _storage.key(i)!, + growable: false, + ), + ); + } } diff --git a/lib/src/json_cache_mem.dart b/lib/src/json_cache_mem.dart index 65be8a9..807cb9f 100644 --- a/lib/src/json_cache_mem.dart +++ b/lib/src/json_cache_mem.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:collection'; import 'package:json_cache/json_cache.dart'; import 'package:mutex/mutex.dart'; @@ -93,8 +94,6 @@ class JsonCacheMem implements JsonCache { static final _shrMutex = ReadWriteMutex(); /// Frees up storage space in both the level2 cache and in-memory cache. - /// - /// Throws [JsonCacheException] to indicate operation failure. @override Future clear() async { await _mutex.protectWrite(() async { @@ -105,8 +104,6 @@ class JsonCacheMem implements JsonCache { /// Updates the data located at [key] in both the _level 2_ cache and /// in-memory cache. - /// - /// Throws [JsonCacheException] to indicate operation failure. @override Future refresh(String key, Map data) async { // ATTENTION: It is safer to copy the content of [data] before calling an @@ -160,4 +157,11 @@ class JsonCacheMem implements JsonCache { return foundInMemory ? foundInMemory : await _level2.contains(key); }); } + + @override + Future> keys() { + return _mutex.protectRead(() async { + return UnmodifiableListView(_memory.keys); + }); + } } diff --git a/lib/src/json_cache_safe_local_storage.dart b/lib/src/json_cache_safe_local_storage.dart index 0ca86bd..b426e3f 100644 --- a/lib/src/json_cache_safe_local_storage.dart +++ b/lib/src/json_cache_safe_local_storage.dart @@ -1,5 +1,7 @@ // ignore_for_file: avoid_dynamic_calls +import 'dart:collection'; + import 'package:json_cache/json_cache.dart'; import 'package:safe_local_storage/safe_local_storage.dart'; @@ -43,6 +45,14 @@ class JsonCacheSafeLocalStorage implements JsonCache { return (await _cachedData)[key] as Map?; } + @override + Future> keys() async { + final data = await _cachedData; + return UnmodifiableListView( + data.keys.map((k) => k as String), + ); + } + /// Gets the cached data stored in the local storage file. Future> get _cachedData async { return await _localStorage.read() as Map; diff --git a/lib/src/json_cache_shared_preferences.dart b/lib/src/json_cache_shared_preferences.dart index 9a58e5e..12749bc 100644 --- a/lib/src/json_cache_shared_preferences.dart +++ b/lib/src/json_cache_shared_preferences.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'package:json_cache/json_cache.dart'; @@ -47,4 +48,9 @@ class JsonCacheSharedPreferences implements JsonCache { Future contains(String key) async { return _sharedPreferences.containsKey(key); } + + @override + Future> keys() async { + return UnmodifiableListView(_sharedPreferences.getKeys()); + } } diff --git a/lib/src/json_cache_try.dart b/lib/src/json_cache_try.dart index 820211e..0b59007 100644 --- a/lib/src/json_cache_try.dart +++ b/lib/src/json_cache_try.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + import 'package:json_cache/json_cache.dart'; /// A [JsonCache] decorator that provides improved information about @@ -103,4 +105,19 @@ class JsonCacheTry implements JsonCache { ); } } + + @override + Future> keys() async { + try { + return await _wrapped.keys(); + } on Exception catch (ex, st) { + Error.throwWithStackTrace( + JsonCacheException( + extra: "Error retreiving the cache keys.", + exception: ex, + ), + st, + ); + } + } } diff --git a/lib/src/json_cache_wrap.dart b/lib/src/json_cache_wrap.dart index 7a25d78..5e39e04 100644 --- a/lib/src/json_cache_wrap.dart +++ b/lib/src/json_cache_wrap.dart @@ -1,3 +1,5 @@ +import 'dart:collection'; + import 'package:json_cache/json_cache.dart'; /// Decorator Envelope of [JsonCache]. @@ -30,4 +32,8 @@ abstract class JsonCacheWrap implements JsonCache { /// Forwards to the [JsonCache.contains] of its wrapped instance. @override Future contains(String key) => _wrapped.contains(key); + + /// Forwards to the [JsonCache.keys] of its wrapped instance. + @override + Future> keys() => _wrapped.keys(); } diff --git a/test/flutter_secure_storage_mock.dart b/test/flutter_secure_storage_mock.dart index ae0f976..34afcb1 100644 --- a/test/flutter_secure_storage_mock.dart +++ b/test/flutter_secure_storage_mock.dart @@ -90,9 +90,14 @@ class FlutterSecureStorageMock implements FlutterSecureStorage { WebOptions? webOptions, MacOsOptions? mOptions, WindowsOptions? wOptions, - }) { - // TODO: implement readAll - throw UnimplementedError(); + }) async { + // final jsonObj = await _fakeCache.value(key); + final keys = await _fakeCache.keys(); + final data = {}; + for (final key in keys) { + data[key] = json.encode(await _fakeCache.value(key)); + } + return data; } @override diff --git a/test/json_cache_fake_test.dart b/test/json_cache_fake_test.dart index 49129ec..f263966 100644 --- a/test/json_cache_fake_test.dart +++ b/test/json_cache_fake_test.dart @@ -4,8 +4,8 @@ import 'package:json_cache/json_cache.dart'; void main() { group('JsonCacheFake', () { const profKey = 'profile'; - const profData = {'id': 1, 'name': 'John Due'}; const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; const prefData = { 'theme': 'dark', 'notifications': {'enabled': true}, @@ -52,11 +52,6 @@ void main() { }); test('contains', () async { - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final fake = JsonCacheFake(); // update data await fake.refresh(profKey, profData); @@ -70,6 +65,14 @@ void main() { expect(await fake.contains(prefKey), false); expect(await fake.contains('a key'), false); }); + test('keys', () async { + final fake = JsonCacheFake(); + // update data + await fake.refresh(profKey, profData); + await fake.refresh(prefKey, prefData); + + expect(await fake.keys(), [profKey, prefKey]); + }); group('remove', () { test('default ctor', () async { final JsonCacheFake fakeCache = JsonCacheFake(); diff --git a/test/json_cache_flutter_secure_storage_test.dart b/test/json_cache_flutter_secure_storage_test.dart index 2c99c7c..b91dcc1 100644 --- a/test/json_cache_flutter_secure_storage_test.dart +++ b/test/json_cache_flutter_secure_storage_test.dart @@ -5,12 +5,17 @@ import 'flutter_secure_storage_mock.dart'; void main() { group('JsonCacheFlutterSecureStorage', () { + const profKey = 'profile'; + const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; test('clear, value, refresh', () async { final secStorageMock = FlutterSecureStorageMock(); final JsonCacheFlutterSecureStorage flutterSecureCache = JsonCacheFlutterSecureStorage(secStorageMock); - const profKey = 'profile'; - const profData = {'id': 1, 'name': 'John Due'}; await flutterSecureCache.refresh(profKey, profData); expect(secStorageMock.writeInvokations, 1); @@ -27,13 +32,6 @@ void main() { }); test('contains', () async { - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final secStorageMock = FlutterSecureStorageMock(); final JsonCacheFlutterSecureStorage flutterSecureCache = JsonCacheFlutterSecureStorage(secStorageMock); @@ -52,17 +50,20 @@ void main() { await flutterSecureCache.remove(prefKey); expect(await flutterSecureCache.contains(prefKey), false); }); + test('keys', () async { + final secStorageMock = FlutterSecureStorageMock(); + final JsonCacheFlutterSecureStorage flutterSecureCache = + JsonCacheFlutterSecureStorage(secStorageMock); + // update data + await flutterSecureCache.refresh(profKey, profData); + await flutterSecureCache.refresh(prefKey, prefData); + + expect(await flutterSecureCache.keys(), [profKey, prefKey]); + }); test('remove', () async { final secStorageMock = FlutterSecureStorageMock(); final JsonCacheFlutterSecureStorage flutterSecureCache = JsonCacheFlutterSecureStorage(secStorageMock); - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; await flutterSecureCache.refresh(profKey, profData); await flutterSecureCache.refresh(prefKey, prefData); expect(secStorageMock.writeInvokations, 2); diff --git a/test/json_cache_hive_test.dart b/test/json_cache_hive_test.dart index 0d74a6d..bbb0ad9 100644 --- a/test/json_cache_hive_test.dart +++ b/test/json_cache_hive_test.dart @@ -11,10 +11,15 @@ void main() { await tearDownTestHive(); }); group('JsonCacheHive', () { + const profKey = 'profile'; + const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; test('clear, value, refresh', () async { final box = await Hive.openBox('test-clear-value-refresh'); - const profKey = 'profile'; - const profData = {'id': 1, 'name': 'John Due'}; final JsonCacheHive hiveCache = JsonCacheHive(box); await hiveCache.refresh(profKey, profData); var prof = await hiveCache.value(profKey); @@ -25,13 +30,6 @@ void main() { }); test('contains', () async { - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final box = await Hive.openBox('test-contains-method'); final JsonCacheHive hiveCache = JsonCacheHive(box); // update data @@ -49,14 +47,17 @@ void main() { await hiveCache.remove(prefKey); expect(await hiveCache.contains(prefKey), false); }); + + test('keys', () async { + final box = await Hive.openBox('test-contains-method'); + final JsonCacheHive hiveCache = JsonCacheHive(box); + // update data + await hiveCache.refresh(profKey, profData); + await hiveCache.refresh(prefKey, prefData); + + expect(await hiveCache.keys(), [prefKey, profKey]); + }); test('remove', () async { - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final box = await Hive.openBox('test-remove'); final hiveCache = JsonCacheHive(box); await hiveCache.refresh(profKey, profData); diff --git a/test/json_cache_hollow_test.dart b/test/json_cache_hollow_test.dart new file mode 100644 index 0000000..a54b1f2 --- /dev/null +++ b/test/json_cache_hollow_test.dart @@ -0,0 +1,68 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:json_cache/json_cache.dart'; + +void main() { + group('JsonCacheHollow', () { + const profKey = 'profile'; + const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; + + test('clear', () async { + const JsonCacheHollow hollowCache = JsonCacheHollow(); + await hollowCache.refresh(profKey, profData); + await hollowCache.refresh(prefKey, prefData); + await hollowCache.clear(); + final cachedProf = await hollowCache.value(profKey); + expect(cachedProf, isNull); + final cachedPref = await hollowCache.value(prefKey); + expect(cachedPref, isNull); + }); + + test('contains', () async { + const fake = JsonCacheHollow(); + // update data + await fake.refresh(profKey, profData); + await fake.refresh(prefKey, prefData); + + expect(await fake.contains(profKey), false); + expect(await fake.contains(prefKey), false); + + await fake.remove(prefKey); + expect(await fake.contains(prefKey), false); + expect(await fake.contains('a key'), false); + }); + test('keys', () async { + const fake = JsonCacheHollow(); + await fake.refresh(profKey, profData); + await fake.refresh(prefKey, prefData); + + expect(await fake.keys(), const []); + }); + test('remove', () async { + const JsonCacheHollow hollowCache = JsonCacheHollow(); + await hollowCache.refresh(profKey, profData); + await hollowCache.refresh(prefKey, prefData); + await hollowCache.remove(profKey); + final cachedProf = await hollowCache.value(profKey); + expect(cachedProf, isNull); + await hollowCache.remove(prefKey); + final cachedPref = await hollowCache.value(prefKey); + expect(cachedPref, isNull); + }); + test('refresh and value', () async { + const JsonCacheHollow hollowCache = JsonCacheHollow(); + await hollowCache.refresh(profKey, profData); + await hollowCache.refresh(prefKey, prefData); + + final cachedProf = await hollowCache.value(profKey); + expect(cachedProf, isNull); + + final cachedPref = await hollowCache.value(prefKey); + expect(cachedPref, isNull); + }); + }); +} diff --git a/test/json_cache_local_storage_test.dart b/test/json_cache_local_storage_test.dart index 57c0412..b3eae78 100644 --- a/test/json_cache_local_storage_test.dart +++ b/test/json_cache_local_storage_test.dart @@ -5,9 +5,13 @@ import 'fake_local_storage.dart'; void main() { group('JsonCacheLocalStorage', () { + const profKey = 'profile'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; test('clear, recover and refresh', () async { - const profKey = 'profile'; - const profData = {'id': 1, 'name': 'John Due'}; final JsonCacheLocalStorage jsonCache = _fakeInstance; await jsonCache.refresh(profKey, profData); var prof = await jsonCache.value(profKey); @@ -18,13 +22,7 @@ void main() { }); test('contains', () async { - const profKey = 'profile'; const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final JsonCacheLocalStorage jsonCache = _fakeInstance; // update data await jsonCache.refresh(profKey, profData); @@ -41,14 +39,18 @@ void main() { await jsonCache.remove(prefKey); expect(await jsonCache.contains(prefKey), false); }); + + test('keys', () async { + const prefKey = 'preferences'; + final JsonCacheLocalStorage jsonCache = _fakeInstance; + // update data + await jsonCache.refresh(profKey, profData); + await jsonCache.refresh(prefKey, prefData); + + expect(await jsonCache.keys(), [profKey, prefKey]); + }); test('remove', () async { - const profKey = 'profile'; const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final JsonCacheLocalStorage jsonCache = _fakeInstance; await jsonCache.refresh(profKey, profData); await jsonCache.refresh(prefKey, prefData); diff --git a/test/json_cache_mem_test.dart b/test/json_cache_mem_test.dart index aecb4c3..49d0659 100644 --- a/test/json_cache_mem_test.dart +++ b/test/json_cache_mem_test.dart @@ -111,11 +111,6 @@ void main() { }); }); test('contains', () async { - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final memCache = JsonCacheMem(); // update data await memCache.refresh(profKey, profData); @@ -130,6 +125,14 @@ void main() { expect(await memCache.contains(prefKey), false); expect(await memCache.contains('a key'), false); }); + test('keys', () async { + final memCache = JsonCacheMem(); + // update data + await memCache.refresh(profKey, profData); + await memCache.refresh(prefKey, prefData); + + expect(await memCache.keys(), [profKey, prefKey]); + }); group('remove', () { test('default ctor', () async { final JsonCacheMem memCache = JsonCacheMem(); diff --git a/test/json_cache_safe_local_storage_test.dart b/test/json_cache_safe_local_storage_test.dart index be7eb47..98d7d5f 100644 --- a/test/json_cache_safe_local_storage_test.dart +++ b/test/json_cache_safe_local_storage_test.dart @@ -123,6 +123,13 @@ void main() { }, ); + test('keys', () async { + // update data + await jsonCacheSafeLocalStorage.refresh(profKey, profData); + await jsonCacheSafeLocalStorage.refresh(prefKey, prefData); + + expect(await jsonCacheSafeLocalStorage.keys(), [profKey, prefKey]); + }); test( 'clear', () async { diff --git a/test/json_cache_shared_preferences_test.dart b/test/json_cache_shared_preferences_test.dart index b31e374..c0172af 100644 --- a/test/json_cache_shared_preferences_test.dart +++ b/test/json_cache_shared_preferences_test.dart @@ -5,9 +5,14 @@ import 'package:shared_preferences/shared_preferences.dart'; void main() { SharedPreferences.setMockInitialValues({}); group('JsonCacheSharedPreferences', () { + const profKey = 'profile'; + const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; test('clear, value, refresh', () async { - const profKey = 'profile'; - const profData = {'id': 1, 'name': 'John Due'}; final JsonCacheSharedPreferences prefsCache = JsonCacheSharedPreferences(await SharedPreferences.getInstance()); await prefsCache.refresh(profKey, profData); @@ -19,13 +24,6 @@ void main() { }); test('contains', () async { - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final JsonCacheSharedPreferences prefs = JsonCacheSharedPreferences(await SharedPreferences.getInstance()); // update data @@ -43,14 +41,17 @@ void main() { await prefs.remove(prefKey); expect(await prefs.contains(prefKey), false); }); + + test('keys', () async { + final JsonCacheSharedPreferences prefs = + JsonCacheSharedPreferences(await SharedPreferences.getInstance()); + // update data + await prefs.refresh(profKey, profData); + await prefs.refresh(prefKey, prefData); + + expect(await prefs.keys(), [profKey, prefKey]); + }); test('remove', () async { - const profKey = 'profile'; - const prefKey = 'preferences'; - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final JsonCacheSharedPreferences prefsCache = JsonCacheSharedPreferences(await SharedPreferences.getInstance()); await prefsCache.refresh(profKey, profData); diff --git a/test/json_cache_try_test.dart b/test/json_cache_try_test.dart index 1f0cb68..b0f375d 100644 --- a/test/json_cache_try_test.dart +++ b/test/json_cache_try_test.dart @@ -42,6 +42,20 @@ void main() { // Check if the interaction occurred only once. verify(() => jsonCacheMock.contains('aKey')).called(1); }); + test('keys should throw "JsonCacheException"', () async { + final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock); + // Stub the 'contains' method. + when(() => jsonCacheMock.keys()).thenThrow(Exception('Cache Failure')); + // Verify no interactions have occurred. + verifyNever(() => jsonCacheMock.keys()); + // Interact with the jsonCacheTry instance. + expect( + () async => jsonCacheTry.keys(), + throwsA(const TypeMatcher()), + ); + // Check if the interaction occurred only once. + verify(() => jsonCacheMock.keys()).called(1); + }); test('refresh should throw "JsonCacheException"', () async { final JsonCacheTry jsonCacheTry = JsonCacheTry(jsonCacheMock); // Stub the 'refresh' method. diff --git a/test/json_cache_wrap_test.dart b/test/json_cache_wrap_test.dart index a48148a..fa24736 100644 --- a/test/json_cache_wrap_test.dart +++ b/test/json_cache_wrap_test.dart @@ -9,6 +9,11 @@ void main() { group('JsonCacheWrap', () { const profKey = 'profile'; const prefKey = 'preferences'; + const profData = {'id': 1, 'name': 'John Due'}; + const prefData = { + 'theme': 'dark', + 'notifications': {'enabled': true}, + }; group('clear, value and refresh', () { test('default ctor', () async { const Map data = { @@ -25,11 +30,6 @@ void main() { }); }); test('contains', () async { - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final wrap = JsonCacheTestWrap(); // update data @@ -45,12 +45,16 @@ void main() { expect(await wrap.contains(prefKey), false); expect(await wrap.contains('a key'), false); }); + test('keys', () async { + final wrap = JsonCacheTestWrap(); + + // update data + await wrap.refresh(profKey, profData); + await wrap.refresh(prefKey, prefData); + + expect(await wrap.keys(), [profKey, prefKey]); + }); test('remove', () async { - final profData = {'id': 1, 'name': 'John Due'}; - final prefData = { - 'theme': 'dark', - 'notifications': {'enabled': true}, - }; final wrap = JsonCacheTestWrap(); await wrap.refresh(profKey, profData); await wrap.refresh(prefKey, prefData);