ExpectAnyArgs should not be generated for functions with no arguments (#201)

This commit is contained in:
Mark VanderVoord
2019-10-23 15:51:22 -04:00
parent b37b3cd86b
commit 741b1067a3
3 changed files with 34 additions and 22 deletions
+3 -3
View File
@@ -117,11 +117,11 @@ ExpectAnyArgs:
This behaves just like the Expects calls, except that it doesn't really This behaves just like the Expects calls, except that it doesn't really
care what the arguments are that the mock gets called with. It still counts care what the arguments are that the mock gets called with. It still counts
the number of times the mock is called and it still handles return values the number of times the mock is called and it still handles return values
if there are some. if there are some. Note that an ExpectAnyArgs call is not generated for
functions that have no arguments, because it would act exactly like the existing
Expect and ExpectAndReturn calls.
* `void func(void)` => `void func_ExpectAnyArgs(void)`
* `void func(params)` => `void func_ExpectAnyArgs(void)` * `void func(params)` => `void func_ExpectAnyArgs(void)`
* `retval func(void)` => `void func_ExpectAnyArgsAndReturn(retval_to_return)`
* `retval func(params)` => `void func_ExpectAnyArgsAndReturn(retval_to_return)` * `retval func(params)` => `void func_ExpectAnyArgsAndReturn(retval_to_return)`
+21 -16
View File
@@ -20,27 +20,32 @@ class CMockGeneratorPluginExpectAnyArgs
end end
def mock_function_declarations(function) def mock_function_declarations(function)
if (function[:return][:void?]) unless (function[:args].empty?)
return "#define #{function[:name]}_ExpectAnyArgs() #{function[:name]}_CMockExpectAnyArgs(__LINE__)\n" + if (function[:return][:void?])
"void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n" return "#define #{function[:name]}_ExpectAnyArgs() #{function[:name]}_CMockExpectAnyArgs(__LINE__)\n" +
else "void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n"
return "#define #{function[:name]}_ExpectAnyArgsAndReturn(cmock_retval) #{function[:name]}_CMockExpectAnyArgsAndReturn(__LINE__, cmock_retval)\n" + else
"void #{function[:name]}_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n" return "#define #{function[:name]}_ExpectAnyArgsAndReturn(cmock_retval) #{function[:name]}_CMockExpectAnyArgsAndReturn(__LINE__, cmock_retval)\n" +
"void #{function[:name]}_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n"
end
end end
end end
def mock_interfaces(function) def mock_interfaces(function)
lines = "" lines = ""
if (function[:return][:void?]) unless (function[:args].empty?)
lines << "void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line)\n{\n" if (function[:return][:void?])
else lines << "void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line)\n{\n"
lines << "void #{function[:name]}_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]})\n{\n" else
lines << "void #{function[:name]}_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]})\n{\n"
end
lines << @utils.code_add_base_expectation(function[:name], true)
unless (function[:return][:void?])
lines << " cmock_call_instance->ReturnVal = cmock_to_return;\n"
end
lines << " cmock_call_instance->ExpectAnyArgsBool = (int)1;\n"
lines << "}\n\n"
end end
lines << @utils.code_add_base_expectation(function[:name], true) return lines
unless (function[:return][:void?])
lines << " cmock_call_instance->ReturnVal = cmock_to_return;\n"
end
lines << " cmock_call_instance->ExpectAnyArgsBool = (int)1;\n"
lines << "}\n\n"
end end
end end
@@ -26,15 +26,22 @@ describe CMockGeneratorPluginExpectAnyArgs, "Verify CMockGeneratorPluginExpectAn
assert(!@cmock_generator_plugin_expect_any_args.respond_to?(:include_files)) assert(!@cmock_generator_plugin_expect_any_args.respond_to?(:include_files))
end end
it "ignore functions without arguments" do
function = {:name => "Mold", :args_string => "void", :args => [], :return => test_return[:void]}
expected = ""
returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function)
assert_equal(expected, returned)
end
it "handle function declarations for functions without return values" do it "handle function declarations for functions without return values" do
function = {:name => "Mold", :args_string => "void", :return => test_return[:void]} function = {:name => "Mold", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:void]}
expected = "#define Mold_ExpectAnyArgs() Mold_CMockExpectAnyArgs(__LINE__)\nvoid Mold_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n" expected = "#define Mold_ExpectAnyArgs() Mold_CMockExpectAnyArgs(__LINE__)\nvoid Mold_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n"
returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function) returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function)
assert_equal(expected, returned) assert_equal(expected, returned)
end end
it "handle function declarations for functions that returns something" do it "handle function declarations for functions that returns something" do
function = {:name => "Fungus", :args_string => "void", :return => test_return[:string]} function = {:name => "Fungus", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:string]}
expected = "#define Fungus_ExpectAnyArgsAndReturn(cmock_retval) Fungus_CMockExpectAnyArgsAndReturn(__LINE__, cmock_retval)\n"+ expected = "#define Fungus_ExpectAnyArgsAndReturn(cmock_retval) Fungus_CMockExpectAnyArgsAndReturn(__LINE__, cmock_retval)\n"+
"void Fungus_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, const char* cmock_to_return);\n" "void Fungus_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, const char* cmock_to_return);\n"
returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function) returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function)
@@ -46,7 +53,7 @@ describe CMockGeneratorPluginExpectAnyArgs, "Verify CMockGeneratorPluginExpectAn
end end
it "add a new mock interface for ignoring when function had no return value" do it "add a new mock interface for ignoring when function had no return value" do
function = {:name => "Slime", :args => [], :args_string => "void", :return => test_return[:void]} function = {:name => "Slime", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:void]}
expected = ["void Slime_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line)\n", expected = ["void Slime_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line)\n",
"{\n", "{\n",
"mock_return_1", "mock_return_1",