cexception plugin tested

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@24 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2008-09-15 01:42:33 +00:00
parent 844f0ebbf9
commit eb4b7f957d
2 changed files with 177 additions and 10 deletions
+14 -10
View File
@@ -15,17 +15,19 @@ class CMockGeneratorPluginCException
def instance_structure(function_name, function_args_as_array, function_return_type)
lines = []
lines << "#{@tab}#{@config.call_count_type} *#{function_name}_ThrowOnCallCount;\n"
lines << "#{@tab}#{@config.call_count_type} *#{function_name}_ThrowOnCallCount_Head;\n"
lines << "#{@tab}#{@config.call_count_type} *#{function_name}_ThrowOnCallCount_HeadTail;\n"
lines << "#{@tab}#{@config.throw_type} *#{function_name}_ThrowValue;\n"
lines << "#{@tab}#{@config.throw_type} *#{function_name}_ThrowValue_Head;\n"
lines << "#{@tab}#{@config.throw_type} *#{function_name}_ThrowValue_HeadTail;\n"
call_count_type = @config.call_count_type
throw_type = @config.throw_type
lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount;\n"
lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount_Head;\n"
lines << "#{@tab}#{call_count_type} *#{function_name}_ThrowOnCallCount_HeadTail;\n"
lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue;\n"
lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue_Head;\n"
lines << "#{@tab}#{throw_type} *#{function_name}_ThrowValue_HeadTail;\n"
end
def mock_function_declarations(function_name, function_args, function_return_type)
if (function_args == "void")
return "void #{function_name}_ExpectAndThrow(int toThrow);\n"
return "void #{function_name}_ExpectAndThrow(#{@config.throw_type} toThrow);\n"
else
return "void #{function_name}_ExpectAndThrow(#{function_args}, #{@config.throw_type} toThrow);\n"
end
@@ -53,14 +55,16 @@ class CMockGeneratorPluginCException
def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
arg_insert = (function_args == "void") ? "" : "#{function_args}, "
call_count_type = @config.call_count_type
throw_type = @config.throw_type
lines = []
lines << "void #{function_name}_ExpectAndThrow(#{arg_insert}#{@config.throw_type} toThrow)\n"
lines << "void #{function_name}_ExpectAndThrow(#{arg_insert}#{throw_type} toThrow)\n"
lines << "{\n"
lines << "#{@tab}Mock.#{function_name}_CallsExpected++;\n"
lines << @utils.make_expand_array(@config.call_count_type, "Mock.#{function_name}_ThrowOnCallCount_Head", "Mock.#{function_name}_CallsExpected")
lines << @utils.make_expand_array(call_count_type, "Mock.#{function_name}_ThrowOnCallCount_Head", "Mock.#{function_name}_CallsExpected")
lines << "#{@tab}Mock.#{function_name}_ThrowOnCallCount = Mock.#{function_name}_ThrowOnCallCount_Head;\n"
lines << "#{@tab}Mock.#{function_name}_ThrowOnCallCount += Mock.#{function_name}_CallCount;\n"
lines << @utils.make_expand_array(@config.throw_type, "Mock.#{function_name}_ThrowValue_Head", "toThrow")
lines << @utils.make_expand_array(throw_type, "Mock.#{function_name}_ThrowValue_Head", "toThrow")
lines << "#{@tab}Mock.#{function_name}_ThrowValue = Mock.#{function_name}_ThrowValue_Head;\n"
lines << "#{@tab}Mock.#{function_name}_ThrowValue += Mock.#{function_name}_CallCount;\n"
lines << "#{@tab}ExpectParameters_#{function_name}(#{@utils.create_call_list(function_args_as_array)});\n" if (function_args != "void")
@@ -16,4 +16,167 @@ class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
assert_equal(@utils, @cmock_generator_plugin_cexception.utils)
assert_equal(" ", @cmock_generator_plugin_cexception.tab)
end
should "include the cexception library" do
expected = "#include \"Exception.h\"\n"
returned = @cmock_generator_plugin_cexception.include_files
assert_equal(expected, returned)
end
should "add to control structure mock needs" do
function_name = "Oak"
function_args_as_array = []
function_return_type = "void"
@config.expect.call_count_type.returns("uint32")
@config.expect.throw_type.returns("EXCEPTION_TYPE")
expected = [" uint32 *Oak_ThrowOnCallCount;\n",
" uint32 *Oak_ThrowOnCallCount_Head;\n",
" uint32 *Oak_ThrowOnCallCount_HeadTail;\n",
" EXCEPTION_TYPE *Oak_ThrowValue;\n",
" EXCEPTION_TYPE *Oak_ThrowValue_Head;\n",
" EXCEPTION_TYPE *Oak_ThrowValue_HeadTail;\n"
]
returned = @cmock_generator_plugin_cexception.instance_structure(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add mock function declarations for functions without arguments" do
function_name = "Spruce"
function_args_as_array = "void"
function_return_type = "void"
@config.expect.throw_type.returns("EXCEPTION_TYPE")
expected = "void Spruce_ExpectAndThrow(EXCEPTION_TYPE toThrow);\n"
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add mock function declarations for functions with arguments" do
function_name = "Spruce"
function_args_as_array = "const char* Petunia, uint32_t Lily"
function_return_type = "void"
@config.expect.throw_type.returns("EXCEPTION_TYPE")
expected = "void Spruce_ExpectAndThrow(const char* Petunia, uint32_t Lily, EXCEPTION_TYPE toThrow);\n"
returned = @cmock_generator_plugin_cexception.mock_function_declarations(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add nothing during implementation prefix" do
function_name = "Pine"
function_return_type = "void"
expected = []
returned = @cmock_generator_plugin_cexception.mock_implementation_prefix(function_name, function_return_type)
assert_equal(expected, returned)
end
should "add a mock implementation" do
function_name = "Cherry"
function_args_as_array = []
function_return_type = "void"
@config.expect.throw_type.returns("EXCEPTION_TYPE")
expected = ["\n",
" if((Mock.Cherry_ThrowOnCallCount != Mock.Cherry_ThrowOnCallCount_HeadTail) &&\n",
" (Mock.Cherry_ThrowValue != Mock.Cherry_ThrowValue_HeadTail))\n",
" {\n",
" if (*Mock.Cherry_ThrowOnCallCount && \n",
" (Mock.Cherry_CallCount == *Mock.Cherry_ThrowOnCallCount))\n",
" {\n",
" EXCEPTION_TYPE toThrow = *Mock.Cherry_ThrowValue;\n",
" Mock.Cherry_ThrowOnCallCount++;\n",
" Mock.Cherry_ThrowValue++;\n",
" Throw(toThrow);\n",
" }\n",
" }\n"
]
returned = @cmock_generator_plugin_cexception.mock_implementation(function_name, function_args_as_array)
assert_equal(expected, returned)
end
should "add a mock interfaces for functions without arguments" do
function_name = "Pear"
function_args = "void"
function_args_as_array = []
function_return_type = "void"
@config.expect.call_count_type.returns("uint32")
@config.expect.throw_type.returns("EXCEPTION_TYPE")
@utils.expect.make_expand_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.make_expand_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2")
expected = ["void Pear_ExpectAndThrow(EXCEPTION_TYPE toThrow)\n",
"{\n",
" Mock.Pear_CallsExpected++;\n",
"mock_return_1",
" Mock.Pear_ThrowOnCallCount = Mock.Pear_ThrowOnCallCount_Head;\n",
" Mock.Pear_ThrowOnCallCount += Mock.Pear_CallCount;\n",
"mock_return_2",
" Mock.Pear_ThrowValue = Mock.Pear_ThrowValue_Head;\n",
" Mock.Pear_ThrowValue += Mock.Pear_CallCount;\n",
"}\n\n"
]
returned = @cmock_generator_plugin_cexception.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add a mock interfaces for functions with arguments" do
function_name = "Pear"
function_args = "int blah"
function_args_as_array = [{ :type => "int", :name => "blah" }]
function_return_type = "void"
@config.expect.call_count_type.returns("uint32")
@config.expect.throw_type.returns("EXCEPTION_TYPE")
@utils.expect.make_expand_array("uint32", "Mock.Pear_ThrowOnCallCount_Head", "Mock.Pear_CallsExpected").returns("mock_return_1")
@utils.expect.make_expand_array("EXCEPTION_TYPE", "Mock.Pear_ThrowValue_Head", "toThrow").returns("mock_return_2")
@utils.expect.create_call_list(function_args_as_array).returns("mock_return_3")
expected = ["void Pear_ExpectAndThrow(int blah, EXCEPTION_TYPE toThrow)\n",
"{\n",
" Mock.Pear_CallsExpected++;\n",
"mock_return_1",
" Mock.Pear_ThrowOnCallCount = Mock.Pear_ThrowOnCallCount_Head;\n",
" Mock.Pear_ThrowOnCallCount += Mock.Pear_CallCount;\n",
"mock_return_2",
" Mock.Pear_ThrowValue = Mock.Pear_ThrowValue_Head;\n",
" Mock.Pear_ThrowValue += Mock.Pear_CallCount;\n",
" ExpectParameters_Pear(mock_return_3);\n",
"}\n\n"
]
returned = @cmock_generator_plugin_cexception.mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "have nothing to say about verifying" do
assert_equal([], @cmock_generator_plugin_cexception.mock_verify("Maple"))
end
should "add necessary baggage to destroy function" do
function_name = "Banana"
function_args_as_array = []
function_return_type = "void"
expected = [" if(Mock.Banana_ThrowOnCallCount_Head)\n",
" {\n",
" free(Mock.Banana_ThrowOnCallCount_Head);\n",
" Mock.Banana_ThrowOnCallCount_Head=NULL;\n",
" Mock.Banana_ThrowOnCallCount_HeadTail=NULL;\n",
" }\n",
" if(Mock.Banana_ThrowValue_Head)\n",
" {\n",
" free(Mock.Banana_ThrowValue_Head);\n",
" Mock.Banana_ThrowValue_Head=NULL;\n",
" Mock.Banana_ThrowValue_HeadTail=NULL;\n",
" }\n"
]
returned = @cmock_generator_plugin_cexception.mock_destroy(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
end