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