diff --git a/polymorphism/test/CMakeLists.txt b/polymorphism/test/CMakeLists.txt index d351f0c..8ae12fd 100644 --- a/polymorphism/test/CMakeLists.txt +++ b/polymorphism/test/CMakeLists.txt @@ -15,17 +15,22 @@ add_library(polymorphism_lib ) target_compile_features(polymorphism_lib PUBLIC cxx_std_23) -target_include_directories(polymorphism_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include) -target_include_directories(polymorphism_lib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) +target_include_directories(polymorphism_lib + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) add_executable(test_consume - test_consume.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/test_consume.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/mocking.cpp ) target_compile_features(test_consume PUBLIC cxx_std_23) - -target_include_directories(test_consume PUBLIC ../include) -target_include_directories(test_consume PUBLIC "${ut_SOURCE_DIR}") +target_include_directories(test_consume + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../include + "${ut_SOURCE_DIR}" + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${CMAKE_CURRENT_SOURCE_DIR}/../src) target_link_libraries(test_consume PRIVATE polymorphism_lib) diff --git a/polymorphism/test/include/test_polymorphism/mocking.hpp b/polymorphism/test/include/test_polymorphism/mocking.hpp new file mode 100644 index 0000000..182aef5 --- /dev/null +++ b/polymorphism/test/include/test_polymorphism/mocking.hpp @@ -0,0 +1,33 @@ +#ifndef POYMORPHISM_MOCKING_HPP +#define POYMORPHISM_MOCKING_HPP + +#include +#include +#include + +namespace mocking { + +struct Mock { + std::list collectedSetArguments; + mutable std::size_t numberOfCallsToCoolFeature { 0 }; + + std::string coolFeature() const + { + ++numberOfCallsToCoolFeature; + + if (0 != collectedSetArguments.size()) { + return collectedSetArguments.back(); + } else { + return ""; + } + } + + void set(std::string s) + { + collectedSetArguments.emplace_back(std::move(s)); + } +}; + +} // namespace mocking + +#endif // POYMORPHISM_MOCKING_HPP \ No newline at end of file diff --git a/polymorphism/test/src/mocking.cpp b/polymorphism/test/src/mocking.cpp new file mode 100644 index 0000000..9089aeb --- /dev/null +++ b/polymorphism/test/src/mocking.cpp @@ -0,0 +1,7 @@ +#include +#include + +#include + +// explicit instantiation of consume() for Mock +template std::string modern::consume(mocking::Mock&); diff --git a/polymorphism/test/test_consume.cpp b/polymorphism/test/src/test_consume.cpp similarity index 61% rename from polymorphism/test/test_consume.cpp rename to polymorphism/test/src/test_consume.cpp index 1d7f51b..133eab6 100644 --- a/polymorphism/test/test_consume.cpp +++ b/polymorphism/test/src/test_consume.cpp @@ -5,6 +5,8 @@ #include #include +#include + int main() { using namespace boost::ut; @@ -37,6 +39,30 @@ int main() expect("The answer to all questions is 42"s == result); }; + then("the state of the argument should be modified as a side effect") = [=] { + expect("42"s == impl.coolFeature()); + }; + }; + }; + }; + + "[modern mock]"_test + = [] { + given("I have a an mock that adheres to a concept") = [] { + mocking::Mock impl; + expect(""s == impl.coolFeature()); + + when("I pass it to a function that expects an argument that fulfils the constraints") = [&] { + auto result = modern::consume(impl); + + then("set() should be called twice") = [=] { + expect(2 == impl.numberOfCallsToCoolFeature); + }; + + then("the answer to all questions should be given") = [=] { + expect("The answer to all questions is 42"s == result); + }; + then("the state of the argument should be modified as a side effect") = [=] { expect("42"s == impl.coolFeature()); };