Skip to content

polymorphism: test mocking #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 POLYMORPHISM_MOCKING_HPP
#define POLYMORPHISM_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