diff --git a/lib/experimental/binding.dart b/lib/experimental/binding.dart index 3ab9beb..6dc0e2a 100644 --- a/lib/experimental/binding.dart +++ b/lib/experimental/binding.dart @@ -7,8 +7,8 @@ class Binding { late Mode _mode; late Type _key; late String _name; - late T _instance; - late T Function() _provider; + T? _instance = null; + T? Function()? _provider = null; late bool _isSingeltone = false; late bool _isNamed = false; @@ -84,7 +84,7 @@ class Binding { /// return [Binding] Binding singeltone() { if (_mode == Mode.PROVIDER_INSTANCE) { - _instance = _provider.call(); + _instance = _provider?.call(); } _isSingeltone = true; return this; @@ -100,5 +100,5 @@ class Binding { /// ENG: Resolve instance. /// /// return [T] - T? get provider => _provider.call(); + T? get provider => _provider?.call(); } diff --git a/test/experimental/module_test.dart b/test/experimental/module_test.dart new file mode 100644 index 0000000..db57e7b --- /dev/null +++ b/test/experimental/module_test.dart @@ -0,0 +1,296 @@ +import 'package:dart_di/experimental/binding.dart'; +import 'package:test/test.dart'; + +void main() { + group("Check instance.", () { + group("Without name.", () { + test("Binding resolves null", () { + final binding = Binding(); + expect(binding.instance, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = Binding().toInstance(expectedValue); + + expect(binding.mode, Mode.INSTANCE); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = Binding().toInstance(expectedValue); + + expect(binding.isSingeltone, true); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding().toInstance(expectedValue); + + expect(binding.instance, expectedValue); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = Binding().toInstance(expectedValue); + expect(binding.instance, expectedValue); + }); + }); + + group("With name.", () { + test("Binding resolves null", () { + final binding = Binding().withName("expectedValue"); + expect(binding.instance, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + + expect(binding.mode, Mode.INSTANCE); + }); + + test("Binding check key", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + + expect(binding.key, int); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + + expect(binding.isSingeltone, true); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + + expect(binding.instance, expectedValue); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + + expect(binding.name, "expectedValue"); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = + Binding().withName("expectedValue").toInstance(expectedValue); + expect(binding.instance, expectedValue); + }); + }); + }); + + group("Check provide.", () { + group("Without name.", () { + test("Binding resolves null", () { + final binding = Binding(); + expect(binding.provider, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = Binding().toProvide(() => expectedValue); + + expect(binding.mode, Mode.PROVIDER_INSTANCE); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = Binding().toProvide(() => expectedValue); + + expect(binding.isSingeltone, false); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding().toProvide(() => expectedValue); + + expect(binding.provider, expectedValue); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = Binding().toProvide(() => expectedValue); + expect(binding.provider, expectedValue); + }); + }); + + group("With name.", () { + test("Binding resolves null", () { + final binding = Binding().withName("expectedValue"); + expect(binding.provider, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + + expect(binding.mode, Mode.PROVIDER_INSTANCE); + }); + + test("Binding check key", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + + expect(binding.key, int); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + + expect(binding.isSingeltone, false); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + + expect(binding.provider, expectedValue); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + + expect(binding.name, "expectedValue"); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue); + expect(binding.provider, expectedValue); + }); + }); + }); + + group("Check singeltone provide.", () { + group("Without name.", () { + test("Binding resolves null", () { + final binding = Binding().singeltone(); + expect(binding.provider, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = + Binding().toProvide(() => expectedValue).singeltone(); + + expect(binding.mode, Mode.PROVIDER_INSTANCE); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = + Binding().toProvide(() => expectedValue).singeltone(); + + expect(binding.isSingeltone, true); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = + Binding().toProvide(() => expectedValue).singeltone(); + + expect(binding.provider, expectedValue); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = + Binding().toProvide(() => expectedValue).singeltone(); + expect(binding.provider, expectedValue); + }); + }); + + group("With name.", () { + test("Binding resolves null", () { + final binding = Binding().withName("expectedValue").singeltone(); + expect(binding.provider, null); + }); + + test("Binding check mode", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + + expect(binding.mode, Mode.PROVIDER_INSTANCE); + }); + + test("Binding check key", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + + expect(binding.key, int); + }); + + test("Binding check singeltone", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + + expect(binding.isSingeltone, true); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + + expect(binding.provider, expectedValue); + }); + + test("Binding check value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + + expect(binding.name, "expectedValue"); + }); + + test("Binding resolves value", () { + final expectedValue = 5; + final binding = Binding() + .withName("expectedValue") + .toProvide(() => expectedValue) + .singeltone(); + expect(binding.provider, expectedValue); + }); + }); + }); +}