From 07a0e257b56c8cb2196e4a6a9d172da5059d343b Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 14 Mar 2024 08:59:31 -0400 Subject: [PATCH] :beetle: Added fail messages when using Expect for ExpectAndReturn or vice versa. (see issue #462) --- lib/cmock_generator_plugin_expect.rb | 4 + .../wrong_expect_and_return.yml | 101 ++++++++++++++++++ .../cmock_generator_plugin_expect_a_test.rb | 9 +- .../cmock_generator_plugin_expect_b_test.rb | 9 +- 4 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 test/system/test_interactions/wrong_expect_and_return.yml diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 3a79c1a4..ed44dab2 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -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 diff --git a/test/system/test_interactions/wrong_expect_and_return.yml b/test/system/test_interactions/wrong_expect_and_return.yml new file mode 100644 index 00000000..8b26385e --- /dev/null +++ b/test/system/test_interactions/wrong_expect_and_return.yml @@ -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(); + } + + +... diff --git a/test/unit/cmock_generator_plugin_expect_a_test.rb b/test/unit/cmock_generator_plugin_expect_a_test.rb index dd86689a..db57996e 100644 --- a/test/unit/cmock_generator_plugin_expect_a_test.rb +++ b/test/unit/cmock_generator_plugin_expect_a_test.rb @@ -64,7 +64,8 @@ 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) @@ -72,7 +73,8 @@ 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) @@ -80,7 +82,8 @@ 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) diff --git a/test/unit/cmock_generator_plugin_expect_b_test.rb b/test/unit/cmock_generator_plugin_expect_b_test.rb index 35d48715..2ba2622d 100644 --- a/test/unit/cmock_generator_plugin_expect_b_test.rb +++ b/test/unit/cmock_generator_plugin_expect_b_test.rb @@ -64,7 +64,8 @@ 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) @@ -72,7 +73,8 @@ 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) @@ -80,7 +82,8 @@ 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)