diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index c74c58d2..ad7220e6 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -68,7 +68,7 @@ def create_mock_header_file(parsed_stuff) 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 @@ def create_mock_header_footer(header) 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 @@ def create_source_header_section(file, filename) 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 @@ def create_mock_implementation(file, function) 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 ba59e784..bfa506f3 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -81,7 +81,7 @@ def mock_interfaces(function) 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 584cb812..8ad43b69 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -121,7 +121,7 @@ def code_verify_an_arg_expectation_with_no_arrays(function, arg) 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 @@ def code_verify_an_arg_expectation_with_normal_arrays(function, arg) 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 @@ def code_verify_an_arg_expectation_with_smart_arrays(function, arg) 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 8b2041d2..e2d99885 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -267,6 +267,10 @@ def mock_implementation(name, args) 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 @@ def mock_implementation(name, args) "#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 @@ def mock_implementation(name, args) 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 @@ def mock_implementation(name, args) 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 e1a6529c..38ffd1df 100644 --- a/test/unit/cmock_generator_plugin_expect_test.rb +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -201,7 +201,7 @@ 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 f62083bf..4bf253ba 100644 --- a/test/unit/cmock_generator_utils_test.rb +++ b/test/unit/cmock_generator_utils_test.rb @@ -205,7 +205,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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