diff --git a/docs/CMock_Summary.md b/docs/CMock_Summary.md index b78327d..0c621fd 100644 --- a/docs/CMock_Summary.md +++ b/docs/CMock_Summary.md @@ -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 diff --git a/lib/cmock_generator_plugin_callback.rb b/lib/cmock_generator_plugin_callback.rb index 5371960..1d5a736 100644 --- a/lib/cmock_generator_plugin_callback.rb +++ b/lib/cmock_generator_plugin_callback.rb @@ -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 diff --git a/test/unit/cmock_generator_plugin_callback_test.rb b/test/unit/cmock_generator_plugin_callback_test.rb index 9f2148e..f44593d 100644 --- a/test/unit/cmock_generator_plugin_callback_test.rb +++ b/test/unit/cmock_generator_plugin_callback_test.rb @@ -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