- lots of optimization (except in parsing... we've left that for another day!)

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@139 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2009-07-10 20:46:27 +00:00
parent c9fdbe29fd
commit 6bb989a3c0
15 changed files with 499 additions and 418 deletions
-3
View File
@@ -8,11 +8,8 @@ class CMockConfig
:plugins => ['cexception', 'ignore'],
:includes => [],
:attributes => ['__ramfunc', '__irq', '__fiq'],
:expect_call_count_type => 'unsigned short',
:enforce_strict_ordering => false,
:ignore_bool_type => 'unsigned char',
:cexception_include => nil,
:cexception_throw_type => 'int',
:unity_helper => false,
:treat_as => {},
:memcmp_if_unknown => true,
+1 -1
View File
@@ -164,7 +164,7 @@ class CMockGenerator
# Return expected value, if necessary
if (function[:return_type] != "void")
file << @utils.code_handle_return_value(function, " ").join
file << @utils.code_handle_return_value(function)
end
# Close out the function
+65 -56
View File
@@ -7,87 +7,96 @@ class CMockGeneratorPluginCException
@config = config
@utils = utils
["cexception_include", "cexception_throw_type"].each do |req|
raise "'#{req}' needs to be defined in config" unless @config.respond_to?(req)
end
raise "'cexception_include' needs to be defined in config" unless @config.respond_to?(:cexception_include)
end
def include_files
include = @config.cexception_include
include = "Exception.h" if (include.nil?)
return ["#include \"#{include}\"\n"]
return "#include \"#{include}\"\n"
end
def instance_structure(function)
call_count_type = @config.expect_call_count_type
throw_type = @config.cexception_throw_type
[ " #{call_count_type} *#{function[:name]}_ThrowOnCallCount;\n",
" #{call_count_type} *#{function[:name]}_ThrowOnCallCount_Head;\n",
" #{call_count_type} *#{function[:name]}_ThrowOnCallCount_Tail;\n",
" #{throw_type} *#{function[:name]}_ThrowValue;\n",
" #{throw_type} *#{function[:name]}_ThrowValue_Head;\n",
" #{throw_type} *#{function[:name]}_ThrowValue_Tail;\n" ]
INSTANCE_STRUCTURE_SNIPPET % function[:name]
end
def mock_function_declarations(function)
if (function[:args_string] == "void")
return ["void #{function[:name]}_ExpectAndThrow(#{@config.cexception_throw_type} toThrow);\n"]
return "void #{function[:name]}_ExpectAndThrow(EXCEPTION_T toThrow);\n"
else
return ["void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, #{@config.cexception_throw_type} toThrow);\n"]
return "void #{function[:name]}_ExpectAndThrow(#{function[:args_string]}, EXCEPTION_T toThrow);\n"
end
end
def mock_implementation(function)
[ "\n",
" if((Mock.#{function[:name]}_ThrowOnCallCount != Mock.#{function[:name]}_ThrowOnCallCount_Tail) &&\n",
" (Mock.#{function[:name]}_ThrowValue != Mock.#{function[:name]}_ThrowValue_Tail))\n",
" {\n",
" if (*Mock.#{function[:name]}_ThrowOnCallCount && \n",
" (Mock.#{function[:name]}_CallCount == *Mock.#{function[:name]}_ThrowOnCallCount))\n",
" {\n",
" #{@config.cexception_throw_type} toThrow = *Mock.#{function[:name]}_ThrowValue;\n",
" Mock.#{function[:name]}_ThrowOnCallCount++;\n",
" Mock.#{function[:name]}_ThrowValue++;\n",
" Throw(toThrow);\n",
" }\n",
" }\n" ]
MOCK_IMPLEMENTATION_SNIPPET % function[:name]
end
def mock_interfaces(function)
arg_insert = (function[:args_string] == "void") ? "" : "#{function[:args_string]}, "
call_count_type = @config.expect_call_count_type
throw_type = @config.cexception_throw_type
[ "void #{function[:name]}_ExpectAndThrow(#{arg_insert}#{throw_type} toThrow)\n",
"{\n",
[ "void #{function[:name]}_ExpectAndThrow(#{arg_insert}EXCEPTION_T toThrow)\n{\n",
@utils.code_add_base_expectation(function[:name]),
@utils.code_insert_item_into_expect_array(call_count_type, "Mock.#{function[:name]}_ThrowOnCallCount_Head", "Mock.#{function[:name]}_CallsExpected"),
@utils.code_insert_item_into_expect_array(throw_type, "Mock.#{function[:name]}_ThrowValue_Head", "toThrow"),
" Mock.#{function[:name]}_ThrowValue = Mock.#{function[:name]}_ThrowValue_Head;\n",
" Mock.#{function[:name]}_ThrowOnCallCount = Mock.#{function[:name]}_ThrowOnCallCount_Head;\n",
" while ((*Mock.#{function[:name]}_ThrowOnCallCount <= Mock.#{function[:name]}_CallCount) && (Mock.#{function[:name]}_ThrowOnCallCount < Mock.#{function[:name]}_ThrowOnCallCount_Tail))\n",
" {\n",
" Mock.#{function[:name]}_ThrowValue++;\n",
" Mock.#{function[:name]}_ThrowOnCallCount++;\n",
" }\n",
@utils.code_insert_item_into_expect_array('int', "Mock.#{function[:name]}_ThrowOnCallCount", "Mock.#{function[:name]}_CallsExpected"),
@utils.code_insert_item_into_expect_array('EXCEPTION_T', "Mock.#{function[:name]}_ThrowValue", "toThrow"),
(MOCK_INTERFACE_THROW_HANDLING_SNIPPET % function[:name]),
(function[:args_string] != "void") ? " ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n" : nil,
"}\n\n" ].compact
"}\n\n" ].join
end
def mock_destroy(function)
[ " if(Mock.#{function[:name]}_ThrowOnCallCount_Head)\n",
" {\n",
" free(Mock.#{function[:name]}_ThrowOnCallCount_Head);\n",
" }\n",
" Mock.#{function[:name]}_ThrowOnCallCount=NULL;\n",
" Mock.#{function[:name]}_ThrowOnCallCount_Head=NULL;\n",
" Mock.#{function[:name]}_ThrowOnCallCount_Tail=NULL;\n",
" if(Mock.#{function[:name]}_ThrowValue_Head)\n",
" {\n",
" free(Mock.#{function[:name]}_ThrowValue_Head);\n",
" }\n",
" Mock.#{function[:name]}_ThrowValue=NULL;\n",
" Mock.#{function[:name]}_ThrowValue_Head=NULL;\n",
" Mock.#{function[:name]}_ThrowValue_Tail=NULL;\n"
]
MOCK_DESTROY_SNIPPET % function[:name]
end
private ############
INSTANCE_STRUCTURE_SNIPPET = %q[
int *%1$s_ThrowOnCallCount;
int *%1$s_ThrowOnCallCount_Head;
int *%1$s_ThrowOnCallCount_Tail;
EXCEPTION_T *%1$s_ThrowValue;
EXCEPTION_T *%1$s_ThrowValue_Head;
EXCEPTION_T *%1$s_ThrowValue_Tail;
]
MOCK_IMPLEMENTATION_SNIPPET = %q[
if ((Mock.%1$s_ThrowOnCallCount != Mock.%1$s_ThrowOnCallCount_Tail) &&
(Mock.%1$s_ThrowValue != Mock.%1$s_ThrowValue_Tail))
{
if (*Mock.%1$s_ThrowOnCallCount &&
(Mock.%1$s_CallCount == *Mock.%1$s_ThrowOnCallCount))
{
EXCEPTION_T toThrow = *Mock.%1$s_ThrowValue;
Mock.%1$s_ThrowOnCallCount++;
Mock.%1$s_ThrowValue++;
Throw(toThrow);
}
}
]
MOCK_DESTROY_SNIPPET = %q[
if(Mock.%1$s_ThrowOnCallCount_Head)
{
free(Mock.%1$s_ThrowOnCallCount_Head);
}
Mock.%1$s_ThrowOnCallCount=NULL;
Mock.%1$s_ThrowOnCallCount_Head=NULL;
Mock.%1$s_ThrowOnCallCount_Tail=NULL;
if(Mock.%1$s_ThrowValue_Head)
{
free(Mock.%1$s_ThrowValue_Head);
}
Mock.%1$s_ThrowValue=NULL;
Mock.%1$s_ThrowValue_Head=NULL;
Mock.%1$s_ThrowValue_Tail=NULL;
]
MOCK_INTERFACE_THROW_HANDLING_SNIPPET = %q[
Mock.%1$s_ThrowValue = Mock.%1$s_ThrowValue_Head;
Mock.%1$s_ThrowOnCallCount = Mock.%1$s_ThrowOnCallCount_Head;
while ((*Mock.%1$s_ThrowOnCallCount <= Mock.%1$s_CallCount) && (Mock.%1$s_ThrowOnCallCount < Mock.%1$s_ThrowOnCallCount_Tail))
{
Mock.%1$s_ThrowValue++;
Mock.%1$s_ThrowOnCallCount++;
}
]
end
+100 -76
View File
@@ -12,83 +12,57 @@ class CMockGeneratorPluginExpect
end
def instance_structure(function)
call_count_type = @config.expect_call_count_type
lines = [ " #{call_count_type} #{function[:name]}_CallCount;\n",
" #{call_count_type} #{function[:name]}_CallsExpected;\n" ]
lines = INSTANCE_STRUCTURE_CALL_SNIPPET % function[:name]
if (function[:return_type] != "void")
lines << [ " #{function[:return_type]} *#{function[:name]}_Return;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Head;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Tail;\n" ]
lines << INSTANCE_STRUCTURE_ITEM_SNIPPET % "#{function[:return_type]} *#{function[:name]}_Return"
end
if (@ordered)
lines << [ " int *#{function[:name]}_CallOrder;\n",
" int *#{function[:name]}_CallOrder_Head;\n",
" int *#{function[:name]}_CallOrder_Tail;\n" ]
lines << INSTANCE_STRUCTURE_ITEM_SNIPPET % "int *#{function[:name]}_CallOrder"
end
function[:args].each do |arg|
lines << [ " #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]};\n",
" #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Head;\n",
" #{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}_Tail;\n" ]
lines << INSTANCE_STRUCTURE_ITEM_SNIPPET % "#{arg[:type]} *#{function[:name]}_Expected_#{arg[:name]}"
end
lines.flatten
lines
end
def mock_function_declarations(function)
if (function[:args_string] == "void")
if (function[:return_type] == 'void')
return ["void #{function[:name]}_Expect(void);\n"]
return "void #{function[:name]}_Expect(void);\n"
else
return ["void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"]
return "void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"
end
else
if (function[:return_type] == 'void')
return ["void #{function[:name]}_Expect(#{function[:args_string]});\n"]
return "void #{function[:name]}_Expect(#{function[:args_string]});\n"
else
return ["void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"]
return "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"
end
end
end
def mock_implementation(function)
lines = [ " Mock.#{function[:name]}_CallCount++;\n",
" if (Mock.#{function[:name]}_CallCount > Mock.#{function[:name]}_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Function '#{function[:name]}' called more times than expected\");\n",
" }\n" ]
lines = MOCK_IMPLEMENT_SNIPPET % function[:name]
if (@ordered)
err_msg = "Out of order function calls. Function '#{function[:name]}'" #" expected to be call %i but was call %i"
lines << [ " {\n",
" int* p_expected = Mock.#{function[:name]}_CallOrder;\n",
" ++GlobalVerifyOrder;\n",
" if (Mock.#{function[:name]}_CallOrder != Mock.#{function[:name]}_CallOrder_Tail)\n",
" Mock.#{function[:name]}_CallOrder++;\n",
" if ((*p_expected != GlobalVerifyOrder) && (GlobalOrderError == NULL))\n",
" {\n",
" const char* ErrStr = \"#{err_msg}\";\n",
" GlobalOrderError = malloc(#{err_msg.size + 1});\n",
" if (GlobalOrderError)\n",
" strcpy(GlobalOrderError, ErrStr);\n",
" }\n",
" }\n" ]
err_msg = "Out of order function calls. Function '#{function[:name]}'" #would eventually like to be " expected to be call %i but was call %i"
lines << MOCK_IMPLEMENT_ORDERED_SNIPPET % [function[:name], err_msg, (err_msg.size + 1).to_s]
end
function[:args].each do |arg|
lines << @utils.code_verify_an_arg_expectation(function, arg[:type], arg[:name])
end
lines.flatten
lines
end
def mock_interfaces(function)
lines = []
func_name = function[:name]
# Parameter Helper Function
if (function[:args_string] != "void")
lines << "void ExpectParameters_#{function[:name]}(#{function[:args_string]})\n"
lines << "{\n"
lines << "void ExpectParameters_#{func_name}(#{function[:args_string]})\n{\n"
function[:args].each do |arg|
lines << @utils.code_add_an_arg_expectation(function, arg[:type], arg[:name])
end
@@ -97,65 +71,115 @@ class CMockGeneratorPluginExpect
#Main Mock Interface
if (function[:return_type] == "void")
lines << "void #{function[:name]}_Expect(#{function[:args_string]})\n"
lines << "void #{func_name}_Expect(#{function[:args_string]})\n"
else
if (function[:args_string] == "void")
lines << "void #{function[:name]}_ExpectAndReturn(#{function[:return_string]})\n"
lines << "void #{func_name}_ExpectAndReturn(#{function[:return_string]})\n"
else
lines << "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]})\n"
lines << "void #{func_name}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]})\n"
end
end
lines << "{\n"
lines << @utils.code_add_base_expectation(function[:name])
lines << @utils.code_add_base_expectation(func_name)
if (function[:args_string] != "void")
lines << " ExpectParameters_#{function[:name]}(#{@utils.create_call_list(function)});\n"
lines << " ExpectParameters_#{func_name}(#{@utils.create_call_list(function)});\n"
end
if (function[:return_type] != "void")
lines << @utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{function[:name]}_Return_Head", 'toReturn')
lines << " Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n"
lines << " Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n"
lines << @utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{func_name}_Return", 'toReturn')
lines << " Mock.#{func_name}_Return = Mock.#{func_name}_Return_Head;\n"
lines << " Mock.#{func_name}_Return += Mock.#{func_name}_CallCount;\n"
end
lines << "}\n\n"
end
def mock_verify(function)
[" TEST_ASSERT_EQUAL_MESSAGE(Mock.#{function[:name]}_CallsExpected, Mock.#{function[:name]}_CallCount, \"Function '#{function[:name]}' called unexpected number of times.\");\n"]
func_name = function[:name]
" TEST_ASSERT_EQUAL_MESSAGE(Mock.#{func_name}_CallsExpected, Mock.#{func_name}_CallCount, \"Function '#{func_name}' called unexpected number of times.\");\n"
end
def mock_destroy(function)
lines = []
func_name = function[:name]
if (function[:return_type] != "void")
lines << [ " if (Mock.#{function[:name]}_Return_Head)\n",
" {\n",
" free(Mock.#{function[:name]}_Return_Head);\n",
" }\n",
" Mock.#{function[:name]}_Return=NULL;\n",
" Mock.#{function[:name]}_Return_Head=NULL;\n",
" Mock.#{function[:name]}_Return_Tail=NULL;\n"
]
lines << DESTROY_RETURN_SNIPPET % func_name
end
if (@ordered)
lines << [ " if (Mock.#{function[:name]}_CallOrder_Head)\n",
" {\n",
" free(Mock.#{function[:name]}_CallOrder_Head);\n",
" }\n",
" Mock.#{function[:name]}_CallOrder=NULL;\n",
" Mock.#{function[:name]}_CallOrder_Head=NULL;\n",
" Mock.#{function[:name]}_CallOrder_Tail=NULL;\n"
]
lines << DESTROY_CALL_ORDER_SNIPPET % func_name
end
function[:args].each do |arg|
lines << [ " if (Mock.#{function[:name]}_Expected_#{arg[:name]}_Head)\n",
" {\n",
" free(Mock.#{function[:name]}_Expected_#{arg[:name]}_Head);\n",
" }\n",
" Mock.#{function[:name]}_Expected_#{arg[:name]}=NULL;\n",
" Mock.#{function[:name]}_Expected_#{arg[:name]}_Head=NULL;\n",
" Mock.#{function[:name]}_Expected_#{arg[:name]}_Tail=NULL;\n"
]
lines << DESTROY_BASE_SNIPPET % "#{func_name}_Expected_#{arg[:name]}"
end
lines.flatten
end
private #####################
INSTANCE_STRUCTURE_CALL_SNIPPET = %q[
int %1$s_CallCount;
int %1$s_CallsExpected;
]
INSTANCE_STRUCTURE_ITEM_SNIPPET = %q[
%1$s;
%1$s_Head;
%1$s_Tail;
]
MOCK_IMPLEMENT_SNIPPET = %q[
Mock.%1$s_CallCount++;
if (Mock.%1$s_CallCount > Mock.%1$s_CallsExpected)
{
TEST_FAIL("Function '%1$s' called more times than expected");
}
]
MOCK_IMPLEMENT_ORDERED_SNIPPET = %q[ {
int* p_expected = Mock.%1$s_CallOrder;
++GlobalVerifyOrder;
if (Mock.%1$s_CallOrder != Mock.%1$s_CallOrder_Tail)
Mock.%1$s_CallOrder++;
if ((*p_expected != GlobalVerifyOrder) && (GlobalOrderError == NULL))
{
const char* ErrStr = "%2$s";
GlobalOrderError = malloc(%3$s);
if (GlobalOrderError)
strcpy(GlobalOrderError, ErrStr);
}
}
]
DESTROY_RETURN_SNIPPET = %q[
if (Mock.%1$s_Return_Head)
{
free(Mock.%1$s_Return_Head);
}
Mock.%1$s_Return=NULL;
Mock.%1$s_Return_Head=NULL;
Mock.%1$s_Return_Tail=NULL;
]
DESTROY_CALL_ORDER_SNIPPET = %q[
if (Mock.%1$s_CallOrder_Head)
{
free(Mock.%1$s_CallOrder_Head);
}
Mock.%1$s_CallOrder=NULL;
Mock.%1$s_CallOrder_Head=NULL;
Mock.%1$s_CallOrder_Tail=NULL;
]
DESTROY_BASE_SNIPPET = %q[
if (Mock.%1$s_Head)
{
free(Mock.%1$s_Head);
}
Mock.%1$s=NULL;
Mock.%1$s_Head=NULL;
Mock.%1$s_Tail=NULL;
]
end
+47 -37
View File
@@ -6,63 +6,73 @@ class CMockGeneratorPluginIgnore
def initialize(config, utils)
@config = config
@utils = utils
["ignore_bool_type"].each do |req|
raise "'#{req}' needs to be defined in config" unless @config.respond_to?(req)
end
end
def instance_structure(function)
return [" #{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"]
return " int #{function[:name]}_IgnoreBool;\n"
end
def mock_function_declarations(function)
if (function[:return_type] == "void")
return ["void #{function[:name]}_Ignore(void);\n"]
return "void #{function[:name]}_Ignore(void);\n"
else
return ["void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]});\n"]
return "void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]});\n"
end
end
def mock_implementation_prefix(function)
lines = [ " if (Mock.#{function[:name]}_IgnoreBool)\n",
" {\n"
]
lines = " if (Mock.#{function[:name]}_IgnoreBool)\n {"
if (function[:return_type] == "void")
lines << [" return;\n"]
lines << "\n return;\n"
else
lines << [ " if (Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_Tail)\n",
" {\n",
" #{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n",
" Mock.#{function[:name]}_Return++;\n",
" Mock.#{function[:name]}_CallCount++;\n",
" Mock.#{function[:name]}_CallsExpected++;\n",
" return toReturn;\n",
" }\n",
" else\n",
" {\n",
" return *(Mock.#{function[:name]}_Return_Tail - 1);\n",
" }\n"
]
lines << MOCK_IMPLEMENT_PREFIX_SNIPPET % [function[:name], function[:return_type]]
end
lines << [ " }\n" ]
lines.flatten
lines << " }\n"
end
def mock_interfaces(function)
if (function[:return_type] == "void")
[ "void #{function[:name]}_Ignore(void)\n",
"{\n",
" Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n",
"}\n\n" ]
MOCK_INTERFACE_VOID_SNIPPET % function[:name]
else
[ "void #{function[:name]}_IgnoreAndReturn(#{function[:return_string]})\n",
"{\n",
" Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n",
@utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{function[:name]}_Return_Head", 'toReturn'),
" Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n",
" Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n",
"}\n\n" ]
item_insert = @utils.code_insert_item_into_expect_array(function[:return_type], "Mock.#{function[:name]}_Return", 'toReturn')
MOCK_INTERFACE_FULL_SNIPPET % [function[:name], function[:return_string], item_insert]
end
end
private ##############
MOCK_IMPLEMENT_PREFIX_SNIPPET = %q[
if (Mock.%1$s_Return != Mock.%1$s_Return_Tail)
{
%2$s toReturn = *Mock.%1$s_Return;
Mock.%1$s_Return++;
Mock.%1$s_CallCount++;
Mock.%1$s_CallsExpected++;
return toReturn;
}
else
{
return *(Mock.%1$s_Return_Tail - 1);
}
]
MOCK_INTERFACE_VOID_SNIPPET = %q[
void %1$s_Ignore(void)
{
Mock.%1$s_IgnoreBool = (int)1;
}
]
MOCK_INTERFACE_FULL_SNIPPET = %q[
void %1$s_IgnoreAndReturn(%2$s)
{
Mock.%1$s_IgnoreBool = (int)1;
%3$s
Mock.%1$s_Return = Mock.%1$s_Return_Head;
Mock.%1$s_Return += Mock.%1$s_CallCount;
}
]
end
+70 -59
View File
@@ -23,59 +23,33 @@ class CMockGeneratorUtils
end
def code_insert_item_into_expect_array(type, array, newValue)
tail = array.gsub(/Head$/,'Tail')
lines = ["\n"]
lines << " {\n"
lines << " int sz = 0;\n"
lines << " #{type} *pointer = #{array};\n"
lines << " while (pointer && pointer != #{tail}) { sz++; pointer++; }\n"
lines << " if (sz == 0)\n"
lines << " {\n"
lines << " #{array} = (#{type}*)malloc(2*sizeof(#{type}));\n"
lines << " if (!#{array})\n"
lines << " Mock.allocFailure++;\n"
lines << " }\n"
lines << " else\n"
lines << " {\n"
lines << " #{type} *ptmp = (#{type}*)realloc(#{array}, sizeof(#{type}) * (sz+1));\n"
lines << " if (!ptmp)\n"
lines << " Mock.allocFailure++;\n"
lines << " else\n"
lines << " #{array} = ptmp;\n"
lines << " }\n"
lines << " memcpy(&#{array}[sz], &#{newValue}, sizeof(#{type}));\n"
lines << " #{tail} = &#{array}[sz+1];\n"
lines << " }\n"
INSERT_EXPECT_CODE_SNIPPET % [type, array, newValue]
end
def code_add_an_arg_expectation(function, arg_type, expected)
lines = code_insert_item_into_expect_array(arg_type, "Mock.#{function[:name]}_Expected_#{expected}_Head", expected)
lines << " Mock.#{function[:name]}_Expected_#{expected} = Mock.#{function[:name]}_Expected_#{expected}_Head;\n"
lines << " Mock.#{function[:name]}_Expected_#{expected} += Mock.#{function[:name]}_CallCount;\n"
var = "Mock.#{function[:name]}_Expected_#{expected}"
lines = code_insert_item_into_expect_array(arg_type, var, expected)
lines << INSERT_EXPECT_SETUP_SNIPPET % [var, function[:name]]
end
def code_add_base_expectation(func_name)
lines = [" Mock.#{func_name}_CallsExpected++;\n"]
lines = " Mock.#{func_name}_CallsExpected++;\n"
if (@ordered)
lines << [ " ++GlobalExpectCount;\n",
code_insert_item_into_expect_array("int", "Mock.#{func_name}_CallOrder_Head", "GlobalExpectCount"),
" Mock.#{func_name}_CallOrder = Mock.#{func_name}_CallOrder_Head;\n",
" Mock.#{func_name}_CallOrder += Mock.#{func_name}_CallCount;\n" ]
var = "Mock.#{func_name}_CallOrder"
lines << " ++GlobalExpectCount;\n"
lines << code_insert_item_into_expect_array('int', var, 'GlobalExpectCount')
lines << INSERT_EXPECT_SETUP_SNIPPET % [var, func_name]
end
lines.flatten
lines
end
def code_verify_an_arg_expectation(function, arg_type, actual)
[ "\n",
" if (Mock.#{function[:name]}_Expected_#{actual} != Mock.#{function[:name]}_Expected_#{actual}_Tail)\n",
" {\n",
" #{arg_type}* p_expected = Mock.#{function[:name]}_Expected_#{actual};\n",
" Mock.#{function[:name]}_Expected_#{actual}++;\n",
expect_helper(arg_type, '*p_expected', actual, "\"Function '#{function[:name]}' called with unexpected value for argument '#{actual}'.\""," "),
" }\n" ].flatten
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"
end
def expect_helper(c_type, expected, actual, msg, indent)
def expect_helper(c_type, expected, arg, msg)
if ((c_type.strip[-1] == 42) and (@ptr_handling == :compare_ptr))
unity_func = "TEST_ASSERT_EQUAL_INT_MESSAGE"
else
@@ -85,28 +59,65 @@ class CMockGeneratorUtils
case(unity_func)
when "TEST_ASSERT_EQUAL_MEMORY_MESSAGE"
full_expected = (expected =~ /^\*/) ? expected.slice(1..-1) : "&(#{expected})"
return "#{indent}#{unity_func}((void*)#{full_expected}, (void*)&(#{actual}), sizeof(#{c_type})#{unity_msg});\n"
return " #{unity_func}((void*)#{full_expected}, (void*)&(#{arg}), sizeof(#{c_type})#{unity_msg});\n"
when /_ARRAY/
return [ "#{indent}if (*p_expected == NULL)\n",
"#{indent} { TEST_ASSERT_NULL(#{actual}); }\n",
"#{indent}else\n",
"#{indent} { #{unity_func}(#{expected}, #{actual}, 1#{unity_msg}); }\n" ]
return " if (*p_expected == NULL)\n { TEST_ASSERT_NULL(#{arg}); }\n else\n { #{unity_func}(#{expected}, #{arg}, 1#{unity_msg}); }\n"
else
return "#{indent}#{unity_func}(#{expected}, #{actual}#{unity_msg});\n"
return " #{unity_func}(#{expected}, #{arg}#{unity_msg});\n"
end
end
def code_handle_return_value(function, indent)
[ "\n",
"#{indent}if (Mock.#{function[:name]}_Return != Mock.#{function[:name]}_Return_Tail)\n",
"#{indent}{\n",
"#{indent} #{function[:return_type]} toReturn = *Mock.#{function[:name]}_Return;\n",
"#{indent} Mock.#{function[:name]}_Return++;\n",
"#{indent} return toReturn;\n",
"#{indent}}\n",
"#{indent}else\n",
"#{indent}{\n",
"#{indent} return *(Mock.#{function[:name]}_Return_Tail - 1);\n",
"#{indent}}\n" ]
def code_handle_return_value(function)
INSERT_RETURN_TYPE_SNIPPET % [ function[:name], function[:return_type] ]
end
private ###################
INSERT_EXPECT_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;
}
memcpy(&%2$s_Head[sz], &%3$s, sizeof(%1$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"
INSERT_RETURN_TYPE_SNIPPET = %q[
if (Mock.%1$s_Return != Mock.%1$s_Return_Tail)
{
%2$s toReturn = *Mock.%1$s_Return;
Mock.%1$s_Return++;
return toReturn;
}
else
{
return *(Mock.%1$s_Return_Tail - 1);
}
]
INSERT_ARG_VERIFY_START_SNIPPET = %q[
if (Mock.%1$s != Mock.%1$s_Tail)
{
%2$s* p_expected = Mock.%1$s;
Mock.%1$s++;
]
end
+6
View File
@@ -54,4 +54,10 @@ namespace :test do
run_system_test_compilations(FileList[SYSTEST_COMPILE_MOCKABLES_PATH + '*.h'])
end
desc "Profile Mock Generation"
task :profile => [:clobber] do
run_system_test_profiles(FileList[SYSTEST_COMPILE_MOCKABLES_PATH + '*.h'])
end
end
+41 -2
View File
@@ -276,6 +276,25 @@ module RakefileHelpers
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)
require 'cmock'
@@ -288,12 +307,32 @@ module RakefileHelpers
puts "------------------------------------\n"
mockables.each do |header|
mock_filename = 'mock_' + File.basename(header).ext('.c')
cmock = CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml')
cmock.setup_mocks(header)
CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header)
puts "Compiling #{mock_filename}..."
compile(SYSTEST_GENERATED_FILES_PATH + mock_filename)
end
end
def run_system_test_profiles(mockables)
require 'cmock'
load_configuration($cfg_file)
$cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
puts "\n"
puts "--------------------------\n"
puts "SYSTEM TEST MOCK PROFILING\n"
puts "--------------------------\n"
mockables.each do |header|
mock_filename = 'mock_' + File.basename(header).ext('.c')
profile_this(mock_filename.gsub('.c','')) do
2.times do
CMock.new(SYSTEST_COMPILE_MOCKABLES_PATH + 'config.yml').setup_mocks(header)
end
end
puts "Compiling #{mock_filename}..."
compile(SYSTEST_GENERATED_FILES_PATH + mock_filename)
end
end
end
+3 -13
View File
@@ -14,37 +14,27 @@ class CMockConfigTest < Test::Unit::TestCase
assert_equal(CMockConfig::CMockDefaultOptions[:includes], config.includes)
assert_equal(CMockConfig::CMockDefaultOptions[:attributes], config.attributes)
assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins)
assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type],config.expect_call_count_type)
assert_equal(CMockConfig::CMockDefaultOptions[:ignore_bool_type], config.ignore_bool_type)
assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include)
assert_equal(CMockConfig::CMockDefaultOptions[:cexception_throw_type], config.cexception_throw_type)
end
should "replace only options specified in a hash" do
test_includes = ['hello']
test_attributes = ['blah', 'bleh']
test_bool_type = 'bool'
config = CMockConfig.new(:includes => test_includes, :ignore_bool_type => test_bool_type, :attributes => test_attributes)
config = CMockConfig.new(:includes => test_includes, :attributes => test_attributes)
assert_equal(CMockConfig::CMockDefaultOptions[:mock_path], config.mock_path)
assert_equal(test_includes, config.includes)
assert_equal(test_attributes, config.attributes)
assert_equal(CMockConfig::CMockDefaultOptions[:plugins], config.plugins)
assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type], config.expect_call_count_type)
assert_equal(test_bool_type, config.ignore_bool_type)
assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include)
assert_equal(CMockConfig::CMockDefaultOptions[:cexception_throw_type], config.cexception_throw_type)
end
should "replace only options specified in a yaml file" do
test_plugins = ['soda','pizza']
test_throw_type = 'uint32'
test_include = 'MySillyException.h'
config = CMockConfig.new("#{File.expand_path(File.dirname(__FILE__))}/cmock_config_test.yml")
assert_equal(CMockConfig::CMockDefaultOptions[:mock_path], config.mock_path)
assert_equal(CMockConfig::CMockDefaultOptions[:includes], config.includes)
assert_equal(test_plugins, config.plugins)
assert_equal(CMockConfig::CMockDefaultOptions[:expect_call_count_type], config.expect_call_count_type)
assert_equal(CMockConfig::CMockDefaultOptions[:ignore_bool_type], config.ignore_bool_type)
assert_equal(CMockConfig::CMockDefaultOptions[:cexception_include], config.cexception_include)
assert_equal(test_throw_type, config.cexception_throw_type)
assert_equal(test_include, config.cexception_include)
end
end
+1 -1
View File
@@ -2,4 +2,4 @@
:plugins:
- 'soda'
- 'pizza'
:cexception_throw_type: 'uint32'
:cexception_include: 'MySillyException.h'
+2 -2
View File
@@ -332,7 +332,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
]
@plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.bool"," PreSupaFunctionDos.bool"])
@plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches, const char* named)"," MockSupaFunctionDos(uint32 sandwiches, const char* named)"])
@utils.expect.code_handle_return_value(function," ").returns([" UtilsSupaFunction.bool"])
@utils.expect.code_handle_return_value(function).returns([" UtilsSupaFunction.bool"])
@cmock_generator.create_mock_implementation(output, function)
@@ -360,7 +360,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
]
@plugins.expect.run(:mock_implementation_prefix, function).returns([" PreSupaFunctionUno.int"," PreSupaFunctionDos.int"])
@plugins.expect.run(:mock_implementation, function).returns([" MockSupaFunctionUno(uint32 sandwiches)"," MockSupaFunctionDos(uint32 sandwiches)"])
@utils.expect.code_handle_return_value(function," ").returns([" UtilsSupaFunction.int"])
@utils.expect.code_handle_return_value(function).returns([" UtilsSupaFunction.int"])
@cmock_generator.create_mock_implementation(output, function)
@@ -17,14 +17,14 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
end
should "include the cexception library" do
expected = ["#include \"Exception.h\"\n"]
expected = "#include \"Exception.h\"\n"
@config.expect.cexception_include.returns(nil)
returned = @cmock_generator_plugin_cexception.include_files
assert_equal(expected, returned)
end
should "include the cexception library with a custom path if specified" do
expected = ["#include \"../cexception/lib/Exception.h\"\n"]
expected = "#include \"../cexception/lib/Exception.h\"\n"
@config.expect.cexception_include.returns("../cexception/lib/Exception.h")
returned = @cmock_generator_plugin_cexception.include_files
assert_equal(expected, returned)
@@ -32,34 +32,28 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
should "add to control structure mock needs" do
function = { :name => "Oak", :args => [], :return_type => "void" }
@config.expect.expect_call_count_type.returns("uint32")
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = [" uint32 *Oak_ThrowOnCallCount;\n",
" uint32 *Oak_ThrowOnCallCount_Head;\n",
" uint32 *Oak_ThrowOnCallCount_Tail;\n",
" EXCEPTION_TYPE *Oak_ThrowValue;\n",
" EXCEPTION_TYPE *Oak_ThrowValue_Head;\n",
" EXCEPTION_TYPE *Oak_ThrowValue_Tail;\n"
]
expected = ["\n",
" int *Oak_ThrowOnCallCount;\n",
" int *Oak_ThrowOnCallCount_Head;\n",
" int *Oak_ThrowOnCallCount_Tail;\n",
" EXCEPTION_T *Oak_ThrowValue;\n",
" EXCEPTION_T *Oak_ThrowValue_Head;\n",
" EXCEPTION_T *Oak_ThrowValue_Tail;\n"
].join
returned = @cmock_generator_plugin_cexception.instance_structure(function)
assert_equal(expected, returned)
end
should "add mock function declarations for functions without arguments" do
function = { :name => "Spruce", :args_string => "void", :return_type => "void" }
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = ["void Spruce_ExpectAndThrow(EXCEPTION_TYPE toThrow);\n"]
expected = "void Spruce_ExpectAndThrow(EXCEPTION_T toThrow);\n"
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function)
assert_equal(expected, returned)
end
should "add mock function declarations for functions with arguments" do
function = { :name => "Spruce", :args_string => "const char* Petunia, uint32_t Lily", :return_type => "void" }
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = ["void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_TYPE toThrow);\n"]
expected = "void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_T toThrow);\n"
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -70,39 +64,36 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
should "add a mock implementation" do
function = {:name => "Cherry", :args => [], :return_type => "void"}
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
expected = ["\n",
" if((Mock.Cherry_ThrowOnCallCount != Mock.Cherry_ThrowOnCallCount_Tail) &&\n",
" (Mock.Cherry_ThrowValue != Mock.Cherry_ThrowValue_Tail))\n",
" if ((Mock.Cherry_ThrowOnCallCount != Mock.Cherry_ThrowOnCallCount_Tail) &&\n",
" (Mock.Cherry_ThrowValue != Mock.Cherry_ThrowValue_Tail))\n",
" {\n",
" if (*Mock.Cherry_ThrowOnCallCount && \n",
" (Mock.Cherry_CallCount == *Mock.Cherry_ThrowOnCallCount))\n",
" {\n",
" EXCEPTION_TYPE toThrow = *Mock.Cherry_ThrowValue;\n",
" EXCEPTION_T toThrow = *Mock.Cherry_ThrowValue;\n",
" Mock.Cherry_ThrowOnCallCount++;\n",
" Mock.Cherry_ThrowValue++;\n",
" Throw(toThrow);\n",
" }\n",
" }\n"
]
].join
returned = @cmock_generator_plugin_cexception.mock_implementation(function)
assert_equal(expected, returned)
end
should "add mock interfaces for functions without arguments" do
function = {:name => "Pear", :args_string => "void", :args => [], :return_type => "void"}
@config.expect.expect_call_count_type.returns("uint32")
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
@utils.expect.code_add_base_expectation("Pear").returns("mock_retval_0")
@utils.expect.code_insert_item_into_expect_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.code_insert_item_into_expect_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2")
@utils.expect.code_insert_item_into_expect_array("int", "Mock.Pear_ThrowOnCallCount", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.code_insert_item_into_expect_array("EXCEPTION_T", "Mock.Pear_ThrowValue", "toThrow").returns("mock_return_2")
expected = ["void Pear_ExpectAndThrow(EXCEPTION_TYPE toThrow)\n",
expected = ["void Pear_ExpectAndThrow(EXCEPTION_T toThrow)\n",
"{\n",
"mock_retval_0",
"mock_return_1",
"mock_return_2",
"\n",
" Mock.Pear_ThrowValue = Mock.Pear_ThrowValue_Head;\n",
" Mock.Pear_ThrowOnCallCount = Mock.Pear_ThrowOnCallCount_Head;\n",
" while ((*Mock.Pear_ThrowOnCallCount <= Mock.Pear_CallCount) && (Mock.Pear_ThrowOnCallCount < Mock.Pear_ThrowOnCallCount_Tail))\n",
@@ -111,25 +102,24 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
" Mock.Pear_ThrowOnCallCount++;\n",
" }\n",
"}\n\n"
]
].join
returned = @cmock_generator_plugin_cexception.mock_interfaces(function)
assert_equal(expected, returned)
end
should "add a mock interfaces for functions with arguments" do
function = {:name => "Pear", :args_string => "int blah", :args => [{ :type => "int", :name => "blah" }], :return_type => "void"}
@config.expect.expect_call_count_type.returns("uint32")
@config.expect.cexception_throw_type.returns("EXCEPTION_TYPE")
@utils.expect.code_add_base_expectation("Pear").returns("mock_retval_0")
@utils.expect.code_insert_item_into_expect_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.code_insert_item_into_expect_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2")
@utils.expect.code_insert_item_into_expect_array("int", "Mock.Pear_ThrowOnCallCount", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.code_insert_item_into_expect_array("EXCEPTION_T", "Mock.Pear_ThrowValue", "toThrow").returns("mock_return_2")
@utils.expect.create_call_list(function).returns("mock_return_3")
expected = ["void Pear_ExpectAndThrow(int blah, EXCEPTION_TYPE toThrow)\n",
expected = ["void Pear_ExpectAndThrow(int blah, EXCEPTION_T toThrow)\n",
"{\n",
"mock_retval_0",
"mock_return_1",
"mock_return_2",
"\n",
" Mock.Pear_ThrowValue = Mock.Pear_ThrowValue_Head;\n",
" Mock.Pear_ThrowOnCallCount = Mock.Pear_ThrowOnCallCount_Head;\n",
" while ((*Mock.Pear_ThrowOnCallCount <= Mock.Pear_CallCount) && (Mock.Pear_ThrowOnCallCount < Mock.Pear_ThrowOnCallCount_Tail))\n",
@@ -139,7 +129,7 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
" }\n",
" ExpectParameters_Pear(mock_return_3);\n",
"}\n\n"
]
].join
returned = @cmock_generator_plugin_cexception.mock_interfaces(function)
assert_equal(expected, returned)
end
@@ -150,7 +140,8 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
should "add necessary baggage to destroy function" do
function = {:name => "Banana", :args_string => "", :args => [], :return_type => "void"}
expected = [" if(Mock.Banana_ThrowOnCallCount_Head)\n",
expected = ["\n",
" if(Mock.Banana_ThrowOnCallCount_Head)\n",
" {\n",
" free(Mock.Banana_ThrowOnCallCount_Head);\n",
" }\n",
@@ -164,7 +155,7 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
" Mock.Banana_ThrowValue=NULL;\n",
" Mock.Banana_ThrowValue_Head=NULL;\n",
" Mock.Banana_ThrowValue_Tail=NULL;\n"
]
].join
returned = @cmock_generator_plugin_cexception.mock_destroy(function)
assert_equal(expected, returned)
end
+99 -95
View File
@@ -35,85 +35,81 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add to control structure mock needs of functions of style 'void func(void)'" do
function = {:name => "Oak", :args => [], :return_type => "void"}
count_type = "uint32"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n"
]
expected = ["\n",
" int Oak_CallCount;\n",
" int Oak_CallsExpected;\n"
].join
returned = @cmock_generator_plugin_expect.instance_structure(function)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'int func(void)'" do
function = {:name => "Elm", :args => [], :return_type => "int"}
count_type = "int16"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n",
" #{function[:return_type]} *#{function[:name]}_Return;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Head;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Tail;\n"
]
expected = ["\n",
" int Elm_CallCount;\n",
" int Elm_CallsExpected;\n",
"\n",
" int *Elm_Return;\n",
" int *Elm_Return_Head;\n",
" int *Elm_Return_Tail;\n"
].join
returned = @cmock_generator_plugin_expect.instance_structure(function)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'void func(int chicken, char* pork)'" do
function = {:name => "Cedar", :args => [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}], :return_type => "void"}
count_type = "unsigned char"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n",
" int *#{function[:name]}_Expected_chicken;\n",
" int *#{function[:name]}_Expected_chicken_Head;\n",
" int *#{function[:name]}_Expected_chicken_Tail;\n",
" char* *#{function[:name]}_Expected_pork;\n",
" char* *#{function[:name]}_Expected_pork_Head;\n",
" char* *#{function[:name]}_Expected_pork_Tail;\n"
]
expected = ["\n",
" int Cedar_CallCount;\n",
" int Cedar_CallsExpected;\n",
"\n",
" int *Cedar_Expected_chicken;\n",
" int *Cedar_Expected_chicken_Head;\n",
" int *Cedar_Expected_chicken_Tail;\n",
"\n",
" char* *Cedar_Expected_pork;\n",
" char* *Cedar_Expected_pork_Head;\n",
" char* *Cedar_Expected_pork_Tail;\n"
].join
returned = @cmock_generator_plugin_expect.instance_structure(function)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'int func(float beef)'" do
function = {:name => "Birch", :args => [{ :name => "beef", :type => "float"}], :return_type => "int"}
count_type = "unsigned int"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n",
" #{function[:return_type]} *#{function[:name]}_Return;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Head;\n",
" #{function[:return_type]} *#{function[:name]}_Return_Tail;\n",
" float *#{function[:name]}_Expected_beef;\n",
" float *#{function[:name]}_Expected_beef_Head;\n",
" float *#{function[:name]}_Expected_beef_Tail;\n",
]
expected = ["\n",
" int Birch_CallCount;\n",
" int Birch_CallsExpected;\n",
"\n",
" int *Birch_Return;\n",
" int *Birch_Return_Head;\n",
" int *Birch_Return_Tail;\n",
"\n",
" float *Birch_Expected_beef;\n",
" float *Birch_Expected_beef_Head;\n",
" float *Birch_Expected_beef_Tail;\n",
].join
returned = @cmock_generator_plugin_expect.instance_structure(function)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'void func(void)' and global ordering" do
function = {:name => "Oak", :args => [], :return_type => "void"}
count_type = "uint32"
@config.expect.expect_call_count_type.returns(count_type)
expected = [" #{count_type} #{function[:name]}_CallCount;\n",
" #{count_type} #{function[:name]}_CallsExpected;\n",
" int *#{function[:name]}_CallOrder;\n",
" int *#{function[:name]}_CallOrder_Head;\n",
" int *#{function[:name]}_CallOrder_Tail;\n"
]
expected = ["\n",
" int Oak_CallCount;\n",
" int Oak_CallsExpected;\n",
"\n",
" int *Oak_CallOrder;\n",
" int *Oak_CallOrder_Head;\n",
" int *Oak_CallOrder_Tail;\n"
].join
returned = @cmock_generator_plugin_expect_strict.instance_structure(function)
assert_equal(expected, returned)
end
should "add mock function declaration for functions of style 'void func(void)'" do
function = {:name => "Maple", :args_string => "void", :return_type => "void"}
expected = ["void #{function[:name]}_Expect(#{function[:args_string]});\n"]
expected = "void #{function[:name]}_Expect(#{function[:args_string]});\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -121,7 +117,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function declaration for functions of style 'int func(void)'" do
function = {:name => "Spruce", :args_string => "void", :return_string => "int toReturn"}
expected = ["void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"]
expected = "void #{function[:name]}_ExpectAndReturn(#{function[:return_string]});\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -129,7 +125,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function declaration for functions of style 'const char* func(int tofu)'" do
function = {:name => "Pine", :args_string => "int tofu", :return_string => "const char* toReturn"}
expected = ["void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"]
expected = "void #{function[:name]}_ExpectAndReturn(#{function[:args_string]}, #{function[:return_string]});\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -140,12 +136,13 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function implementation for functions of style 'void func(void)'" do
function = {:name => "Apple", :args => [], :return_type => "void"}
expected = [" Mock.Apple_CallCount++;\n",
expected = ["\n",
" Mock.Apple_CallCount++;\n",
" if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Function 'Apple' called more times than expected\");\n",
" }\n"
]
].join
returned = @cmock_generator_plugin_expect.mock_implementation(function)
assert_equal(expected, returned)
end
@@ -156,21 +153,23 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
@utils.expect.code_verify_an_arg_expectation(function, function[:args][0][:type], function[:args][0][:name]).returns("mocked_retval_1")
@utils.expect.code_verify_an_arg_expectation(function, function[:args][1][:type], function[:args][1][:name]).returns("mocked_retval_2")
expected = [" Mock.Cherry_CallCount++;\n",
expected = ["\n",
" Mock.Cherry_CallCount++;\n",
" if (Mock.Cherry_CallCount > Mock.Cherry_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Function 'Cherry' called more times than expected\");\n",
" }\n",
"mocked_retval_1",
"mocked_retval_2"
]
].join
returned = @cmock_generator_plugin_expect.mock_implementation(function)
assert_equal(expected, returned)
end
should "add mock function implementation using ordering if needed" do
function = {:name => "Apple", :args => [], :return_type => "void"}
expected = [" Mock.Apple_CallCount++;\n",
expected = ["\n",
" Mock.Apple_CallCount++;\n",
" if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Function 'Apple' called more times than expected\");\n",
@@ -188,7 +187,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
" strcpy(GlobalOrderError, ErrStr);\n",
" }\n",
" }\n"
]
].join
@cmock_generator_plugin_expect.ordered = true
returned = @cmock_generator_plugin_expect.mock_implementation(function)
assert_equal(expected, returned)
@@ -196,7 +195,8 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock function implementation for functions of style 'void func(void)' and strict ordering" do
function = {:name => "Apple", :args => [], :return_type => "void"}
expected = [" Mock.Apple_CallCount++;\n",
expected = ["\n",
" Mock.Apple_CallCount++;\n",
" if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n",
" {\n",
" TEST_FAIL(\"Function 'Apple' called more times than expected\");\n",
@@ -214,7 +214,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
" strcpy(GlobalOrderError, ErrStr);\n",
" }\n",
" }\n"
]
].join
returned = @cmock_generator_plugin_expect_strict.mock_implementation(function)
assert_equal(expected, returned)
end
@@ -234,7 +234,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock interfaces for functions of style 'unsigned short func(void)'" do
function = {:name => "Orange", :args => [], :args_string => "void", :return_type => "unsigned short", :return_string => "unsigned short toReturn"}
@utils.expect.code_add_base_expectation("Orange").returns("mock_retval_0")
@utils.expect.code_insert_item_into_expect_array(function[:return_type], "Mock.Orange_Return_Head","toReturn").returns("mock_retval_1")
@utils.expect.code_insert_item_into_expect_array(function[:return_type], "Mock.Orange_Return","toReturn").returns("mock_retval_1")
expected = ["void Orange_ExpectAndReturn(unsigned short toReturn)\n",
"{\n",
@@ -253,7 +253,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
@utils.expect.code_add_an_arg_expectation(function, "char*", "pescado").returns("mock_retval_2")
@utils.expect.code_add_base_expectation("Lemon").returns("mock_retval_0")
@utils.expect.create_call_list(function).returns("mock_retval_3")
@utils.expect.code_insert_item_into_expect_array(function[:return_type], "Mock.Lemon_Return_Head", 'toReturn').returns("mock_retval_1")
@utils.expect.code_insert_item_into_expect_array(function[:return_type], "Mock.Lemon_Return", 'toReturn').returns("mock_retval_1")
expected = ["void ExpectParameters_Lemon(char* pescado)\n",
"{\n",
@@ -267,8 +267,8 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
" Mock.Lemon_Return = Mock.Lemon_Return_Head;\n",
" Mock.Lemon_Return += Mock.Lemon_CallCount;\n",
"}\n\n"
]
returned = @cmock_generator_plugin_expect.mock_interfaces(function)
].join
returned = @cmock_generator_plugin_expect.mock_interfaces(function).join
assert_equal(expected, returned)
end
@@ -288,7 +288,7 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock verify lines" do
function = {:name => "Banana" }
expected = [" TEST_ASSERT_EQUAL_MESSAGE(Mock.Banana_CallsExpected, Mock.Banana_CallCount, \"Function 'Banana' called unexpected number of times.\");\n"]
expected = " TEST_ASSERT_EQUAL_MESSAGE(Mock.Banana_CallsExpected, Mock.Banana_CallCount, \"Function 'Banana' called unexpected number of times.\");\n"
returned = @cmock_generator_plugin_expect.mock_verify(function)
assert_equal(expected, returned)
end
@@ -302,49 +302,53 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
should "add mock destroy for functions of style 'char func(void)'" do
function = {:name => "Palm", :args => [], :return_type => "char" }
expected = [" if (Mock.Palm_Return_Head)\n",
" {\n",
" free(Mock.Palm_Return_Head);\n",
" }\n",
" Mock.Palm_Return=NULL;\n",
" Mock.Palm_Return_Head=NULL;\n",
" Mock.Palm_Return_Tail=NULL;\n"
]
expected = [ %q[
if (Mock.Palm_Return_Head)
{
free(Mock.Palm_Return_Head);
}
Mock.Palm_Return=NULL;
Mock.Palm_Return_Head=NULL;
Mock.Palm_Return_Tail=NULL;
] ]
returned = @cmock_generator_plugin_expect.mock_destroy(function)
assert_equal(expected, returned)
end
should "add mock destroy for functions of style 'int func(uint32 grease)'" do
function = {:name => "Coconut", :args => [{ :type => "uint32", :name => "grease"}], :return_type => "int" }
expected = [" if (Mock.Coconut_Return_Head)\n",
" {\n",
" free(Mock.Coconut_Return_Head);\n",
" }\n",
" Mock.Coconut_Return=NULL;\n",
" Mock.Coconut_Return_Head=NULL;\n",
" Mock.Coconut_Return_Tail=NULL;\n",
" if (Mock.Coconut_Expected_grease_Head)\n",
" {\n",
" free(Mock.Coconut_Expected_grease_Head);\n",
" }\n",
" Mock.Coconut_Expected_grease=NULL;\n",
" Mock.Coconut_Expected_grease_Head=NULL;\n",
" Mock.Coconut_Expected_grease_Tail=NULL;\n"
]
expected = [ %q[
if (Mock.Coconut_Return_Head)
{
free(Mock.Coconut_Return_Head);
}
Mock.Coconut_Return=NULL;
Mock.Coconut_Return_Head=NULL;
Mock.Coconut_Return_Tail=NULL;
] , %q[
if (Mock.Coconut_Expected_grease_Head)
{
free(Mock.Coconut_Expected_grease_Head);
}
Mock.Coconut_Expected_grease=NULL;
Mock.Coconut_Expected_grease_Head=NULL;
Mock.Coconut_Expected_grease_Tail=NULL;
] ]
returned = @cmock_generator_plugin_expect.mock_destroy(function)
assert_equal(expected, returned)
end
should "add mock destroy for functions with strict ordering" do
function = {:name => "Peach", :args => [], :return_type => "void" }
expected = [ " if (Mock.Peach_CallOrder_Head)\n",
" {\n",
" free(Mock.Peach_CallOrder_Head);\n",
" }\n",
" Mock.Peach_CallOrder=NULL;\n",
" Mock.Peach_CallOrder_Head=NULL;\n",
" Mock.Peach_CallOrder_Tail=NULL;\n"
]
expected = [ %q[
if (Mock.Peach_CallOrder_Head)
{
free(Mock.Peach_CallOrder_Head);
}
Mock.Peach_CallOrder=NULL;
Mock.Peach_CallOrder_Head=NULL;
Mock.Peach_CallOrder_Tail=NULL;
] ]
returned = @cmock_generator_plugin_expect_strict.mock_destroy(function)
assert_equal(expected, returned)
end
+15 -15
View File
@@ -22,23 +22,21 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
should "add a required variable to the instance structure" do
function = {:name => "Grass", :args => [], :return_type => "void"}
@config.expect.ignore_bool_type.returns("BOOL")
expected = [" BOOL Grass_IgnoreBool;\n"]
expected = " int Grass_IgnoreBool;\n"
returned = @cmock_generator_plugin_ignore.instance_structure(function)
assert_equal(expected, returned)
end
should "handle function declarations for functions without return values" do
function = {:name => "Mold", :args_string => "void", :return_type => "void"}
expected = ["void Mold_Ignore(void);\n"]
expected = "void Mold_Ignore(void);\n"
returned = @cmock_generator_plugin_ignore.mock_function_declarations(function)
assert_equal(expected, returned)
end
should "handle function declarations for functions that returns something" do
function = {:name => "Fungus", :args_string => "void", :return_type => "const char*", :return_string => "const char* toReturn"}
expected = ["void Fungus_IgnoreAndReturn(const char* toReturn);\n"]
expected = "void Fungus_IgnoreAndReturn(const char* toReturn);\n"
returned = @cmock_generator_plugin_ignore.mock_function_declarations(function)
assert_equal(expected, returned)
end
@@ -49,7 +47,7 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
" {\n",
" return;\n",
" }\n"
]
].join
returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function)
assert_equal(expected, returned)
end
@@ -71,7 +69,7 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
" return *(Mock.Fungus_Return_Tail - 1);\n",
" }\n",
" }\n"
]
].join
returned = @cmock_generator_plugin_ignore.mock_implementation_prefix(function)
assert_equal(expected, returned)
end
@@ -82,27 +80,29 @@ class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
should "add a new mock interface for ignoring when function had no return value" do
function = {:name => "Slime", :args => [], :args_string => "void", :return_type => "void"}
expected = ["void Slime_Ignore(void)\n",
expected = ["\n",
"void Slime_Ignore(void)\n",
"{\n",
" Mock.Slime_IgnoreBool = (unsigned char)1;\n",
" Mock.Slime_IgnoreBool = (int)1;\n",
"}\n\n"
]
].join
returned = @cmock_generator_plugin_ignore.mock_interfaces(function)
assert_equal(expected, returned)
end
should "add a new mock interface for ignoring when function has return value" do
function = {:name => "Slime", :args => [], :args_string => "void", :return_type => "uint32", :return_string => "uint32 toReturn"}
@utils.expect.code_insert_item_into_expect_array("uint32", "Mock.Slime_Return_Head", "toReturn").returns("mock_return_1")
@utils.expect.code_insert_item_into_expect_array("uint32", "Mock.Slime_Return", "toReturn").returns("mock_return_1")
expected = ["void Slime_IgnoreAndReturn(uint32 toReturn)\n",
expected = ["\n",
"void Slime_IgnoreAndReturn(uint32 toReturn)\n",
"{\n",
" Mock.Slime_IgnoreBool = (unsigned char)1;\n",
"mock_return_1",
" Mock.Slime_IgnoreBool = (int)1;\n",
"mock_return_1\n",
" Mock.Slime_Return = Mock.Slime_Return_Head;\n",
" Mock.Slime_Return += Mock.Slime_CallCount;\n",
"}\n\n"
]
].join
returned = @cmock_generator_plugin_ignore.mock_interfaces(function)
assert_equal(expected, returned)
end
+20 -20
View File
@@ -49,31 +49,31 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
should "make expand array" do
the_type = "int"
the_array = "arrayHead"
the_array = "array"
new_value = "new_value"
expected = ["\n",
" {\n",
" int sz = 0;\n",
" int *pointer = arrayHead;\n",
" while (pointer && pointer != arrayTail) { sz++; pointer++; }\n",
" int *pointer = array_Head;\n",
" while (pointer && pointer != array_Tail) { sz++; pointer++; }\n",
" if (sz == 0)\n",
" {\n",
" arrayHead = (int*)malloc(2*sizeof(int));\n",
" if (!arrayHead)\n",
" array_Head = (int*)malloc(2*sizeof(int));\n",
" if (!array_Head)\n",
" Mock.allocFailure++;\n",
" }\n",
" else\n",
" {\n",
" int *ptmp = (int*)realloc(arrayHead, sizeof(int) * (sz+1));\n",
" int *ptmp = (int*)realloc(array_Head, sizeof(int) * (sz+1));\n",
" if (!ptmp)\n",
" Mock.allocFailure++;\n",
" else\n",
" arrayHead = ptmp;\n"," }\n",
" memcpy(&arrayHead[sz], &new_value, sizeof(int));\n",
" arrayTail = &arrayHead[sz+1];\n",
" array_Head = ptmp;\n"," }\n",
" memcpy(&array_Head[sz], &new_value, sizeof(int));\n",
" array_Tail = &array_Head[sz+1];\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_insert_item_into_expect_array(the_type, the_array, new_value)
assert_equal(expected, returned)
end
@@ -91,8 +91,8 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" {\n",
" return *(Mock.Spatula_Return_Tail - 1);\n",
" }\n"
]
returned = @cmock_generator_utils.code_handle_return_value(function, ' ')
].join
returned = @cmock_generator_utils.code_handle_return_value(function)
assert_equal(expected, returned)
end
@@ -125,13 +125,13 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" }\n",
" Mock.PizzaCutter_Expected_Spork = Mock.PizzaCutter_Expected_Spork_Head;\n",
" Mock.PizzaCutter_Expected_Spork += Mock.PizzaCutter_CallCount;\n"
]
].join
returned = @cmock_generator_utils.code_add_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
should "add base expectations, with nothing else when strict ordering not turned on" do
expected = [" Mock.Nectarine_CallsExpected++;\n"]
expected = " Mock.Nectarine_CallsExpected++;\n"
returned = @cmock_generator_utils.code_add_base_expectation("Nectarine")
assert_equal(expected, returned)
@@ -163,7 +163,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.Nectarine_CallOrder_Tail = &Mock.Nectarine_CallOrder_Head[sz+1];\n",
" }\n",
" Mock.Nectarine_CallOrder = Mock.Nectarine_CallOrder_Head;\n",
" Mock.Nectarine_CallOrder += Mock.Nectarine_CallCount;\n" ]
" Mock.Nectarine_CallOrder += Mock.Nectarine_CallCount;\n" ].join
@cmock_generator_utils.ordered = true
returned = @cmock_generator_utils.code_add_base_expectation("Nectarine")
assert_equal(expected, returned)
@@ -181,7 +181,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.CanOpener_Expected_CorkScrew++;\n",
" TEST_ASSERT_EQUAL_MESSAGE(*p_expected, CorkScrew, \"Function 'CanOpener' called with unexpected value for argument 'CorkScrew'.\");\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
@@ -201,7 +201,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.MeasureCup_Expected_TeaSpoon++;\n",
" TEST_ASSERT_EQUAL_STRING_MESSAGE(*p_expected, TeaSpoon, \"Function 'MeasureCup' called with unexpected value for argument 'TeaSpoon'.\");\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
@@ -221,7 +221,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.TeaPot_Expected_TeaSpoon++;\n",
" TEST_ASSERT_EQUAL_MANDELBROT_SET_T_MESSAGE(*p_expected, TeaSpoon, \"Function 'TeaPot' called with unexpected value for argument 'TeaSpoon'.\");\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
@@ -241,7 +241,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" Mock.Toaster_Expected_Bread++;\n",
" TEST_ASSERT_EQUAL_MEMORY_MESSAGE((void*)p_expected, (void*)&(Bread), sizeof(SOME_STRUCT), \"Function 'Toaster' called with unexpected value for argument 'Bread'.\");\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end
@@ -264,7 +264,7 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
" else\n",
" { TEST_ASSERT_EQUAL_FRUIT_ARRAY_MESSAGE(*p_expected, Strawberry, 1, \"Function 'Blender' called with unexpected value for argument 'Strawberry'.\"); }\n",
" }\n"
]
].join
returned = @cmock_generator_utils.code_verify_an_arg_expectation(function, var_type, var_name)
assert_equal(expected, returned)
end