Skip to content

Commit

Permalink
Finish giving clues when you've chosen the wrong AndReturn version.
Browse files Browse the repository at this point in the history
ReturnArrayThruPtr (and others) now protect sizes in parenthesis #415
  • Loading branch information
mvandervoord committed Mar 21, 2024
1 parent 3a6f1f2 commit 597a6d8
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 33 deletions.
13 changes: 8 additions & 5 deletions lib/cmock_generator_plugin_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ def instance_typedefs(function)
def mock_function_declarations(function)
return nil unless function[:contains_ptr?]

args_call = function[:args].map { |m| m[:ptr?] ? "#{m[:name]}, #{m[:name]}_Depth" : (m[:name]).to_s }.join(', ')
args_call_i = function[:args].map { |m| m[:ptr?] ? "#{m[:name]}, #{m[:name]}_Depth" : (m[:name]).to_s }.join(', ')
args_call_o = function[:args].map { |m| m[:ptr?] ? "#{m[:name]}, (#{m[:name]}_Depth)" : (m[:name]).to_s }.join(', ')
args_string = function[:args].map do |m|
type = @utils.arg_type_with_const(m)
m[:ptr?] ? "#{type} #{m[:name]}, int #{m[:name]}_Depth" : "#{type} #{m[:name]}"
end.join(', ')
if function[:return][:void?]
"#define #{function[:name]}_ExpectWithArray(#{args_call}) #{function[:name]}_CMockExpectWithArray(__LINE__, #{args_call})\n" \
"void #{function[:name]}_CMockExpectWithArray(UNITY_LINE_TYPE cmock_line, #{args_string});\n"
"#define #{function[:name]}_ExpectWithArrayAndReturn(#{args_call_i}, cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectWithArray (not AndReturn)\");\n" \
"#define #{function[:name]}_ExpectWithArray(#{args_call_i}) #{function[:name]}_CMockExpectWithArray(__LINE__, #{args_call_o})\n" \
"void #{function[:name]}_CMockExpectWithArray(UNITY_LINE_TYPE cmock_line, #{args_string});\n"
else
"#define #{function[:name]}_ExpectWithArrayAndReturn(#{args_call}, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, #{args_call}, cmock_retval)\n" \
"void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, #{args_string}, #{function[:return][:str]});\n"
"#define #{function[:name]}_ExpectWithArray(#{args_call_i}) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectWithArrayAndReturn\");\n" \
"#define #{function[:name]}_ExpectWithArrayAndReturn(#{args_call_i}, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, #{args_call_o}, cmock_retval)\n" \
"void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, #{args_string}, #{function[:return][:str]});\n"
end
end

Expand Down
8 changes: 4 additions & 4 deletions lib/cmock_generator_plugin_expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ def mock_function_declarations(function)
if function[:return][:void?]
"#define #{function[:name]}_ExpectAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _Expect (not AndReturn)\");\n" \
"#define #{function[:name]}_Expect() #{function[:name]}_CMockExpect(__LINE__)\n" \
"void #{function[:name]}_CMockExpect(UNITY_LINE_TYPE cmock_line);\n"
"void #{function[:name]}_CMockExpect(UNITY_LINE_TYPE cmock_line);\n"
else
"#define #{function[:name]}_Expect() TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectAndReturn\");\n" \
"#define #{function[:name]}_ExpectAndReturn(cmock_retval) #{function[:name]}_CMockExpectAndReturn(__LINE__, cmock_retval)\n" \
"void #{function[:name]}_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n"
"void #{function[:name]}_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n"
end
elsif function[:return][:void?]
"#define #{function[:name]}_ExpectAndReturn(#{function[:args_call]}, cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _Expect (not AndReturn)\");\n" \
"#define #{function[:name]}_Expect(#{function[:args_call]}) #{function[:name]}_CMockExpect(__LINE__, #{function[:args_call]})\n" \
"void #{function[:name]}_CMockExpect(UNITY_LINE_TYPE cmock_line, #{function[:args_string]});\n"
"void #{function[:name]}_CMockExpect(UNITY_LINE_TYPE cmock_line, #{function[:args_string]});\n"
else
"#define #{function[:name]}_Expect(#{function[:args_call]}) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectAndReturn\");\n" \
"#define #{function[:name]}_ExpectAndReturn(#{function[:args_call]}, cmock_retval) #{function[:name]}_CMockExpectAndReturn(__LINE__, #{function[:args_call]}, cmock_retval)\n" \
"void #{function[:name]}_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:args_string]}, #{function[:return][:str]});\n"
"void #{function[:name]}_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:args_string]}, #{function[:return][:str]});\n"
end
end

