Skip to content

Commit

Permalink
Merge pull request #329 from subzero911/isFinal_test
Browse files Browse the repository at this point in the history
isFinal test
  • Loading branch information
escamoteur authored May 26, 2023
2 parents ebfaee6 + 62cd16a commit 847089b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ abstract class GetIt {
void Function(GetIt getIt)? init,
String? scopeName,
ScopeDisposeFunc? dispose,
bool isFinal,
});

/// Creates a new registration scope. If you register types after creating
Expand Down
56 changes: 34 additions & 22 deletions test/scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class TestClass extends TestBaseClass {
}
}

class TestClassShadowChangHandler extends TestBaseClass
with ShadowChangeHandlers {
class TestClassShadowChangHandler extends TestBaseClass with ShadowChangeHandlers {
final String? id;
final void Function(bool isShadowed, Object shadowIngObject) onShadowChange;

Expand Down Expand Up @@ -124,8 +123,7 @@ void main() {

getIt.pushNewScope();

final testClassShadowChangHandlerInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
final testClassShadowChangHandlerInstance = TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
getIt.registerSingleton<TestClassShadowChangHandler>(
testClassShadowChangHandlerInstance,
);
Expand Down Expand Up @@ -158,8 +156,7 @@ void main() {

getIt.pushNewScope();

final testClassShadowChangHandlerInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
final testClassShadowChangHandlerInstance = TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
getIt.registerSingleton<TestClassShadowChangHandler>(
testClassShadowChangHandlerInstance,
);
Expand All @@ -173,9 +170,7 @@ void main() {
expect(isShadowed, false);
expect(shadowingObject, testClassShadowChangHandlerInstance);
});
test(
'register lazySingleton in two scopes with ShadowChangeHandlers and scopeChangedHandler',
() async {
test('register lazySingleton in two scopes with ShadowChangeHandlers and scopeChangedHandler', () async {
final getIt = GetIt.instance;

int scopeChanged = 0;
Expand All @@ -196,8 +191,7 @@ void main() {

getIt.pushNewScope();

var testClassShadowChangHandlerInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
var testClassShadowChangHandlerInstance = TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
getIt.registerSingleton<TestBaseClass>(
testClassShadowChangHandlerInstance,
);
Expand All @@ -211,8 +205,7 @@ void main() {
final lazyInstance = getIt<TestBaseClass>();

getIt.pushNewScope();
testClassShadowChangHandlerInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');
testClassShadowChangHandlerInstance = TestClassShadowChangHandler((shadowState, shadow) {}, 'Scope 2');

getIt.registerSingleton<TestBaseClass>(
testClassShadowChangHandlerInstance,
Expand All @@ -229,8 +222,7 @@ void main() {
expect(scopeChanged, 4);
});

test('register AsyncSingleton in two scopes with ShadowChangeHandlers',
() async {
test('register AsyncSingleton in two scopes with ShadowChangeHandlers', () async {
final getIt = GetIt.instance;

bool isShadowed = false;
Expand All @@ -252,8 +244,7 @@ void main() {
getIt.registerSingletonAsync<TestBaseClass>(
() async {
await Future.delayed(const Duration(milliseconds: 100));
final newInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, '2, Scope');
final newInstance = TestClassShadowChangHandler((shadowState, shadow) {}, '2, Scope');
shadowingInstance = newInstance;
return newInstance;
},
Expand All @@ -277,9 +268,7 @@ void main() {
expect(shadowingObject, shadowingInstance);
});

test(
'register SingletonWidthDependies in two scopes with ShadowChangeHandlers',
() async {
test('register SingletonWidthDependies in two scopes with ShadowChangeHandlers', () async {
final getIt = GetIt.instance;

bool isShadowed = false;
Expand Down Expand Up @@ -307,8 +296,7 @@ void main() {
Object? shadowingInstance;
getIt.registerSingletonWithDependencies<TestBaseClass>(
() {
final newInstance =
TestClassShadowChangHandler((shadowState, shadow) {}, '2, Scope');
final newInstance = TestClassShadowChangHandler((shadowState, shadow) {}, '2, Scope');
shadowingInstance = newInstance;
return newInstance;
},
Expand Down Expand Up @@ -521,6 +509,30 @@ void main() {
);
});

test('isFinal', () async {
final getIt = GetIt.instance;

getIt.pushNewScope(
isFinal: true,
init: (getIt) {
getIt.registerSingleton(
TestClass(),
dispose: (x) => x.dispose(),
);
},
);

getIt.registerSingleton(TestClass2()); // gets into baseScope

getIt.popScope(); // it shouldn't affect the TestClass2

expect(
() => getIt.get<TestClass>(),
throwsA(const TypeMatcher<StateError>()),
);
expect(getIt.get<TestClass2>(), isNotNull);
});

test('resetScope', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
Expand Down

0 comments on commit 847089b

Please sign in to comment.