implement scope tests

This commit is contained in:
Sergey Penkovsky
2025-05-19 11:10:10 +03:00
parent 869f9123bc
commit 50652a14a9

View File

@@ -73,6 +73,120 @@ void main() {
expect(() => scope.resolve<int>(), throwsA(isA<StateError>()));
});
});
group('Named dependencies', () {
test('Resolve named binding', () {
final scope = Scope(null)
..installModules([
TestModule<String>(value: "first"),
TestModule<String>(value: "second", name: "special")
]);
expect(scope.resolve<String>(named: "special"), "second");
expect(scope.resolve<String>(), "first");
});
test('Named binding does not clash with unnamed', () {
final scope = Scope(null)
..installModules([
TestModule<String>(value: "foo", name: "bar"),
]);
expect(() => scope.resolve<String>(), throwsA(isA<StateError>()));
expect(scope.resolve<String>(named: "bar"), "foo");
});
test("tryResolve returns null for missing named", () {
final scope = Scope(null)
..installModules([
TestModule<String>(value: "foo"),
]);
expect(scope.tryResolve<String>(named: "bar"), isNull);
});
});
group('Provider with params', () {
test('Resolve dependency using providerWithParams', () {
final scope = Scope(null)
..installModules([
_InlineModule((m, s) {
m.bind<int>().toProvideWithParams((param) => (param as int) * 2);
}),
]);
expect(scope.resolve<int>(params: 3), 6);
expect(
() => scope.resolve<int>(),
throwsA(isA<StateError>()),
);
});
});
group('Async resolution', () {
test('Resolve async instance', () async {
final scope = Scope(null)
..installModules([
_InlineModule((m, s) {
m.bind<String>().toInstanceAsync(Future.value('async value'));
}),
]);
expect(await scope.resolveAsync<String>(), "async value");
});
test('Resolve async provider', () async {
final scope = Scope(null)
..installModules([
_InlineModule((m, s) {
m.bind<int>().toProvideAsync(() async => 7);
}),
]);
expect(await scope.resolveAsync<int>(), 7);
});
test('Resolve async provider with param', () async {
final scope = Scope(null)
..installModules([
_InlineModule((m, s) {
m.bind<int>().toProvideAsyncWithParams((x) async => (x as int) * 3);
}),
]);
expect(await scope.resolveAsync<int>(params: 2), 6);
expect(
() => scope.resolveAsync<int>(),
throwsA(isA<StateError>()),
);
});
test('tryResolveAsync returns null for missing', () async {
final scope = Scope(null);
final result = await scope.tryResolveAsync<String>();
expect(result, isNull);
});
});
group("Drop modules", () {
test("After dropModules resolves fail", () {
final scope = Scope(null)..installModules([TestModule<int>(value: 5)]);
expect(scope.resolve<int>(), 5);
scope.dropModules();
expect(() => scope.resolve<int>(), throwsA(isA<StateError>()));
});
});
group("Subscope closing", () {
test("closeSubScope removes subscope", () {
final scope = Scope(null);
final subScope = scope.openSubScope("child");
expect(scope.openSubScope("child"), same(subScope));
scope.closeSubScope("child");
final newSubScope = scope.openSubScope("child");
expect(newSubScope, isNot(same(subScope))); // New instance after close
});
});
group("tryResolve returns null if not found", () {
test("Returns null for missing dependency", () {
final scope = Scope(null);
expect(scope.tryResolve<int>(), isNull);
});
});
}
class TestModule<T> extends Module {
@@ -89,3 +203,12 @@ class TestModule<T> extends Module {
}
}
}
/// Вспомогательный модуль для подстановки builder'а через конструктор
class _InlineModule extends Module {
final void Function(Module, Scope) _builder;
_InlineModule(this._builder);
@override
void builder(Scope s) => _builder(this, s);
}