Skip to content

Commit

Permalink
reorganize test code, add mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
daixtrose committed Oct 23, 2024
1 parent ce16916 commit 3caf553
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
17 changes: 11 additions & 6 deletions polymorphism/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
33 changes: 33 additions & 0 deletions polymorphism/test/include/test_polymorphism/mocking.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef POYMORPHISM_MOCKING_HPP
#define POYMORPHISM_MOCKING_HPP

#include <cstddef>
#include <list>
#include <string>

namespace mocking {

struct Mock {
std::list<std::string> collectedSetArguments;
mutable std::size_t numberOfCallsToCoolFeature { 0 };

std::string coolFeature() const
{
++numberOfCallsToCoolFeature;

if (0 != collectedSetArguments.size()) {
return collectedSetArguments.back();
} else {
return "<default value>";
}
}

void set(std::string s)
{
collectedSetArguments.emplace_back(std::move(s));
}
};

} // namespace mocking

#endif // POYMORPHISM_MOCKING_HPP
7 changes: 7 additions & 0 deletions polymorphism/test/src/mocking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <consume_class_that_adheres_to_concept.ipp>
#include <test_polymorphism/mocking.hpp>

#include <string>

// explicit instantiation of consume() for Mock
template std::string modern::consume<mocking::Mock>(mocking::Mock&);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <polymorphism/impl_with_interface.hpp>
#include <polymorphism/impl_without_interface.hpp>

#include <test_polymorphism/mocking.hpp>

int main()
{
using namespace boost::ut;
Expand Down Expand Up @@ -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("<default value>"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());
};
Expand Down

0 comments on commit 3caf553

Please sign in to comment.