Add tests verifying that dependency injection is performed for value injectors

This commit is contained in:
Mike Bierlee 2016-12-11 17:12:21 +01:00
parent 341fad5db5
commit 7c718ec159

View file

@ -10,6 +10,8 @@ import poodinis;
import std.exception; import std.exception;
version(unittest) { version(unittest) {
class Dependency {}
struct Thing { struct Thing {
int x; int x;
} }
@ -130,4 +132,55 @@ version(unittest) {
assertThrown!ResolveException(container.resolve!ConfigWithMandatory); assertThrown!ResolveException(container.resolve!ConfigWithMandatory);
assertThrown!ValueInjectionException(autowire(container, new ConfigWithMandatory())); assertThrown!ValueInjectionException(autowire(container, new ConfigWithMandatory()));
} }
// Test injecting dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
auto dependency = new Dependency();
container.register!Dependency.existingInstance(dependency);
class IntInjector : ValueInjector!int {
@Autowire
public Dependency dependency;
public override int get(string key) {
return 2345;
}
}
container.register!(ValueInjector!int, IntInjector);
auto injector = cast(IntInjector) container.resolve!(ValueInjector!int);
assert(injector.dependency is dependency);
}
// Test injecting circular dependencies within value injectors
unittest {
auto container = new shared DependencyContainer();
class IntInjector : ValueInjector!int {
@Autowire
public ValueInjector!int dependency;
private int count = 0;
public override int get(string key) {
count += 1;
if (count >= 3) {
return count;
}
return dependency.get(key);
}
}
container.register!(ValueInjector!int, IntInjector);
auto injector = cast(IntInjector) container.resolve!(ValueInjector!int);
assert(injector.dependency is injector);
assert(injector.get("whatever") == 3);
}
} }