From 4fcca4bab768a0e347f75dfe4ccd8a8fa0654376 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 10 Dec 2015 11:09:26 -0500 Subject: [PATCH 1/7] - Moved the "Out of Memory" string into a const to stop repeating it. - Added test for Out of Memory error - reorder the way we handle includes in CMock internals to match the way it works in Unity --- Rakefile | 4 +- lib/cmock_generator_utils.rb | 2 +- src/cmock.c | 4 +- src/cmock.h | 2 + src/cmock_internals.h | 3 +- targets/clang_strict.yml | 1 + targets/gcc.yml | 1 + targets/gcc_64.yml | 1 + targets/gcc_tiny.yml | 79 +++++++++++++++++++ targets/iar_arm_v4.yml | 7 +- targets/iar_arm_v5.yml | 7 +- test/c/TestCMockC.c | 2 +- test/c/TestCMockC.yml | 1 + .../test_interactions/out_of_memory.yml | 65 +++++++++++++++ test/unit/cmock_generator_utils_test.rb | 6 +- 15 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 targets/gcc_tiny.yml create mode 100644 test/system/test_interactions/out_of_memory.yml diff --git a/Rakefile b/Rakefile index 32498fa..34ffdb5 100644 --- a/Rakefile +++ b/Rakefile @@ -62,7 +62,9 @@ namespace :test do desc "Run C Unit Tests" task :c => [:prep_system_tests] do - build_and_test_c_files + unless ($cfg['unsupported'].include? "C") + build_and_test_c_files + end end desc "Run System Tests" diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 0f4511c..d24a243 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -37,7 +37,7 @@ class CMockGeneratorUtils def code_add_base_expectation(func_name, global_ordering_supported=true) lines = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_#{func_name}_CALL_INSTANCE));\n" lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" - lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"CMock has run out of memory. Please allocate more.\");\n" + lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" lines << " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" lines << " Mock.#{func_name}_CallInstance = CMock_Guts_MemChain(Mock.#{func_name}_CallInstance, cmock_guts_index);\n" lines << " Mock.#{func_name}_IgnoreBool = (int)0;\n" if (@ignore) diff --git a/src/cmock.c b/src/cmock.c index b2a0824..0aa720d 100644 --- a/src/cmock.c +++ b/src/cmock.c @@ -5,8 +5,10 @@ ========================================== */ #include "unity.h" +#include "cmock.h" -#include "cmock_internals.h" +//public constants to be used by mocks +const char* CMockStringOutOfMemory = "CMock has run out of memory. Please allocate more."; //private variables #ifdef CMOCK_MEM_DYNAMIC diff --git a/src/cmock.h b/src/cmock.h index 2a5310c..af960a3 100644 --- a/src/cmock.h +++ b/src/cmock.h @@ -7,6 +7,8 @@ #ifndef CMOCK_FRAMEWORK_H #define CMOCK_FRAMEWORK_H +#include "cmock_internals.h" + //should be big enough to index full range of CMOCK_MEM_MAX #ifndef CMOCK_MEM_INDEX_TYPE #define CMOCK_MEM_INDEX_TYPE unsigned int diff --git a/src/cmock_internals.h b/src/cmock_internals.h index 08fb15c..149e125 100644 --- a/src/cmock_internals.h +++ b/src/cmock_internals.h @@ -7,7 +7,8 @@ #ifndef CMOCK_FRAMEWORK_INTERNALS_H #define CMOCK_FRAMEWORK_INTERNALS_H -#include "cmock.h" +//These are constants that the generated mocks have access to +const char* CMockStringOutOfMemory; //define CMOCK_MEM_DYNAMIC to grab memory as needed with malloc //when you do that, CMOCK_MEM_SIZE is used for incremental size instead of total diff --git a/targets/clang_strict.yml b/targets/clang_strict.yml index 5286971..3380e74 100644 --- a/targets/clang_strict.yml +++ b/targets/clang_strict.yml @@ -83,6 +83,7 @@ linker: destination: *systest_build_path unsupported: + - out_of_memory - callingconv colour: true diff --git a/targets/gcc.yml b/targets/gcc.yml index 76c7551..2375a73 100644 --- a/targets/gcc.yml +++ b/targets/gcc.yml @@ -50,6 +50,7 @@ linker: destination: *systest_build_path unsupported: + - out_of_memory - unity_64bit_support - callingconv diff --git a/targets/gcc_64.yml b/targets/gcc_64.yml index d21686a..0ac60b2 100644 --- a/targets/gcc_64.yml +++ b/targets/gcc_64.yml @@ -51,6 +51,7 @@ linker: destination: *systest_build_path unsupported: + - out_of_memory - callingconv colour: true diff --git a/targets/gcc_tiny.yml b/targets/gcc_tiny.yml new file mode 100644 index 0000000..ccd492e --- /dev/null +++ b/targets/gcc_tiny.yml @@ -0,0 +1,79 @@ +--- +compiler: + path: gcc + source_path: &systest_generated_path 'test/system/generated/' + unit_tests_path: &unit_tests_path 'examples/test/' + mocks_path: &systest_mocks_path 'test/system/generated/' + build_path: &systest_build_path 'test/system/build/' + options: + - '-c' + - '-Wall' + - '-Wextra' + - '-Wunused-parameter' + - '-Wno-address' + - '-Wno-invalid-token-paste' + - '-std=c99' + - '-pedantic' + - '-O0' + includes: + prefix: '-I' + items: + - *systest_generated_path + - *unit_tests_path + - *systest_mocks_path + - 'src/' + - 'vendor/unity/src/' + - 'vendor/c_exception/lib/' + - 'test/system/test_compilation/' + - 'test/' + defines: + prefix: '-D' + items: + - 'CMOCK_MEM_STATIC' + - 'CMOCK_MEM_SIZE=1024' + object_files: + prefix: '-o' + extension: '.o' + destination: *systest_build_path + +linker: + path: gcc + options: + - -lm + includes: + prefix: '-I' + object_files: + path: *systest_build_path + extension: '.o' + bin_files: + prefix: '-o' + extension: '.exe' + destination: *systest_build_path + +unsupported: + - all_plugins_but_other_limits + - all_plugins_coexist + - array_and_pointer_handling + - const_primitives_handling + - enforce_strict_ordering + - expect_and_return_custom_types + - expect_and_return_treat_as + - expect_and_throw + - expect_any_args + - fancy_pointer_handling + - function_pointer_handling + - newer_standards_stuff1 + - nonstandard_pased_stuff_1 + - nonstandard_pased_stuff_2 + - parsing_challenges + - return_thru_ptr_and_expect_any_args + - return_thru_ptr_ignore_arg + - struct_union_enum_expect_and_return + - struct_union_enum_expect_and_return_with_plugins + - stubs_with_callbacks + - unity_64bit_support + - unity_ignores + - callingconv + - C + +colour: true diff --git a/targets/iar_arm_v4.yml b/targets/iar_arm_v4.yml index fdda582..6e88a74 100644 --- a/targets/iar_arm_v4.yml +++ b/targets/iar_arm_v4.yml @@ -15,7 +15,7 @@ compiler: - --no_code_motion - --no_tbaa - --no_clustering - - --no_scheduling + - --no_scheduling - --debug - --cpu_mode thumb - --endian little @@ -50,7 +50,7 @@ compiler: prefix: '-o' extension: '.r79' destination: *systest_build_path - + linker: path: [*tools_root, 'common\bin\xlink.exe'] options: @@ -80,7 +80,7 @@ linker: prefix: '-o' extension: '.d79' destination: *systest_build_path - + simulator: path: [*tools_root, 'common\bin\CSpyBat.exe'] pre_support: @@ -100,6 +100,7 @@ simulator: - sim unsupported: + - out_of_memory - nonstandard_parsed_stuff_1 - const - callingconv diff --git a/targets/iar_arm_v5.yml b/targets/iar_arm_v5.yml index e44f631..f5a8541 100644 --- a/targets/iar_arm_v5.yml +++ b/targets/iar_arm_v5.yml @@ -14,7 +14,7 @@ compiler: - --no_code_motion - --no_tbaa - --no_clustering - - --no_scheduling + - --no_scheduling - --debug - --cpu_mode thumb - --endian=little @@ -49,7 +49,7 @@ compiler: prefix: '-o' extension: '.r79' destination: *systest_build_path - + linker: path: [*tools_root, 'arm\bin\ilinkarm.exe'] options: @@ -65,7 +65,7 @@ linker: prefix: '-o' extension: '.out' destination: *systest_build_path - + simulator: path: [*tools_root, 'common\bin\CSpyBat.exe'] pre_support: @@ -85,6 +85,7 @@ simulator: - sim unsupported: + - out_of_memory - nonstandard_parsed_stuff_1 - const - callingconv diff --git a/test/c/TestCMockC.c b/test/c/TestCMockC.c index 5fb32b6..385c177 100644 --- a/test/c/TestCMockC.c +++ b/test/c/TestCMockC.c @@ -5,7 +5,7 @@ ========================================== */ #include "unity.h" -#include "cmock_internals.h" +#include "cmock.h" #define TEST_MEM_INDEX_SIZE (sizeof(CMOCK_MEM_INDEX_TYPE)) diff --git a/test/c/TestCMockC.yml b/test/c/TestCMockC.yml index cd76154..d1e61ba 100644 --- a/test/c/TestCMockC.yml +++ b/test/c/TestCMockC.yml @@ -6,6 +6,7 @@ - 'vendor/unity/src/unity.c' :options: - 'TEST' + - 'CMOCK_MEM_STATIC' - 'CMOCK_MEM_SIZE=128' - 'CMOCK_MEM_ALIGN=2' - 'CMOCK_MEM_INDEX_TYPE=int' diff --git a/test/system/test_interactions/out_of_memory.yml b/test/system/test_interactions/out_of_memory.yml new file mode 100644 index 0000000..ed7bed5 --- /dev/null +++ b/test/system/test_interactions/out_of_memory.yml @@ -0,0 +1,65 @@ +--- +:cmock: + :plugins: [] + :treat_as: + custom_type: INT + +:systest: + :types: | + typedef struct _BIG_FAT_STRUCT_T + { + char bytes[512]; + } BIG_FAT_STRUCT_T; + + :mockable: | + void foo(BIG_FAT_STRUCT_T a); + + :source: + :header: | + void function_a(void); + void function_b(void); + + :code: | + void function_a(void) + { + BIG_FAT_STRUCT_T stuff = { { 8, 0 } }; + foo(stuff); + } + + void function_b(void) + { + BIG_FAT_STRUCT_T stuff1 = { { 9, 1, 0 } }; + BIG_FAT_STRUCT_T stuff2 = { { 9, 2, 0 } }; + foo(stuff1); + foo(stuff2); + } + + :tests: + :common: | + void setUp(void) {} + void tearDown(void) {} + + :units: + - :pass: TRUE + :should: 'successfully should be able to run function a because it only takes half the memory' + :code: | + test() + { + BIG_FAT_STRUCT_T expected = { { 8, 0 } }; + foo_Expect(expected); + function_a(); + } + + - :pass: FALSE + :should: 'should error out because we do not have eough memory to handle two of these structures' + :code: | + test() + { + BIG_FAT_STRUCT_T expected1 = { { 9, 1, 0 } }; + BIG_FAT_STRUCT_T expected2 = { { 9, 2, 0 } }; + foo_Expect(expected1); + foo_Expect(expected2); + function_b(); + } + +... diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index 0e96ced..b832077 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -58,7 +58,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do expected = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"CMock has run out of memory. Please allocate more.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " cmock_call_instance->LineNumber = cmock_line;\n" @@ -70,7 +70,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do expected = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"CMock has run out of memory. Please allocate more.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " Mock.Apple_IgnoreBool = (int)0;\n" + @@ -85,7 +85,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do expected = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"CMock has run out of memory. Please allocate more.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + " Mock.Apple_IgnoreBool = (int)0;\n" + From ada6dd2d220cd478d5ef9a60760326780de87a87 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 10 Dec 2015 13:08:12 -0500 Subject: [PATCH 2/7] - Pull in latest version of Unity --- vendor/unity | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/unity b/vendor/unity index b922d62..dfbf21c 160000 --- a/vendor/unity +++ b/vendor/unity @@ -1 +1 @@ -Subproject commit b922d622a6463a5ba5c745579c4428263e706bbf +Subproject commit dfbf21c2a3d3c69f0ca4dfb540451976855ca94e From 647876644b46eaef183e120397bab63cafc41ed5 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 10 Dec 2015 16:58:10 -0500 Subject: [PATCH 3/7] - Centralized error strings to a few constants. - Made use of the new UNITY_SET_DETAILS feature to background set up function & argument details for messages - CMock now takes up a lot less memory! woo! --- lib/cmock_generator.rb | 10 +- lib/cmock_generator_plugin_cexception.rb | 9 +- lib/cmock_generator_plugin_expect.rb | 18 +- lib/cmock_generator_plugin_ignore.rb | 1 + lib/cmock_generator_plugin_ignore_arg.rb | 2 +- lib/cmock_generator_plugin_return_thru_ptr.rb | 2 +- lib/cmock_generator_utils.rb | 55 ++-- src/cmock.c | 9 + src/cmock_internals.h | 11 +- targets/clang_strict.yml | 1 + targets/gcc.yml | 1 + targets/gcc_64.yml | 1 + targets/gcc_tiny.yml | 1 + targets/iar_arm_v4.yml | 1 + targets/iar_arm_v5.yml | 1 + .../doesnt_leave_details_behind.yml | 272 ++++++++++++++++++ .../enforce_strict_ordering.yml | 46 +-- test/unit/cmock_generator_main_test.rb | 8 +- .../cmock_generator_plugin_cexception_test.rb | 7 +- .../cmock_generator_plugin_expect_test.rb | 7 +- .../cmock_generator_plugin_ignore_arg_test.rb | 6 +- .../cmock_generator_plugin_ignore_test.rb | 2 + ...k_generator_plugin_return_thru_ptr_test.rb | 2 +- test/unit/cmock_generator_utils_test.rb | 77 +++-- 24 files changed, 456 insertions(+), 94 deletions(-) create mode 100644 test/system/test_interactions/doesnt_leave_details_behind.yml diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 9e93f10..c74c58d 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -199,20 +199,22 @@ class CMockGenerator file << "#{function_mod_and_rettype} #{function[:name]}(#{args_string})\n" file << "{\n" file << " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n" + file << " UNITY_SET_DETAIL(\"#{function[:name]}\");\n" file << " CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{function[:name]}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.#{function[:name]}_CallInstance);\n" file << " Mock.#{function[:name]}_CallInstance = CMock_Guts_MemNext(Mock.#{function[:name]}_CallInstance);\n" file << @plugins.run(:mock_implementation_precheck, function) - file << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"Function '#{function[:name]}' called more times than expected.\");\n" + file << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n" file << " cmock_line = cmock_call_instance->LineNumber;\n" if (@ordered) file << " if (cmock_call_instance->CallOrder > ++GlobalVerifyOrder)\n" - file << " UNITY_TEST_FAIL(cmock_line, \"Function '#{function[:name]}' called earlier than expected.\");\n" + file << " UNITY_TEST_FAIL(cmock_line, CMockStringCalledEarly);\n" file << " if (cmock_call_instance->CallOrder < GlobalVerifyOrder)\n" - file << " UNITY_TEST_FAIL(cmock_line, \"Function '#{function[:name]}' called later than expected.\");\n" - # file << " UNITY_TEST_ASSERT((cmock_call_instance->CallOrder == ++GlobalVerifyOrder), cmock_line, \"Out of order function calls. Function '#{function[:name]}'\");\n" + file << " UNITY_TEST_FAIL(cmock_line, CMockStringCalledLate);\n" + # file << " UNITY_TEST_ASSERT((cmock_call_instance->CallOrder == ++GlobalVerifyOrder), cmock_line, CMockStringCallOrder);\n" end return_type = function[:return][:const?] ? "(const #{function[:return][:type]})" : ((function[:return][:type] =~ /cmock/) ? "(#{function[:return][:type]})" : '') file << @plugins.run(:mock_implementation, function) + file << " UNITY_CLR_DETAILS();\n" file << " return #{return_type}cmock_call_instance->ReturnVal;\n" unless (function[:return][:void?]) file << "}\n\n" end diff --git a/lib/cmock_generator_plugin_cexception.rb b/lib/cmock_generator_plugin_cexception.rb index c9e7e2f..abd9059 100644 --- a/lib/cmock_generator_plugin_cexception.rb +++ b/lib/cmock_generator_plugin_cexception.rb @@ -2,13 +2,13 @@ # CMock Project - Automatic Mock Generation for C # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams # [Released under MIT License. Please refer to license.txt for details] -# ========================================== +# ========================================== class CMockGeneratorPluginCexception attr_reader :priority attr_reader :config, :utils - + def initialize(config, utils) @config = config @utils = utils @@ -22,7 +22,7 @@ class CMockGeneratorPluginCexception def instance_typedefs(function) " CEXCEPTION_T ExceptionToThrow;\n" end - + def mock_function_declarations(function) if (function[:args_string] == "void") return "#define #{function[:name]}_ExpectAndThrow(cmock_to_throw) #{function[:name]}_CMockExpectAndThrow(__LINE__, cmock_to_throw)\n" + @@ -34,7 +34,8 @@ class CMockGeneratorPluginCexception end def mock_implementation(function) - " if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n {\n" + + " if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n {\n" + + " UNITY_CLR_DETAILS();\n" + " Throw(cmock_call_instance->ExceptionToThrow);\n }\n" end diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 5359b10..ba59e78 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -2,7 +2,7 @@ # CMock Project - Automatic Mock Generation for C # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams # [Released under MIT License. Please refer to license.txt for details] -# ========================================== +# ========================================== class CMockGeneratorPluginExpect @@ -17,7 +17,7 @@ class CMockGeneratorPluginExpect @unity_helper = @utils.helpers[:unity_helper] @priority = 5 end - + def instance_typedefs(function) lines = "" lines << " #{function[:return][:type]} ReturnVal;\n" unless (function[:return][:void?]) @@ -27,7 +27,7 @@ class CMockGeneratorPluginExpect end lines end - + def mock_function_declarations(function) if (function[:args].empty?) if (function[:return][:void?]) @@ -37,7 +37,7 @@ class CMockGeneratorPluginExpect return "#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 - else + else if (function[:return][:void?]) return "#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" @@ -47,7 +47,7 @@ class CMockGeneratorPluginExpect end end end - + def mock_implementation(function) lines = "" function[:args].each do |arg| @@ -55,7 +55,7 @@ class CMockGeneratorPluginExpect end lines end - + def mock_interfaces(function) lines = "" func_name = function[:name] @@ -75,12 +75,14 @@ class CMockGeneratorPluginExpect lines << @utils.code_add_base_expectation(func_name) lines << @utils.code_call_argument_loader(function) lines << @utils.code_assign_argument_quickly("cmock_call_instance->ReturnVal", function[:return]) unless (function[:return][:void?]) + lines << " UNITY_CLR_DETAILS();\n" lines << "}\n\n" end - + def mock_verify(function) func_name = function[:name] - " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.#{func_name}_CallInstance, cmock_line, \"Function '#{func_name}' called less times than expected.\");\n" + " UNITY_SET_DETAIL(\"#{function[:name]}\");\n" + + " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.#{func_name}_CallInstance, cmock_line, CMockStringCalledLess);\n" end end diff --git a/lib/cmock_generator_plugin_ignore.rb b/lib/cmock_generator_plugin_ignore.rb index f031d72..26ad90f 100644 --- a/lib/cmock_generator_plugin_ignore.rb +++ b/lib/cmock_generator_plugin_ignore.rb @@ -35,6 +35,7 @@ class CMockGeneratorPluginIgnore def mock_implementation_precheck(function) lines = " if (Mock.#{function[:name]}_IgnoreBool)\n {\n" + lines << " UNITY_CLR_DETAILS();\n" if (function[:return][:void?]) lines << " return;\n }\n" else diff --git a/lib/cmock_generator_plugin_ignore_arg.rb b/lib/cmock_generator_plugin_ignore_arg.rb index 87ccbb2..4ea6350 100644 --- a/lib/cmock_generator_plugin_ignore_arg.rb +++ b/lib/cmock_generator_plugin_ignore_arg.rb @@ -35,7 +35,7 @@ class CMockGeneratorPluginIgnoreArg lines << "{\n" lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.#{func_name}_CallInstance));\n" - lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"#{arg_name} IgnoreArg called before Expect on '#{func_name}'.\");\n" + lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" lines << " cmock_call_instance->IgnoreArg_#{arg_name} = 1;\n" lines << "}\n\n" end diff --git a/lib/cmock_generator_plugin_return_thru_ptr.rb b/lib/cmock_generator_plugin_return_thru_ptr.rb index 716ec86..bf39922 100644 --- a/lib/cmock_generator_plugin_return_thru_ptr.rb +++ b/lib/cmock_generator_plugin_return_thru_ptr.rb @@ -46,7 +46,7 @@ class CMockGeneratorPluginReturnThruPtr lines << "{\n" lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.#{func_name}_CallInstance));\n" - lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"#{arg_name} ReturnThruPtr called before Expect on '#{func_name}'.\");\n" + lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringPtrPreExp);\n" lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Used = 1;\n" lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Val = #{arg_name};\n" lines << " cmock_call_instance->ReturnThruPtr_#{arg_name}_Size = cmock_size;\n" diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index d24a243..584cb81 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -121,30 +121,31 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" + lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n" when "UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY" if (pre == '&') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n" lines << " else\n" - lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, \"#{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, CMockStringMismatch); }\n" end when /_ARRAY/ if (pre == '&') - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n" lines << " else\n" - lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, \"#{unity_msg}\"); }\n" + lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, CMockStringMismatch); }\n" end else - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n" end lines << " }\n" lines @@ -156,30 +157,31 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" + lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n" when "UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY" if (pre == '&') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n" lines << " else\n" - lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, \"#{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, CMockStringMismatch); }\n" end when /_ARRAY/ if (pre == '&') - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n" lines << " else\n" - lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, \"#{unity_msg}\"); }\n" + lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch); }\n" end else - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n" end lines << " }\n" lines @@ -191,32 +193,33 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" + lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n" when "UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY" if (pre == '&') - lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" - lines << ((depth_name != 1) ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, \"#{unity_msg}\"); }\n" : "") + lines << " { UNITY_TEST_ASSERT_NULL(#{arg_name}, cmock_line, CMockStringExpNULL); }\n" + lines << ((depth_name != 1) ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch); }\n" : "") lines << " else\n" - lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, \"#{unity_msg}\"); }\n" + lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*','')}), #{depth_name}, cmock_line, CMockStringMismatch); }\n" end when /_ARRAY/ if (pre == '&') - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch);\n" else lines << " if (#{pre}#{expected} == NULL)\n" - lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, \"Expected NULL. #{unity_msg}\"); }\n" - lines << ((depth_name != 1) ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, \"#{unity_msg}\"); }\n" : "") + lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n" + lines << ((depth_name != 1) ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch); }\n" : "") lines << " else\n" - lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, \"#{unity_msg}\"); }\n" + lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch); }\n" end else - lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, \"#{unity_msg}\");\n" + lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n" end lines << " }\n" lines diff --git a/src/cmock.c b/src/cmock.c index 0aa720d..ba8a8bd 100644 --- a/src/cmock.c +++ b/src/cmock.c @@ -9,6 +9,15 @@ //public constants to be used by mocks const char* CMockStringOutOfMemory = "CMock has run out of memory. Please allocate more."; +const char* CMockStringCalledMore = "Called more times than expected."; +const char* CMockStringCalledLess = "Called less times than expected."; +const char* CMockStringCalledEarly = "Called earlier than expected."; +const char* CMockStringCalledLate = "Called later than expected."; +const char* CMockStringCallOrder = "Called out of order."; +const char* CMockStringIgnPreExp = "IgnoreArg called before Expect."; +const char* CMockStringPtrPreExp = "ReturnThruPtr called before Expect."; +const char* CMockStringExpNULL = "Expected NULL."; +const char* CMockStringMismatch = "Function called with unexpected argument value."; //private variables #ifdef CMOCK_MEM_DYNAMIC diff --git a/src/cmock_internals.h b/src/cmock_internals.h index 149e125..1100171 100644 --- a/src/cmock_internals.h +++ b/src/cmock_internals.h @@ -8,7 +8,16 @@ #define CMOCK_FRAMEWORK_INTERNALS_H //These are constants that the generated mocks have access to -const char* CMockStringOutOfMemory; +extern const char* CMockStringOutOfMemory; +extern const char* CMockStringCalledMore; +extern const char* CMockStringCalledLess; +extern const char* CMockStringCalledEarly; +extern const char* CMockStringCalledLate; +extern const char* CMockStringCallOrder; +extern const char* CMockStringIgnPreExp; +extern const char* CMockStringPtrPreExp; +extern const char* CMockStringExpNULL; +extern const char* CMockStringMismatch; //define CMOCK_MEM_DYNAMIC to grab memory as needed with malloc //when you do that, CMOCK_MEM_SIZE is used for incremental size instead of total diff --git a/targets/clang_strict.yml b/targets/clang_strict.yml index 3380e74..b552694 100644 --- a/targets/clang_strict.yml +++ b/targets/clang_strict.yml @@ -61,6 +61,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK - 'UNITY_SUPPORT_64' - 'UNITY_POINTER_WIDTH=64' object_files: diff --git a/targets/gcc.yml b/targets/gcc.yml index 2375a73..13fff4c 100644 --- a/targets/gcc.yml +++ b/targets/gcc.yml @@ -29,6 +29,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK - 'UNITY_SUPPORT_64' object_files: prefix: '-o' diff --git a/targets/gcc_64.yml b/targets/gcc_64.yml index 0ac60b2..354b9e9 100644 --- a/targets/gcc_64.yml +++ b/targets/gcc_64.yml @@ -28,6 +28,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK - 'UNITY_SUPPORT_64' - 'UNITY_POINTER_WIDTH=64' object_files: diff --git a/targets/gcc_tiny.yml b/targets/gcc_tiny.yml index ccd492e..7965349 100644 --- a/targets/gcc_tiny.yml +++ b/targets/gcc_tiny.yml @@ -29,6 +29,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK - 'CMOCK_MEM_STATIC' - 'CMOCK_MEM_SIZE=1024' object_files: diff --git a/targets/iar_arm_v4.yml b/targets/iar_arm_v4.yml index 6e88a74..dae46b9 100644 --- a/targets/iar_arm_v4.yml +++ b/targets/iar_arm_v4.yml @@ -46,6 +46,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK object_files: prefix: '-o' extension: '.r79' diff --git a/targets/iar_arm_v5.yml b/targets/iar_arm_v5.yml index f5a8541..886faff 100644 --- a/targets/iar_arm_v5.yml +++ b/targets/iar_arm_v5.yml @@ -45,6 +45,7 @@ compiler: defines: prefix: '-D' items: + - CMOCK object_files: prefix: '-o' extension: '.r79' diff --git a/test/system/test_interactions/doesnt_leave_details_behind.yml b/test/system/test_interactions/doesnt_leave_details_behind.yml new file mode 100644 index 0000000..5f12292 --- /dev/null +++ b/test/system/test_interactions/doesnt_leave_details_behind.yml @@ -0,0 +1,272 @@ +--- +:cmock: + :enforce_strict_ordering: 1 + :plugins: + - :array + - :cexception + - :ignore + - :callback + - :return_thru_ptr + - :ignore_arg + - :expect_any_args + :callback_after_arg_check: false + :callback_include_count: false + :treat_externs: :include + +:systest: + :types: | + typedef struct _POINT_T { + int x; + int y; + } POINT_T; + + :mockable: | + #include "CException.h" + extern void foo(POINT_T* a); + POINT_T* bar(void); + void no_args(void); + + :source: + :header: | + #include "CException.h" + void function_a(void); + void function_b(void); + void function_c(void); + int function_d(void); + void function_e(void); + + :code: | + void function_a(void) + { + foo(bar()); + no_args(); + } + + :tests: + :common: | + #include "CException.h" + void setUp(void) {} + void tearDown(void) {} + void my_foo_callback(POINT_T* a) { TEST_ASSERT_EQUAL_INT(2, a->x); } + + :units: + - :pass: TRUE + :should: 'just pass if we do not insert anything ugly into it' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after an expect and return' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after an expect' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after throw expectation' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + CEXCEPTION_T e; + + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_ExpectAndThrow(5); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + Try { function_a(); } Catch(e) {} + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after a mock call' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after throw' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + CEXCEPTION_T e; + + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_ExpectAndThrow(5); + + Try { function_a(); } Catch(e) {} + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignore' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Ignore(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignored mock' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Ignore(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after callback setup' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_StubWithCallback(my_foo_callback); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock with callback' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_StubWithCallback(my_foo_callback); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after expect any args' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_ExpectAnyArgs(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which expected any args' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_ExpectAnyArgs(); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignored arg' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_Expect(NULL); + foo_IgnoreArg_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which ignored an arg' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_Expect(NULL); + foo_IgnoreArg_a(); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + +... diff --git a/test/system/test_interactions/enforce_strict_ordering.yml b/test/system/test_interactions/enforce_strict_ordering.yml index 61e2999..53d1a68 100644 --- a/test/system/test_interactions/enforce_strict_ordering.yml +++ b/test/system/test_interactions/enforce_strict_ordering.yml @@ -8,7 +8,7 @@ :systest: :types: | #define UINT32 unsigned int - + typedef signed int custom_type; :mockable: | @@ -17,7 +17,7 @@ UINT32 bar(custom_type b); void baz(custom_type c); - :source: + :source: :header: | #include "CException.h" UINT32 function_a(int a, int b); @@ -26,13 +26,13 @@ void function_d(void); :code: | - UINT32 function_a(int a, int b) + UINT32 function_a(int a, int b) { return foo((custom_type)a) + bar((custom_type)b); - } + } - void function_b(void) - { + void function_b(void) + { baz((custom_type)1); foo((custom_type)2); bar((custom_type)3); @@ -42,7 +42,7 @@ baz((custom_type)7); } - void function_c(void) + void function_c(void) { foo((custom_type)1); foo((custom_type)2); @@ -50,7 +50,7 @@ bar((custom_type)4); foo((custom_type)5); } - + void function_d(void) { CEXCEPTION_T e; @@ -70,13 +70,13 @@ } Catch(e) {} } - + :tests: :common: | #include "CException.h" void setUp(void) {} void tearDown(void) {} - + :units: - :pass: TRUE :should: 'successfully exercise two simple ExpectAndReturn mock calls' @@ -87,20 +87,20 @@ bar_ExpectAndReturn((custom_type)2, 20); TEST_ASSERT_EQUAL(30, function_a(1, 2)); } - + - :pass: FALSE :should: 'fail because bar() is called but is not expected' - :verify_error: 'called more times than expected' + :verify_error: 'Called more times than expected' :code: | test() { foo_ExpectAndReturn((custom_type)1, 10); TEST_ASSERT_EQUAL(30, function_a(1, 2)); } - + - :pass: FALSE :should: 'fail because bar() is called twice but is expected once' - :verify_error: 'called less times than expected' + :verify_error: 'Called less times than expected' :code: | test() { @@ -112,7 +112,7 @@ - :pass: FALSE :should: 'fail because bar and foo called in reverse order' - :verify_error: 'called earlier than expected' + :verify_error: 'Called earlier than expected' :code: | test() { @@ -185,7 +185,7 @@ bar_ExpectAndReturn((custom_type)6, 10); function_b(); } - + - :pass: TRUE :should: 'pass when using cexception, as long as the order is right' :code: | @@ -207,7 +207,7 @@ foo_ExpectAndReturn((custom_type)3, 10); function_d(); } - + - :pass: TRUE :should: 'successfully handle back to back ExpectAndReturn setup and mock calls' :code: | @@ -216,30 +216,30 @@ foo_ExpectAndReturn((custom_type)1, 10); bar_ExpectAndReturn((custom_type)2, 20); TEST_ASSERT_EQUAL(30, function_a(1, 2)); - + foo_ExpectAndReturn((custom_type)3, 30); bar_ExpectAndReturn((custom_type)4, 40); TEST_ASSERT_EQUAL(70, function_a(3, 4)); - + foo_ExpectAndReturn((custom_type)1, 50); bar_ExpectAndReturn((custom_type)9, 60); TEST_ASSERT_EQUAL(110, function_a(1, 9)); } - + - :pass: FALSE :should: 'successfully catch errors during back to back ExpectAndReturn setup and mock calls' - :verify_error: 'called earlier than expected' + :verify_error: 'Called earlier than expected' :code: | test() { foo_ExpectAndReturn((custom_type)1, 10); bar_ExpectAndReturn((custom_type)2, 20); TEST_ASSERT_EQUAL(30, function_a(1, 2)); - + foo_ExpectAndReturn((custom_type)3, 30); bar_ExpectAndReturn((custom_type)4, 40); TEST_ASSERT_EQUAL(70, function_a(3, 4)); - + bar_ExpectAndReturn((custom_type)9, 60); foo_ExpectAndReturn((custom_type)1, 50); TEST_ASSERT_EQUAL(110, function_a(1, 9)); diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index a38059e..8b2041d 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -440,13 +440,15 @@ describe CMockGenerator, "Verify CMockGenerator Module" do expected = [ "static int SupaFunction(uint32 sandwiches, const char* named)\n", "{\n", " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " UNITY_SET_DETAIL(\"SupaFunction\");\n", " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", " uno", - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"Function 'SupaFunction' called more times than expected.\");\n", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", " cmock_line = cmock_call_instance->LineNumber;\n", " dos", " tres", + " UNITY_CLR_DETAILS();\n", " return cmock_call_instance->ReturnVal;\n", "}\n\n" ] @@ -472,13 +474,15 @@ describe CMockGenerator, "Verify CMockGenerator Module" do expected = [ "int __stdcall SupaFunction(uint32 sandwiches, corn ...)\n", "{\n", " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " UNITY_SET_DETAIL(\"SupaFunction\");\n", " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", " uno", - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"Function 'SupaFunction' called more times than expected.\");\n", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", " cmock_line = cmock_call_instance->LineNumber;\n", " dos", " tres", + " UNITY_CLR_DETAILS();\n", " return cmock_call_instance->ReturnVal;\n", "}\n\n" ] diff --git a/test/unit/cmock_generator_plugin_cexception_test.rb b/test/unit/cmock_generator_plugin_cexception_test.rb index 4a366e8..931d9c0 100644 --- a/test/unit/cmock_generator_plugin_cexception_test.rb +++ b/test/unit/cmock_generator_plugin_cexception_test.rb @@ -52,8 +52,11 @@ describe CMockGeneratorPluginCexception, "Verify CMockGeneratorPluginCexception it "add a mock implementation" do function = {:name => "Cherry", :args => [], :return => test_return[:void]} - expected = " if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n {\n" + - " Throw(cmock_call_instance->ExceptionToThrow);\n }\n" + expected = " if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n" + + " {\n" + + " UNITY_CLR_DETAILS();\n" + + " Throw(cmock_call_instance->ExceptionToThrow);\n" + + " }\n" returned = @cmock_generator_plugin_cexception.mock_implementation(function) assert_equal(expected, returned) end diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index 7ed4f1a..e1a6529 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -142,6 +142,7 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" "{\n", "mock_retval_0 ", "mock_retval_1 ", + " UNITY_CLR_DETAILS();\n", "}\n\n" ].join returned = @cmock_generator_plugin_expect.mock_interfaces(function) @@ -158,6 +159,7 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" "mock_retval_0 ", "mock_retval_1 ", "mock_retval_2", + " UNITY_CLR_DETAILS();\n", "}\n\n" ].join returned = @cmock_generator_plugin_expect.mock_interfaces(function) @@ -174,6 +176,7 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" "mock_retval_0 ", "mock_retval_1 ", "mock_retval_2", + " UNITY_CLR_DETAILS();\n", "}\n\n" ].join returned = @cmock_generator_plugin_expect.mock_interfaces(function) @@ -188,6 +191,7 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" "{\n", "mock_retval_0 ", "mock_retval_1 ", + " UNITY_CLR_DETAILS();\n", "}\n\n" ].join @cmock_generator_plugin_expect.ordered = true @@ -197,7 +201,8 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" it "add mock verify lines" do function = {:name => "Banana" } - expected = " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.Banana_CallInstance, cmock_line, \"Function 'Banana' called less times than expected.\");\n" + expected = " UNITY_SET_DETAIL(\"Banana\");\n" + + " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.Banana_CallInstance, cmock_line, CMockStringCalledLess);\n" returned = @cmock_generator_plugin_expect.mock_verify(function) assert_equal(expected, returned) end diff --git a/test/unit/cmock_generator_plugin_ignore_arg_test.rb b/test/unit/cmock_generator_plugin_ignore_arg_test.rb index a6025d5..f5329d3 100644 --- a/test/unit/cmock_generator_plugin_ignore_arg_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_arg_test.rb @@ -85,7 +85,7 @@ describe CMockGeneratorPluginIgnoreArg, "Verify CMockGeneratorPluginIgnoreArg Mo "{\n" + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"chicken IgnoreArg called before Expect on 'Pine'.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + " cmock_call_instance->IgnoreArg_chicken = 1;\n" + "}\n\n" + @@ -93,7 +93,7 @@ describe CMockGeneratorPluginIgnoreArg, "Verify CMockGeneratorPluginIgnoreArg Mo "{\n" + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"beef IgnoreArg called before Expect on 'Pine'.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + " cmock_call_instance->IgnoreArg_beef = 1;\n" + "}\n\n" + @@ -101,7 +101,7 @@ describe CMockGeneratorPluginIgnoreArg, "Verify CMockGeneratorPluginIgnoreArg Mo "{\n" + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"tofu IgnoreArg called before Expect on 'Pine'.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + " cmock_call_instance->IgnoreArg_tofu = 1;\n" + "}\n\n" diff --git a/test/unit/cmock_generator_plugin_ignore_test.rb b/test/unit/cmock_generator_plugin_ignore_test.rb index 2692e35..2933b02 100644 --- a/test/unit/cmock_generator_plugin_ignore_test.rb +++ b/test/unit/cmock_generator_plugin_ignore_test.rb @@ -52,6 +52,7 @@ describe CMockGeneratorPluginIgnore, "Verify CMockGeneratorPluginIgnore Module" function = {:name => "Mold", :args_string => "void", :return => test_return[:void]} expected = [" if (Mock.Mold_IgnoreBool)\n", " {\n", + " UNITY_CLR_DETAILS();\n", " return;\n", " }\n" ].join @@ -65,6 +66,7 @@ describe CMockGeneratorPluginIgnore, "Verify CMockGeneratorPluginIgnore Module" @utils.expect :code_assign_argument_quickly, ' mock_retval_0', ["Mock.Fungus_FinalReturn", retval] expected = [" if (Mock.Fungus_IgnoreBool)\n", " {\n", + " UNITY_CLR_DETAILS();\n", " if (cmock_call_instance == NULL)\n", " return Mock.Fungus_FinalReturn;\n", " mock_retval_0", diff --git a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb index 839a087..7b93f00 100644 --- a/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb +++ b/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb @@ -108,7 +108,7 @@ describe CMockGeneratorPluginReturnThruPtr, "Verify CMockGeneratorPluginReturnTh "{\n" + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + - " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, \"tofu ReturnThruPtr called before Expect on 'Pine'.\");\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringPtrPreExp);\n" + " cmock_call_instance->ReturnThruPtr_tofu_Used = 1;\n" + " cmock_call_instance->ReturnThruPtr_tofu_Val = tofu;\n" + " cmock_call_instance->ReturnThruPtr_tofu_Size = cmock_size;\n" + diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index b832077..f62083b 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -204,7 +204,10 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle a simple assert when requested' do function = { :name => 'Pear' } arg = test_arg[:int] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyInt'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyInt\");\n" + + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT', ''], ['int'] assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -213,14 +216,20 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle a pointer comparison when configured to do so' do function = { :name => 'Pear' } arg = test_arg[:int_ptr] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyIntPtr'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyIntPtr\");\n" + + " UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, CMockStringMismatch);\n" + + " }\n" assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) end it 'handle const char as string compares ' do function = { :name => 'Pear' } arg = test_arg[:string] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyStr'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyStr\");\n" + + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['char*'] assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -229,7 +238,10 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle custom types as memory compares when we have no better way to do it' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType), sizeof(MY_TYPE), cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType), sizeof(MY_TYPE), cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MEMORY','&'], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -238,7 +250,10 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle custom types with custom handlers when available, even if they do not support the extra message' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE',''], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -247,7 +262,10 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle pointers to custom types with array handlers, even if the array extension is turned off' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " {\n UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\");\n }\n" + expected = " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY','&'], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -256,7 +274,11 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle a simple assert when requested with array plugin enabled' do function = { :name => 'Pear' } arg = test_arg[:int] - expected = " if (!cmock_call_instance->IgnoreArg_MyInt)\n {\n UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyInt'.\");\n }\n" + expected = " if (!cmock_call_instance->IgnoreArg_MyInt)\n" + + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyInt\");\n" + + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT',''], ['int'] assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) @@ -267,12 +289,13 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:int_ptr] expected = " if (!cmock_call_instance->IgnoreArg_MyIntPtr)\n" + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyIntPtr\");\n" + " if (cmock_call_instance->Expected_MyIntPtr == NULL)\n" + - " { UNITY_TEST_ASSERT_NULL(MyIntPtr, cmock_line, \"Expected NULL. Function 'Pear' called with unexpected value for argument 'MyIntPtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_NULL(MyIntPtr, cmock_line, CMockStringExpNULL); }\n" + " else if (cmock_call_instance->Expected_MyIntPtr_Depth == 0)\n" + - " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyIntPtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, CMockStringMismatch); }\n" + " else\n" + - " { UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_call_instance->Expected_MyIntPtr_Depth, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyIntPtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_call_instance->Expected_MyIntPtr_Depth, cmock_line, CMockStringMismatch); }\n" + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT_ARRAY',''], ['int*'] @@ -282,7 +305,11 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle const char as string compares with array plugin enabled' do function = { :name => 'Pear' } arg = test_arg[:string] - expected = " if (!cmock_call_instance->IgnoreArg_MyStr)\n {\n UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyStr'.\");\n }\n" + expected = " if (!cmock_call_instance->IgnoreArg_MyStr)\n" + + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyStr\");\n" + + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['char*'] assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) @@ -291,7 +318,14 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle custom types as memory compares when we have no better way to do it with array plugin enabled' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n {\n if (cmock_call_instance->Expected_MyMyType == NULL)\n { UNITY_TEST_ASSERT_NULL(MyMyType, cmock_line, \"Expected NULL. Function 'Pear' called with unexpected value for argument 'MyMyType'.\"); }\n else\n { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(cmock_call_instance->Expected_MyMyType), (void*)(MyMyType), sizeof(MY_TYPE), 1, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\"); }\n }\n" + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " if (cmock_call_instance->Expected_MyMyType == NULL)\n" + + " { UNITY_TEST_ASSERT_NULL(MyMyType, cmock_line, CMockStringExpNULL); }\n" + + " else\n" + + " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(cmock_call_instance->Expected_MyMyType), (void*)(MyMyType), sizeof(MY_TYPE), 1, cmock_line, CMockStringMismatch); }\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY', ''], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) @@ -300,7 +334,11 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle custom types with custom handlers when available, even if they do not support the extra message with array plugin enabled' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n {\n UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\");\n }\n" + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE', ''], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) @@ -311,12 +349,13 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:mytype_ptr] expected = " if (!cmock_call_instance->IgnoreArg_MyMyTypePtr)\n" + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyTypePtr\");\n" + " if (cmock_call_instance->Expected_MyMyTypePtr == NULL)\n" + - " { UNITY_TEST_ASSERT_NULL(MyMyTypePtr, cmock_line, \"Expected NULL. Function 'Pear' called with unexpected value for argument 'MyMyTypePtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_NULL(MyMyTypePtr, cmock_line, CMockStringExpNULL); }\n" + " else if (cmock_call_instance->Expected_MyMyTypePtr_Depth == 0)\n" + - " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyTypePtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_line, CMockStringMismatch); }\n" + " else\n" + - " { UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_call_instance->Expected_MyMyTypePtr_Depth, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyTypePtr'.\"); }\n" + + " { UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_call_instance->Expected_MyMyTypePtr_Depth, cmock_line, CMockStringMismatch); }\n" + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY', ''], ['MY_TYPE*'] @@ -326,7 +365,11 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do it 'handle custom types with array handlers when array plugin is enabled for non-array types' do function = { :name => 'Pear' } arg = test_arg[:mytype] - expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n {\n UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, \"Function 'Pear' called with unexpected value for argument 'MyMyType'.\");\n }\n" + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + + " }\n" @unity_helper.expect :nil?, false @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY', '&'], ['MY_TYPE'] assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) From ffe2098a5beb5ca72b953c51ef669e6cfb6ae992 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 10 Dec 2015 18:22:17 -0500 Subject: [PATCH 4/7] - Pull in nicer formatting and fixes to RUN_TEST found in Unity --- vendor/unity | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/unity b/vendor/unity index dfbf21c..28195ba 160000 --- a/vendor/unity +++ b/vendor/unity @@ -1 +1 @@ -Subproject commit dfbf21c2a3d3c69f0ca4dfb540451976855ca94e +Subproject commit 28195baabaa7a88508526ab95d3aa0ed1356fb04 From f78ab04001a00d09e04e24f75b658d3530d4438c Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 10 Dec 2015 21:49:34 -0500 Subject: [PATCH 5/7] - Grab the latest version of Unity in order to inherit detail display for fail, null, etc. --- vendor/unity | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/unity b/vendor/unity index 28195ba..e8662ae 160000 --- a/vendor/unity +++ b/vendor/unity @@ -1 +1 @@ -Subproject commit 28195baabaa7a88508526ab95d3aa0ed1356fb04 +Subproject commit e8662ae1cc0ed58f43bab769b340fab05be75501 From cda8d3a7e1561851b267470ee5bf7905018e4881 Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Sat, 12 Dec 2015 12:24:38 -0500 Subject: [PATCH 6/7] - push the internal function and argument names into a single each per mock to further reduce const overhead. --- lib/cmock_generator.rb | 15 ++++++++--- lib/cmock_generator_plugin_expect.rb | 2 +- lib/cmock_generator_utils.rb | 6 ++--- test/unit/cmock_generator_main_test.rb | 17 +++++++++--- .../cmock_generator_plugin_expect_test.rb | 2 +- test/unit/cmock_generator_utils_test.rb | 26 +++++++++---------- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index c74c58d..ad7220e 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -68,7 +68,7 @@ class CMockGenerator def create_mock_source_file(parsed_stuff) @file_writer.create_file(@mock_name + ".c", @subdir) do |file, filename| - create_source_header_section(file, filename) + create_source_header_section(file, filename, parsed_stuff[:functions]) create_instance_structure(file, parsed_stuff[:functions]) create_extern_declarations(file) create_mock_verify_function(file, parsed_stuff[:functions]) @@ -120,7 +120,7 @@ class CMockGenerator header << "\n#endif\n" end - def create_source_header_section(file, filename) + def create_source_header_section(file, filename, functions) header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",".h") file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" file << "#include \n" @@ -132,6 +132,15 @@ class CMockGenerator file << "#include \"#{header_file}\"\n" @includes_c_post_header.each {|inc| file << "#include #{inc}\n"} file << "\n" + strs = [] + functions.each do |func| + strs << func[:name] + func[:args].each {|arg| strs << arg[:name] } + end + strs.uniq.sort.each do |str| + file << "static const char* CMockString_#{str} = \"#{str}\";\n" + end + file << "\n" end def create_instance_structure(file, functions) @@ -199,7 +208,7 @@ class CMockGenerator file << "#{function_mod_and_rettype} #{function[:name]}(#{args_string})\n" file << "{\n" file << " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n" - file << " UNITY_SET_DETAIL(\"#{function[:name]}\");\n" + file << " UNITY_SET_DETAIL(CMockString_#{function[:name]});\n" file << " CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{function[:name]}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.#{function[:name]}_CallInstance);\n" file << " Mock.#{function[:name]}_CallInstance = CMock_Guts_MemNext(Mock.#{function[:name]}_CallInstance);\n" file << @plugins.run(:mock_implementation_precheck, function) diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index ba59e78..bfa506f 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -81,7 +81,7 @@ class CMockGeneratorPluginExpect def mock_verify(function) func_name = function[:name] - " UNITY_SET_DETAIL(\"#{function[:name]}\");\n" + + " UNITY_SET_DETAIL(CMockString_#{function[:name]});\n" + " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.#{func_name}_CallInstance, cmock_line, CMockStringCalledLess);\n" end diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index 584cb81..8ad43b6 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -121,7 +121,7 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" - lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" + lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') @@ -157,7 +157,7 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" - lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" + lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') @@ -193,7 +193,7 @@ class CMockGeneratorUtils lines = "" lines << " if (!#{ignore})\n" if @ignore_arg lines << " {\n" - lines << " UNITY_SET_DETAILS(\"#{function[:name]}\",\"#{arg_name}\");\n" + lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n" case(unity_func) when "UNITY_TEST_ASSERT_EQUAL_MEMORY" c_type_local = c_type.gsub(/\*$/,'') diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 8b2041d..e2d9988 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -267,6 +267,10 @@ describe CMockGenerator, "Verify CMockGenerator Module" do it "create a proper heading for a source file" do output = [] + functions = [ { :name => "uno", :args => [ { :name => "arg1" }, { :name => "arg2" } ] }, + { :name => "dos", :args => [ { :name => "arg3" }, { :name => "arg2" } ] }, + { :name => "tres", :args => [] } + ] expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", "#include \n", "#include \n", @@ -274,10 +278,17 @@ describe CMockGenerator, "Verify CMockGenerator Module" do "#include \"unity.h\"\n", "#include \"cmock.h\"\n", "#include \"MockPoutPoutFish.h\"\n", + "\n", + "static const char* CMockString_arg1 = \"arg1\";\n", + "static const char* CMockString_arg2 = \"arg2\";\n", + "static const char* CMockString_arg3 = \"arg3\";\n", + "static const char* CMockString_dos = \"dos\";\n", + "static const char* CMockString_tres = \"tres\";\n", + "static const char* CMockString_uno = \"uno\";\n", "\n" ] - @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c") + @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c", functions) assert_equal(expected, output) end @@ -440,7 +451,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do expected = [ "static int SupaFunction(uint32 sandwiches, const char* named)\n", "{\n", " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", - " UNITY_SET_DETAIL(\"SupaFunction\");\n", + " UNITY_SET_DETAIL(CMockString_SupaFunction);\n", " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", " uno", @@ -474,7 +485,7 @@ describe CMockGenerator, "Verify CMockGenerator Module" do expected = [ "int __stdcall SupaFunction(uint32 sandwiches, corn ...)\n", "{\n", " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", - " UNITY_SET_DETAIL(\"SupaFunction\");\n", + " UNITY_SET_DETAIL(CMockString_SupaFunction);\n", " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", " uno", diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb index e1a6529..38ffd1d 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -201,7 +201,7 @@ describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module" it "add mock verify lines" do function = {:name => "Banana" } - expected = " UNITY_SET_DETAIL(\"Banana\");\n" + + expected = " UNITY_SET_DETAIL(CMockString_Banana);\n" + " UNITY_TEST_ASSERT(CMOCK_GUTS_NONE == Mock.Banana_CallInstance, cmock_line, CMockStringCalledLess);\n" returned = @cmock_generator_plugin_expect.mock_verify(function) assert_equal(expected, returned) diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb index f62083b..4bf253b 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -205,7 +205,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:int] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyInt\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyInt);\n" + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -217,7 +217,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:int_ptr] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyIntPtr\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyIntPtr);\n" + " UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, CMockStringMismatch);\n" + " }\n" assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) @@ -227,7 +227,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:string] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyStr\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyStr);\n" + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -239,7 +239,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:mytype] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType), sizeof(MY_TYPE), cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -251,7 +251,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:mytype] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -263,7 +263,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do function = { :name => 'Pear' } arg = test_arg[:mytype] expected = " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -276,7 +276,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:int] expected = " if (!cmock_call_instance->IgnoreArg_MyInt)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyInt\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyInt);\n" + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -289,7 +289,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:int_ptr] expected = " if (!cmock_call_instance->IgnoreArg_MyIntPtr)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyIntPtr\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyIntPtr);\n" + " if (cmock_call_instance->Expected_MyIntPtr == NULL)\n" + " { UNITY_TEST_ASSERT_NULL(MyIntPtr, cmock_line, CMockStringExpNULL); }\n" + " else if (cmock_call_instance->Expected_MyIntPtr_Depth == 0)\n" + @@ -307,7 +307,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:string] expected = " if (!cmock_call_instance->IgnoreArg_MyStr)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyStr\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyStr);\n" + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -320,7 +320,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:mytype] expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " if (cmock_call_instance->Expected_MyMyType == NULL)\n" + " { UNITY_TEST_ASSERT_NULL(MyMyType, cmock_line, CMockStringExpNULL); }\n" + " else\n" + @@ -336,7 +336,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:mytype] expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false @@ -349,7 +349,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:mytype_ptr] expected = " if (!cmock_call_instance->IgnoreArg_MyMyTypePtr)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyTypePtr\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyTypePtr);\n" + " if (cmock_call_instance->Expected_MyMyTypePtr == NULL)\n" + " { UNITY_TEST_ASSERT_NULL(MyMyTypePtr, cmock_line, CMockStringExpNULL); }\n" + " else if (cmock_call_instance->Expected_MyMyTypePtr_Depth == 0)\n" + @@ -367,7 +367,7 @@ describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do arg = test_arg[:mytype] expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + " {\n" + - " UNITY_SET_DETAILS(\"Pear\",\"MyMyType\");\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + " }\n" @unity_helper.expect :nil?, false From d1ab630308ecc303bc027025ee10ad1a4c841eaa Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Wed, 30 Dec 2015 16:33:33 -0500 Subject: [PATCH 7/7] - Added a couple more tests to our details test - Added a gitattributes file to force line endings to be correct. --- .gitattributes | 30 + lib/cmock_generator_plugin_return_thru_ptr.rb | 2 +- rakefile_helper.rb | 762 +++++++++--------- .../doesnt_leave_details_behind.yml | 580 ++++++------- 4 files changed, 720 insertions(+), 654 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..ad95226 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,30 @@ +* text=auto + +# These files are text and should be normalized (convert crlf to lf) +*.rb text +*.test text +*.c text +*.cpp text +*.h text +*.txt text +*.yml text +*.s79 text +*.bat text +*.xcl text +*.inc text +*.info text +*.md text +makefile text +rakefile text + + +#These files are binary and should not be normalized +*.doc binary +*.odt binary +*.pdf binary +*.ewd binary +*.eww binary +*.dni binary +*.wsdt binary +*.dbgdt binary +*.mac binary diff --git a/lib/cmock_generator_plugin_return_thru_ptr.rb b/lib/cmock_generator_plugin_return_thru_ptr.rb index bf39922..67935b4 100644 --- a/lib/cmock_generator_plugin_return_thru_ptr.rb +++ b/lib/cmock_generator_plugin_return_thru_ptr.rb @@ -4,7 +4,7 @@ class CMockGeneratorPluginReturnThruPtr def initialize(config, utils) @utils = utils - @priority = 1 + @priority = 1 #TODO: it's 1 to come before ignore_arg returns... but should be after expects otherwise (like 9) end def instance_typedefs(function) diff --git a/rakefile_helper.rb b/rakefile_helper.rb index ea86347..88b3231 100644 --- a/rakefile_helper.rb +++ b/rakefile_helper.rb @@ -1,381 +1,381 @@ -# ========================================== -# CMock Project - Automatic Mock Generation for C -# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams -# [Released under MIT License. Please refer to license.txt for details] -# ========================================== - -require 'yaml' -require 'fileutils' -require './vendor/unity/auto/generate_test_runner' -require './vendor/unity/auto/unity_test_summary' -require './test/system/systest_generator' -require './vendor/unity/auto/colour_reporter.rb' - -module RakefileHelpers - - SYSTEST_GENERATED_FILES_PATH = 'test/system/generated/' - SYSTEST_BUILD_FILES_PATH = 'test/system/build/' - SYSTEST_COMPILE_MOCKABLES_PATH = 'test/system/test_compilation/' - C_EXTENSION = '.c' - RESULT_EXTENSION = '.result' - - def load_configuration(config_file) - $cfg_file = config_file - $cfg = YAML.load(File.read('./targets/' + $cfg_file)) - $colour_output = false unless $cfg['colour'] - end - - def configure_clean - CLEAN.include(SYSTEST_GENERATED_FILES_PATH + '*.*') - CLEAN.include(SYSTEST_BUILD_FILES_PATH + '*.*') - end - - def configure_toolchain(config_file) - load_configuration(config_file) - configure_clean - end - - def get_local_include_dirs - include_dirs = $cfg['compiler']['includes']['items'].dup - include_dirs.delete_if {|dir| dir.is_a?(Array)} - return include_dirs - end - - def extract_headers(filename) - includes = [] - lines = File.readlines(filename) - lines.each do |line| - m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/) - if not m.nil? - includes << m[1] - end - end - return includes - end - - def find_source_file(header, paths) - paths.each do |dir| - src_file = dir + header.ext(C_EXTENSION) - if (File.exists?(src_file)) - return src_file - end - end - return nil - end - - def squash(prefix, items) - result = '' - items.each { |item| result += " #{prefix}#{tackit(item)}" } - return result - end - - def build_compiler_fields - command = tackit($cfg['compiler']['path']) - if $cfg['compiler']['defines']['items'].nil? - defines = '' - else - defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items']) - end - options = squash('', $cfg['compiler']['options']) - includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items']) - includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) - return {:command => command, :defines => defines, :options => options, :includes => includes} - end - - def compile(file, defines=[]) - compiler = build_compiler_fields - cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{defines.inject(''){|all, a| ' -D'+a+all }}#{compiler[:options]}#{compiler[:includes]} #{file} " + - "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" - obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}" - execute(cmd_str + obj_file) - return obj_file - end - - def build_linker_fields - command = tackit($cfg['linker']['path']) - if $cfg['linker']['options'].nil? - options = '' - else - options = squash('', $cfg['linker']['options']) - end - if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?) - includes = '' - else - includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items']) - end - includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) - return {:command => command, :options => options, :includes => includes} - end - - def link_it(exe_name, obj_list) - linker = build_linker_fields - cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " + - (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).uniq.join + - $cfg['linker']['bin_files']['prefix'] + ' ' + - $cfg['linker']['bin_files']['destination'] + - exe_name + $cfg['linker']['bin_files']['extension'] - execute(cmd_str) - end - - def build_simulator_fields - return nil if $cfg['simulator'].nil? - if $cfg['simulator']['path'].nil? - command = '' - else - command = (tackit($cfg['simulator']['path']) + ' ') - end - if $cfg['simulator']['pre_support'].nil? - pre_support = '' - else - pre_support = squash('', $cfg['simulator']['pre_support']) - end - if $cfg['simulator']['post_support'].nil? - post_support = '' - else - post_support = squash('', $cfg['simulator']['post_support']) - end - return {:command => command, :pre_support => pre_support, :post_support => post_support} - end - - def execute(command_string, verbose=true, raise_on_failure=true) - #report command_string - output = `#{command_string}`.chomp - report(output) if (verbose && !output.nil? && (output.length > 0)) - if ($?.exitstatus != 0) and (raise_on_failure) - raise "#{command_string} failed. (Returned #{$?.exitstatus})" - end - return output - end - - def tackit(strings) - case(strings) - when Array - "\"#{strings.join}\"" - when /^-/ - strings - when /\s/ - "\"#{strings}\"" - else - strings - end - end - - def report_summary - summary = UnityTestSummary.new - summary.set_root_path(File.expand_path(File.dirname(__FILE__)) + '/') - results_glob = "#{$cfg['compiler']['build_path']}*.test*" - results_glob.gsub!(/\\/, '/') - results = Dir[results_glob] - summary.set_targets(results) - summary.run - fail_out "FAIL: There were failures" if (summary.failures > 0) - end - - def run_system_test_interactions(test_case_files) - load './lib/cmock.rb' - - SystemTestGenerator.new.generate_files(test_case_files) - test_files = FileList.new(SYSTEST_GENERATED_FILES_PATH + 'test*.c') - - load_configuration($cfg_file) - $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? - - include_dirs = get_local_include_dirs - - # Build and execute each unit test - test_files.each do |test| - - obj_list = [] - - test_base = File.basename(test, C_EXTENSION) - cmock_config = test_base.gsub(/test_/, '') + '_cmock.yml' - - report "Executing system tests in #{File.basename(test)}..." - - # Detect dependencies and build required required modules - extract_headers(test).each do |header| - - # Generate any needed mocks - if header =~ /^mock_(.*)\.h/i - module_name = $1 - cmock = CMock.new(SYSTEST_GENERATED_FILES_PATH + cmock_config) - cmock.setup_mocks("#{$cfg['compiler']['source_path']}#{module_name}.h") - end - # Compile corresponding source file if it exists - src_file = find_source_file(header, include_dirs) - if !src_file.nil? - obj_list << compile(src_file) - end - end - - # Generate and build the test suite runner - runner_name = test_base + '_runner.c' - runner_path = $cfg['compiler']['source_path'] + runner_name - UnityTestRunnerGenerator.new(SYSTEST_GENERATED_FILES_PATH + cmock_config).run(test, runner_path) - obj_list << compile(runner_path) - - # Build the test module - obj_list << compile(test) - - # Link the test executable - link_it(test_base, obj_list) - - # Execute unit test and generate results file - simulator = build_simulator_fields - executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension'] - if simulator.nil? - cmd_str = executable - else - cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}" - end - output = execute(cmd_str, false, false) - test_results = $cfg['compiler']['build_path'] + test_base + RESULT_EXTENSION - File.open(test_results, 'w') { |f| f.print output } - end - - # Parse and report test results - total_tests = 0 - total_failures = 0 - failure_messages = [] - - test_case_files.each do |test_case| - tests = (YAML.load_file(test_case))[:systest][:tests][:units] - total_tests += tests.size - - test_file = 'test_' + File.basename(test_case).ext(C_EXTENSION) - result_file = test_file.ext(RESULT_EXTENSION) - test_results = File.readlines(SYSTEST_BUILD_FILES_PATH + result_file).reject {|line| line.size < 10 } # we're rejecting lines that are too short to be realistic, which handles line ending problems - tests.each_with_index do |test, index| - # compare test's intended pass/fail state with pass/fail state in actual results; - # if they don't match, the system test has failed - this_failed = case(test[:pass]) - when :ignore - (test_results[index] =~ /:IGNORE/).nil? - when true - (test_results[index] =~ /:PASS/).nil? - when false - (test_results[index] =~ /:FAIL/).nil? - end - if (this_failed) - total_failures += 1 - test_results[index] =~ /test#{index+1}:(.+)/ - failure_messages << "#{test_file}:test#{index+1}:should #{test[:should]}:#{$1}" - end - # some tests have additional requirements to check for (checking the actual output message) - if (test[:verify_error]) and not (test_results[index] =~ /test#{index+1}:.*#{test[:verify_error]}/) - total_failures += 1 - failure_messages << "#{test_file}:test#{index+1}:should #{test[:should]}:should have output matching '#{test[:verify_error]}'" - end - end - end - - report "\n" - report "------------------------------------\n" - report "SYSTEM TEST MOCK INTERACTION SUMMARY\n" - report "------------------------------------\n" - report "#{total_tests} Tests #{total_failures} Failures 0 Ignored\n" - report "\n" - - if (failure_messages.size > 0) - report 'System test failures:' - failure_messages.each do |failure| - report failure - end - end - - report '' - - return total_failures - end - - def profile_this(filename) - profile = true - begin - require 'ruby-prof' - RubyProf.start - rescue - profile = false - end - - yield - - if (profile) - profile_result = RubyProf.stop - File.open("Profile_#{filename}.html", 'w') do |f| - RubyProf::GraphHtmlPrinter.new(profile_result).print(f) - end - end - end - - def run_system_test_compilations(mockables) - load './lib/cmock.rb' - - load_configuration($cfg_file) - $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? - - report "\n" - report "------------------------------------\n" - report "SYSTEM TEST MOCK COMPILATION SUMMARY\n" - report "------------------------------------\n" - mockables.each do |header| - mock_filename = 'mock_' + File.basename(header).ext('.c') - CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header) - report "Compiling #{mock_filename}..." - compile(SYSTEST_GENERATED_FILES_PATH + mock_filename) - end - end - - def run_system_test_profiles(mockables) - load './lib/cmock.rb' - - load_configuration($cfg_file) - $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? - - report "\n" - report "--------------------------\n" - report "SYSTEM TEST MOCK PROFILING\n" - report "--------------------------\n" - mockables.each do |header| - mock_filename = 'mock_' + File.basename(header).ext('.c') - profile_this(mock_filename.gsub('.c','')) do - 10.times do - CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header) - end - end - report "Compiling #{mock_filename}..." - compile(SYSTEST_GENERATED_FILES_PATH + mock_filename) - end - end - - def build_and_test_c_files - report "\n" - report "----------------\n" - report "UNIT TEST C CODE\n" - report "----------------\n" - errors = false - FileList.new("test/c/*.yml").each do |yaml_file| - test = YAML.load(File.read(yaml_file)) - report "\nTesting #{yaml_file.sub('.yml','')}" - report "(#{test[:options].join(', ')})" - test[:files].each { |f| compile(f, test[:options]) } - obj_files = test[:files].map { |f| f.gsub!(/.*\//,'').gsub!(C_EXTENSION, $cfg['compiler']['object_files']['extension']) } - link_it('TestCMockC', obj_files) - if $cfg['simulator'].nil? - execute($cfg['linker']['bin_files']['destination'] + 'TestCMockC' + $cfg['linker']['bin_files']['extension']) - else - execute(tackit($cfg['simulator']['path'].join) + ' ' + - $cfg['simulator']['pre_support'].map{|o| tackit(o)}.join(' ') + ' ' + - $cfg['linker']['bin_files']['destination'] + - 'TestCMockC' + - $cfg['linker']['bin_files']['extension'] + ' ' + - $cfg['simulator']['post_support'].map{|o| tackit(o)}.join(' ') ) - end - end - end - - def fail_out(msg) - puts msg - exit(-1) - end -end - +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require 'yaml' +require 'fileutils' +require './vendor/unity/auto/generate_test_runner' +require './vendor/unity/auto/unity_test_summary' +require './test/system/systest_generator' +require './vendor/unity/auto/colour_reporter.rb' + +module RakefileHelpers + + SYSTEST_GENERATED_FILES_PATH = 'test/system/generated/' + SYSTEST_BUILD_FILES_PATH = 'test/system/build/' + SYSTEST_COMPILE_MOCKABLES_PATH = 'test/system/test_compilation/' + C_EXTENSION = '.c' + RESULT_EXTENSION = '.result' + + def load_configuration(config_file) + $cfg_file = config_file + $cfg = YAML.load(File.read('./targets/' + $cfg_file)) + $colour_output = false unless $cfg['colour'] + end + + def configure_clean + CLEAN.include(SYSTEST_GENERATED_FILES_PATH + '*.*') + CLEAN.include(SYSTEST_BUILD_FILES_PATH + '*.*') + end + + def configure_toolchain(config_file) + load_configuration(config_file) + configure_clean + end + + def get_local_include_dirs + include_dirs = $cfg['compiler']['includes']['items'].dup + include_dirs.delete_if {|dir| dir.is_a?(Array)} + return include_dirs + end + + def extract_headers(filename) + includes = [] + lines = File.readlines(filename) + lines.each do |line| + m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/) + if not m.nil? + includes << m[1] + end + end + return includes + end + + def find_source_file(header, paths) + paths.each do |dir| + src_file = dir + header.ext(C_EXTENSION) + if (File.exists?(src_file)) + return src_file + end + end + return nil + end + + def squash(prefix, items) + result = '' + items.each { |item| result += " #{prefix}#{tackit(item)}" } + return result + end + + def build_compiler_fields + command = tackit($cfg['compiler']['path']) + if $cfg['compiler']['defines']['items'].nil? + defines = '' + else + defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items']) + end + options = squash('', $cfg['compiler']['options']) + includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items']) + includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) + return {:command => command, :defines => defines, :options => options, :includes => includes} + end + + def compile(file, defines=[]) + compiler = build_compiler_fields + cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{defines.inject(''){|all, a| ' -D'+a+all }}#{compiler[:options]}#{compiler[:includes]} #{file} " + + "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" + obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}" + execute(cmd_str + obj_file) + return obj_file + end + + def build_linker_fields + command = tackit($cfg['linker']['path']) + if $cfg['linker']['options'].nil? + options = '' + else + options = squash('', $cfg['linker']['options']) + end + if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?) + includes = '' + else + includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items']) + end + includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) + return {:command => command, :options => options, :includes => includes} + end + + def link_it(exe_name, obj_list) + linker = build_linker_fields + cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " + + (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).uniq.join + + $cfg['linker']['bin_files']['prefix'] + ' ' + + $cfg['linker']['bin_files']['destination'] + + exe_name + $cfg['linker']['bin_files']['extension'] + execute(cmd_str) + end + + def build_simulator_fields + return nil if $cfg['simulator'].nil? + if $cfg['simulator']['path'].nil? + command = '' + else + command = (tackit($cfg['simulator']['path']) + ' ') + end + if $cfg['simulator']['pre_support'].nil? + pre_support = '' + else + pre_support = squash('', $cfg['simulator']['pre_support']) + end + if $cfg['simulator']['post_support'].nil? + post_support = '' + else + post_support = squash('', $cfg['simulator']['post_support']) + end + return {:command => command, :pre_support => pre_support, :post_support => post_support} + end + + def execute(command_string, verbose=true, raise_on_failure=true) + #report command_string + output = `#{command_string}`.chomp + report(output) if (verbose && !output.nil? && (output.length > 0)) + if ($?.exitstatus != 0) and (raise_on_failure) + raise "#{command_string} failed. (Returned #{$?.exitstatus})" + end + return output + end + + def tackit(strings) + case(strings) + when Array + "\"#{strings.join}\"" + when /^-/ + strings + when /\s/ + "\"#{strings}\"" + else + strings + end + end + + def report_summary + summary = UnityTestSummary.new + summary.set_root_path(File.expand_path(File.dirname(__FILE__)) + '/') + results_glob = "#{$cfg['compiler']['build_path']}*.test*" + results_glob.gsub!(/\\/, '/') + results = Dir[results_glob] + summary.set_targets(results) + summary.run + fail_out "FAIL: There were failures" if (summary.failures > 0) + end + + def run_system_test_interactions(test_case_files) + load './lib/cmock.rb' + + SystemTestGenerator.new.generate_files(test_case_files) + test_files = FileList.new(SYSTEST_GENERATED_FILES_PATH + 'test*.c') + + load_configuration($cfg_file) + $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? + + include_dirs = get_local_include_dirs + + # Build and execute each unit test + test_files.each do |test| + + obj_list = [] + + test_base = File.basename(test, C_EXTENSION) + cmock_config = test_base.gsub(/test_/, '') + '_cmock.yml' + + report "Executing system tests in #{File.basename(test)}..." + + # Detect dependencies and build required required modules + extract_headers(test).each do |header| + + # Generate any needed mocks + if header =~ /^mock_(.*)\.h/i + module_name = $1 + cmock = CMock.new(SYSTEST_GENERATED_FILES_PATH + cmock_config) + cmock.setup_mocks("#{$cfg['compiler']['source_path']}#{module_name}.h") + end + # Compile corresponding source file if it exists + src_file = find_source_file(header, include_dirs) + if !src_file.nil? + obj_list << compile(src_file) + end + end + + # Generate and build the test suite runner + runner_name = test_base + '_runner.c' + runner_path = $cfg['compiler']['source_path'] + runner_name + UnityTestRunnerGenerator.new(SYSTEST_GENERATED_FILES_PATH + cmock_config).run(test, runner_path) + obj_list << compile(runner_path) + + # Build the test module + obj_list << compile(test) + + # Link the test executable + link_it(test_base, obj_list) + + # Execute unit test and generate results file + simulator = build_simulator_fields + executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension'] + if simulator.nil? + cmd_str = executable + else + cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}" + end + output = execute(cmd_str, false, false) + test_results = $cfg['compiler']['build_path'] + test_base + RESULT_EXTENSION + File.open(test_results, 'w') { |f| f.print output } + end + + # Parse and report test results + total_tests = 0 + total_failures = 0 + failure_messages = [] + + test_case_files.each do |test_case| + tests = (YAML.load_file(test_case))[:systest][:tests][:units] + total_tests += tests.size + + test_file = 'test_' + File.basename(test_case).ext(C_EXTENSION) + result_file = test_file.ext(RESULT_EXTENSION) + test_results = File.readlines(SYSTEST_BUILD_FILES_PATH + result_file).reject {|line| line.size < 10 } # we're rejecting lines that are too short to be realistic, which handles line ending problems + tests.each_with_index do |test, index| + # compare test's intended pass/fail state with pass/fail state in actual results; + # if they don't match, the system test has failed + this_failed = case(test[:pass]) + when :ignore + (test_results[index] =~ /:IGNORE/).nil? + when true + (test_results[index] =~ /:PASS/).nil? + when false + (test_results[index] =~ /:FAIL/).nil? + end + if (this_failed) + total_failures += 1 + test_results[index] =~ /test#{index+1}:(.+)/ + failure_messages << "#{test_file}:test#{index+1}:should #{test[:should]}:#{$1}" + end + # some tests have additional requirements to check for (checking the actual output message) + if (test[:verify_error]) and not (test_results[index] =~ /test#{index+1}:.*#{test[:verify_error]}/) + total_failures += 1 + failure_messages << "#{test_file}:test#{index+1}:should #{test[:should]}:should have output matching '#{test[:verify_error]}' but was '#{test_results[index]}'" + end + end + end + + report "\n" + report "------------------------------------\n" + report "SYSTEM TEST MOCK INTERACTION SUMMARY\n" + report "------------------------------------\n" + report "#{total_tests} Tests #{total_failures} Failures 0 Ignored\n" + report "\n" + + if (failure_messages.size > 0) + report 'System test failures:' + failure_messages.each do |failure| + report failure + end + end + + report '' + + return total_failures + end + + def profile_this(filename) + profile = true + begin + require 'ruby-prof' + RubyProf.start + rescue + profile = false + end + + yield + + if (profile) + profile_result = RubyProf.stop + File.open("Profile_#{filename}.html", 'w') do |f| + RubyProf::GraphHtmlPrinter.new(profile_result).print(f) + end + end + end + + def run_system_test_compilations(mockables) + load './lib/cmock.rb' + + load_configuration($cfg_file) + $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? + + report "\n" + report "------------------------------------\n" + report "SYSTEM TEST MOCK COMPILATION SUMMARY\n" + report "------------------------------------\n" + mockables.each do |header| + mock_filename = 'mock_' + File.basename(header).ext('.c') + CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header) + report "Compiling #{mock_filename}..." + compile(SYSTEST_GENERATED_FILES_PATH + mock_filename) + end + end + + def run_system_test_profiles(mockables) + load './lib/cmock.rb' + + load_configuration($cfg_file) + $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? + + report "\n" + report "--------------------------\n" + report "SYSTEM TEST MOCK PROFILING\n" + report "--------------------------\n" + mockables.each do |header| + mock_filename = 'mock_' + File.basename(header).ext('.c') + profile_this(mock_filename.gsub('.c','')) do + 10.times do + CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header) + end + end + report "Compiling #{mock_filename}..." + compile(SYSTEST_GENERATED_FILES_PATH + mock_filename) + end + end + + def build_and_test_c_files + report "\n" + report "----------------\n" + report "UNIT TEST C CODE\n" + report "----------------\n" + errors = false + FileList.new("test/c/*.yml").each do |yaml_file| + test = YAML.load(File.read(yaml_file)) + report "\nTesting #{yaml_file.sub('.yml','')}" + report "(#{test[:options].join(', ')})" + test[:files].each { |f| compile(f, test[:options]) } + obj_files = test[:files].map { |f| f.gsub!(/.*\//,'').gsub!(C_EXTENSION, $cfg['compiler']['object_files']['extension']) } + link_it('TestCMockC', obj_files) + if $cfg['simulator'].nil? + execute($cfg['linker']['bin_files']['destination'] + 'TestCMockC' + $cfg['linker']['bin_files']['extension']) + else + execute(tackit($cfg['simulator']['path'].join) + ' ' + + $cfg['simulator']['pre_support'].map{|o| tackit(o)}.join(' ') + ' ' + + $cfg['linker']['bin_files']['destination'] + + 'TestCMockC' + + $cfg['linker']['bin_files']['extension'] + ' ' + + $cfg['simulator']['post_support'].map{|o| tackit(o)}.join(' ') ) + end + end + end + + def fail_out(msg) + puts msg + exit(-1) + end +end + diff --git a/test/system/test_interactions/doesnt_leave_details_behind.yml b/test/system/test_interactions/doesnt_leave_details_behind.yml index 5f12292..ce15a63 100644 --- a/test/system/test_interactions/doesnt_leave_details_behind.yml +++ b/test/system/test_interactions/doesnt_leave_details_behind.yml @@ -1,272 +1,308 @@ ---- -:cmock: - :enforce_strict_ordering: 1 - :plugins: - - :array - - :cexception - - :ignore - - :callback - - :return_thru_ptr - - :ignore_arg - - :expect_any_args - :callback_after_arg_check: false - :callback_include_count: false - :treat_externs: :include - -:systest: - :types: | - typedef struct _POINT_T { - int x; - int y; - } POINT_T; - - :mockable: | - #include "CException.h" - extern void foo(POINT_T* a); - POINT_T* bar(void); - void no_args(void); - - :source: - :header: | - #include "CException.h" - void function_a(void); - void function_b(void); - void function_c(void); - int function_d(void); - void function_e(void); - - :code: | - void function_a(void) - { - foo(bar()); - no_args(); - } - - :tests: - :common: | - #include "CException.h" - void setUp(void) {} - void tearDown(void) {} - void my_foo_callback(POINT_T* a) { TEST_ASSERT_EQUAL_INT(2, a->x); } - - :units: - - :pass: TRUE - :should: 'just pass if we do not insert anything ugly into it' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after an expect and return' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - foo_Expect(NULL); - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after an expect' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after throw expectation' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - CEXCEPTION_T e; - - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_ExpectAndThrow(5); - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - - Try { function_a(); } Catch(e) {} - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after a mock call' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_Expect(); - - function_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after throw' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - CEXCEPTION_T e; - - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_ExpectAndThrow(5); - - Try { function_a(); } Catch(e) {} - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after ignore' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_Ignore(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after ignored mock' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - bar_ExpectAndReturn(NULL); - foo_Expect(NULL); - no_args_Ignore(); - - function_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after callback setup' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_StubWithCallback(my_foo_callback); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after mock with callback' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_StubWithCallback(my_foo_callback); - no_args_Expect(); - - function_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after expect any args' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_ExpectAnyArgs(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after mock which expected any args' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_ExpectAnyArgs(); - no_args_Expect(); - - function_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after ignored arg' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_Expect(NULL); - foo_IgnoreArg_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - - no_args_Expect(); - - function_a(); - } - - - :pass: FALSE - :should: 'not contain mock details in failed assertion after mock which ignored an arg' - :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' - :code: | - test() - { - POINT_T pt = { 2, 2 }; - bar_ExpectAndReturn(&pt); - foo_Expect(NULL); - foo_IgnoreArg_a(); - no_args_Expect(); - - function_a(); - - TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); - } - -... +--- +:cmock: + :enforce_strict_ordering: 1 + :plugins: + - :array + - :cexception + - :ignore + - :callback + - :return_thru_ptr + - :ignore_arg + - :expect_any_args + :callback_after_arg_check: false + :callback_include_count: false + :treat_externs: :include + +:systest: + :types: | + typedef struct _POINT_T { + int x; + int y; + } POINT_T; + + :mockable: | + #include "CException.h" + extern void foo(POINT_T* a); + POINT_T* bar(void); + void no_args(void); + + :source: + :header: | + #include "CException.h" + void function_a(void); + int function_b(void); + + :code: | + void function_a(void) + { + foo(bar()); + no_args(); + } + + int function_b(void) + { + POINT_T pt = { 1, 2 }; + foo(&pt); + return (pt.x + pt.y); + } + + :tests: + :common: | + #include "CException.h" + void setUp(void) {} + void tearDown(void) {} + void my_foo_callback(POINT_T* a) { TEST_ASSERT_EQUAL_INT(2, a->x); } + + :units: + - :pass: TRUE + :should: 'just pass if we do not insert anything ugly into it' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after an expect and return' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after an expect' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after throw expectation' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + CEXCEPTION_T e; + + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_ExpectAndThrow(5); + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + Try { function_a(); } Catch(e) {} + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after a mock call' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after throw' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + CEXCEPTION_T e; + + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_ExpectAndThrow(5); + + Try { function_a(); } Catch(e) {} + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignore' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Ignore(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignored mock' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + bar_ExpectAndReturn(NULL); + foo_Expect(NULL); + no_args_Ignore(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after callback setup' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_StubWithCallback(my_foo_callback); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock with callback' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_StubWithCallback(my_foo_callback); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after expect any args' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_ExpectAnyArgs(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which expected any args' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_ExpectAnyArgs(); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after ignored arg' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_Expect(NULL); + foo_IgnoreArg_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + + no_args_Expect(); + + function_a(); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which ignored an arg' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + POINT_T pt = { 2, 2 }; + bar_ExpectAndReturn(&pt); + foo_Expect(NULL); + foo_IgnoreArg_a(); + no_args_Expect(); + + function_a(); + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which threw a CException' + :verify_error: 'FAIL: Expected 1 Was 2. CustomFail' + :code: | + test() + { + CEXCEPTION_T e; + bar_ExpectAndThrow(0x12); + + Try { + function_a(); + } + Catch(e) {} + + TEST_ASSERT_EQUAL_INT_MESSAGE(1,2,"CustomFail"); + } + + - :pass: FALSE + :should: 'not contain mock details in failed assertion after mock which used a return thru ptr' + #:verify_error: 'FAIL: Expected 3 Was 7. CustomFail' #TODO: put back in once ReturnThruPtr fixed + :code: | + test() + { + POINT_T pt1 = { 1, 2 }; + POINT_T pt2 = { 3, 4 }; + + foo_Expect(&pt1); + foo_ReturnThruPtr_a(&pt2); + + TEST_ASSERT_EQUAL_INT(3, function_b()); + } + +...