From 14242b65a0e1342a0be47ffefcc41ecb5459b329 Mon Sep 17 00:00:00 2001 From: mvandervoord Date: Sat, 16 Aug 2008 20:58:19 +0000 Subject: [PATCH] starting unit testing of generator git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@22 bf332499-1b4d-0410-844d-d2d48d5cc64c --- lib/cmock_generator.rb | 11 +- lib/cmock_generator_plugin_cexception.rb | 2 + lib/cmock_generator_plugin_expect.rb | 7 +- lib/cmock_generator_plugin_ignore.rb | 2 + lib/cmock_generator_utils.rb | 2 + .../cmock_generator_plugin_cexception_test.rb | 19 +++ .../cmock_generator_plugin_expect_test.rb | 136 ++++++++++++++++++ .../cmock_generator_plugin_ignore_test.rb | 19 +++ test/unit/cmock_generator_test.rb | 32 +++++ test/unit/cmock_generator_utils_test.rb | 18 +++ test/unit/thing_test.rb | 29 ---- 11 files changed, 241 insertions(+), 36 deletions(-) create mode 100644 test/unit/cmock_generator_plugin_cexception_test.rb create mode 100644 test/unit/cmock_generator_plugin_expect_test.rb create mode 100644 test/unit/cmock_generator_plugin_ignore_test.rb create mode 100644 test/unit/cmock_generator_test.rb create mode 100644 test/unit/cmock_generator_utils_test.rb delete mode 100644 test/unit/thing_test.rb diff --git a/lib/cmock_generator.rb b/lib/cmock_generator.rb index 1f0e4d0..a50a76b 100644 --- a/lib/cmock_generator.rb +++ b/lib/cmock_generator.rb @@ -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] diff --git a/lib/cmock_generator_plugin_cexception.rb b/lib/cmock_generator_plugin_cexception.rb index 2a0a8c5..02b3a51 100644 --- a/lib/cmock_generator_plugin_cexception.rb +++ b/lib/cmock_generator_plugin_cexception.rb @@ -1,6 +1,8 @@ class CMockGeneratorPluginCException + attr_reader :config, :utils, :tab + def initialize(config, utils) @config = config @tab = @config.tab diff --git a/lib/cmock_generator_plugin_expect.rb b/lib/cmock_generator_plugin_expect.rb index 59493ee..4f98178 100644 --- a/lib/cmock_generator_plugin_expect.rb +++ b/lib/cmock_generator_plugin_expect.rb @@ -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" diff --git a/lib/cmock_generator_plugin_ignore.rb b/lib/cmock_generator_plugin_ignore.rb index af596db..a01ba19 100644 --- a/lib/cmock_generator_plugin_ignore.rb +++ b/lib/cmock_generator_plugin_ignore.rb @@ -1,6 +1,8 @@ class CMockGeneratorPluginIgnore + attr_reader :config, :utils, :tab + def initialize(config, utils) @config = config @tab = @config.tab diff --git a/lib/cmock_generator_utils.rb b/lib/cmock_generator_utils.rb index be6afba..45cb6b4 100644 --- a/lib/cmock_generator_utils.rb +++ b/lib/cmock_generator_utils.rb @@ -1,6 +1,8 @@ class CMockGeneratorUtils + attr_reader :config, :tab + def initialize(config) @config = config @tab = @config.tab diff --git a/test/unit/cmock_generator_plugin_cexception_test.rb b/test/unit/cmock_generator_plugin_cexception_test.rb new file mode 100644 index 0000000..164b5cd --- /dev/null +++ b/test/unit/cmock_generator_plugin_cexception_test.rb @@ -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 diff --git a/test/unit/cmock_generator_plugin_expect_test.rb b/test/unit/cmock_generator_plugin_expect_test.rb new file mode 100644 index 0000000..4964fa5 --- /dev/null +++ b/test/unit/cmock_generator_plugin_expect_test.rb @@ -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 diff --git a/test/unit/cmock_generator_plugin_ignore_test.rb b/test/unit/cmock_generator_plugin_ignore_test.rb new file mode 100644 index 0000000..14ea398 --- /dev/null +++ b/test/unit/cmock_generator_plugin_ignore_test.rb @@ -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 diff --git a/test/unit/cmock_generator_test.rb b/test/unit/cmock_generator_test.rb new file mode 100644 index 0000000..0a0abd0 --- /dev/null +++ b/test/unit/cmock_generator_test.rb @@ -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 diff --git a/test/unit/cmock_generator_utils_test.rb b/test/unit/cmock_generator_utils_test.rb new file mode 100644 index 0000000..54176a2 --- /dev/null +++ b/test/unit/cmock_generator_utils_test.rb @@ -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 diff --git a/test/unit/thing_test.rb b/test/unit/thing_test.rb deleted file mode 100644 index cfa07b6..0000000 --- a/test/unit/thing_test.rb +++ /dev/null @@ -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