refactored code

This commit is contained in:
Sergey Penkovsky
2021-10-20 09:15:51 +03:00
parent 4cb210d0c2
commit c49c9012ac
11 changed files with 17 additions and 21 deletions

296
test/src/binding_test.dart Normal file
View File

@@ -0,0 +1,296 @@
import 'package:cherrypick/src/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);
});
});
});
}

91
test/src/scope_test.dart Normal file
View File

@@ -0,0 +1,91 @@
import 'package:cherrypick/src/module.dart';
import 'package:cherrypick/src/scope.dart';
import 'package:test/test.dart';
void main() {
group('Without parent scope.', () {
test('Parent scope is null.', () {
final scope = Scope(null);
expect(scope.parentScope, null);
});
test('Open sub scope.', () {
final scope = 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 = Scope(null);
expect(() => scope.resolve<String>(), throwsA(isA<StateError>()));
});
test('Container resolves value after adding a dependency', () {
final expectedValue = 'test string';
final scope = 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);
}
}
}