Skip to content

Commit

Permalink
Merge pull request #385 from kuhnroyal/popScopesTill-tests
Browse files Browse the repository at this point in the history
Adjust and add tests for popScopesTill
  • Loading branch information
escamoteur authored Oct 24, 2024
2 parents c48f5ca + 4f14690 commit 954c600
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
21 changes: 12 additions & 9 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1474,18 +1474,21 @@ class _GetItImplementation implements GetIt {
}
String? poppedScopeName;
_Scope nextScopeToPop = _currentScope;
do {
bool somethingWasPopped = false;

while (nextScopeToPop.name != _baseScopeName &&
hasScope(scopeName) &&
(nextScopeToPop.name != scopeName || inclusive)) {
poppedScopeName = nextScopeToPop.name;
await dropScope(poppedScopeName!);
somethingWasPopped = true;
nextScopeToPop = _scopes.lastWhere((x) => x.isPopping == false);
if (nextScopeToPop.name == _baseScopeName) {
return true;
}
} while (hasScope(scopeName) && inclusive
? (poppedScopeName != scopeName)
: (nextScopeToPop.name != scopeName));
onScopeChanged?.call(false);
return true;
}

if (somethingWasPopped) {
onScopeChanged?.call(false);
}
return somethingWasPopped;
}

/// Disposes all registered factories and singletons in the provided scope
Expand Down
75 changes: 53 additions & 22 deletions test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ void main() {
expect(isShadowed, false);
expect(shadowingObject, shadowingInstance);
});

test('popscope', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
Expand Down Expand Up @@ -362,63 +363,92 @@ void main() {
);
});

test('popscopeuntil inclusive=true', () async {
test('popScopesTill inclusive=true', () async {
final getIt = GetIt.instance;
constructorCounter = 0;

getIt.registerSingleton<TestClass>(TestClass('Basescope'));

getIt.pushNewScope(scopeName: 'Level1');

getIt.registerSingleton<TestClass>(TestClass('2. scope'));
getIt.registerSingleton<TestClass>(TestClass('1. scope'));

getIt.pushNewScope(scopeName: 'Level2');
getIt.registerSingleton<TestClass>(TestClass('2. scope'));

getIt.pushNewScope(scopeName: 'Level3');
getIt.registerSingleton<TestClass>(TestClass('3. scope'));
expect(getIt.get<TestClass>().id, '3. scope');

final instanceTestClassScope3 = getIt.get<TestClass>();

expect(instanceTestClassScope3.id, '3. scope');
await getIt.popScopesTill('Level2');

await getIt.popScopesTill('Level1');

final instanceTestClassScope1 = getIt.get<TestClass>();

expect(instanceTestClassScope1.id, 'Basescope');
expect(getIt.get<TestClass>().id, '1. scope');
expect(
() => getIt.get<TestClass2>(),
throwsStateError,
);
});
test('popscopeuntil inclusive=false', () async {

test('popScopesTill inclusive=false', () async {
final getIt = GetIt.instance;
constructorCounter = 0;

getIt.registerSingleton<TestClass>(TestClass('Basescope'));

getIt.pushNewScope(scopeName: 'Level1');

getIt.registerSingleton<TestClass>(TestClass('2. scope'));
getIt.registerSingleton<TestClass>(TestClass('1. scope'));

getIt.pushNewScope(scopeName: 'Level2');
getIt.registerSingleton<TestClass>(TestClass('2. scope'));

getIt.pushNewScope(scopeName: 'Level3');
getIt.registerSingleton<TestClass>(TestClass('3. scope'));
expect(getIt.get<TestClass>().id, '3. scope');

final instanceTestClassScope3 = getIt.get<TestClass>();

expect(instanceTestClassScope3.id, '3. scope');
await getIt.popScopesTill('Level2', inclusive: false);

await getIt.popScopesTill('Level1', inclusive: false);

final instanceTestClassScope1 = getIt.get<TestClass>();

expect(instanceTestClassScope1.id, '2. scope');
expect(getIt.get<TestClass>().id, '2. scope');
expect(
() => getIt.get<TestClass2>(),
throwsStateError,
);
});

test('popScopesTill invalid scope', () async {
final getIt = GetIt.instance;

getIt.pushNewScope(scopeName: 'Level1');
getIt.pushNewScope(scopeName: 'Level2');
getIt.pushNewScope(scopeName: 'Level3');

expect(getIt.hasScope('Level1'), isTrue);
expect(getIt.hasScope('Level2'), isTrue);
expect(getIt.hasScope('Level3'), isTrue);

await getIt.popScopesTill('Level4');

expect(getIt.hasScope('Level1'), isTrue);
expect(getIt.hasScope('Level2'), isTrue);
expect(getIt.hasScope('Level3'), isTrue);
});

test('popScopesTill inclusive=false top scope', () async {
final getIt = GetIt.instance;

getIt.pushNewScope(scopeName: 'Level1');
getIt.pushNewScope(scopeName: 'Level2');
getIt.pushNewScope(scopeName: 'Level3');

expect(getIt.hasScope('Level1'), isTrue);
expect(getIt.hasScope('Level2'), isTrue);
expect(getIt.hasScope('Level3'), isTrue);

await getIt.popScopesTill('Level3', inclusive: false);

expect(getIt.hasScope('Level1'), isTrue);
expect(getIt.hasScope('Level2'), isTrue);
expect(getIt.hasScope('Level3'), isTrue);
});

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

Expand Down Expand Up @@ -446,6 +476,7 @@ void main() {

expect(disposeCounter, 3);
});

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

Expand Down

0 comments on commit 954c600

Please sign in to comment.