From 98f12c5eb79c1f664af8daf5846f031d040d452b Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Fri, 23 Apr 2021 10:34:33 +0300 Subject: [PATCH] refactored code and implemented unit tests --- lib/experimental/scope.dart | 3 +- test/experimental/scope_test.dart | 91 +++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 test/experimental/scope_test.dart diff --git a/lib/experimental/scope.dart b/lib/experimental/scope.dart index 75d8684..3cc40e1 100644 --- a/lib/experimental/scope.dart +++ b/lib/experimental/scope.dart @@ -27,9 +27,8 @@ class Scope { /// /// return [Scope] Scope openSubScope(String name) { - final subScope = Scope(this); if (!_scopeMap.containsKey(name)) { - _scopeMap[name] = subScope; + _scopeMap[name] = Scope(this); } return _scopeMap[name]!; } diff --git a/test/experimental/scope_test.dart b/test/experimental/scope_test.dart new file mode 100644 index 0000000..96017cb --- /dev/null +++ b/test/experimental/scope_test.dart @@ -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(), throwsA(isA())); + }); + + test('Container resolves value after adding a dependency', () { + final expectedValue = "test string"; + final scope = new Scope(null) + .installModules([TestModule(value: expectedValue)]); + expect(scope.resolve(), 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(value: "string one")]); + final scope = new Scope(parentScope); + + expect( + () => scope.installModules([TestModule(value: "string two")]), + throwsA(isA())); + }); +*/ + test("Container resolve() returns a value from parent container.", () { + final expectedValue = 5; + final parentScope = Scope(null); + final scope = Scope(parentScope); + + parentScope.installModules([TestModule(value: expectedValue)]); + + expect(scope.resolve(), expectedValue); + }); + + test("Container resolve() returns a several value from parent container.", + () { + final expectedIntValue = 5; + final expectedStringValue = "Hello world"; + final parentScope = Scope(null).installModules([ + TestModule(value: expectedIntValue), + TestModule(value: expectedStringValue) + ]); + final scope = Scope(parentScope); + + expect(scope.resolve(), expectedIntValue); + expect(scope.resolve(), 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(), throwsA(isA())); + }); + }); +} + +class TestModule extends Module { + final T value; + final String? name; + + TestModule({required this.value, this.name}); + @override + void builder(Scope currentScope) { + if (name == null) { + bind().toInstance(value); + } else { + bind().withName(name!).toInstance(value); + } + } +}