From c37965b789331ba121654c9cf6ff3a57cb0c9467 Mon Sep 17 00:00:00 2001 From: Gabriel Barbosa Date: Mon, 15 Jan 2024 15:49:14 -0300 Subject: [PATCH 1/2] feature(is-registered): added a boolean method to indicate if a provider type or name is registered. --- kiwi/lib/src/kiwi_container.dart | 13 +++++++--- kiwi/test/kiwi_test.dart | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/kiwi/lib/src/kiwi_container.dart b/kiwi/lib/src/kiwi_container.dart index 2f0de40..bd02e12 100644 --- a/kiwi/lib/src/kiwi_container.dart +++ b/kiwi/lib/src/kiwi_container.dart @@ -150,11 +150,16 @@ class KiwiContainer { _namedProviders.clear(); } + /// Returns if an instance or builder of type [T] is registered. + /// + /// If an instance or builder of type [T] is registered with a name, + /// and the name is not passed to [isRegistered], returns false. + bool isRegistered({String? name}) { + return (_namedProviders.containsKey(name) && _namedProviders[name]!.containsKey(T)); + } + void _setProvider(String? name, _Provider provider) { - final nameProviders = _namedProviders; - if (!silent && - (nameProviders.containsKey(name) && - nameProviders[name]!.containsKey(T))) { + if (!silent && isRegistered(name: name)) { throw KiwiError( 'The type `$T` was already registered${name == null ? '' : ' for the name `$name`'}'); } diff --git a/kiwi/test/kiwi_test.dart b/kiwi/test/kiwi_test.dart index 57576ae..afb1f35 100644 --- a/kiwi/test/kiwi_test.dart +++ b/kiwi/test/kiwi_test.dart @@ -393,6 +393,49 @@ void main() { 'Not Registered KiwiError:\n\n\nFailed to resolve `int`:\n\nThe type `int` was not registered\n\nMake sure `int` is added to your KiwiContainer and rerun build_runner build\n(If you are using the kiwi_generator)\n\nWhen using Flutter, most of the time a hot restart is required to setup the KiwiContainer again.\n\n\n', ))); }); + + test('checks that the instances are registered', () { + final scoped = KiwiContainer.scoped(); + + // Unnamed instances + expect(scoped.isRegistered(), false); + + scoped.registerInstance(5); + + expect(scoped.isRegistered(), true); + expect(scoped.resolve(), 5); + + scoped.unregister(); + + expect(scoped.isRegistered(), false); + expect( + () => container.resolve(), + throwsA(TypeMatcher().having( + (f) => f.toString(), + 'toString()', + 'Not Registered KiwiError:\n\n\nFailed to resolve `int`:\n\nThe type `int` was not registered\n\nMake sure `int` is added to your KiwiContainer and rerun build_runner build\n(If you are using the kiwi_generator)\n\nWhen using Flutter, most of the time a hot restart is required to setup the KiwiContainer again.\n\n\n', + ))); + + // Named instances + expect(scoped.isRegistered(name: 'named_string_instance'), false); + + scoped.registerInstance('random_string', name: 'named_string_instance'); + + expect(scoped.isRegistered(), false); // [isRegistered] cannot be true if String it is named and is tested unnamed. + expect(scoped.isRegistered(name: 'named_string_instance'), true); + expect(scoped.resolve('named_string_instance'), 'random_string'); + + scoped.unregister('named_string_instance'); + + expect(scoped.isRegistered(), false); + expect( + () => container.resolve(), + throwsA(TypeMatcher().having( + (f) => f.toString(), + 'toString()', + 'Not Registered KiwiError:\n\n\nFailed to resolve `String`:\n\nThe type `String` was not registered\n\nMake sure `String` is added to your KiwiContainer and rerun build_runner build\n(If you are using the kiwi_generator)\n\nWhen using Flutter, most of the time a hot restart is required to setup the KiwiContainer again.\n\n\n', + ))); + }); }); } From f0b611c19c06fc7790f33cc0776ce5dc1cf020dd Mon Sep 17 00:00:00 2001 From: Gabriel Barbosa Date: Mon, 15 Jan 2024 15:51:39 -0300 Subject: [PATCH 2/2] fix(format): ran dart format in changed files. --- kiwi/lib/src/kiwi_container.dart | 3 ++- kiwi/test/kiwi_test.dart | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/kiwi/lib/src/kiwi_container.dart b/kiwi/lib/src/kiwi_container.dart index bd02e12..fd6c7df 100644 --- a/kiwi/lib/src/kiwi_container.dart +++ b/kiwi/lib/src/kiwi_container.dart @@ -155,7 +155,8 @@ class KiwiContainer { /// If an instance or builder of type [T] is registered with a name, /// and the name is not passed to [isRegistered], returns false. bool isRegistered({String? name}) { - return (_namedProviders.containsKey(name) && _namedProviders[name]!.containsKey(T)); + return (_namedProviders.containsKey(name) && + _namedProviders[name]!.containsKey(T)); } void _setProvider(String? name, _Provider provider) { diff --git a/kiwi/test/kiwi_test.dart b/kiwi/test/kiwi_test.dart index afb1f35..78e1ffe 100644 --- a/kiwi/test/kiwi_test.dart +++ b/kiwi/test/kiwi_test.dart @@ -419,9 +419,11 @@ void main() { // Named instances expect(scoped.isRegistered(name: 'named_string_instance'), false); - scoped.registerInstance('random_string', name: 'named_string_instance'); + scoped.registerInstance('random_string', + name: 'named_string_instance'); - expect(scoped.isRegistered(), false); // [isRegistered] cannot be true if String it is named and is tested unnamed. + expect(scoped.isRegistered(), + false); // [isRegistered] cannot be true if String it is named and is tested unnamed. expect(scoped.isRegistered(name: 'named_string_instance'), true); expect(scoped.resolve('named_string_instance'), 'random_string');