Skip to content

Commit

Permalink
added test to get_it for reverse reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin De Keyser committed Jan 3, 2024
1 parent 5d955d1 commit b09abeb
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ class TestClassParam {
TestClassParam({this.param1, this.param2});
}

class TestClassDisposableWithDependency with Disposable {
final TestClassDisposable testClass;
TestClassDisposableWithDependency(this.testClass) {
constructorCounter *=
3; // with this multiplication we can detect the order of the constructor.
}

void dispose() {
disposeCounter *=
3; // with this multiplication we can detect the order of disposal.
}

@override
void onDispose() {
dispose();
}
}

void main() {
setUp(() async {
// make sure the instance is cleared before each test
Expand Down Expand Up @@ -936,6 +954,100 @@ void main() {

expect(instance, const TypeMatcher<Injector>());
});

test('deregister in the same order of registering', () async {
final getIt = GetIt.instance;
disposeCounter = 0;
constructorCounter = 0;

getIt.registerSingleton<TestClassDisposable>(TestClassDisposable());

final instance1 = getIt.get<TestClassDisposable>();

expect(instance1 is TestClassDisposable, true);

expect(constructorCounter, 1);

getIt.registerSingleton<TestClassDisposableWithDependency>(
TestClassDisposableWithDependency(getIt.get<TestClassDisposable>()),
);

final instance2 = getIt.get<TestClassDisposableWithDependency>();
expect(constructorCounter, 3);
expect(instance1 == instance2.testClass, true);

await getIt.unregister<TestClassDisposable>();
expect(disposeCounter, 1);

final instance3 = getIt.get<TestClassDisposableWithDependency>();

expect(instance2.testClass == instance3.testClass, true);

await getIt.unregister<TestClassDisposableWithDependency>();
expect(disposeCounter, 3);
});

test('deregister in reverse order of registering', () async {
final getIt = GetIt.instance;
disposeCounter = 0;
constructorCounter = 0;

getIt.registerSingleton<TestClassDisposable>(TestClassDisposable());

final instance1 = getIt.get<TestClassDisposable>();

expect(instance1 is TestClassDisposable, true);

expect(constructorCounter, 1);

getIt.registerSingleton<TestClassDisposableWithDependency>(
TestClassDisposableWithDependency(getIt.get<TestClassDisposable>()),
);

final instance2 = getIt.get<TestClassDisposableWithDependency>();
expect(constructorCounter, 3);
expect(instance1 == instance2.testClass, true);

await getIt.unregister<TestClassDisposableWithDependency>();
expect(disposeCounter, 0);

expect(
() => getIt<TestClassDisposableWithDependency>(),
throwsA(const TypeMatcher<StateError>()),
);

await getIt.unregister<TestClassDisposable>();
expect(disposeCounter, 1);
});

test('deregister in reverse order of registering using reset', () async {
final getIt = GetIt.instance;
disposeCounter = 0;
constructorCounter = 0;

getIt.registerSingleton<TestClassDisposable>(TestClassDisposable());

final instance1 = getIt.get<TestClassDisposable>();

expect(instance1 is TestClassDisposable, true);

expect(constructorCounter, 1);

getIt.registerSingleton<TestClassDisposableWithDependency>(
TestClassDisposableWithDependency(getIt.get<TestClassDisposable>()),
);

final instance2 = getIt.get<TestClassDisposableWithDependency>();
expect(constructorCounter, 3);
expect(instance1 == instance2.testClass, true);

await getIt.reset();
expect(
disposeCounter,
1,
reason: "getIt.reset() did not dispose in reverse order",
);
});
}

class SingletonInjector {
Expand Down

0 comments on commit b09abeb

Please sign in to comment.