mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-08-21 10:39:58 +00:00
continuing to dissect generator; this time it's ignores that are removed from the core
git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@18 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
+11
-70
@@ -1,6 +1,7 @@
|
||||
$here = File.dirname __FILE__
|
||||
|
||||
require "#{$here}/cmock_generator_utils.rb"
|
||||
require "#{$here}/cmock_generator_plugin_ignore.rb"
|
||||
require "#{$here}/cmock_generator_plugin_cexception.rb"
|
||||
|
||||
class CMockGenerator
|
||||
@@ -17,6 +18,7 @@ class CMockGenerator
|
||||
@utils = CMockGeneratorUtils.new( @config )
|
||||
@plugins = []
|
||||
@plugins << CMockGeneratorPluginCException.new( @config, @utils ) if @config.use_cexception
|
||||
@plugins << CMockGeneratorPluginIgnore.new( @config, @utils ) if @config.allow_ignore_mock
|
||||
end
|
||||
|
||||
def create_mock(parsed_stuff)
|
||||
@@ -50,8 +52,7 @@ class CMockGenerator
|
||||
parsed_stuff[:functions].each do |function|
|
||||
create_mock_implementation(file, function)
|
||||
create_mock_function_expectation(file, function)
|
||||
@plugins.each { |plugin| file << plugin.mock_interfaces( function[:name], function[:args_string_without_varargs], function[:args]) }
|
||||
create_mock_function_ignore(file, function) if (@config.allow_ignore_mock)
|
||||
@plugins.each { |plugin| file << plugin.mock_interfaces( function[:name], function[:args_string_without_varargs], function[:args], function[:rettype]) }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -95,15 +96,8 @@ class CMockGenerator
|
||||
end
|
||||
end
|
||||
|
||||
@plugins.each { |plugin| header << plugin.mock_function_declarations(function[:name], function[:args_string_without_varargs]) }
|
||||
@plugins.each { |plugin| header << plugin.mock_function_declarations(function[:name], function[:args_string_without_varargs], function[:rettype]) }
|
||||
|
||||
if (@config.allow_ignore_mock)
|
||||
if (function[:args].empty?)
|
||||
header << "void #{function[:name]}_Ignore(void);\n"
|
||||
else
|
||||
header << "void #{function[:name]}_IgnoreAndReturn(#{function[:rettype]} toReturn);\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_mock_header_footer(header)
|
||||
@@ -117,7 +111,7 @@ class CMockGenerator
|
||||
file << "#include <stdlib.h>\n"
|
||||
file << "#include <setjmp.h>\n"
|
||||
file << "#include \"unity.h\"\n"
|
||||
file << "#include \"Exception.h\"\n" if (@config.use_cexception) ####MSV : REMOVE ME
|
||||
@plugins.each { |plugin| file << plugin.include_files }
|
||||
|
||||
#(@config.includes + include_files).uniq.each {|include| file << "#include \"#{include}\"\n"} #### MSV This is what the comments said in original version, but it wasnt actually doing this
|
||||
@config.includes.each {|include| file << "#include \"#{include}\"\n"}
|
||||
@@ -138,12 +132,9 @@ class CMockGenerator
|
||||
|
||||
file << "#{@tab}#{@config.call_count_type} #{function[:name]}_CallCount;\n"
|
||||
file << "#{@tab}#{@config.call_count_type} #{function[:name]}_CallsExpected;\n"
|
||||
|
||||
if (@config.allow_ignore_mock)
|
||||
file << "#{@tab}#{@config.ignore_bool_type} #{function[:name]}_IgnoreBool;\n"
|
||||
end
|
||||
|
||||
|
||||
@plugins.each { |plugin| file << plugin.instance_structure( function[:name] ) }
|
||||
@plugins.each { |plugin| file << plugin.instance_structure( function[:name] ) }
|
||||
|
||||
if (function[:rettype] != "void")
|
||||
file << "#{@tab}#{function[:rettype]} *#{function[:name]}_Return;\n"
|
||||
@@ -240,12 +231,7 @@ class CMockGenerator
|
||||
file << "#{function_mod_and_rettype} #{function[:name]}(#{function[:args_string]})\n"
|
||||
file << "{\n"
|
||||
|
||||
# start ignore block
|
||||
if (@config.allow_ignore_mock)
|
||||
newtab = "#{@tab}#{@tab}"
|
||||
file << "#{@tab}if (!Mock.#{function[:name]}_IgnoreBool)\n"
|
||||
file << "#{@tab}{\n"
|
||||
end
|
||||
@plugins.each { |plugin| file << plugin.mock_implementation_prefix(function[:name], function[:rettype]) }
|
||||
|
||||
file << "#{newtab}Mock.#{function[:name]}_CallCount++;\n"
|
||||
|
||||
@@ -261,16 +247,11 @@ class CMockGenerator
|
||||
file << "#{newtab}AssertParameters_#{function[:name]}(#{@utils.create_call_list(function[:args])});\n"
|
||||
end
|
||||
|
||||
@plugins.each { |plugin| file << plugin.mock_implementation(function[:name]) }
|
||||
|
||||
# end ignore block
|
||||
if (@config.allow_ignore_mock)
|
||||
file << "#{@tab}}\n"
|
||||
end
|
||||
@plugins.each { |plugin| file << plugin.mock_implementation(function[:name]) }
|
||||
|
||||
# Return expected value, if necessary
|
||||
if function[:rettype] != "void"
|
||||
file << make_handle_return(function)
|
||||
if (function[:rettype] != "void")
|
||||
file << @utils.make_handle_return(function[:name], function[:rettype], "#{@tab}")
|
||||
end
|
||||
|
||||
# Close out the function
|
||||
@@ -325,46 +306,6 @@ class CMockGenerator
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_mock_function_ignore(file, function)
|
||||
# Function has void return type with no arguments
|
||||
if function[:rettype] == "void"
|
||||
file << "void #{function[:name]}_Ignore(void)\n"
|
||||
file << "{\n"
|
||||
file << "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n"
|
||||
file << "}\n\n"
|
||||
# Function has non-void return type with no arguments
|
||||
else
|
||||
file << "void #{function[:name]}_IgnoreAndReturn(#{function[:rettype]} toReturn)\n"
|
||||
file << "{\n"
|
||||
file << "#{@tab}Mock.#{function[:name]}_IgnoreBool = (unsigned char)1;\n"
|
||||
file << @utils.make_expand_array(function[:rettype], "Mock.#{function[:name]}_Return_Head", "toReturn")
|
||||
file << "#{@tab}Mock.#{function[:name]}_Return = Mock.#{function[:name]}_Return_Head;\n"
|
||||
file << "#{@tab}Mock.#{function[:name]}_Return += Mock.#{function[:name]}_CallCount;\n"
|
||||
file << "}\n\n"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
def make_handle_return(function)
|
||||
|
||||
method = function[:name]
|
||||
return_block = <<EOS
|
||||
|
||||
#{@tab}if(Mock.#{method}_Return != Mock.#{method}_Return_HeadTail)
|
||||
#{@tab}{
|
||||
#{@tab}#{@tab}#{function[:rettype]} toReturn = *Mock.#{method}_Return;
|
||||
#{@tab}#{@tab}Mock.#{method}_Return++;
|
||||
#{@tab}#{@tab}return toReturn;
|
||||
#{@tab}}
|
||||
#{@tab}else
|
||||
#{@tab}{
|
||||
#{@tab}#{@tab}return *Mock.#{method}_Return_Head;
|
||||
#{@tab}}
|
||||
EOS
|
||||
return return_block
|
||||
end
|
||||
|
||||
def make_add_new_expected(function, type, expected)
|
||||
method = function[:name]
|
||||
|
||||
@@ -7,6 +7,10 @@ class CMockGeneratorPluginCException
|
||||
@utils = utils
|
||||
end
|
||||
|
||||
def include_files
|
||||
return "#include \"Exception.h\"\n"
|
||||
end
|
||||
|
||||
def instance_structure(function_name)
|
||||
lines = []
|
||||
lines << "#{@tab}#{@config.call_count_type} *#{function_name}_ThrowOnCallCount;\n"
|
||||
@@ -17,7 +21,7 @@ class CMockGeneratorPluginCException
|
||||
lines << "#{@tab}#{@config.throw_type} *#{function_name}_ThrowValue_HeadTail;\n"
|
||||
end
|
||||
|
||||
def mock_function_declarations(function_name, function_args)
|
||||
def mock_function_declarations(function_name, function_args, function_return_type)
|
||||
if (function_args == "void")
|
||||
return "void #{function_name}_ExpectAndThrow(int toThrow);\n"
|
||||
else
|
||||
@@ -25,6 +29,10 @@ class CMockGeneratorPluginCException
|
||||
end
|
||||
end
|
||||
|
||||
def mock_implementation_prefix(function_name, function_return_type)
|
||||
[]
|
||||
end
|
||||
|
||||
def mock_implementation(function_name)
|
||||
lines = ["\n"]
|
||||
lines << "#{@tab}if((Mock.#{function_name}_ThrowOnCallCount != Mock.#{function_name}_ThrowOnCallCount_HeadTail) &&\n"
|
||||
@@ -41,7 +49,7 @@ class CMockGeneratorPluginCException
|
||||
lines << "#{@tab}}\n"
|
||||
end
|
||||
|
||||
def mock_interfaces(function_name, function_args, function_args_as_array)
|
||||
def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
|
||||
arg_insert = (function_args == "void") ? "" : "#{function_args}, "
|
||||
lines = []
|
||||
lines << "void #{function_name}_ExpectAndThrow(#{arg_insert}#{@config.throw_type} toThrow)\n"
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
|
||||
class CMockGeneratorPluginIgnore
|
||||
|
||||
def initialize(config, utils)
|
||||
@config = config
|
||||
@tab = @config.tab
|
||||
@utils = utils
|
||||
end
|
||||
|
||||
def include_files
|
||||
[]
|
||||
end
|
||||
|
||||
def instance_structure(function_name)
|
||||
return "#{@tab}#{@config.ignore_bool_type} #{function_name}_IgnoreBool;\n"
|
||||
end
|
||||
|
||||
def mock_function_declarations(function_name, function_args, function_return_type)
|
||||
if (function_args == "void")
|
||||
return "void #{function_name}_Ignore(void);\n"
|
||||
else
|
||||
return "void #{function_name}_IgnoreAndReturn(#{function_return_type} toReturn);\n"
|
||||
end
|
||||
end
|
||||
|
||||
def mock_implementation_prefix(function_name, function_return_type)
|
||||
lines = []
|
||||
lines << "#{@tab}if (!Mock.#{function_name}_IgnoreBool)\n"
|
||||
lines << "#{@tab}{\n"
|
||||
lines << @utils.make_handle_return(function_name, function_return_type, "#{@tab}#{@tab}")
|
||||
lines << "#{@tab}}\n"
|
||||
end
|
||||
|
||||
def mock_implementation(function_name)
|
||||
[]
|
||||
end
|
||||
|
||||
def mock_interfaces(function_name, function_args, function_args_as_array, function_return_type)
|
||||
lines = []
|
||||
if (function_return_type == "void")
|
||||
lines << "void #{function_name}_Ignore(void)\n"
|
||||
lines << "{\n"
|
||||
lines << "#{@tab}Mock.#{function_name}_IgnoreBool = (unsigned char)1;\n"
|
||||
lines << "}\n\n"
|
||||
else
|
||||
lines << "void #{function_name}_IgnoreAndReturn(#{function_return_type} toReturn)\n"
|
||||
lines << "{\n"
|
||||
lines << "#{@tab}Mock.#{function_name}_IgnoreBool = (unsigned char)1;\n"
|
||||
lines << @utils.make_expand_array(function_return_type, "Mock.#{function_name}_Return_Head", "toReturn")
|
||||
lines << "#{@tab}Mock.#{function_name}_Return = Mock.#{function_name}_Return_Head;\n"
|
||||
lines << "#{@tab}Mock.#{function_name}_Return += Mock.#{function_name}_CallCount;\n"
|
||||
lines << "}\n\n"
|
||||
end
|
||||
return lines
|
||||
end
|
||||
|
||||
def mock_destroy(function_name)
|
||||
[]
|
||||
end
|
||||
end
|
||||
@@ -42,4 +42,18 @@ class CMockGeneratorUtils
|
||||
lines << "#{@tab}#{@tab}#{array}Tail = &#{array}[sz+1];\n"
|
||||
lines << "#{@tab}}\n"
|
||||
end
|
||||
|
||||
def make_handle_return(function_name, function_return_type, indent)
|
||||
lines = ["\n"]
|
||||
lines << "#{indent}if(Mock.#{function_name}_Return != Mock.#{function_name}_Return_HeadTail)\n"
|
||||
lines << "#{indent}{\n"
|
||||
lines << "#{indent}#{@tab}#{function_return_type} toReturn = *Mock.#{function_name}_Return;\n"
|
||||
lines << "#{indent}#{@tab}Mock.#{function_name}_Return++;\n"
|
||||
lines << "#{indent}#{@tab}return toReturn;\n"
|
||||
lines << "#{indent}}\n"
|
||||
lines << "#{indent}else\n"
|
||||
lines << "#{indent}{\n"
|
||||
lines << "#{indent}#{@tab}return *Mock.#{function_name}_Return_Head;\n"
|
||||
lines << "#{indent}}\n"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user