From 94926747a3e6f1ac7143fff71d865449b9ebc658 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 30 Jun 2022 11:14:03 +0100 Subject: [PATCH 1/5] feat: made resetAdapters public --- hive/lib/src/hive.dart | 8 +++++ hive/lib/src/hive_impl.dart | 3 ++ hive/lib/src/registry/type_registry_impl.dart | 1 - hive/test/tests/hive_impl_test.dart | 32 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index aa2231269..8bdaeb812 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -69,6 +69,14 @@ abstract class HiveInterface implements TypeRegistry { /// Checks if a box exists Future boxExists(String name, {String? path}); + + /// Clears all registered adapters. + /// + /// To register an adapter use [TypeRegistry.registerAdapter]. + /// + /// NOTE: [resetAdapters] also clears the default adapters registered + /// by Hive. + void resetAdapters(); } /// diff --git a/hive/lib/src/hive_impl.dart b/hive/lib/src/hive_impl.dart index 5ed61e21b..1744a5368 100644 --- a/hive/lib/src/hive_impl.dart +++ b/hive/lib/src/hive_impl.dart @@ -255,4 +255,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface { return await _manager.boxExists( lowerCaseName, path ?? homePath, collection); } + + + } diff --git a/hive/lib/src/registry/type_registry_impl.dart b/hive/lib/src/registry/type_registry_impl.dart index ee3ae7bfd..267fbff64 100644 --- a/hive/lib/src/registry/type_registry_impl.dart +++ b/hive/lib/src/registry/type_registry_impl.dart @@ -132,7 +132,6 @@ class TypeRegistryImpl implements TypeRegistry { return findAdapterForTypeId(typeId) != null; } - /// Not part of public API void resetAdapters() { _typeAdapters.clear(); } diff --git a/hive/test/tests/hive_impl_test.dart b/hive/test/tests/hive_impl_test.dart index d461e7daf..740653e33 100644 --- a/hive/test/tests/hive_impl_test.dart +++ b/hive/test/tests/hive_impl_test.dart @@ -9,6 +9,19 @@ import 'package:test/test.dart'; import 'common.dart'; +class _TestAdapter extends TypeAdapter { + _TestAdapter([this.typeId = 0]); + + @override + final int typeId; + + @override + int read(_) => 5; + + @override + void write(_, __) {} +} + void main() { group('HiveImpl', () { Future initHive() async { @@ -316,5 +329,24 @@ void main() { await hive.close(); }); }); + + group('.resetAdapters()', () { + test('returns normally', () async { + final hive = await initHive(); + expect(hive.resetAdapters, returnsNormally); + }); + + test('clears an adapter', () async { + final hive = await initHive(); + final adapter = _TestAdapter(1); + + expect(hive.isAdapterRegistered(adapter.typeId), isFalse); + hive.registerAdapter(adapter); + expect(hive.isAdapterRegistered(adapter.typeId), isTrue); + + hive.resetAdapters(); + expect(hive.isAdapterRegistered(adapter.typeId), isFalse); + }); + }); }); } From 58e3c4c89baed8db7ff7fb039c8b1125c19b17a7 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 30 Jun 2022 11:19:37 +0100 Subject: [PATCH 2/5] chore: run dart format --- hive/lib/src/hive.dart | 4 ++-- hive/lib/src/hive_impl.dart | 3 --- hive/test/tests/hive_impl_test.dart | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index 8bdaeb812..a67512654 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -71,9 +71,9 @@ abstract class HiveInterface implements TypeRegistry { Future boxExists(String name, {String? path}); /// Clears all registered adapters. - /// + /// /// To register an adapter use [TypeRegistry.registerAdapter]. - /// + /// /// NOTE: [resetAdapters] also clears the default adapters registered /// by Hive. void resetAdapters(); diff --git a/hive/lib/src/hive_impl.dart b/hive/lib/src/hive_impl.dart index 1744a5368..5ed61e21b 100644 --- a/hive/lib/src/hive_impl.dart +++ b/hive/lib/src/hive_impl.dart @@ -255,7 +255,4 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface { return await _manager.boxExists( lowerCaseName, path ?? homePath, collection); } - - - } diff --git a/hive/test/tests/hive_impl_test.dart b/hive/test/tests/hive_impl_test.dart index 740653e33..a9a113962 100644 --- a/hive/test/tests/hive_impl_test.dart +++ b/hive/test/tests/hive_impl_test.dart @@ -336,7 +336,7 @@ void main() { expect(hive.resetAdapters, returnsNormally); }); - test('clears an adapter', () async { + test('clears an adapter', () async { final hive = await initHive(); final adapter = _TestAdapter(1); From 2d557f5f018000197db055a012ff741ae119d2d0 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 30 Jun 2022 11:47:45 +0100 Subject: [PATCH 3/5] docs: improved documentation --- hive/lib/src/hive.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index a67512654..c697a6bc1 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -72,7 +72,7 @@ abstract class HiveInterface implements TypeRegistry { /// Clears all registered adapters. /// - /// To register an adapter use [TypeRegistry.registerAdapter]. + /// To register an adapter use [registerAdapter]. /// /// NOTE: [resetAdapters] also clears the default adapters registered /// by Hive. From 8f9cb4fecb656b8f211acb1524b078a1a03e8caf Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Thu, 30 Jun 2022 15:04:51 +0100 Subject: [PATCH 4/5] docs: PR suggestion by @themisir Co-authored-by: Misir Jafarov --- hive/lib/src/hive.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index c697a6bc1..0b95670b3 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -76,6 +76,9 @@ abstract class HiveInterface implements TypeRegistry { /// /// NOTE: [resetAdapters] also clears the default adapters registered /// by Hive. + /// + /// WARNING: This method is only intended to be used for integration and unit tests + /// and SHOULD not be used in production code. void resetAdapters(); } From d613ba65db48c1f9b889d7e28e2fe8a349a5b535 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 30 Jun 2022 15:06:13 +0100 Subject: [PATCH 5/5] refactor: included visibleForTesting --- hive/lib/src/hive.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart index 0b95670b3..beb49bd01 100644 --- a/hive/lib/src/hive.dart +++ b/hive/lib/src/hive.dart @@ -79,6 +79,7 @@ abstract class HiveInterface implements TypeRegistry { /// /// WARNING: This method is only intended to be used for integration and unit tests /// and SHOULD not be used in production code. + @visibleForTesting void resetAdapters(); }