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); } } }