Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(is-registered): added a boolean method to indicate if a provider type or name is registered. #103

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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