starting unit testing of generator

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@22 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mvandervoord
2008-08-16 20:58:19 +00:00
parent 1bc8f35708
commit 14242b65a0
11 changed files with 241 additions and 36 deletions
+6 -5
View File
@@ -2,6 +2,8 @@ $here = File.dirname __FILE__
class CMockGenerator
attr_reader :config, :file_writer, :tab, :module_name, :mock_name, :utils, :plugins
def initialize(config, module_name, file_writer, utils, plugins=[])
@config = config
@file_writer = file_writer
@@ -16,7 +18,9 @@ class CMockGenerator
create_mock_header_file(parsed_stuff)
create_mock_source_file(parsed_stuff)
end
private ##############################
def create_mock_header_file(parsed_stuff)
@file_writer.create_file(@mock_name + ".h") do |file, filename|
create_mock_header_header(file, filename)
@@ -43,8 +47,6 @@ class CMockGenerator
end
end
private ##############################
def create_mock_header_header(file, filename)
define_name = filename.gsub(/\.h/, "_h").upcase
orig_filename = filename.gsub("Mock", "")
@@ -132,8 +134,7 @@ class CMockGenerator
file << "}\n\n"
end
def create_mock_implementation(file, function)
def create_mock_implementation(file, function)
# create return value combo
if (function[:modifier].empty?)
function_mod_and_rettype = function[:rettype]
+2
View File
@@ -1,6 +1,8 @@
class CMockGeneratorPluginCException
attr_reader :config, :utils, :tab
def initialize(config, utils)
@config = config
@tab = @config.tab
+5 -2
View File
@@ -1,6 +1,8 @@
class CMockGeneratorPluginExpect
attr_reader :config, :utils, :tab
def initialize(config, utils)
@config = config
@tab = @config.tab
@@ -12,9 +14,10 @@ class CMockGeneratorPluginExpect
end
def instance_structure(function_name, function_args_as_array, function_return_type)
call_count_type = @config.call_count_type
lines = []
lines << "#{@tab}#{@config.call_count_type} #{function_name}_CallCount;\n"
lines << "#{@tab}#{@config.call_count_type} #{function_name}_CallsExpected;\n"
lines << "#{@tab}#{call_count_type} #{function_name}_CallCount;\n"
lines << "#{@tab}#{call_count_type} #{function_name}_CallsExpected;\n"
if (function_return_type != "void")
lines << "#{@tab}#{function_return_type} *#{function_name}_Return;\n"
+2
View File
@@ -1,6 +1,8 @@
class CMockGeneratorPluginIgnore
attr_reader :config, :utils, :tab
def initialize(config, utils)
@config = config
@tab = @config.tab
+2
View File
@@ -1,6 +1,8 @@
class CMockGeneratorUtils
attr_reader :config, :tab
def initialize(config)
@config = config
@tab = @config.tab
@@ -0,0 +1,19 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_generator_plugin_cexception"
class CMockGeneratorPluginCExceptionTest < Test::Unit::TestCase
def setup
create_mocks :config, :utils
@config.expect.tab.returns(" ")
@cmock_generator_plugin_cexception = CMockGeneratorPluginCException.new(@config, @utils)
end
def teardown
end
should "have set up internal accessors correctly on init" do
assert_equal(@config, @cmock_generator_plugin_cexception.config)
assert_equal(@utils, @cmock_generator_plugin_cexception.utils)
assert_equal(" ", @cmock_generator_plugin_cexception.tab)
end
end
@@ -0,0 +1,136 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_generator_plugin_expect"
class CMockGeneratorPluginExpectTest < Test::Unit::TestCase
def setup
create_mocks :config, :utils
@config.expect.tab.returns(" ")
@cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils)
end
def teardown
end
should "have set up internal accessors correctly on init" do
assert_equal(@config, @cmock_generator_plugin_expect.config)
assert_equal(@utils, @cmock_generator_plugin_expect.utils)
assert_equal(" ", @cmock_generator_plugin_expect.tab)
end
should "not include any additional include files" do
assert_equal([], @cmock_generator_plugin_expect.include_files)
end
should "add to control structure mock needs of functions of style 'void func(void)'" do
function_name = "Oak"
function_args_as_array = []
function_return_type = "void"
count_type = "uint32"
@config.expect.call_count_type.returns(count_type)
expected = [" #{count_type} #{function_name}_CallCount;\n",
" #{count_type} #{function_name}_CallsExpected;\n"
]
returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'int func(void)'" do
function_name = "Elm"
function_args_as_array = []
function_return_type = "int"
count_type = "int16"
@config.expect.call_count_type.returns(count_type)
expected = [" #{count_type} #{function_name}_CallCount;\n",
" #{count_type} #{function_name}_CallsExpected;\n",
" #{function_return_type} *#{function_name}_Return;\n",
" #{function_return_type} *#{function_name}_Return_Head;\n",
" #{function_return_type} *#{function_name}_Return_HeadTail;\n"
]
returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'void func(int chicken, char* pork)'" do
function_name = "Cedar"
function_args_as_array = [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}]
function_return_type = "void"
count_type = "unsigned char"
@config.expect.call_count_type.returns(count_type)
expected = [" #{count_type} #{function_name}_CallCount;\n",
" #{count_type} #{function_name}_CallsExpected;\n",
" int *#{function_name}_Expected_chicken;\n",
" int *#{function_name}_Expected_chicken_Head;\n",
" int *#{function_name}_Expected_chicken_HeadTail;\n",
" char* *#{function_name}_Expected_pork;\n",
" char* *#{function_name}_Expected_pork_Head;\n",
" char* *#{function_name}_Expected_pork_HeadTail;\n"
]
returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add to control structure mock needs of functions of style 'int func(float beef)'" do
function_name = "Birch"
function_args_as_array = [{ :name => "beef", :type => "float"}]
function_return_type = "int"
count_type = "unsigned int"
@config.expect.call_count_type.returns(count_type)
expected = [" #{count_type} #{function_name}_CallCount;\n",
" #{count_type} #{function_name}_CallsExpected;\n",
" #{function_return_type} *#{function_name}_Return;\n",
" #{function_return_type} *#{function_name}_Return_Head;\n",
" #{function_return_type} *#{function_name}_Return_HeadTail;\n",
" float *#{function_name}_Expected_beef;\n",
" float *#{function_name}_Expected_beef_Head;\n",
" float *#{function_name}_Expected_beef_HeadTail;\n",
]
returned = @cmock_generator_plugin_expect.instance_structure(function_name, function_args_as_array, function_return_type)
assert_equal(expected, returned)
end
should "add mock function declaration for functions of style 'void func(void)'" do
function_name = "Maple"
function_args = "void"
function_return_type = "void"
expected = "void #{function_name}_Expect(#{function_args});\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type)
assert_equal(expected, returned)
end
should "add mock function declaration for functions of style 'int func(void)'" do
function_name = "Spruce"
function_args = "void"
function_return_type = "int"
expected = "void #{function_name}_ExpectAndReturn(#{function_return_type} toReturn);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type)
assert_equal(expected, returned)
end
should "add mock function declaration for functions of style 'const char* func(int tofu)'" do
function_name = "Pine"
function_args = "int tofu"
function_return_type = "const char*"
expected = "void #{function_name}_ExpectAndReturn(#{function_args}, #{function_return_type} toReturn);\n"
returned = @cmock_generator_plugin_expect.mock_function_declarations(function_name, function_args, function_return_type)
assert_equal(expected, returned)
end
should "not require anything for implementation prefix" do
function_name = "Ash"
function_return_type = "int"
assert_equal([], @cmock_generator_plugin_expect.mock_implementation_prefix(function_name, function_return_type))
end
####NEXT UP: Mock Implementation
end
@@ -0,0 +1,19 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_generator_plugin_ignore"
class CMockGeneratorPluginIgnoreTest < Test::Unit::TestCase
def setup
create_mocks :config, :utils
@config.expect.tab.returns(" ")
@cmock_generator_plugin_ignore = CMockGeneratorPluginIgnore.new(@config, @utils)
end
def teardown
end
should "have set up internal accessors correctly on init" do
assert_equal(@config, @cmock_generator_plugin_ignore.config)
assert_equal(@utils, @cmock_generator_plugin_ignore.utils)
assert_equal(" ", @cmock_generator_plugin_ignore.tab)
end
end
+32
View File
@@ -0,0 +1,32 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_generator"
class CMockGeneratorTest < Test::Unit::TestCase
def setup
create_mocks :config, :file_writer, :utils, :plugins
@module_name = "PoutPoutFish"
@config.expect.tab.returns(" ")
@cmock_generator = CMockGenerator.new(@config, @module_name, @file_writer, @utils, @plugins)
end
def teardown
end
should "have set up internal accessors correctly on init" do
assert_equal(@config, @cmock_generator.config)
assert_equal(@module_name, @cmock_generator.module_name)
assert_equal(@file_writer, @cmock_generator.file_writer)
assert_equal(@utils, @cmock_generator.utils)
assert_equal(@plugins, @cmock_generator.plugins)
assert_equal("Mock#{@module_name}", @cmock_generator.mock_name)
assert_equal(" ", @cmock_generator.tab)
end
#should "create a very basic pair of files when there was no parsed stuff" do
# test_file_handle = 5
# @file_writer.expect.create_file("Mock#{@module_name}.h").returns([test_file_handle, "MockTurtle.h"])
# parsed_stuff = { :functions => [], :includes => [], :externs => [] }
# @cmock_generator.create_mock(parsed_stuff)
#end
end
+18
View File
@@ -0,0 +1,18 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
require File.expand_path(File.dirname(__FILE__)) + "/../../lib/cmock_generator_utils"
class CMockGeneratorUtilsTest < Test::Unit::TestCase
def setup
create_mocks :config
@config.expect.tab.returns(" ")
@cmock_generator_utils = CMockGeneratorUtils.new(@config)
end
def teardown
end
should "have set up internal accessors correctly on init" do
assert_equal(@config, @cmock_generator_utils.config)
assert_equal(" ", @cmock_generator_utils.tab)
end
end
-29
View File
@@ -1,29 +0,0 @@
require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
class Thing
def initialize foo, bar
@foo = foo
@bar = bar
end
def snafu
return @foo.val + @bar.val
end
end
class ThingTest < Test::Unit::TestCase
def setup
create_mocks :foo, :bar
@thing = Thing.new(@foo, @bar)
end
def teardown
end
should "perform a stupid simple test" do
@foo.expect.val.returns(1)
@bar.expect.val.returns(2)
assert_equal(3, @thing.snafu)
end
end