Skip to content

Commit

Permalink
popScope now throws an exeption if already on the
Browse files Browse the repository at this point in the history
bsescope and not only an assertion
  • Loading branch information
escamoteur committed Apr 12, 2021
1 parent b8e143e commit 54b2cbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
8 changes: 4 additions & 4 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,10 @@ class _GetItImplementation implements GetIt {
/// As dispose functions can be async, you should await this function.
@override
Future<void> popScope() async {
assert(
_scopes.length > 1,
"You are already on the base scope. you can't pop this one",
);
throwIfNot(
_scopes.length > 1,
StateError(
"GetIt: You are already on the base scope. you can't pop this one"));
await _currentScope.dispose();
await _currentScope.reset(dispose: true);
_scopes.removeLast();
Expand Down
45 changes: 16 additions & 29 deletions test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,30 @@ void main() {

expect(disposeCounter, 3);
});

test('popscope until', () async {
test('popscope with destructors', () async {
final getIt = GetIt.instance;
constructorCounter = 0;

getIt.registerSingleton<TestClass>(TestClass(),
instanceName: 'scope0', dispose: (x) => x.dispose());

getIt.pushNewScope(scopeName: 'scope1', dispose: () => disposeCounter++);
getIt.registerSingleton<TestClass>(TestClass(),
instanceName: 'scope1', dispose: (x) => x.dispose());
getIt.registerSingleton<TestClass>(TestClass('Basescope'),
dispose: (x) => x.dispose());

getIt.pushNewScope(scopeName: 'scope2', dispose: () => disposeCounter++);
getIt.registerSingleton<TestClass>(TestClass(),
instanceName: 'scope2', dispose: (x) => x.dispose());
getIt.pushNewScope(dispose: () {
return disposeCounter++;
});

getIt.pushNewScope(scopeName: 'scope3', dispose: () => disposeCounter++);
getIt.registerSingleton<TestClass>(TestClass(),
instanceName: 'scope3', dispose: (x) => x.dispose());
getIt.registerSingleton<TestClass>(TestClass('2. scope'),
dispose: (x) => x.dispose());
getIt.registerSingleton<TestClass2>(TestClass2('2. scope'),
dispose: (x) => x.dispose());

expect(getIt<TestClass>(instanceName: 'scope0'), isNotNull);
expect(getIt<TestClass>(instanceName: 'scope1'), isNotNull);
expect(getIt<TestClass>(instanceName: 'scope2'), isNotNull);
expect(getIt<TestClass>(instanceName: 'scope3'), isNotNull);
expect(() => getIt.get<TestClass>(),
throwsA(const TypeMatcher<AssertionError>()));
await getIt.popScope();

await getIt.popScopesTill('scope2');
expect(disposeCounter, 3);
});

expect(getIt<TestClass>(instanceName: 'scope0'), isNotNull);
expect(getIt<TestClass>(instanceName: 'scope1'), isNotNull);
expect(() => getIt.get<TestClass>(instanceName: 'scope2'),
throwsA(const TypeMatcher<AssertionError>()));
expect(() => getIt.get<TestClass>(instanceName: 'scope3'),
throwsA(const TypeMatcher<AssertionError>()));
test('popscope throws if already on the base scope', () async {
final getIt = GetIt.instance;

expect(disposeCounter, 4);
expect(() => getIt.popScope(), throwsA(const TypeMatcher<StateError>()));
});

test('resetScope', () async {
Expand Down

0 comments on commit 54b2cbc

Please sign in to comment.