Improved the way stubs and callbacks deal with call counts (Implements #132)

This commit is contained in:
Mark VanderVoord
2026-06-30 10:15:18 -04:00
parent dd922f17b0
commit ebdc8ee128
3 changed files with 30 additions and 6 deletions
+8
View File
@@ -294,6 +294,14 @@ order (based on any Expects you've set up) before calling the callback.
to the callback instead. In this case, you are replacing the normal mock calls
with your own custom stub function.
Calling either `func_AddCallback` or `func_Stub` resets the call count for
that function to zero, so `NumCalls` always reflects calls made since the
current stub or callback was installed.
You can read the current call count at any time using `func_CallCount()`.
This is useful when you need to verify how many times a stub was invoked
without setting up formal Expects.
There is also an older name, `func_StubWithCallback`, which is just an alias
for either `func_AddCallback` or `func_Stub` depending on setting of the
`:callback_after_arg_check` toggle. This is deprecated and we recommend using
+6 -1
View File
@@ -33,7 +33,8 @@ class CMockGeneratorPluginCallback
"typedef #{return_type} (* CMOCK_#{func_name}_CALLBACK)(#{styles[style]});\n" \
"void #{func_name}_AddCallback(CMOCK_#{func_name}_CALLBACK Callback);\n" \
"void #{func_name}_Stub(CMOCK_#{func_name}_CALLBACK Callback);\n" \
"#define #{func_name}_StubWithCallback #{func_name}_#{action}\n"
"#define #{func_name}_StubWithCallback #{func_name}_#{action}\n" \
"int #{func_name}_CallCount(void);\n"
end
def generate_call(function)
@@ -73,10 +74,14 @@ class CMockGeneratorPluginCallback
lines << "void #{func_name}_AddCallback(CMOCK_#{func_name}_CALLBACK Callback)\n{\n"
lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if has_ignore
lines << " Mock.#{func_name}_CallbackBool = (char)1;\n"
lines << " Mock.#{func_name}_CallbackCalls = 0;\n"
lines << " Mock.#{func_name}_CallbackFunctionPointer = Callback;\n}\n\n"
lines << "int #{func_name}_CallCount(void)\n{\n"
lines << " return Mock.#{func_name}_CallbackCalls;\n}\n\n"
lines << "void #{func_name}_Stub(CMOCK_#{func_name}_CALLBACK Callback)\n{\n"
lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if has_ignore
lines << " Mock.#{func_name}_CallbackBool = (char)0;\n"
lines << " Mock.#{func_name}_CallbackCalls = 0;\n"
lines << " Mock.#{func_name}_CallbackFunctionPointer = Callback;\n}\n\n"
end
@@ -45,7 +45,8 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int cmock_num_calls);\n",
"void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
"void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
"#define Maple_StubWithCallback Maple_Stub\n" ].join
"#define Maple_StubWithCallback Maple_Stub\n",
"int Maple_CallCount(void);\n" ].join
returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -55,7 +56,8 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(void);\n",
"void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
"void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
"#define Maple_StubWithCallback Maple_Stub\n" ].join
"#define Maple_StubWithCallback Maple_Stub\n",
"int Maple_CallCount(void);\n" ].join
@cmock_generator_plugin_callback.include_count = false
returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
assert_equal(expected, returned)
@@ -66,7 +68,8 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n",
"void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
"void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
"#define Maple_StubWithCallback Maple_Stub\n" ].join
"#define Maple_StubWithCallback Maple_Stub\n",
"int Maple_CallCount(void);\n" ].join
returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -76,7 +79,8 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n",
"void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
"void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
"#define Maple_StubWithCallback Maple_Stub\n" ].join
"#define Maple_StubWithCallback Maple_Stub\n",
"int Maple_CallCount(void);\n" ].join
returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -86,7 +90,8 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu);\n",
"void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
"void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
"#define Maple_StubWithCallback Maple_Stub\n" ].join
"#define Maple_StubWithCallback Maple_Stub\n",
"int Maple_CallCount(void);\n" ].join
@cmock_generator_plugin_callback.include_count = false
returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
assert_equal(expected, returned)
@@ -273,12 +278,18 @@ describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Modu
"{\n",
" Mock.Lemon_IgnoreBool = (char)0;\n",
" Mock.Lemon_CallbackBool = (char)1;\n",
" Mock.Lemon_CallbackCalls = 0;\n",
" Mock.Lemon_CallbackFunctionPointer = Callback;\n",
"}\n\n",
"int Lemon_CallCount(void)\n",
"{\n",
" return Mock.Lemon_CallbackCalls;\n",
"}\n\n",
"void Lemon_Stub(CMOCK_Lemon_CALLBACK Callback)\n",
"{\n",
" Mock.Lemon_IgnoreBool = (char)0;\n",
" Mock.Lemon_CallbackBool = (char)0;\n",
" Mock.Lemon_CallbackCalls = 0;\n",
" Mock.Lemon_CallbackFunctionPointer = Callback;\n",
"}\n\n"
].join