mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Move value injection test to its own test file
This commit is contained in:
parent
e7e114d09f
commit
4954979574
|
@ -224,21 +224,6 @@ version(unittest) {
|
||||||
this(Ola ola) {}
|
this(Ola ola) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Thing {
|
|
||||||
int x;
|
|
||||||
}
|
|
||||||
|
|
||||||
class MyConfig {
|
|
||||||
@Value("conf.stuffs")
|
|
||||||
int stuffs;
|
|
||||||
|
|
||||||
@Value("conf.name")
|
|
||||||
string name;
|
|
||||||
|
|
||||||
@Value("conf.thing")
|
|
||||||
Thing thing;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test register concrete type
|
// Test register concrete type
|
||||||
unittest {
|
unittest {
|
||||||
auto container = new shared DependencyContainer();
|
auto container = new shared DependencyContainer();
|
||||||
|
@ -762,40 +747,4 @@ version(unittest) {
|
||||||
container.register!Hello;
|
container.register!Hello;
|
||||||
container.resolve!Hello;
|
container.resolve!Hello;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test injection of values
|
|
||||||
unittest {
|
|
||||||
auto container = new shared DependencyContainer();
|
|
||||||
container.register!MyConfig;
|
|
||||||
|
|
||||||
class IntInjector : ValueInjector!int {
|
|
||||||
public override int get(string key) {
|
|
||||||
assert(key == "conf.stuffs");
|
|
||||||
return 364;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class StringInjector : ValueInjector!string {
|
|
||||||
public override string get(string key) {
|
|
||||||
assert(key == "conf.name");
|
|
||||||
return "Le Chef";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ThingInjector : ValueInjector!Thing {
|
|
||||||
public override Thing get(string key) {
|
|
||||||
assert(key == "conf.thing");
|
|
||||||
return Thing(8899);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
container.register!(ValueInjector!int, IntInjector);
|
|
||||||
container.register!(ValueInjector!string, StringInjector);
|
|
||||||
container.register!(ValueInjector!Thing, ThingInjector);
|
|
||||||
|
|
||||||
auto instance = container.resolve!MyConfig;
|
|
||||||
assert(instance.stuffs == 364);
|
|
||||||
assert(instance.name == "Le Chef");
|
|
||||||
assert(instance.thing.x == 8899);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
61
test/poodinis/valueinjectiontest.d
Normal file
61
test/poodinis/valueinjectiontest.d
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
/**
|
||||||
|
* Poodinis Dependency Injection Framework
|
||||||
|
* Copyright 2014-2016 Mike Bierlee
|
||||||
|
* This software is licensed under the terms of the MIT license.
|
||||||
|
* The full terms of the license can be found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import poodinis;
|
||||||
|
|
||||||
|
version(unittest) {
|
||||||
|
struct Thing {
|
||||||
|
int x;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyConfig {
|
||||||
|
@Value("conf.stuffs")
|
||||||
|
int stuffs;
|
||||||
|
|
||||||
|
@Value("conf.name")
|
||||||
|
string name;
|
||||||
|
|
||||||
|
@Value("conf.thing")
|
||||||
|
Thing thing;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test injection of values
|
||||||
|
unittest {
|
||||||
|
auto container = new shared DependencyContainer();
|
||||||
|
container.register!MyConfig;
|
||||||
|
|
||||||
|
class IntInjector : ValueInjector!int {
|
||||||
|
public override int get(string key) {
|
||||||
|
assert(key == "conf.stuffs");
|
||||||
|
return 364;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StringInjector : ValueInjector!string {
|
||||||
|
public override string get(string key) {
|
||||||
|
assert(key == "conf.name");
|
||||||
|
return "Le Chef";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThingInjector : ValueInjector!Thing {
|
||||||
|
public override Thing get(string key) {
|
||||||
|
assert(key == "conf.thing");
|
||||||
|
return Thing(8899);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
container.register!(ValueInjector!int, IntInjector);
|
||||||
|
container.register!(ValueInjector!string, StringInjector);
|
||||||
|
container.register!(ValueInjector!Thing, ThingInjector);
|
||||||
|
|
||||||
|
auto instance = container.resolve!MyConfig;
|
||||||
|
assert(instance.stuffs == 364);
|
||||||
|
assert(instance.name == "Le Chef");
|
||||||
|
assert(instance.thing.x == 8899);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue