Skip to content

Commit

Permalink
🪲 Added fail messages when using Expect for ExpectAndReturn or vice v…
Browse files Browse the repository at this point in the history
…ersa. (see issue #462)
  • Loading branch information
mvandervoord committed Mar 14, 2024
1 parent 7925641 commit 07a0e25
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/cmock_generator_plugin_expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ def instance_typedefs(function)
def mock_function_declarations(function)
if function[:args].empty?
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"
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"
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"
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"
end
Expand Down
101 changes: 101 additions & 0 deletions test/system/test_interactions/wrong_expect_and_return.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
:cmock:
:plugins:
- # none

:systest:
:types: |
#define UINT32 unsigned int
typedef signed int custom_type;
:mockable: |
UINT32 foo(custom_type a);
UINT32 bar(void);
UINT32 foo_varargs(custom_type a, ...);
void do_it(void);
void do_another(int i);
:source:
:header: |
UINT32 function_a(int a);
void function_b(void);
UINT32 function_c(int a);
void function_d(void);
:code: |
UINT32 function_a(int a)
{
return foo((custom_type)a) + bar();
}
void function_b(void) { }
UINT32 function_c(int a)
{
return foo_varargs((custom_type)a, "ignored", 5);
}
void function_d(void)
{
do_it();
do_another(2);
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
:units:
- :pass: FALSE
:should: 'successfully report using Expect instead of ExpectAndReturn with args'
:code: |
test()
{
foo_Expect((custom_type)1);
bar_ExpectAndReturn(2);
function_a(1);
}
- :pass: FALSE
:should: 'successfully report using Expect instead of ExpectAndReturn without args'
:code: |
test()
{
foo_ExpectAndReturn((custom_type)1, 4);
bar_Expect();
function_a(1);
}
- :pass: FALSE
:should: 'successfully report using Expect instead of ExpectAndReturn with varargs'
:code: |
test()
{
foo_varargs_Expect((custom_type)3);
function_c(3);
}
- :pass: FALSE
:should: 'successfully report using ExpectAndReturn instead of Expect with args'
:code: |
test()
{
do_it_Expect();
do_another_ExpectAndReturn(2, 2);
function_d();
}
- :pass: FALSE
:should: 'successfully report using ExpectAndReturn instead of Expect without args'
:code: |
test()
{
do_it_ExpectAndReturn(6);
do_another_Expect(2);
function_d();
}
...
9 changes: 6 additions & 3 deletions test/unit/cmock_generator_plugin_expect_a_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@

it "add mock function declaration for functions of style 'void func(void)'" do
function = {:name => "Maple", :args => [], :return => test_return[:void]}
expected = "#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" +
expected = "#define Maple_ExpectAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"Maple requires _Expect (not AndReturn)\");\n" +
"#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" +
"void Maple_CMockExpect(UNITY_LINE_TYPE cmock_line);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end

it "add mock function declaration for functions of style 'int func(void)'" do
function = {:name => "Spruce", :args => [], :return => test_return[:int]}
expected = "#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" +
expected = "#define Spruce_Expect() TEST_FAIL_MESSAGE(\"Spruce requires _ExpectAndReturn\");\n" +
"#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" +
"void Spruce_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end

it "add mock function declaration for functions of style 'const char* func(int tofu)'" do
function = {:name => "Pine", :args => ["int tofu"], :args_string => "int tofu", :args_call => 'tofu', :return => test_return[:string]}
expected = "#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" +
expected = "#define Pine_Expect(tofu) TEST_FAIL_MESSAGE(\"Pine requires _ExpectAndReturn\");\n" +
"#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" +
"void Pine_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int tofu, const char* cmock_to_return);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
Expand Down
9 changes: 6 additions & 3 deletions test/unit/cmock_generator_plugin_expect_b_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@

it "add mock function declaration for functions of style 'void func(void)'" do
function = {:name => "Maple", :args => [], :return => test_return[:void]}
expected = "#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" +
expected = "#define Maple_ExpectAndReturn(cmock_retval) TEST_FAIL_MESSAGE(\"Maple requires _Expect (not AndReturn)\");\n" +
"#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" +
"void Maple_CMockExpect(UNITY_LINE_TYPE cmock_line);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end

it "add mock function declaration for functions of style 'int func(void)'" do
function = {:name => "Spruce", :args => [], :return => test_return[:int]}
expected = "#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" +
expected = "#define Spruce_Expect() TEST_FAIL_MESSAGE(\"Spruce requires _ExpectAndReturn\");\n" +
"#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" +
"void Spruce_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end

it "add mock function declaration for functions of style 'const char* func(int tofu)'" do
function = {:name => "Pine", :args => ["int tofu"], :args_string => "int tofu", :args_call => 'tofu', :return => test_return[:string]}
expected = "#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" +
expected = "#define Pine_Expect(tofu) TEST_FAIL_MESSAGE(\"Pine requires _ExpectAndReturn\");\n" +
"#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" +
"void Pine_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int tofu, const char* cmock_to_return);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
Expand Down

0 comments on commit 07a0e25

Please sign in to comment.