mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add global setting for disabling type validity
This commit is contained in:
parent
423f21230a
commit
da1f604ef7
|
@ -28,6 +28,16 @@ class Container {
|
|||
|
||||
private static Registration[TypeInfo] registrations;
|
||||
|
||||
private static bool _globalTypeValidityCheckEnabled = true;
|
||||
|
||||
@property public static void globalTypeValidityCheckEnabled(bool enabled) {
|
||||
_globalTypeValidityCheckEnabled = enabled;
|
||||
}
|
||||
|
||||
@property public static bool globalTypeValidityCheckEnabled() {
|
||||
return _globalTypeValidityCheckEnabled;
|
||||
}
|
||||
|
||||
private this() {
|
||||
}
|
||||
|
||||
|
@ -39,7 +49,7 @@ class Container {
|
|||
TypeInfo registeredType = typeid(InterfaceType);
|
||||
TypeInfo_Class instantiatableType = typeid(ConcreteType);
|
||||
|
||||
if (checkTypeValidity) {
|
||||
if (globalTypeValidityCheckEnabled && checkTypeValidity) {
|
||||
checkValidity!(InterfaceType)(registeredType, instantiatableType);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,58 +1,68 @@
|
|||
import poodinis.container;
|
||||
|
||||
import std.exception;
|
||||
|
||||
version(unittest) {
|
||||
interface TestInterface {
|
||||
}
|
||||
|
||||
class TestClass : TestInterface {
|
||||
}
|
||||
|
||||
class UnrelatedClass{
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register concrete type
|
||||
Container.clearRegistrations();
|
||||
auto registration = Container.register!(TestClass)();
|
||||
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test resolve registered type
|
||||
Container.clearRegistrations();
|
||||
Container.register!(TestClass)();
|
||||
TestClass actualInstance = Container.resolve!(TestClass)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
assert(cast(TestClass) actualInstance, "Resolved class is not the same type as expected");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register interface
|
||||
Container.clearRegistrations();
|
||||
Container.register!(TestInterface, TestClass)();
|
||||
TestInterface actualInstance = Container.resolve!(TestInterface)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register unrelated types fails
|
||||
Container.clearRegistrations();
|
||||
assertThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register unrelated types with disabled check on registration
|
||||
Container.clearRegistrations();
|
||||
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test resolve non-registered type
|
||||
Container.clearRegistrations();
|
||||
assertThrown!ResolveException(Container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
|
||||
}
|
||||
|
||||
import poodinis.container;
|
||||
|
||||
import std.exception;
|
||||
|
||||
version(unittest) {
|
||||
interface TestInterface {
|
||||
}
|
||||
|
||||
class TestClass : TestInterface {
|
||||
}
|
||||
|
||||
class UnrelatedClass{
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register concrete type
|
||||
Container.clearRegistrations();
|
||||
auto registration = Container.register!(TestClass)();
|
||||
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test resolve registered type
|
||||
Container.clearRegistrations();
|
||||
Container.register!(TestClass)();
|
||||
TestClass actualInstance = Container.resolve!(TestClass)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
assert(cast(TestClass) actualInstance, "Resolved class is not the same type as expected");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register interface
|
||||
Container.clearRegistrations();
|
||||
Container.register!(TestInterface, TestClass)();
|
||||
TestInterface actualInstance = Container.resolve!(TestInterface)();
|
||||
assert(actualInstance !is null, "Resolved type is null");
|
||||
assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register unrelated types fails
|
||||
Container.clearRegistrations();
|
||||
assertThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register unrelated types with disabled check on registration
|
||||
Container.clearRegistrations();
|
||||
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test resolve non-registered type
|
||||
Container.clearRegistrations();
|
||||
assertThrown!ResolveException(Container.resolve!(TestClass)(), "Resolving non-registered type does not fail");
|
||||
}
|
||||
|
||||
unittest {
|
||||
// Test register unrelated class with disable global type validity disabled
|
||||
bool currentSetting = Container.globalTypeValidityCheckEnabled;
|
||||
Container.globalTypeValidityCheckEnabled = false;
|
||||
|
||||
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types while disabling global type validity fails");
|
||||
|
||||
Container.globalTypeValidityCheckEnabled = currentSetting;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue