mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-06-06 05:25:29 +00:00
filled out more generator tests
git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@23 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
@@ -132,5 +132,156 @@ class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
|
||||
assert_equal([], @cmock_generator_plugin_expect.mock_implementation_prefix(function_name, function_return_type))
|
||||
end
|
||||
|
||||
####NEXT UP: Mock Implementation
|
||||
should "add mock function implementation for functions of style 'void func(void)'" do
|
||||
function_name = "Apple"
|
||||
function_args = []
|
||||
function_return_type = "void"
|
||||
|
||||
expected = [" Mock.Apple_CallCount++;\n",
|
||||
" if (Mock.Apple_CallCount > Mock.Apple_CallsExpected)\n",
|
||||
" {\n",
|
||||
" TEST_THROW(\"Apple Called More Times Than Expected\");\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_implementation(function_name, function_args)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock function implementation for functions of style 'int func(int veal, unsigned int sushi)'" do
|
||||
function_name = "Cherry"
|
||||
function_args = [ { :type => "int", :name => "veal" }, { :type => "unsigned int", :name => "sushi" } ]
|
||||
function_return_type = "int"
|
||||
|
||||
@utils.expect.make_handle_expected(function_name, function_args[0][:type], function_args[0][:name]).returns("mocked_retval_1")
|
||||
@utils.expect.make_handle_expected(function_name, function_args[1][:type], function_args[1][:name]).returns("mocked_retval_2")
|
||||
|
||||
expected = [" Mock.Cherry_CallCount++;\n",
|
||||
" if (Mock.Cherry_CallCount > Mock.Cherry_CallsExpected)\n",
|
||||
" {\n",
|
||||
" TEST_THROW(\"Cherry Called More Times Than Expected\");\n",
|
||||
" }\n",
|
||||
"mocked_retval_1",
|
||||
"mocked_retval_2"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_implementation(function_name, function_args)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock interfaces for functions of style 'void func(void)'" do
|
||||
function_name = "Pear"
|
||||
function_args = "void"
|
||||
function_args_as_array = []
|
||||
function_return_type = "void"
|
||||
|
||||
expected = ["void Pear_Expect(void)\n",
|
||||
"{\n",
|
||||
" Mock.Pear_CallsExpected++;\n",
|
||||
"}\n\n"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock interfaces for functions of style 'unsigned short func(void)'" do
|
||||
function_name = "Orange"
|
||||
function_args = "void"
|
||||
function_args_as_array = []
|
||||
function_return_type = "unsigned short"
|
||||
|
||||
@utils.expect.make_expand_array(function_return_type, "Mock.Orange_Return_Head","toReturn").returns("mock_retval_1")
|
||||
|
||||
expected = ["void Orange_ExpectAndReturn(unsigned short toReturn)\n",
|
||||
"{\n",
|
||||
" Mock.Orange_CallsExpected++;\n",
|
||||
"mock_retval_1",
|
||||
" Mock.Orange_Return = Mock.Orange_Return_Head;\n",
|
||||
" Mock.Orange_Return += Mock.Orange_CallCount;\n",
|
||||
"}\n\n"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock interfaces for functions of style 'int func(char* pescado)'" do
|
||||
function_name = "Lemon"
|
||||
function_args = "char* pescado"
|
||||
function_args_as_array = [{ :type => "char*", :name => "pescado"}]
|
||||
function_return_type = "int"
|
||||
|
||||
@utils.expect.make_add_new_expected(function_name, "char*", "pescado").returns("mock_retval_2")
|
||||
@utils.expect.create_call_list(function_args_as_array).returns("mock_retval_3")
|
||||
@utils.expect.make_expand_array(function_return_type, "Mock.Lemon_Return_Head","toReturn").returns("mock_retval_1")
|
||||
|
||||
expected = ["void ExpectParameters_Lemon(char* pescado)\n",
|
||||
"{\n",
|
||||
"mock_retval_2",
|
||||
"}\n\n",
|
||||
"void Lemon_ExpectAndReturn(char* pescado, int toReturn)\n",
|
||||
"{\n",
|
||||
" Mock.Lemon_CallsExpected++;\n",
|
||||
" ExpectParameters_Lemon(mock_retval_3);\n",
|
||||
"mock_retval_1",
|
||||
" 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_name, function_args, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
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"
|
||||
returned = @cmock_generator_plugin_expect.mock_verify(function_name)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock destroy for functions of style 'void func(void)'" do
|
||||
function_name = "Peach"
|
||||
function_args_as_array = []
|
||||
function_return_type = "void"
|
||||
|
||||
expected = []
|
||||
returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock destroy for functions of style 'char func(void)'" do
|
||||
function_name = "Palm"
|
||||
function_args_as_array = []
|
||||
function_return_type = "char"
|
||||
|
||||
expected = [" if (Mock.Palm_Return_Head)\n",
|
||||
" {\n",
|
||||
" free(Mock.Palm_Return_Head);\n",
|
||||
" Mock.Palm_Return_Head=NULL;\n",
|
||||
" Mock.Palm_Return_HeadTail=NULL;\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add mock destroy for functions of style 'int func(uint32 grease)'" do
|
||||
function_name = "Coconut"
|
||||
function_args_as_array = [{ :type => "uint32", :name => "grease"}]
|
||||
function_return_type = "int"
|
||||
|
||||
expected = [" if (Mock.Coconut_Return_Head)\n",
|
||||
" {\n",
|
||||
" free(Mock.Coconut_Return_Head);\n",
|
||||
" Mock.Coconut_Return_Head=NULL;\n",
|
||||
" Mock.Coconut_Return_HeadTail=NULL;\n",
|
||||
" }\n",
|
||||
" if (Mock.Coconut_Expected_grease_Head)\n",
|
||||
" {\n",
|
||||
" free(Mock.Coconut_Expected_grease_Head);\n",
|
||||
" Mock.Coconut_Expected_grease_Head=NULL;\n",
|
||||
" Mock.Coconut_Expected_grease_HeadTail=NULL;\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_plugin_expect.mock_destroy(function_name, function_args_as_array, function_return_type)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,4 +15,148 @@ class CMockGeneratorUtilsTest < Test::Unit::TestCase
|
||||
assert_equal(@config, @cmock_generator_utils.config)
|
||||
assert_equal(" ", @cmock_generator_utils.tab)
|
||||
end
|
||||
|
||||
should "set up an empty call list if no arguments passed" do
|
||||
args = []
|
||||
|
||||
expected = ""
|
||||
returned = @cmock_generator_utils.create_call_list(args)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "set up a single call list if one arguments passed" do
|
||||
args = [{ :type => "const char*", :name => "spoon"}]
|
||||
|
||||
expected = "spoon"
|
||||
returned = @cmock_generator_utils.create_call_list(args)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "set up a call list if multiple arguments passed" do
|
||||
args = [{ :type => "const char*", :name => "spoon"}, { :type => "int", :name => "fork"}, { :type => "unsigned int", :name => "knife"}]
|
||||
|
||||
expected = "spoon, fork, knife"
|
||||
returned = @cmock_generator_utils.create_call_list(args)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "make expand array" do
|
||||
the_type = "int"
|
||||
the_array = "array"
|
||||
new_value = "new_value"
|
||||
|
||||
expected = ["\n",
|
||||
" {\n",
|
||||
" int sz = 0;\n",
|
||||
" int *pointer = array;\n",
|
||||
" while(pointer && pointer != arrayTail) { sz++; pointer++; }\n",
|
||||
" if(sz == 0)\n",
|
||||
" {\n",
|
||||
" array = (int*)malloc(2*sizeof(int));\n",
|
||||
" if(!array)\n",
|
||||
" Mock.allocFailure++;\n",
|
||||
" }\n",
|
||||
" else\n",
|
||||
" {\n",
|
||||
" int *ptmp = (int*)realloc(array, sizeof(int) * (sz+1));\n",
|
||||
" if(!ptmp)\n",
|
||||
" Mock.allocFailure++;\n",
|
||||
" else\n",
|
||||
" array = ptmp;\n"," }\n",
|
||||
" memcpy(&array[sz], &new_value, sizeof(int));\n",
|
||||
" arrayTail = &array[sz+1];\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_utils.make_expand_array(the_type, the_array, new_value)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "make handle return" do
|
||||
func_name = "Spatula"
|
||||
func_rettype = "uint64"
|
||||
indent = "[tab]"
|
||||
|
||||
expected = ["\n",
|
||||
"[tab]if(Mock.Spatula_Return != Mock.Spatula_Return_HeadTail)\n",
|
||||
"[tab]{\n",
|
||||
"[tab] uint64 toReturn = *Mock.Spatula_Return;\n",
|
||||
"[tab] Mock.Spatula_Return++;\n",
|
||||
"[tab] return toReturn;\n",
|
||||
"[tab]}\n",
|
||||
"[tab]else\n",
|
||||
"[tab]{\n",
|
||||
"[tab] return *Mock.Spatula_Return_Head;\n",
|
||||
"[tab]}\n"
|
||||
]
|
||||
returned = @cmock_generator_utils.make_handle_return(func_name, func_rettype, indent)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "add new expected handler" do
|
||||
func_name = "PizzaCutter"
|
||||
var_type = "uint16"
|
||||
var_name = "Spork"
|
||||
|
||||
expected = ["\n",
|
||||
" {\n",
|
||||
" int sz = 0;\n",
|
||||
" uint16 *pointer = Mock.PizzaCutter_Expected_Spork_Head;\n",
|
||||
" while(pointer && pointer != Mock.PizzaCutter_Expected_Spork_HeadTail) { sz++; pointer++; }\n",
|
||||
" if(sz == 0)\n",
|
||||
" {\n",
|
||||
" Mock.PizzaCutter_Expected_Spork_Head = (uint16*)malloc(2*sizeof(uint16));\n",
|
||||
" if(!Mock.PizzaCutter_Expected_Spork_Head)\n",
|
||||
" Mock.allocFailure++;\n",
|
||||
" }\n",
|
||||
" else\n",
|
||||
" {\n",
|
||||
" uint16 *ptmp = (uint16*)realloc(Mock.PizzaCutter_Expected_Spork_Head, sizeof(uint16) * (sz+1));\n",
|
||||
" if(!ptmp)\n",
|
||||
" Mock.allocFailure++;\n",
|
||||
" else\n",
|
||||
" Mock.PizzaCutter_Expected_Spork_Head = ptmp;\n",
|
||||
" }\n",
|
||||
" memcpy(&Mock.PizzaCutter_Expected_Spork_Head[sz], &Spork, sizeof(uint16));\n",
|
||||
" Mock.PizzaCutter_Expected_Spork_HeadTail = &Mock.PizzaCutter_Expected_Spork_Head[sz+1];\n",
|
||||
" }\n",
|
||||
" Mock.PizzaCutter_Expected_Spork = Mock.PizzaCutter_Expected_Spork_Head;\n",
|
||||
" Mock.PizzaCutter_Expected_Spork += Mock.PizzaCutter_CallCount;\n"
|
||||
]
|
||||
returned = @cmock_generator_utils.make_add_new_expected(func_name, var_type, var_name)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "make handle expected for non character strings" do
|
||||
func_name = "CanOpener"
|
||||
var_type = "uint16"
|
||||
var_name = "CorkScrew"
|
||||
|
||||
expected = ["\n",
|
||||
" if(Mock.CanOpener_Expected_CorkScrew != Mock.CanOpener_Expected_CorkScrew_HeadTail)\n",
|
||||
" {\n",
|
||||
" uint16* p_expected = Mock.CanOpener_Expected_CorkScrew;\n",
|
||||
" Mock.CanOpener_Expected_CorkScrew++;\n",
|
||||
" TEST_ASSERT_EQUAL_MESSAGE(*p_expected, CorkScrew, \"Function 'CanOpener' called with unexpected value for parameter 'CorkScrew'.\");\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_utils.make_handle_expected(func_name, var_type, var_name)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
|
||||
should "make handle expected for character strings" do
|
||||
func_name = "MeasureCup"
|
||||
var_type = "const char*"
|
||||
var_name = "TeaSpoon"
|
||||
|
||||
expected = ["\n",
|
||||
" if(Mock.MeasureCup_Expected_TeaSpoon != Mock.MeasureCup_Expected_TeaSpoon_HeadTail)\n",
|
||||
" {\n",
|
||||
" const char** p_expected = Mock.MeasureCup_Expected_TeaSpoon;\n",
|
||||
" Mock.MeasureCup_Expected_TeaSpoon++;\n",
|
||||
" TEST_ASSERT_EQUAL_STRING_MESSAGE(*p_expected, TeaSpoon, \"Function 'MeasureCup' called with unexpected string for parameter 'TeaSpoon'.\");\n",
|
||||
" }\n"
|
||||
]
|
||||
returned = @cmock_generator_utils.make_handle_expected(func_name, var_type, var_name)
|
||||
assert_equal(expected, returned)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user