Expand Down
6 changes: 4 additions & 2 deletions lib/cmock_generator_plugin_expect_any_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def mock_function_declarations(function)
if function[:args].empty?
''
elsif function[:return][:void?]
"#define #{function[:name]}_ExpectAnyArgsAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectAnyArgs (not AndReturn)\");\n" \
"#define #{function[:name]}_ExpectAnyArgs() #{function[:name]}_CMockExpectAnyArgs(__LINE__)\n" \
"void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n"
"void #{function[:name]}_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n"
else
"#define #{function[:name]}_ExpectAnyArgs() TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectAnyArgsAndReturn\");\n" \
"#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"
"void #{function[:name]}_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n"
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/cmock_generator_plugin_ignore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def instance_structure(function)

def mock_function_declarations(function)
lines = if function[:return][:void?]
"#define #{function[:name]}_IgnoreAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _Ignore (not AndReturn)\");\n" \
"#define #{function[:name]}_Ignore() #{function[:name]}_CMockIgnore()\n" \
"void #{function[:name]}_CMockIgnore(void);\n"
else
"#define #{function[:name]}_Ignore() TEST_FAIL_MESSAGE(\"#{function[:name]} requires _IgnoreAndReturn\");\n" \
"#define #{function[:name]}_IgnoreAndReturn(cmock_retval) #{function[:name]}_CMockIgnoreAndReturn(__LINE__, cmock_retval)\n" \
"void #{function[:name]}_CMockIgnoreAndReturn(UNITY_LINE_TYPE cmock_line, #{function[:return][:str]});\n"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/cmock_generator_plugin_ignore_stateless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def instance_structure(function)

def mock_function_declarations(function)
lines = if function[:return][:void?]
"#define #{function[:name]}_IgnoreAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _Ignore (not AndReturn)\");\n" \
"#define #{function[:name]}_Ignore() #{function[:name]}_CMockIgnore()\n" \
"void #{function[:name]}_CMockIgnore(void);\n"
else
"#define #{function[:name]}_Ignore() TEST_FAIL_MESSAGE(\"#{function[:name]} requires _IgnoreAndReturn\");\n" \
"#define #{function[:name]}_IgnoreAndReturn(cmock_retval) #{function[:name]}_CMockIgnoreAndReturn(cmock_retval)\n" \
"void #{function[:name]}_CMockIgnoreAndReturn(#{function[:return][:str]});\n"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cmock_generator_plugin_return_thru_ptr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def mock_function_declarations(function)
" #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, sizeof(*#{arg[:name]}))\n"
end
lines << "#define #{function[:name]}_ReturnArrayThruPtr_#{arg[:name]}(#{arg[:name]}, cmock_len)"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, cmock_len * sizeof(*#{arg[:name]}))\n"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, (cmock_len * sizeof(*#{arg[:name]})))\n"
lines << "#define #{function[:name]}_ReturnMemThruPtr_#{arg[:name]}(#{arg[:name]}, cmock_size)"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, cmock_size)\n"
lines << " #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(__LINE__, #{arg[:name]}, (cmock_size))\n"
lines << "void #{function[:name]}_CMockReturnMemThruPtr_#{arg[:name]}(UNITY_LINE_TYPE cmock_line, #{ptr_to_const(arg[:type])} #{arg[:name]}, size_t cmock_size);\n"
end
lines
Expand Down
43 changes: 42 additions & 1 deletion test/system/test_interactions/wrong_expect_and_return.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
:cmock:
:plugins:
- # none
- :ignore
- :expect_any_args

:systest:
:types: |
Expand Down Expand Up @@ -97,5 +98,45 @@
function_d();
}
- :pass: FALSE
:should: 'successfully report using ExpectAnyArgs instead of ExpectAnyArgsAndReturn'
:code: |
test()
{
foo_ExpectAnyArgs();
bar_ExpectAndReturn(2);
function_a(1);
}
- :pass: FALSE
:should: 'successfully report using ExpectAnyArgsAndReturn instead of ExpectAnyArgs'
:code: |
test()
{
do_it_Expect();
do_another_ExpectAnyArgsAndReturn(2);
function_d();
}
- :pass: FALSE
:should: 'successfully report using Ignore instead of IgnoreAndReturn'
:code: |
test()
{
foo_Ignore();
bar_IgnoreAndReturn(2);
function_a(1);
}
- :pass: FALSE
:should: 'successfully report using IgnoreAndReturn instead of Ignore'
:code: |
test()
{
do_it_Ignore();
do_another_IgnoreAndReturn(2);
function_d();
}
...
9 changes: 6 additions & 3 deletions test/unit/cmock_generator_plugin_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def code_add_base_expectation(func)
:return => test_return[:void],
:contains_ptr? => true }

expected = "#define #{function[:name]}_ExpectWithArray(tofu, tofu_Depth) #{function[:name]}_CMockExpectWithArray(__LINE__, tofu, tofu_Depth)\n" +
expected = "#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectWithArray (not AndReturn)\");\n" +
"#define #{function[:name]}_ExpectWithArray(tofu, tofu_Depth) #{function[:name]}_CMockExpectWithArray(__LINE__, tofu, (tofu_Depth))\n" +
"void #{function[:name]}_CMockExpectWithArray(UNITY_LINE_TYPE cmock_line, int* tofu, int tofu_Depth);\n"
returned = @cmock_generator_plugin_array.mock_function_declarations(function)
assert_equal(expected, returned)
Expand All @@ -88,7 +89,8 @@ def code_add_base_expectation(func)
:return => test_return[:string],
:contains_ptr? => true }

