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 all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef POYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
#define POYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
#ifndef POLYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
#define POLYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Misspelled header guard found in has_super_cool_features.hpp

The header guard spelling correction should be applied consistently across the codebase. Found remaining instances of "POYMORPHISM" in:

  • polymorphism/include/polymorphism/has_super_cool_features.hpp
    • Line 1: #ifndef POYMORPHISM_HAS_SUPER_COOL_FEATURES_HPP
    • Line 2: #define POYMORPHISM_HAS_SUPER_COOL_FEATURES_HPP
    • Line 15: #endif // POYMORPHISM_HAS_SUPER_COOL_FEATURES_HPP
🔗 Analysis chain

Fixed header guard spelling.

The header guard spelling has been corrected from "POYMORPHISM" to "POLYMORPHISM", which improves code clarity and maintainability.

Let's verify there are no remaining instances of the misspelled header guard in the codebase:

Also applies to: 15-15

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining instances of the misspelled header guard
rg "POYMORPHISM" --type cpp

Length of output: 359


#include <polymorphism/has_super_cool_features.hpp>

Expand All @@ -12,4 +12,4 @@ std::string consume(has_super_cool_features auto& s);

} // namespace modern

#endif // POYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
#endif // POLYMORPHISM_CONSUME_CLASS_THAT_ADHERES_TO_CONCEPT_HPP
19 changes: 13 additions & 6 deletions polymorphism/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ 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
src/test_consume.cpp
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
# Library includes
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../src
# Test includes
${CMAKE_CURRENT_SOURCE_DIR}/include
"${ut_SOURCE_DIR}")

target_link_libraries(test_consume PRIVATE polymorphism_lib)

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

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

namespace mocking {

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

/**
* @brief Returns the last set value or a default value if none exists.
* @note Thread-safe. Multiple concurrent calls are safe.
* @return The last set string or "<default value>" if no strings were set
*/
std::string coolFeature() const
{
++numberOfCallsToCoolFeature;

return collectedSetArguments.empty()
? "<default value>"
: collectedSetArguments.back();
}

/**
* @brief Stores a string value for later retrieval by coolFeature
* @param s The string to store (must not be empty)
* @throws std::invalid_argument if the string is empty
*/
void set(std::string s)
{
if (s.empty()) {
throw std::invalid_argument("Empty strings are not allowed");
}
collectedSetArguments.emplace_back(std::move(s));
}
};

} // namespace mocking

#endif // POLYMORPHISM_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 @@ -43,4 +45,31 @@ int main()
};
};
};

"[modern mock]"_test = [] {
static constexpr auto EXPECTED_COOLFEATURE_CALLS = 2; // coolFeature() is called:
// 1. During initial value check (line 56)
// 2. During side effect verification (line 70)
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(EXPECTED_COOLFEATURE_CALLS == 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());
};
};
};
};
}