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

Adjust and add tests for popScopesTill #385

Merged
merged 2 commits into from
Oct 24, 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
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
Loading