expected = "#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, tofu_Depth, cmock_retval)\n" +
expected = "#define #{function[:name]}_ExpectWithArray(tofu, tofu_Depth) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectWithArrayAndReturn\");\n" +
"#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, (tofu_Depth), cmock_retval)\n" +
"void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, int* tofu, int tofu_Depth, const char* cmock_to_return);\n"
returned = @cmock_generator_plugin_array.mock_function_declarations(function)
assert_equal(expected, returned)
Expand All @@ -104,7 +106,8 @@ def code_add_base_expectation(func)
:return => test_return[:string],
:contains_ptr? => true }

expected = "#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, tofu_Depth, cmock_retval)\n" +
expected = "#define #{function[:name]}_ExpectWithArray(tofu, tofu_Depth) TEST_FAIL_MESSAGE(\"#{function[:name]} requires _ExpectWithArrayAndReturn\");\n" +
"#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, (tofu_Depth), cmock_retval)\n" +
"void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, const int* tofu, int tofu_Depth, const char* cmock_to_return);\n"
returned = @cmock_generator_plugin_array.mock_function_declarations(function)
assert_equal(expected, returned)
Expand Down
7 changes: 5 additions & 2 deletions test/unit/cmock_generator_plugin_expect_any_args_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@

it "handle function declarations for functions without return values" do
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_ExpectAnyArgsAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"Mold requires _ExpectAnyArgs (not AndReturn)\");\n"+
"#define Mold_ExpectAnyArgs() Mold_CMockExpectAnyArgs(__LINE__)\n"+
"void Mold_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n"
returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function)
assert_equal(expected, returned)
end

it "handle function declarations for functions that returns something" do
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_ExpectAnyArgs() TEST_FAIL_MESSAGE(\"Fungus requires _ExpectAnyArgsAndReturn\");\n"+
"#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"
returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function)
assert_equal(expected, returned)
Expand Down
10 changes: 7 additions & 3 deletions test/unit/cmock_generator_plugin_ignore_stateless_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@

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" +
"#define Mold_StopIgnore() Mold_CMockStopIgnore()\nvoid Mold_CMockStopIgnore(void);\n"
expected = "#define Mold_IgnoreAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"Mold requires _Ignore (not AndReturn)\");\n" +
"#define Mold_Ignore() Mold_CMockIgnore()\n" +
"void Mold_CMockIgnore(void);\n" +
"#define Mold_StopIgnore() Mold_CMockStopIgnore()\n" +
"void Mold_CMockStopIgnore(void);\n"
returned = @cmock_generator_plugin_ignore_stateless.mock_function_declarations(function)
assert_equal(expected, returned)
end

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"+
expected = "#define Fungus_Ignore() TEST_FAIL_MESSAGE(\"Fungus requires _IgnoreAndReturn\");\n"+
"#define Fungus_IgnoreAndReturn(cmock_retval) Fungus_CMockIgnoreAndReturn(cmock_retval)\n"+
"void Fungus_CMockIgnoreAndReturn(const char* cmock_to_return);\n" +
"#define Fungus_StopIgnore() Fungus_CMockStopIgnore()\n"+
"void Fungus_CMockStopIgnore(void);\n"
Expand Down
10 changes: 7 additions & 3 deletions test/unit/cmock_generator_plugin_ignore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@

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" +
"#define Mold_StopIgnore() Mold_CMockStopIgnore()\nvoid Mold_CMockStopIgnore(void);\n"
expected = "#define Mold_IgnoreAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"Mold requires _Ignore (not AndReturn)\");\n" +
"#define Mold_Ignore() Mold_CMockIgnore()\n" +
"void Mold_CMockIgnore(void);\n" +
"#define Mold_StopIgnore() Mold_CMockStopIgnore()\n" +
"void Mold_CMockStopIgnore(void);\n"
returned = @cmock_generator_plugin_ignore.mock_function_declarations(function)
assert_equal(expected, returned)
end

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(__LINE__, cmock_retval)\n"+
expected = "#define Fungus_Ignore() TEST_FAIL_MESSAGE(\"Fungus requires _IgnoreAndReturn\");\n"+
"#define Fungus_IgnoreAndReturn(cmock_retval) Fungus_CMockIgnoreAndReturn(__LINE__, cmock_retval)\n"+
"void Fungus_CMockIgnoreAndReturn(UNITY_LINE_TYPE cmock_line, const char* cmock_to_return);\n" +
"#define Fungus_StopIgnore() Fungus_CMockStopIgnore()\n"+
"void Fungus_CMockStopIgnore(void);\n"
Expand Down
Loading

0 comments on commit 597a6d8

Please sign in to comment.