Add global setting for disabling type validity

This commit is contained in:
Mike Bierlee 2014-05-20 00:16:44 +02:00
parent 423f21230a
commit da1f604ef7
2 changed files with 78 additions and 58 deletions

View file

@ -28,6 +28,16 @@ class Container {
private static Registration[TypeInfo] registrations; 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() { private this() {
} }
@ -39,7 +49,7 @@ class Container {
TypeInfo registeredType = typeid(InterfaceType); TypeInfo registeredType = typeid(InterfaceType);
TypeInfo_Class instantiatableType = typeid(ConcreteType); TypeInfo_Class instantiatableType = typeid(ConcreteType);
if (checkTypeValidity) { if (globalTypeValidityCheckEnabled && checkTypeValidity) {
checkValidity!(InterfaceType)(registeredType, instantiatableType); checkValidity!(InterfaceType)(registeredType, instantiatableType);
} }

View file

@ -1,58 +1,68 @@
import poodinis.container; import poodinis.container;
import std.exception; import std.exception;
version(unittest) { version(unittest) {
interface TestInterface { interface TestInterface {
} }
class TestClass : TestInterface { class TestClass : TestInterface {
} }
class UnrelatedClass{ class UnrelatedClass{
} }
unittest { unittest {
// Test register concrete type // Test register concrete type
Container.clearRegistrations(); Container.clearRegistrations();
auto registration = Container.register!(TestClass)(); auto registration = Container.register!(TestClass)();
assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same"); assert(registration.registeredType == typeid(TestClass), "Type of registered type not the same");
} }
unittest { unittest {
// Test resolve registered type // Test resolve registered type
Container.clearRegistrations(); Container.clearRegistrations();
Container.register!(TestClass)(); Container.register!(TestClass)();
TestClass actualInstance = Container.resolve!(TestClass)(); TestClass actualInstance = Container.resolve!(TestClass)();
assert(actualInstance !is null, "Resolved type is null"); assert(actualInstance !is null, "Resolved type is null");
assert(cast(TestClass) actualInstance, "Resolved class is not the same type as expected"); assert(cast(TestClass) actualInstance, "Resolved class is not the same type as expected");
} }
unittest { unittest {
// Test register interface // Test register interface
Container.clearRegistrations(); Container.clearRegistrations();
Container.register!(TestInterface, TestClass)(); Container.register!(TestInterface, TestClass)();
TestInterface actualInstance = Container.resolve!(TestInterface)(); TestInterface actualInstance = Container.resolve!(TestInterface)();
assert(actualInstance !is null, "Resolved type is null"); assert(actualInstance !is null, "Resolved type is null");
assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected"); assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected");
} }
unittest { unittest {
// Test register unrelated types fails // Test register unrelated types fails
Container.clearRegistrations(); Container.clearRegistrations();
assertThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail"); assertThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail");
} }
unittest { unittest {
// Test register unrelated types with disabled check on registration // Test register unrelated types with disabled check on registration
Container.clearRegistrations(); Container.clearRegistrations();
assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails"); assertNotThrown!RegistrationException(Container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails");
} }
unittest { unittest {
// Test resolve non-registered type // Test resolve non-registered type
Container.clearRegistrations(); Container.clearRegistrations();
assertThrown!ResolveException(Container.resolve!(TestClass)(), "Resolving non-registered type does not fail"); 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;
}
} }