diff --git a/README.md b/README.md index bd2a77760..d1f705925 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ include(FetchContent) FetchContent_Declare( mu.tiny GIT_REPOSITORY https://github.com/thetic/mu.tiny.git - GIT_TAG v0.8.1 - FIND_PACKAGE_ARGS 0.8 + GIT_TAG v0.9.0 + FIND_PACKAGE_ARGS 0.9 ) FetchContent_MakeAvailable(mu.tiny) @@ -44,7 +44,7 @@ If _mu::tiny_ is already installed and you prefer not to use [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html) directly: ```cmake -find_package(mu.tiny 0.8 REQUIRED) +find_package(mu.tiny 0.9 REQUIRED) ``` ## Headers diff --git a/cfg/VERSION b/cfg/VERSION index 6f4eebdf6..ac39a106c 100644 --- a/cfg/VERSION +++ b/cfg/VERSION @@ -1 +1 @@ -0.8.1 +0.9.0 diff --git a/docs/getting-started.rst b/docs/getting-started.rst index dd6328e1d..ef834af95 100644 --- a/docs/getting-started.rst +++ b/docs/getting-started.rst @@ -29,8 +29,8 @@ is not installed, CMake fetches and builds it from source automatically: FetchContent_Declare( mu.tiny GIT_REPOSITORY https://github.com/thetic/mu.tiny.git - GIT_TAG v0.8.1 - FIND_PACKAGE_ARGS 0.8 + GIT_TAG v0.9.0 + FIND_PACKAGE_ARGS 0.9 ) FetchContent_MakeAvailable(mu.tiny) @@ -47,7 +47,7 @@ If *mu::tiny* is already installed and you prefer not to use .. code-block:: cmake - find_package(mu.tiny 0.8 REQUIRED) + find_package(mu.tiny 0.9 REQUIRED) Headers ~~~~~~~ diff --git a/docs/mocking.rst b/docs/mocking.rst index 5d95d3b1d..9d18b2091 100644 --- a/docs/mocking.rst +++ b/docs/mocking.rst @@ -137,6 +137,45 @@ For custom types use :cpp:func:`with_output_parameter_of_type() ` with a registered copier. +Capturing Parameters +--------------------- + +Output parameters move data in a fixed, known direction: test → code under +test. Sometimes you need the opposite — inspecting a value the code under test +passes *into* a mocked call, such as a generated id, a timestamp, or a struct +it populated. :cpp:func:`with_parameter_of_type() ` +doesn't help here since it requires an exact match *during* the call; capturing +lets you grab the value and assert on it *after* the call. + +On the expected side: + +.. code-block:: cpp + + Message captured; + mock().expect_one_call("send") + .with_captured_parameter_of_type("Message", "msg", &captured); + +On the actual call side (in your mock implementation): + +.. code-block:: cpp + + mock().actual_call("send") + .with_captured_parameter_of_type("Message", "msg", msg); + +.. code-block:: cpp + + // Back in the test, after exercising the code under test: + CHECK_EQUAL(42, captured.id); + +The framework copies the actual call's parameter into ``captured`` when the +call is matched — the mirror of `Output Parameters`_. For raw memory use +:cpp:func:`with_captured_parameter() ` + +:cpp:func:`with_captured_parameter() ` +(plain memcpy); for custom types use +:cpp:func:`with_captured_parameter_of_type() ` + +:cpp:func:`with_captured_parameter_of_type() ` +with a registered copier. + Return Values ------------- diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index dfa1b7d6a..010f0e365 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,8 +7,8 @@ if(PROJECT_IS_TOP_LEVEL) FetchContent_Declare( mu.tiny GIT_REPOSITORY https://github.com/thetic/mu.tiny.git - GIT_TAG v0.8.1 - FIND_PACKAGE_ARGS 0.8 + GIT_TAG v0.9.0 + FIND_PACKAGE_ARGS 0.9 ) FetchContent_MakeAvailable(mu.tiny) endif() diff --git a/include/mu/tiny/mock.h b/include/mu/tiny/mock.h index a86111f00..5de74416e 100644 --- a/include/mu/tiny/mock.h +++ b/include/mu/tiny/mock.h @@ -267,6 +267,30 @@ extern "C" void* value ); + /** + * @brief Report a captured parameter. + * @param name Parameter name. + * @param value Pointer to the data to copy into the test's buffer. + * @return this. + */ + struct MutinyMockActualCall* (*with_captured_parameter)( + const char* name, + const void* value + ); + + /** + * @brief Report a custom-type captured parameter. + * @param type Type name. + * @param name Parameter name. + * @param value Pointer to the object to copy into the test's buffer. + * @return this. + */ + struct MutinyMockActualCall* (*with_captured_parameter_of_type)( + const char* type, + const char* name, + const void* value + ); + /** @return Non-zero if the expectation set a return value. */ int (*has_return_value)(void); @@ -613,6 +637,32 @@ extern "C" const char* name ); + /** + * @brief Capture a parameter (plain @c memcpy). + * @param name Name. + * @param value Buffer to copy the actual value into. + * @param size Bytes to copy. + * @return this. + */ + struct MutinyMockExpectedCall* (*with_captured_parameter)( + const char* name, + void* value, + size_t size + ); + + /** + * @brief Capture a parameter of a custom object type. + * @param type Type name. + * @param name Name. + * @param value Buffer to copy the actual object into. + * @return this. + */ + struct MutinyMockExpectedCall* (*with_captured_parameter_of_type)( + const char* type, + const char* name, + void* value + ); + /** * @brief Allow the actual call to pass extra unlisted parameters. * @return this. diff --git a/include/mu/tiny/mock/ActualCall.hpp b/include/mu/tiny/mock/ActualCall.hpp index 71b0f9545..b620fa60d 100644 --- a/include/mu/tiny/mock/ActualCall.hpp +++ b/include/mu/tiny/mock/ActualCall.hpp @@ -145,6 +145,39 @@ class MUTINY_EXPORT ActualCall void* output ) = 0; + /** + * @brief Report a captured parameter by name. + * + * The framework copies @p value into the buffer configured on the + * matching expectation via @ref ExpectedCall::with_captured_parameter(), + * so the test can inspect it after the call. This is the mirror of + * with_output_parameter(): data flows from the actual call to the test + * instead of from the expectation to the actual call. + * + * @param name Parameter name. + * @param value Pointer to the data to copy into the test's buffer. + * @return *this for chaining. + */ + virtual ActualCall& with_captured_parameter( + StringView name, + const void* value + ) = 0; + + /** + * @brief Report a captured parameter with a custom object type. + * + * @param type_name Type name; must match the copier installed via + * @ref Support::install_copier(). + * @param name Parameter name. + * @param value Pointer to the object to copy into the test's buffer. + * @return *this for chaining. + */ + virtual ActualCall& with_captured_parameter_of_type( + StringView type_name, + StringView name, + const void* value + ) = 0; + /** @return true if the matching expectation set a return value. */ virtual bool has_return_value() = 0; /** @return The configured return value as a generic NamedValue. */ diff --git a/include/mu/tiny/mock/ExpectedCall.hpp b/include/mu/tiny/mock/ExpectedCall.hpp index 8dbd391b0..94ee09332 100644 --- a/include/mu/tiny/mock/ExpectedCall.hpp +++ b/include/mu/tiny/mock/ExpectedCall.hpp @@ -183,6 +183,43 @@ class MUTINY_EXPORT ExpectedCall */ virtual ExpectedCall& with_unmodified_output_parameter(StringView name) = 0; + /** + * @brief Capture a parameter: the framework will copy the value the actual + * call reports for this parameter into @p value (plain memcpy). + * + * Use this instead of with_parameter_of_type() when the parameter's value + * can't be predicted ahead of time (a generated id, a timestamp, a struct + * populated by the code under test) and the test needs to inspect it after + * the call rather than compare it during the call. + * + * @param name Parameter name. + * @param value Pointer to the buffer to copy the actual value into. + * @param size Number of bytes to copy. + * @return *this for chaining. + */ + virtual ExpectedCall& with_captured_parameter( + StringView name, + void* value, + size_t size + ) = 0; + + /** + * @brief Capture a parameter of a custom object type. + * + * Uses the NamedValueCopier installed for @p type_name to copy the actual + * object into @p value. + * + * @param type_name Type name; must match the copier key. + * @param name Parameter name. + * @param value Pointer to the buffer to copy the actual object into. + * @return *this for chaining. + */ + virtual ExpectedCall& with_captured_parameter_of_type( + StringView type_name, + StringView name, + void* value + ) = 0; + /** * @brief Allow the actual call to pass parameters not listed in this * expectation. diff --git a/src/mock/ActualCallTrace.cpp b/src/mock/ActualCallTrace.cpp index aff1ab98a..8876dbabf 100644 --- a/src/mock/ActualCallTrace.cpp +++ b/src/mock/ActualCallTrace.cpp @@ -70,6 +70,29 @@ ActualCall& ActualCallTrace::with_output_parameter_of_type( return *this; } +ActualCall& ActualCallTrace::with_captured_parameter( + StringView name, + const void* value +) +{ + add_parameter_name(name); + trace_buffer_ += string_from(value); + return *this; +} + +ActualCall& ActualCallTrace::with_captured_parameter_of_type( + StringView type_name, + StringView name, + const void* value +) +{ + trace_buffer_ += " "; + trace_buffer_ += type_name.c_str(); + add_parameter_name(name); + trace_buffer_ += string_from(value); + return *this; +} + bool ActualCallTrace::has_return_value() { return false; diff --git a/src/mock/CheckedActualCall.cpp b/src/mock/CheckedActualCall.cpp index 5780d422a..9122f68c9 100644 --- a/src/mock/CheckedActualCall.cpp +++ b/src/mock/CheckedActualCall.cpp @@ -64,18 +64,40 @@ void CheckedActualCall::copy_output_parameters( p = p->next) { NamedValue output_parameter = expected_call->get_output_parameter(p->name); NamedValueCopier* copier = output_parameter.get_copier(); + const bool to_actual_call = + (p->direction == MutinyCopyDirection::to_actual_call); + const bool raw_pointer_pair = + to_actual_call ? ((output_parameter.get_type() == "const void*") && + (p->type == "void*")) + : ((output_parameter.get_type() == "void*") && + (p->type == "const void*")); + if (copier != nullptr) { - copier->copy(p->ptr, output_parameter.get_const_object_pointer()); - } else if ( - (output_parameter.get_type() == "const void*") && (p->type == "void*") - ) { - const void* data = output_parameter.get_value(); - size_t size = output_parameter.get_size(); - memcpy(p->ptr, data, size); + if (to_actual_call) { + copier->copy( + const_cast(p->value), + output_parameter.get_const_object_pointer() + ); + } else { + copier->copy(output_parameter.get_object_pointer(), p->value); + } + } else if (raw_pointer_pair) { + if (to_actual_call) { + memcpy( + const_cast(p->value), + output_parameter.get_value(), + output_parameter.get_size() + ); + } else { + memcpy( + output_parameter.get_value(), + p->value, + output_parameter.get_size() + ); + } } else if (!output_parameter.get_name().empty()) { fail_with(NoWayToCopyCustomTypeFailure( - get_test(), - expected_call->get_output_parameter(p->name).get_type().c_str() + get_test(), output_parameter.get_type().c_str() )); } } @@ -247,6 +269,35 @@ ActualCall& CheckedActualCall::with_output_parameter_of_type( return *this; } +ActualCall& CheckedActualCall::with_captured_parameter( + StringView name, + const void* value +) +{ + add_captured_parameter(name, "const void*", value); + + NamedValue captured_parameter(name); + captured_parameter.set_value(value); + check_output_parameter(static_cast(captured_parameter)); + + return *this; +} + +ActualCall& CheckedActualCall::with_captured_parameter_of_type( + StringView type, + StringView name, + const void* value +) +{ + add_captured_parameter(name, type, value); + + NamedValue captured_parameter(name); + captured_parameter.set_const_object_pointer(type, value); + check_output_parameter(static_cast(captured_parameter)); + + return *this; +} + bool CheckedActualCall::is_fulfilled() const { return state_ == MutinyActualCallState::success; @@ -353,8 +404,22 @@ void CheckedActualCall::add_output_parameter( void* ptr ) { - auto* new_node = new MockOutputParametersListNode(name, type, ptr); + add_output_parameter_node(new MockOutputParametersListNode(name, type, ptr)); +} + +void CheckedActualCall::add_captured_parameter( + StringView name, + StringView type, + const void* ptr +) +{ + add_output_parameter_node(new MockOutputParametersListNode(name, type, ptr)); +} +void CheckedActualCall::add_output_parameter_node( + MockOutputParametersListNode* new_node +) +{ if (output_parameter_expectations_ == nullptr) { output_parameter_expectations_ = new_node; } else { diff --git a/src/mock/CheckedExpectedCall.cpp b/src/mock/CheckedExpectedCall.cpp index ff24514c6..12b5ac03e 100644 --- a/src/mock/CheckedExpectedCall.cpp +++ b/src/mock/CheckedExpectedCall.cpp @@ -95,6 +95,31 @@ ExpectedCall& CheckedExpectedCall::with_unmodified_output_parameter( return with_output_parameter_returning(name, nullptr, 0); } +ExpectedCall& CheckedExpectedCall::with_captured_parameter( + StringView name, + void* value, + size_t size +) +{ + NamedValue* new_parameter = new MockExpectedFunctionParameter(name); + output_parameters_->add(new_parameter); + new_parameter->set_value(value); + new_parameter->set_size(size); + return *this; +} + +ExpectedCall& CheckedExpectedCall::with_captured_parameter_of_type( + StringView type, + StringView name, + void* value +) +{ + NamedValue* new_parameter = new MockExpectedFunctionParameter(name); + output_parameters_->add(new_parameter); + new_parameter->set_object_pointer(type, value); + return *this; +} + bool CheckedExpectedCall::has_input_parameter_with_name(StringView name) { NamedValue* p = input_parameters_->get_value_by_name(name); diff --git a/src/mock/NamedValue.cpp b/src/mock/NamedValue.cpp index 033fba52c..6497efb0a 100644 --- a/src/mock/NamedValue.cpp +++ b/src/mock/NamedValue.cpp @@ -651,15 +651,25 @@ bool NamedValue::equals(const NamedValue& p) const bool NamedValue::compatible_for_copying(const NamedValue& p) const { - if (type_ == p.type_) { + if ((type_ == "const void*") && (p.type_ == "void*")) { return true; } - if ((type_ == "const void*") && (p.type_ == "void*")) { + if ((type_ == "void*") && (p.type_ == "const void*")) { return true; } - return false; + // with_output_parameter{,_returning} and with_captured_parameter{,_of_type} + // both store their raw-pointer form as "void*"/"const void*", but with the + // roles swapped between expectation and actual call (see the two checks + // above). A same-type match here means the two sides used mismatched + // directions (e.g. an output-parameter expectation paired with an actual + // with_captured_parameter() call), which is never a valid pairing. + if ((type_ == "void*") || (type_ == "const void*")) { + return false; + } + + return type_ == p.type_; } String NamedValue::to_string() const diff --git a/src/mock/mock.c.cpp b/src/mock/mock.c.cpp index b1b209d5f..a6605b293 100644 --- a/src/mock/mock.c.cpp +++ b/src/mock/mock.c.cpp @@ -258,6 +258,16 @@ struct MutinyMockExpectedCall* with_output_parameter_of_type_returning( struct MutinyMockExpectedCall* with_unmodified_output_parameter( const char* name ); +struct MutinyMockExpectedCall* with_captured_parameter( + const char* name, + void* value, + size_t size +); +struct MutinyMockExpectedCall* with_captured_parameter_of_type( + const char* type, + const char* name, + void* value +); struct MutinyMockExpectedCall* ignore_other_parameters(); struct MutinyMockExpectedCall* and_return_bool_value(int value); struct MutinyMockExpectedCall* and_return_int_value(int value); @@ -349,6 +359,15 @@ struct MutinyMockActualCall* with_actual_output_parameter_of_type( const char* name, void* value ); +struct MutinyMockActualCall* with_actual_captured_parameter( + const char* name, + const void* value +); +struct MutinyMockActualCall* with_actual_captured_parameter_of_type( + const char* type, + const char* name, + const void* value +); struct MutinyMockValue return_value(); int bool_return_value(); int return_bool_value_or_default(int default_value); @@ -397,6 +416,8 @@ struct MutinyMockExpectedCall g_expected_call = { with_output_parameter_returning, with_output_parameter_of_type_returning, with_unmodified_output_parameter, + with_captured_parameter, + with_captured_parameter_of_type, ignore_other_parameters, and_return_bool_value, and_return_unsigned_int_value, @@ -429,6 +450,8 @@ struct MutinyMockActualCall g_actual_call = { with_actual_parameter_of_type, with_actual_output_parameter, with_actual_output_parameter_of_type, + with_actual_captured_parameter, + with_actual_captured_parameter_of_type, has_return_value, return_value, bool_return_value, @@ -620,6 +643,27 @@ struct MutinyMockExpectedCall* with_unmodified_output_parameter( return &g_expected_call; } +struct MutinyMockExpectedCall* with_captured_parameter( + const char* name, + void* value, + size_t size +) +{ + expected_call = &expected_call->with_captured_parameter(name, value, size); + return &g_expected_call; +} + +struct MutinyMockExpectedCall* with_captured_parameter_of_type( + const char* type, + const char* name, + void* value +) +{ + expected_call = + &expected_call->with_captured_parameter_of_type(type, name, value); + return &g_expected_call; +} + struct MutinyMockExpectedCall* ignore_other_parameters() { expected_call = &expected_call->ignore_other_parameters(); @@ -952,6 +996,27 @@ struct MutinyMockActualCall* with_actual_output_parameter_of_type( return &g_actual_call; } +struct MutinyMockActualCall* with_actual_captured_parameter( + const char* name, + const void* value +) +{ + current_actual_call = + ¤t_actual_call->with_captured_parameter(name, value); + return &g_actual_call; +} + +struct MutinyMockActualCall* with_actual_captured_parameter_of_type( + const char* type, + const char* name, + const void* value +) +{ + current_actual_call = + ¤t_actual_call->with_captured_parameter_of_type(type, name, value); + return &g_actual_call; +} + struct MutinyMockValue return_value() { return get_mock_value_c_from_named_value(current_actual_call->return_value()); diff --git a/src/mu/tiny/mock/ActualCallTrace.hpp b/src/mu/tiny/mock/ActualCallTrace.hpp index 71f753700..fabbc3604 100644 --- a/src/mu/tiny/mock/ActualCallTrace.hpp +++ b/src/mu/tiny/mock/ActualCallTrace.hpp @@ -29,6 +29,15 @@ class ActualCallTrace : public ActualCall StringView name, void* output ) override; + ActualCall& with_captured_parameter( + StringView name, + const void* value + ) override; + ActualCall& with_captured_parameter_of_type( + StringView type_name, + StringView name, + const void* value + ) override; bool has_return_value() override; NamedValue return_value() override; diff --git a/src/mu/tiny/mock/CheckedActualCall.hpp b/src/mu/tiny/mock/CheckedActualCall.hpp index a3a1be5b0..d0ad0d0ba 100644 --- a/src/mu/tiny/mock/CheckedActualCall.hpp +++ b/src/mu/tiny/mock/CheckedActualCall.hpp @@ -33,6 +33,15 @@ class CheckedActualCall : public ActualCall StringView name, void* output ) override; + ActualCall& with_captured_parameter( + StringView name, + const void* value + ) override; + ActualCall& with_captured_parameter_of_type( + StringView type, + StringView name, + const void* value + ) override; bool has_return_value() override; NamedValue return_value() override; @@ -78,19 +87,33 @@ class CheckedActualCall : public ActualCall ExpectedCallsList potentially_matching_expectations_; const ExpectedCallsList& all_expectations_; + enum class MutinyCopyDirection + { + to_actual_call, + from_actual_call + }; + class MockOutputParametersListNode { public: String name; String type; - void* ptr; + MutinyCopyDirection direction; + const void* value; MockOutputParametersListNode* next{ nullptr }; MockOutputParametersListNode(StringView n, StringView t, void* p) : name(n.data(), n.size()) , type(t.data(), t.size()) - , ptr(p) - + , direction(MutinyCopyDirection::to_actual_call) + , value(p) + { + } + MockOutputParametersListNode(StringView n, StringView t, const void* p) + : name(n.data(), n.size()) + , type(t.data(), t.size()) + , direction(MutinyCopyDirection::from_actual_call) + , value(p) { } }; @@ -102,6 +125,12 @@ class CheckedActualCall : public ActualCall StringView type, void* ptr ); + virtual void add_captured_parameter( + StringView name, + StringView type, + const void* ptr + ); + void add_output_parameter_node(MockOutputParametersListNode* new_node); void clean_up_output_parameter_list(); }; diff --git a/src/mu/tiny/mock/CheckedExpectedCall.hpp b/src/mu/tiny/mock/CheckedExpectedCall.hpp index 9c7225339..a8fff2407 100644 --- a/src/mu/tiny/mock/CheckedExpectedCall.hpp +++ b/src/mu/tiny/mock/CheckedExpectedCall.hpp @@ -44,6 +44,16 @@ class CheckedExpectedCall : public ExpectedCall const void* value ) override; ExpectedCall& with_unmodified_output_parameter(StringView name) override; + ExpectedCall& with_captured_parameter( + StringView name, + void* value, + size_t size + ) override; + ExpectedCall& with_captured_parameter_of_type( + StringView type_name, + StringView name, + void* value + ) override; ExpectedCall& ignore_other_parameters() override; ExpectedCall& and_return_typed_value(NamedValue value) override; diff --git a/src/mu/tiny/mock/IgnoredActualCall.hpp b/src/mu/tiny/mock/IgnoredActualCall.hpp index 3fa578510..4b07e8ab4 100644 --- a/src/mu/tiny/mock/IgnoredActualCall.hpp +++ b/src/mu/tiny/mock/IgnoredActualCall.hpp @@ -46,6 +46,21 @@ class IgnoredActualCall : public ActualCall { return *this; } + ActualCall& with_captured_parameter( + StringView /*name*/, + const void* /*value*/ + ) override + { + return *this; + } + ActualCall& with_captured_parameter_of_type( + StringView /*type_name*/, + StringView /*name*/, + const void* /*value*/ + ) override + { + return *this; + } bool has_return_value() override { return false; } NamedValue return_value() override { return NamedValue(""); } diff --git a/src/mu/tiny/mock/IgnoredExpectedCall.hpp b/src/mu/tiny/mock/IgnoredExpectedCall.hpp index 442f1ac60..39a3f8550 100644 --- a/src/mu/tiny/mock/IgnoredExpectedCall.hpp +++ b/src/mu/tiny/mock/IgnoredExpectedCall.hpp @@ -56,6 +56,22 @@ class IgnoredExpectedCall : public ExpectedCall { return *this; } + ExpectedCall& with_captured_parameter( + StringView /*name*/, + void* /*value*/, + size_t /*size*/ + ) override + { + return *this; + } + ExpectedCall& with_captured_parameter_of_type( + StringView /*type_name*/, + StringView /*name*/, + void* /*value*/ + ) override + { + return *this; + } ExpectedCall& ignore_other_parameters() override { return *this; } ExpectedCall& and_return_typed_value(NamedValue /*value*/) override diff --git a/tests/src/MockComparatorCopier.test.cpp b/tests/src/MockComparatorCopier.test.cpp index c5d3b03cf..cd723b5d7 100644 --- a/tests/src/MockComparatorCopier.test.cpp +++ b/tests/src/MockComparatorCopier.test.cpp @@ -94,6 +94,28 @@ TEST(MockComparatorCopier, customObjectParameterFailsWhenNotHavingACopierReposit CHECK_EXPECTED_MOCK_FAILURE(expected_failure); } +TEST(MockComparatorCopier, customObjectCapturedParameterFailsWhenNotHavingACopierRepository) +{ + FailureReporterInstaller failure_reporter_installer; + + MyTypeForTesting object(1); + mock() + .expect_one_call("function") + .with_captured_parameter_of_type( + "MyTypeForTesting", "parameterName", &object + ); + mock() + .actual_call("function") + .with_captured_parameter_of_type( + "MyTypeForTesting", "parameterName", &object + ); + + mu::tiny::mock::NoWayToCopyCustomTypeFailure expected_failure( + mock_failure_test(), "MyTypeForTesting" + ); + CHECK_EXPECTED_MOCK_FAILURE(expected_failure); +} + TEST(MockComparatorCopier, customObjectParameterSucceeds) { MyTypeForTesting object(1); @@ -246,6 +268,87 @@ TEST(MockComparatorCopier, customTypeOutputParameterMissing) mock().remove_all_comparators_and_copiers(); } +TEST(MockComparatorCopier, customTypeCapturedParameterSucceeds) +{ + MyTypeForTesting captured_object(55); + MyTypeForTesting actual_object(99); + MyTypeForTestingCopier copier; + mock().install_copier("MyTypeForTesting", copier); + + mock() + .expect_one_call("function") + .with_captured_parameter_of_type( + "MyTypeForTesting", "parameterName", &captured_object + ); + mock() + .actual_call("function") + .with_captured_parameter_of_type( + "MyTypeForTesting", "parameterName", &actual_object + ); + + mock().check_expectations(); + CHECK_EQUAL(99, *(captured_object.value)); + CHECK_EQUAL(99, *(actual_object.value)); + + mock().remove_all_comparators_and_copiers(); +} + +TEST(MockComparatorCopier, noActualCallForCustomTypeCapturedParameter) +{ + FailureReporterInstaller failure_reporter_installer; + + MyTypeForTesting captured_object(1); + MyTypeForTestingCopier copier; + mock().install_copier("MyTypeForTesting", copier); + + mock().expect_one_call("foo").with_captured_parameter_of_type( + "MyTypeForTesting", "captured", &captured_object + ); + mock().check_expectations(); + + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); + + mock().remove_all_comparators_and_copiers(); +} + +TEST(MockComparatorCopier, unexpectedCustomTypeCapturedParameter) +{ + FailureReporterInstaller failure_reporter_installer; + + MyTypeForTesting actual_object(8834); + MyTypeForTestingCopier copier; + mock().install_copier("MyTypeForTesting", copier); + + mock().expect_one_call("foo"); + mock().actual_call("foo").with_captured_parameter_of_type( + "MyTypeForTesting", "parameterName", &actual_object + ); + mock().check_expectations(); + + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); + + mock().remove_all_comparators_and_copiers(); +} + +TEST(MockComparatorCopier, customTypeCapturedParameterMissing) +{ + FailureReporterInstaller failure_reporter_installer; + + MyTypeForTesting captured_object(123464); + MyTypeForTestingCopier copier; + mock().install_copier("MyTypeForTesting", copier); + + mock().expect_one_call("foo").with_captured_parameter_of_type( + "MyTypeForTesting", "captured", &captured_object + ); + mock().actual_call("foo"); + + mock().check_expectations(); + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); + + mock().remove_all_comparators_and_copiers(); +} + TEST(MockComparatorCopier, customTypeOutputParameterOfWrongType) { FailureReporterInstaller failure_reporter_installer; diff --git a/tests/src/MockHierarchy.test.cpp b/tests/src/MockHierarchy.test.cpp index 61e599da8..673b72968 100644 --- a/tests/src/MockHierarchy.test.cpp +++ b/tests/src/MockHierarchy.test.cpp @@ -154,6 +154,15 @@ TEST(MockHierarchy, ignoredExpectedCallParameterMethodsReturnSelf) ) ); CHECK_EQUAL(&ignored, &ignored.with_unmodified_output_parameter("unmod")); + int captured = 0; + CHECK_EQUAL( + &ignored, + &ignored.with_captured_parameter("c", &captured, sizeof(captured)) + ); + CHECK_EQUAL( + &ignored, + &ignored.with_captured_parameter_of_type("type", "cot", &captured) + ); CHECK_EQUAL(&ignored, &ignored.ignore_other_parameters()); } diff --git a/tests/src/MockNamedValue.test.cpp b/tests/src/MockNamedValue.test.cpp index 9d3646cc9..fd37758e9 100644 --- a/tests/src/MockNamedValue.test.cpp +++ b/tests/src/MockNamedValue.test.cpp @@ -602,6 +602,14 @@ TEST(NamedValue, CompatibleForCopyingConstVoidAgainstVoid) CHECK(value->compatible_for_copying(other)); } +TEST(NamedValue, CompatibleForCopyingVoidAgainstConstVoid) +{ + value->set_value(nullptr); + mu::tiny::mock::NamedValue other("other"); + other.set_value(nullptr); + CHECK(value->compatible_for_copying(other)); +} + TEST(NamedValue, NotCompatibleForCopyingDifferentTypes) { value->set_value(1); @@ -610,6 +618,22 @@ TEST(NamedValue, NotCompatibleForCopyingDifferentTypes) CHECK(!value->compatible_for_copying(other)); } +TEST(NamedValue, NotCompatibleForCopyingSameVoidType) +{ + value->set_value(nullptr); + mu::tiny::mock::NamedValue other("other"); + other.set_value(nullptr); + CHECK(!value->compatible_for_copying(other)); +} + +TEST(NamedValue, NotCompatibleForCopyingSameConstVoidType) +{ + value->set_value(nullptr); + mu::tiny::mock::NamedValue other("other"); + other.set_value(nullptr); + CHECK(!value->compatible_for_copying(other)); +} + // ─── repository ────────────────────────────────────────────────────────────── TEST_GROUP(NamedValueRepository) diff --git a/tests/src/MockParameter.test.cpp b/tests/src/MockParameter.test.cpp index d818de605..dbcbdaf79 100644 --- a/tests/src/MockParameter.test.cpp +++ b/tests/src/MockParameter.test.cpp @@ -699,6 +699,119 @@ TEST(MockParameter, twoInterleavedOutputParameters) mock().check_expectations(); } +TEST(MockParameter, capturedParameterSucceeds) +{ + int param = 1; + int retval = 2; + + mock() + .expect_one_call("function") + .with_captured_parameter("parameterName", ¶m, sizeof(param)); + mock() + .actual_call("function") + .with_captured_parameter("parameterName", &retval); + + CHECK_EQUAL(param, 2); + CHECK_EQUAL(retval, 2); + mock().check_expectations(); +} + +TEST(MockParameter, noActualCallForCapturedParameter) +{ + FailureReporterInstaller failure_reporter_installer; + + int captured = 0; + mock().expect_one_call("foo").with_captured_parameter( + "captured", &captured, sizeof(captured) + ); + + mock().check_expectations(); + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); +} + +TEST(MockParameter, unexpectedCapturedParameter) +{ + FailureReporterInstaller failure_reporter_installer; + + int retval = 0; + mock().expect_one_call("foo"); + mock().actual_call("foo").with_captured_parameter("parameterName", &retval); + + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); +} + +TEST(MockParameter, capturedParameterMissing) +{ + FailureReporterInstaller failure_reporter_installer; + + int captured = 0; + mock().expect_one_call("foo").with_captured_parameter( + "captured", &captured, sizeof(captured) + ); + mock().actual_call("foo"); + + mock().check_expectations(); + STRCMP_CONTAINS("foo", mock_failure_string().c_str()); +} + +TEST(MockParameter, twoCapturedParameters) +{ + int param1 = 55; + int retval1 = 1; + int param2 = 77; + int retval2 = 2; + + mock() + .expect_one_call("function") + .with_captured_parameter("parameterName", ¶m1, sizeof(param1)) + .with_parameter("id", 1); + mock() + .expect_one_call("function") + .with_captured_parameter("parameterName", ¶m2, sizeof(param2)) + .with_parameter("id", 2); + mock() + .actual_call("function") + .with_captured_parameter("parameterName", &retval1) + .with_parameter("id", 1); + mock() + .actual_call("function") + .with_captured_parameter("parameterName", &retval2) + .with_parameter("id", 2); + + CHECK_EQUAL(retval1, param1); + CHECK_EQUAL(retval2, param2); + mock().check_expectations(); +} + +TEST(MockParameter, twoInterleavedCapturedParameters) +{ + int param1 = 55; + int retval1 = 1; + int param2 = 77; + int retval2 = 2; + + mock() + .expect_one_call("function") + .with_captured_parameter("parameterName", ¶m1, sizeof(param1)) + .with_parameter("id", 1); + mock() + .expect_one_call("function") + .with_captured_parameter("parameterName", ¶m2, sizeof(param2)) + .with_parameter("id", 2); + mock() + .actual_call("function") + .with_captured_parameter("parameterName", &retval2) + .with_parameter("id", 2); + mock() + .actual_call("function") + .with_captured_parameter("parameterName", &retval1) + .with_parameter("id", 1); + + CHECK_EQUAL(retval1, param1); + CHECK_EQUAL(retval2, param2); + mock().check_expectations(); +} + TEST(MockParameter, twoDifferentOutputParametersInSameFunctionCallSucceeds) { int param1 = 1; diff --git a/tests/src/MockSupport.c b/tests/src/MockSupport.c index 95f9b2b38..9785789d5 100644 --- a/tests/src/MockSupport.c +++ b/tests/src/MockSupport.c @@ -71,12 +71,16 @@ void mutiny_all_mock_support_c_calls(void) ->expect_one_call("boo") ->with_parameter_of_type("type", "name", (void*)1) ->with_output_parameter_returning("name", (void*)1, 0UL) - ->with_output_parameter_of_type_returning("type", "name", (void*)1); + ->with_output_parameter_of_type_returning("type", "name", (void*)1) + ->with_captured_parameter("name", (void*)1, 0UL) + ->with_captured_parameter_of_type("type", "name", (void*)1); mutiny_mock() ->actual_call("boo") ->with_parameter_of_type("type", "name", (void*)1) ->with_output_parameter("name", (void*)1) - ->with_output_parameter_of_type("type", "name", (void*)1); + ->with_output_parameter_of_type("type", "name", (void*)1) + ->with_captured_parameter("name", (const void*)1) + ->with_captured_parameter_of_type("type", "name", (const void*)1); mutiny_mock()->enable(); mutiny_mock()->clear(); diff --git a/tests/src/MockSupport.test.cpp b/tests/src/MockSupport.test.cpp index 362bdd68a..90a1b895d 100644 --- a/tests/src/MockSupport.test.cpp +++ b/tests/src/MockSupport.test.cpp @@ -184,6 +184,25 @@ TEST(Support, tracingWithParameterOfType) STRCMP_CONTAINS("param:", mock().get_trace_output()); } +TEST(Support, tracingWithCapturedParameter) +{ + int obj = 0; + mock().tracing(true); + mock().actual_call("func").with_captured_parameter("param", &obj); + STRCMP_CONTAINS("param:", mock().get_trace_output()); +} + +TEST(Support, tracingWithCapturedParameterOfType) +{ + int obj = 0; + mock().tracing(true); + mock().actual_call("func").with_captured_parameter_of_type( + "MyType", "param", &obj + ); + STRCMP_CONTAINS("MyType", mock().get_trace_output()); + STRCMP_CONTAINS("param:", mock().get_trace_output()); +} + TEST(Support, tracingOnObject) { int obj = 0; @@ -369,6 +388,32 @@ TEST(Support, outputParameterAfterFailedCallIsNotWritten) clear_mock_failure(); } +TEST(Support, capturedExpectationDoesNotMatchOutputParameterActualCall) +{ + int captured = 0; + int val = 5; + mock().expect_one_call("func").with_captured_parameter( + "param", &captured, sizeof(captured) + ); + mock().actual_call("func").with_output_parameter("param", &val); + STRCMP_CONTAINS("Unexpected parameter type", mock_failure_string().c_str()); + clear_mock_failure(); + mock().clear(); +} + +TEST(Support, outputExpectationDoesNotMatchCapturedParameterActualCall) +{ + int expected = 5; + int val = 0; + mock().expect_one_call("func").with_output_parameter_returning( + "param", &expected, sizeof(expected) + ); + mock().actual_call("func").with_captured_parameter("param", &val); + STRCMP_CONTAINS("Unexpected parameter type", mock_failure_string().c_str()); + clear_mock_failure(); + mock().clear(); +} + TEST(Support, withParameterOfTypeStringOverloadNoComparatorFails) { int obj = 0; diff --git a/tests/src/MockSupport_c.test.cpp b/tests/src/MockSupport_c.test.cpp index e2b46044f..e7a0d2cac 100644 --- a/tests/src/MockSupport_c.test.cpp +++ b/tests/src/MockSupport_c.test.cpp @@ -286,6 +286,36 @@ TEST(MockSupport_c, outputParametersOfType) mutiny_mock()->remove_all_comparators_and_copiers(); } +TEST(MockSupport_c, capturedParameters) +{ + int captured = 1; + int actual = 2; + mutiny_mock()->expect_one_call("foo")->with_captured_parameter( + "out", &captured, sizeof(captured) + ); + mutiny_mock()->actual_call("foo")->with_captured_parameter("out", &actual); + mutiny_mock()->check_expectations(); + CHECK_EQUAL(2, captured); + CHECK_EQUAL(2, actual); +} + +TEST(MockSupport_c, capturedParametersOfType) +{ + int captured = 1; + int actual = 2; + mutiny_mock()->install_copier("typeName", type_copy); + mutiny_mock()->expect_one_call("foo")->with_captured_parameter_of_type( + "typeName", "out", &captured + ); + mutiny_mock()->actual_call("foo")->with_captured_parameter_of_type( + "typeName", "out", &actual + ); + CHECK_EQUAL(2, captured); + CHECK_EQUAL(2, actual); + mutiny_mock()->check_expectations(); + mutiny_mock()->remove_all_comparators_and_copiers(); +} + TEST(MockSupport_c, ignoreOtherParameters) { mutiny_mock()