- foundation work towards more awesome pointer/array handling. not there yet, so don't try this at home kids.

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@145 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2009-11-26 00:55:02 +00:00
parent f63d8d7813
commit 0f857f24ec
13 changed files with 398 additions and 68 deletions
+74 -13
View File
@@ -7,6 +7,7 @@ class CMockGeneratorUtils
@config = config
@ptr_handling = @config.when_ptr_star
@ordered = @config.enforce_strict_ordering
@arrays = @config.plugins.include? :array
@helpers = helpers
end
@@ -26,10 +27,16 @@ class CMockGeneratorUtils
INSERT_EXPECT_CODE_SNIPPET % [type, array, newValue]
end
def code_add_an_arg_expectation(function, arg_type, expected)
var = "Mock.#{function[:name]}_Expected_#{expected}"
lines = code_insert_item_into_expect_array(arg_type, var, expected)
def code_add_an_arg_expectation(function, arg, depth=1)
var = "Mock.#{function[:name]}_Expected_#{arg[:name]}"
lines = code_insert_item_into_expect_array(arg[:type], var, arg[:name])
lines << INSERT_EXPECT_SETUP_SNIPPET % [var, function[:name]]
if (@arrays and arg[:ptr?])
var += '_Depth'
lines << INSERT_EXPECT_SHORT_CODE_SNIPPET % ['int', var, depth]
lines << INSERT_EXPECT_SETUP_SNIPPET % [var, function[:name]]
end
lines
end
def code_add_base_expectation(func_name)
@@ -43,14 +50,16 @@ class CMockGeneratorUtils
lines
end
def code_verify_an_arg_expectation(function, arg_type, arg)
(INSERT_ARG_VERIFY_START_SNIPPET % ["#{function[:name]}_Expected_#{arg}", arg_type]) +
expect_helper(arg_type, '*p_expected', arg, "\"Function '#{function[:name]}' called with unexpected value for argument '#{arg}'.\"") +
" }\n"
def code_verify_an_arg_expectation(function, arg)
(INSERT_ARG_VERIFY_START_SNIPPET % ["#{function[:name]}_Expected_#{arg[:name]}", arg[:type]]) +
expect_helper(arg, '*p_expected', "\"Function '#{function[:name]}' called with unexpected value for argument '#{arg[:name]}'.\"", "#{function[:name]}_Expected_#{arg[:name]}_Depth") +
"\n }\n"
end
def expect_helper(c_type, expected, arg, msg)
if ((c_type.strip[-1] == 42) and (@ptr_handling == :compare_ptr))
def expect_helper(arg, expected, msg, depth_name='1')
c_type = arg[:type]
name = arg[:name]
if ((arg[:ptr?]) and (@ptr_handling == :compare_ptr))
unity_func = "TEST_ASSERT_EQUAL_INT_MESSAGE"
else
unity_func = (@helpers.nil? or @helpers[:unity_helper].nil?) ? "TEST_ASSERT_EQUAL_MESSAGE" : @helpers[:unity_helper].get_helper(c_type)
@@ -59,13 +68,36 @@ class CMockGeneratorUtils
case(unity_func)
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE"
full_expected = (expected =~ /^\*/) ? expected.slice(1..-1) : "&(#{expected})"
return " TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n"
return " TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)#{full_expected}, (void*)&(#{name}), sizeof(#{c_type})#{unity_msg});\n"
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE_ARRAY"
return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(#{expected}), (void*)#{arg}, sizeof(#{c_type.sub('*','')})#{unity_msg}); }\n"
if (@arrays)
[ (INSERT_ARG_DEPTH_START_SNIPPET % [depth_name]),
" if (*p_expected == NULL)",
" { TEST_ASSERT_NULL(#{name}); }",
" else",
" { TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE((void*)(#{expected}), (void*)#{name}, sizeof(#{c_type.sub('*','')}), Depth#{unity_msg}); }"].join("\n")
else
[ " if (*p_expected == NULL)",
" { TEST_ASSERT_NULL(#{name}); }",
" else",
" { TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)(#{expected}), (void*)#{name}, sizeof(#{c_type.sub('*','')})#{unity_msg}); }"].join("\n")
end
when /_ARRAY/
return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { #{unity_func}(#{expected}, #{arg}, 1#{unity_msg}); }\n"
if (@arrays)
[ (INSERT_ARG_DEPTH_START_SNIPPET % ["#{function[:name]}_Expected_#{name}_Depth"]),
" if (*p_expected == NULL)",
" { TEST_ASSERT_NULL(#{name}); }",
" else",
" { #{unity_func}(#{expected}, #{name}, Depth); }"].join("\n")
else
[ " if (*p_expected == NULL)",
" { TEST_ASSERT_NULL(#{name}); }",
" else",
" { #{unity_func}(#{expected}, #{name}, 1); }"].join("\n")
end
else
return " #{unity_func}(#{expected}, #{arg}#{unity_msg});\n"
return " #{unity_func}(#{expected}, #{name}#{unity_msg});\n"
end
end
@@ -99,6 +131,30 @@ class CMockGeneratorUtils
}
]
INSERT_EXPECT_SHORT_CODE_SNIPPET = %q[
{
int sz = 0;
%1$s *pointer = %2$s_Head;
while (pointer && pointer != %2$s_Tail) { sz++; pointer++; }
if (sz == 0)
{
%2$s_Head = (%1$s*)malloc(2*sizeof(%1$s));
if (!%2$s_Head)
Mock.allocFailure++;
}
else
{
%1$s *ptmp = (%1$s*)realloc(%2$s_Head, sizeof(%1$s) * (sz+1));
if (!ptmp)
Mock.allocFailure++;
else
%2$s_Head = ptmp;
}
%2$s_Head[sz] = %3$s;
%2$s_Tail = &%2$s_Head[sz+1];
}
]
INSERT_EXPECT_SETUP_SNIPPET =
" %1$s = %1$s_Head;\n %1$s += Mock.%2$s_CallCount;\n"
@@ -122,4 +178,9 @@ class CMockGeneratorUtils
Mock.%1$s++;
]
INSERT_ARG_DEPTH_START_SNIPPET = %q[
int Depth = *Mock.%1$s;
Mock.%1$s++;
]
end