From d304ff273a8934c870a1f3cf25bfaf2c44251f4a Mon Sep 17 00:00:00 2001 From: cloudsftp Date: Wed, 19 Aug 2020 11:25:27 +0200 Subject: [PATCH] add StopIgnore to new plugin --- docs/CMock_Summary.md | 5 +- ...cmock_generator_plugin_ignore_stateless.rb | 10 ++++ .../ignore_and_return_stateless.yml | 49 +++++++++++++++++++ ..._generator_plugin_ignore_stateless_test.rb | 15 +++++- 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/docs/CMock_Summary.md b/docs/CMock_Summary.md index 04a9885..efd50a3 100644 --- a/docs/CMock_Summary.md +++ b/docs/CMock_Summary.md @@ -180,8 +180,11 @@ IgnoreStateless: This plugin is similar to the Ignore plugin, but the IgnoreAndReturn functions are stateless. So the Ignored function will always return the last specified return value and does not queue the return values as the IgnoreAndReturn of the default plugin will. + To stop ignoring a function you can call StopIgnore or simply overwrite the Ignore -(resp. IgnoreAndReturn) with an Expect (resp. ExpectAndReturn). +(resp. IgnoreAndReturn) with an Expect (resp. ExpectAndReturn). Note that calling +Ignore (resp IgnoreAndReturn) will clear your previous called Expect +(resp. ExpectAndReturn), so they are not restored after StopIgnore is called. You can use this plugin by using `:ignore_stateless` instead of `:ignore` in your CMock configuration file. diff --git a/lib/cmock_generator_plugin_ignore_stateless.rb b/lib/cmock_generator_plugin_ignore_stateless.rb index 0f215da..0551067 100644 --- a/lib/cmock_generator_plugin_ignore_stateless.rb +++ b/lib/cmock_generator_plugin_ignore_stateless.rb @@ -30,6 +30,11 @@ class CMockGeneratorPluginIgnoreStateless "#define #{function[:name]}_IgnoreAndReturn(cmock_retval) #{function[:name]}_CMockIgnoreAndReturn(cmock_retval)\n" + "void #{function[:name]}_CMockIgnoreAndReturn(#{function[:return][:str]});\n"; end + + # Add stop ignore function. it does not matter if there are any args + lines << "#define #{function[:name]}_StopIgnore() #{function[:name]}_CMockStopIgnore()\n" \ + "void #{function[:name]}_CMockStopIgnore(void);\n" + lines end def mock_implementation_precheck(function) @@ -61,6 +66,11 @@ class CMockGeneratorPluginIgnoreStateless lines << " Mock.#{function[:name]}_IgnoreBool = (char)1;\n" lines << "}\n\n" + # Add stop ignore function. it does not matter if there are any args + lines << "void #{function[:name]}_CMockStopIgnore(void)\n{\n" + lines << " Mock.#{function[:name]}_IgnoreBool = (char)0;\n" + lines << "}\n\n" + lines end diff --git a/test/system/test_interactions/ignore_and_return_stateless.yml b/test/system/test_interactions/ignore_and_return_stateless.yml index 8fcfc48..aefe4dd 100644 --- a/test/system/test_interactions/ignore_and_return_stateless.yml +++ b/test/system/test_interactions/ignore_and_return_stateless.yml @@ -273,4 +273,53 @@ TEST_ASSERT_EQUAL(60, function(4, 5, 6)); } +# StopIgnore + + - :pass: FALSE + :should: 'fail when function is called after ignore is stopped' + :code: | + test() + { + bar_Ignore(); + foo_IgnoreAndReturn(10); + function(0, 0, 0); + bar_StopIgnore(); + function(0, 0, 0); + } + + - :pass: FALSE + :should: 'delete expect after ignore is stopped' + :code: | + test() + { + bar_Ignore(); + foo_ExpectAndReturn(0, 40); + foo_ExpectAndReturn(0, 30); + foo_ExpectAndReturn(0, 40); + + foo_IgnoreAndReturn(20); + foo_StopIgnore(); + + TEST_ASSERT_EQUAL(110, function(0, 0, 0)); // THIS SHOULD FAIL + } + + - :pass: TRUE + :should: 'delete expected return values after ignore is stopped' + :code: | + test() + { + bar_Ignore(); + foo_ExpectAndReturn(0, 40); + foo_ExpectAndReturn(0, 30); + foo_ExpectAndReturn(0, 40); + + foo_IgnoreAndReturn(20); + foo_StopIgnore(); + + foo_ExpectAndReturn(0, 50); + foo_ExpectAndReturn(0, 30); + foo_ExpectAndReturn(0, 40); + TEST_ASSERT_EQUAL(120, function(0, 0, 0)); + } + ... diff --git a/test/unit/cmock_generator_plugin_ignore_stateless_test.rb b/test/unit/cmock_generator_plugin_ignore_stateless_test.rb index 5367e1a..b40d978 100644 --- a/test/unit/cmock_generator_plugin_ignore_stateless_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_stateless_test.rb @@ -35,7 +35,8 @@ describe CMockGeneratorPluginIgnoreStateless, "Verify CMockGeneratorPluginIgnore it "handle function declarations for functions without return values" do function = {:name => "Mold", :args_string => "void", :return => test_return[:void]} - expected = "#define Mold_Ignore() Mold_CMockIgnore()\nvoid Mold_CMockIgnore(void);\n" + expected = "#define Mold_Ignore() Mold_CMockIgnore()\nvoid Mold_CMockIgnore(void);\n" + + "#define Mold_StopIgnore() Mold_CMockStopIgnore()\nvoid Mold_CMockStopIgnore(void);\n" returned = @cmock_generator_plugin_ignore_stateless.mock_function_declarations(function) assert_equal(expected, returned) end @@ -43,7 +44,9 @@ describe CMockGeneratorPluginIgnoreStateless, "Verify CMockGeneratorPluginIgnore it "handle function declarations for functions that returns something" do function = {:name => "Fungus", :args_string => "void", :return => test_return[:string]} expected = "#define Fungus_IgnoreAndReturn(cmock_retval) Fungus_CMockIgnoreAndReturn(cmock_retval)\n"+ - "void Fungus_CMockIgnoreAndReturn(const char* cmock_to_return);\n" + "void Fungus_CMockIgnoreAndReturn(const char* cmock_to_return);\n" + + "#define Fungus_StopIgnore() Fungus_CMockStopIgnore()\n"+ + "void Fungus_CMockStopIgnore(void);\n" returned = @cmock_generator_plugin_ignore_stateless.mock_function_declarations(function) assert_equal(expected, returned) end @@ -82,6 +85,10 @@ describe CMockGeneratorPluginIgnoreStateless, "Verify CMockGeneratorPluginIgnore expected = ["void Slime_CMockIgnore(void)\n", "{\n", " Mock.Slime_IgnoreBool = (char)1;\n", + "}\n\n", + "void Slime_CMockStopIgnore(void)\n", + "{\n", + " Mock.Slime_IgnoreBool = (char)0;\n", "}\n\n" ].join returned = @cmock_generator_plugin_ignore_stateless.mock_interfaces(function) @@ -96,6 +103,10 @@ describe CMockGeneratorPluginIgnoreStateless, "Verify CMockGeneratorPluginIgnore " Mock.Slime_CallInstance = CMOCK_GUTS_NONE;\n", " Mock.Slime_FinalReturn = cmock_to_return;\n", " Mock.Slime_IgnoreBool = (char)1;\n", + "}\n\n", + "void Slime_CMockStopIgnore(void)\n", + "{\n", + " Mock.Slime_IgnoreBool = (char)0;\n", "}\n\n" ].join returned = @cmock_generator_plugin_ignore_stateless.mock_interfaces(function)