mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
fixed binding and writed unit tests
This commit is contained in:
@@ -7,8 +7,8 @@ class Binding<T> {
|
|||||||
late Mode _mode;
|
late Mode _mode;
|
||||||
late Type _key;
|
late Type _key;
|
||||||
late String _name;
|
late String _name;
|
||||||
late T _instance;
|
T? _instance = null;
|
||||||
late T Function() _provider;
|
T? Function()? _provider = null;
|
||||||
late bool _isSingeltone = false;
|
late bool _isSingeltone = false;
|
||||||
late bool _isNamed = false;
|
late bool _isNamed = false;
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ class Binding<T> {
|
|||||||
/// return [Binding]
|
/// return [Binding]
|
||||||
Binding<T> singeltone() {
|
Binding<T> singeltone() {
|
||||||
if (_mode == Mode.PROVIDER_INSTANCE) {
|
if (_mode == Mode.PROVIDER_INSTANCE) {
|
||||||
_instance = _provider.call();
|
_instance = _provider?.call();
|
||||||
}
|
}
|
||||||
_isSingeltone = true;
|
_isSingeltone = true;
|
||||||
return this;
|
return this;
|
||||||
@@ -100,5 +100,5 @@ class Binding<T> {
|
|||||||
/// ENG: Resolve instance.
|
/// ENG: Resolve instance.
|
||||||
///
|
///
|
||||||
/// return [T]
|
/// return [T]
|
||||||
T? get provider => _provider.call();
|
T? get provider => _provider?.call();
|
||||||
}
|
}
|
||||||
|
|||||||
296
test/experimental/module_test.dart
Normal file
296
test/experimental/module_test.dart
Normal file
@@ -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<int>();
|
||||||
|
expect(binding.instance, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.instance, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
expect(binding.instance, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("With name.", () {
|
||||||
|
test("Binding resolves null", () {
|
||||||
|
final binding = Binding<int>().withName("expectedValue");
|
||||||
|
expect(binding.instance, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check key", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.key, int);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.instance, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
|
expect(binding.name, "expectedValue");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
expect(binding.instance, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("Check provide.", () {
|
||||||
|
group("Without name.", () {
|
||||||
|
test("Binding resolves null", () {
|
||||||
|
final binding = Binding<int>();
|
||||||
|
expect(binding.provider, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("With name.", () {
|
||||||
|
test("Binding resolves null", () {
|
||||||
|
final binding = Binding<int>().withName("expectedValue");
|
||||||
|
expect(binding.provider, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check key", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.key, int);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
|
expect(binding.name, "expectedValue");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue);
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("Check singeltone provide.", () {
|
||||||
|
group("Without name.", () {
|
||||||
|
test("Binding resolves null", () {
|
||||||
|
final binding = Binding<int>().singeltone();
|
||||||
|
expect(binding.provider, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding =
|
||||||
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("With name.", () {
|
||||||
|
test("Binding resolves null", () {
|
||||||
|
final binding = Binding<int>().withName("expectedValue").singeltone();
|
||||||
|
expect(binding.provider, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check mode", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
|
||||||
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check key", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
|
||||||
|
expect(binding.key, int);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check singeltone", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
|
||||||
|
expect(binding.isSingeltone, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding check value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
|
||||||
|
expect(binding.name, "expectedValue");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Binding resolves value", () {
|
||||||
|
final expectedValue = 5;
|
||||||
|
final binding = Binding<int>()
|
||||||
|
.withName("expectedValue")
|
||||||
|
.toProvide(() => expectedValue)
|
||||||
|
.singeltone();
|
||||||
|
expect(binding.provider, expectedValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user