diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index e995f98..6ccea9b 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -1,6 +1,6 @@ # This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform. # See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml -name: CMake on multiple platforms +name: Multi-platform Build and Test on: push: diff --git a/include/mioc/detail/ServiceContainer.h b/include/mioc/detail/ServiceContainer.h index dfc7460..8059632 100644 --- a/include/mioc/detail/ServiceContainer.h +++ b/include/mioc/detail/ServiceContainer.h @@ -26,10 +26,10 @@ class ServiceContainer final } // Any copy of service container is not allowed. - ServiceContainer(const ServiceContainer&) = delete; - ServiceContainer(const ServiceContainer&&) = delete; - ServiceContainer& operator=(const ServiceContainer&) = delete; - ServiceContainer& operator=(const ServiceContainer&&) = delete; + ServiceContainer(const ServiceContainer& other) = default; + ServiceContainer& operator=(const ServiceContainer& other) = default; + ServiceContainer(ServiceContainer&& other) noexcept = default; + ServiceContainer& operator=(ServiceContainer&& other) noexcept = default; ~ServiceContainer() = default; diff --git a/include/mioc/detail/SingletonContainer.h b/include/mioc/detail/SingletonContainer.h index 3738e13..1ab51e4 100644 --- a/include/mioc/detail/SingletonContainer.h +++ b/include/mioc/detail/SingletonContainer.h @@ -28,9 +28,6 @@ class SingletonContainer final private: // Prevent instantiation. SingletonContainer() = default; - - static std::shared_ptr _container; - static bool _lazy; }; MIOC_END diff --git a/src/ServiceContainer.cpp b/src/ServiceContainer.cpp index a843209..4ae8d5f 100644 --- a/src/ServiceContainer.cpp +++ b/src/ServiceContainer.cpp @@ -2,6 +2,8 @@ #include +#include + MIOC_BEGIN // This is a random number, change it as you wish. diff --git a/src/SingletonContainer.cpp b/src/SingletonContainer.cpp index 9037997..fb23c02 100644 --- a/src/SingletonContainer.cpp +++ b/src/SingletonContainer.cpp @@ -6,20 +6,13 @@ MIOC_BEGIN ServiceContainerPtr SingletonContainer::GetContainer() { - if (_container == nullptr) - { - _container = ServiceContainer::New(_lazy); - } - return _container; + return GetContainer(DEFAULT_LAZINESS); } ServiceContainerPtr SingletonContainer::GetContainer(bool lazy) { - _lazy = lazy; - return GetContainer(); + static std::shared_ptr container = ServiceContainer::New(lazy); + return container; } -std::shared_ptr SingletonContainer::_container; -bool SingletonContainer::_lazy = DEFAULT_LAZINESS; - MIOC_END diff --git a/tests/AssignmentTest.cpp b/tests/AssignmentTest.cpp new file mode 100644 index 0000000..ebfae91 --- /dev/null +++ b/tests/AssignmentTest.cpp @@ -0,0 +1,37 @@ +#include +#include "Test.h" +#include "doctest.h" + +TEST_CASE("Assignment") +{ + mioc::ServiceContainerPtr containerA = mioc::ServiceContainer::New(); + mioc::ServiceContainerPtr containerB = mioc::ServiceContainer::New(); + + // Register interfaces and implementations. + containerA->AddSingleton(); + containerA->AddSingleton(); + + // Copy container. + *containerB = *containerA; + + auto a1 = containerA->Resolve(); + auto a2 = containerB->Resolve(); + CHECK_EQ(a1->ToString(), a2->ToString()); + + const auto b1 = containerA->Resolve(); + const auto b2 = containerB->Resolve(); + CHECK_EQ(b1->ToString(), b2->ToString()); + + // Register new instance. + const std::shared_ptr c = std::make_shared(b1); + containerA->AddSingleton(c); + CHECK_NE(containerA->Resolve(), nullptr); + CHECK_EQ(containerB->Resolve(), nullptr); + + // Move container. + mioc::ServiceContainerPtr containerC = mioc::ServiceContainer::New(); + *containerC = std::move(*containerA); + CHECK_EQ(containerC->Resolve()->ToString(), a1->ToString()); + CHECK_EQ(containerC->Resolve()->ToString(), b1->ToString()); + CHECK_EQ(containerC->Resolve(), nullptr); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8839cd0..2eb18a0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,9 @@ -aux_source_directory(. TEST_FILES) -add_executable(tests ${TEST_FILES}) +set(LIBRARIES mioc) +set(MIOC_TESTS SingletonTest TransientTest AssignmentTest) -target_include_directories(tests PRIVATE .) - -target_link_libraries(tests mioc) - -add_test(NAME TestAll COMMAND tests) +foreach(test ${MIOC_TESTS}) + add_executable(${test} doctest.cpp ${test}.cpp) + target_include_directories(${test} PRIVATE .) + target_link_libraries(${test} ${LIBRARIES}) + add_test(NAME ${test} COMMAND ${test}) +endforeach(test ${MIOC_TESTS}) diff --git a/tests/Transient.cpp b/tests/TransientTest.cpp similarity index 100% rename from tests/Transient.cpp rename to tests/TransientTest.cpp