Skip to content

Commit

Permalink
Merge pull request #103 from gbtb16/feature/is-registered
Browse files Browse the repository at this point in the history
feature(is-registered): added a boolean method to indicate if a provider type or name is registered.
  • Loading branch information
gbtb16 authored Jan 15, 2024
2 parents 4c6f85f + f0b611c commit 5cb8eb9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
14 changes: 10 additions & 4 deletions kiwi/lib/src/kiwi_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,17 @@ 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<T>({String? name}) {
return (_namedProviders.containsKey(name) &&
_namedProviders[name]!.containsKey(T));
}

void _setProvider<T>(String? name, _Provider<T> provider) {
final nameProviders = _namedProviders;
if (!silent &&
(nameProviders.containsKey(name) &&
nameProviders[name]!.containsKey(T))) {
if (!silent && isRegistered<T>(name: name)) {
throw KiwiError(
'The type `$T` was already registered${name == null ? '' : ' for the name `$name`'}');
}
Expand Down
45 changes: 45 additions & 0 deletions kiwi/test/kiwi_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,51 @@ 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<int>(), false);

scoped.registerInstance<int>(5);

expect(scoped.isRegistered<int>(), true);
expect(scoped.resolve<int>(), 5);

scoped.unregister<int>();

expect(scoped.isRegistered<int>(), false);
expect(
() => container.resolve<int>(),
throwsA(TypeMatcher<KiwiError>().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<String>(name: 'named_string_instance'), false);

scoped.registerInstance<String>('random_string',
name: 'named_string_instance');

expect(scoped.isRegistered<String>(),
false); // [isRegistered] cannot be true if String it is named and is tested unnamed.
expect(scoped.isRegistered<String>(name: 'named_string_instance'), true);
expect(scoped.resolve<String>('named_string_instance'), 'random_string');

scoped.unregister<String>('named_string_instance');

expect(scoped.isRegistered<String>(), false);
expect(
() => container.resolve<String>(),
throwsA(TypeMatcher<KiwiError>().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',
)));
});
});
}

Expand Down

0 comments on commit 5cb8eb9

Please sign in to comment.