Skip to content

Commit

Permalink
- push the internal function and argument names into a single each pe…
Browse files Browse the repository at this point in the history
…r mock to further reduce const overhead.
  • Loading branch information
mvandervoord committed Dec 12, 2015
1 parent f78ab04 commit cda8d3a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 24 deletions.
15 changes: 12 additions & 3 deletions lib/cmock_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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 <string.h>\n"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/cmock_generator_plugin_expect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/cmock_generator_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(/\*$/,'')
Expand Down Expand Up @@ -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(/\*$/,'')
Expand Down Expand Up @@ -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(/\*$/,'')
Expand Down
17 changes: 14 additions & 3 deletions test/unit/cmock_generator_main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,28 @@ 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 <string.h>\n",
"#include <stdlib.h>\n",
"#include <setjmp.h>\n",
"#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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/cmock_generator_plugin_expect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions test/unit/cmock_generator_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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" +
Expand All @@ -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
Expand All @@ -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" +
Expand All @@ -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
Expand All @@ -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" +
Expand All @@ -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
Expand Down

0 comments on commit cda8d3a

Please sign in to comment.