diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 383e01f..fd47535 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -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); } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 829b4ed..5bde6b9 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -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; + } + } \ No newline at end of file