mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
refactored code and implemented unit tests
This commit is contained in:
@@ -27,9 +27,8 @@ class Scope {
|
|||||||
///
|
///
|
||||||
/// return [Scope]
|
/// return [Scope]
|
||||||
Scope openSubScope(String name) {
|
Scope openSubScope(String name) {
|
||||||
final subScope = Scope(this);
|
|
||||||
if (!_scopeMap.containsKey(name)) {
|
if (!_scopeMap.containsKey(name)) {
|
||||||
_scopeMap[name] = subScope;
|
_scopeMap[name] = Scope(this);
|
||||||
}
|
}
|
||||||
return _scopeMap[name]!;
|
return _scopeMap[name]!;
|
||||||
}
|
}
|
||||||
|
|||||||
91
test/experimental/scope_test.dart
Normal file
91
test/experimental/scope_test.dart
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group("Without parent scope.", () {
|
||||||
|
test('Parent scope is null.', () {
|
||||||
|
final scope = new Scope(null);
|
||||||
|
expect(scope.parentScope, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Open sub scope.', () {
|
||||||
|
final scope = new Scope(null);
|
||||||
|
final subScope = scope.openSubScope("subScope");
|
||||||
|
expect(scope.openSubScope("subScope"), subScope);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Container throws state error if the value can't be resolved", () {
|
||||||
|
final scope = new Scope(null);
|
||||||
|
expect(() => scope.resolve<String>(), throwsA(isA<StateError>()));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Container resolves value after adding a dependency', () {
|
||||||
|
final expectedValue = "test string";
|
||||||
|
final scope = new Scope(null)
|
||||||
|
.installModules([TestModule<String>(value: expectedValue)]);
|
||||||
|
expect(scope.resolve<String>(), expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('With parent scope.', () {
|
||||||
|
/*
|
||||||
|
test(
|
||||||
|
"Container bind() throws state error (if it's parent already has a resolver)",
|
||||||
|
() {
|
||||||
|
final parentScope = new Scope(null)
|
||||||
|
.installModules([TestModule<String>(value: "string one")]);
|
||||||
|
final scope = new Scope(parentScope);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
() => scope.installModules([TestModule<String>(value: "string two")]),
|
||||||
|
throwsA(isA<StateError>()));
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
test("Container resolve() returns a value from parent container.", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final parentScope = Scope(null);
|
||||||
|
final scope = Scope(parentScope);
|
||||||
|
|
||||||
|
parentScope.installModules([TestModule<int>(value: expectedValue)]);
|
||||||
|
|
||||||
|
expect(scope.resolve<int>(), expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Container resolve() returns a several value from parent container.",
|
||||||
|
() {
|
||||||
|
final expectedIntValue = 5;
|
||||||
|
final expectedStringValue = "Hello world";
|
||||||
|
final parentScope = Scope(null).installModules([
|
||||||
|
TestModule<int>(value: expectedIntValue),
|
||||||
|
TestModule<String>(value: expectedStringValue)
|
||||||
|
]);
|
||||||
|
final scope = Scope(parentScope);
|
||||||
|
|
||||||
|
expect(scope.resolve<int>(), expectedIntValue);
|
||||||
|
expect(scope.resolve<String>(), expectedStringValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Container resolve() throws a state error if parent hasn't value too.",
|
||||||
|
() {
|
||||||
|
final parentScope = Scope(null);
|
||||||
|
final scope = Scope(parentScope);
|
||||||
|
expect(() => scope.resolve<int>(), throwsA(isA<StateError>()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestModule<T> extends Module {
|
||||||
|
final T value;
|
||||||
|
final String? name;
|
||||||
|
|
||||||
|
TestModule({required this.value, this.name});
|
||||||
|
@override
|
||||||
|
void builder(Scope currentScope) {
|
||||||
|
if (name == null) {
|
||||||
|
bind<T>().toInstance(value);
|
||||||
|
} else {
|
||||||
|
bind<T>().withName(name!).toInstance(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user