From 7c718ec1591c4f4bcc739db3ff368ed12cfef2a3 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 11 Dec 2016 17:12:21 +0100 Subject: [PATCH] Add tests verifying that dependency injection is performed for value injectors --- test/poodinis/valueinjectiontest.d | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/poodinis/valueinjectiontest.d b/test/poodinis/valueinjectiontest.d index 89ff347..de2496b 100644 --- a/test/poodinis/valueinjectiontest.d +++ b/test/poodinis/valueinjectiontest.d @@ -10,6 +10,8 @@ import poodinis; import std.exception; version(unittest) { + class Dependency {} + struct Thing { int x; } @@ -130,4 +132,55 @@ version(unittest) { assertThrown!ResolveException(container.resolve!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); + } }