diff options
Diffstat (limited to 'FreeRTOS-Plus/Test/CMock/test/unit')
17 files changed, 5626 insertions, 0 deletions
diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.rb new file mode 100644 index 000000000..8c5e410a7 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.rb @@ -0,0 +1,126 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_config' + + +describe CMockConfig, "Verify CMockConfig Module" do + + it "use default settings when no parameters are specified" do + config = CMockConfig.new + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:mock_path], config.mock_path) + assert_nil(CMockConfig::CMOCK_DEFAULT_OPTIONS[:includes]) + assert_nil(config.includes) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:attributes], config.attributes) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:plugins], config.plugins) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:treat_externs], config.treat_externs) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:treat_inlines], config.treat_inlines) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:inline_function_patterns], config.inline_function_patterns) + end + + it "replace only options specified in a hash" do + test_includes = ['hello'] + test_attributes = ['blah', 'bleh'] + config = CMockConfig.new(:includes => test_includes, :attributes => test_attributes) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:mock_path], config.mock_path) + assert_equal(test_includes, config.includes) + assert_equal(test_attributes, config.attributes) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:plugins], config.plugins) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:treat_externs], config.treat_externs) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:treat_inlines], config.treat_inlines) + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:inline_function_patterns], config.inline_function_patterns) + end + + it "replace only options specified in a yaml file" do + test_plugins = [:soda, :pizza] + config = CMockConfig.new("#{File.expand_path(File.dirname(__FILE__))}/cmock_config_test.yml") + assert_equal(CMockConfig::CMOCK_DEFAULT_OPTIONS[:mock_path], config.mock_path) + assert_nil(CMockConfig::CMOCK_DEFAULT_OPTIONS[:includes]) + assert_nil(config.includes) + assert_equal(test_plugins, config.plugins) + assert_equal(:include, config.treat_externs) + assert_equal(:include, config.treat_inlines) + assert_equal(['MY_INLINE_FUNCTION_DECLARATION_PATTERN'], config.inline_function_patterns) + end + + it "populate treat_as map with internal standard_treat_as_map defaults, redefine defaults, and add custom values" do + + user_treat_as1 = { + 'BOOL' => 'UINT8', # redefine standard default + 'unsigned long' => 'INT', # redefine standard default + 'U8' => 'UINT8', # custom value + 'U16' => 'UINT16' # custom value + } + user_treat_as2 = { + 'BOOL' => 'INT16', # redefine standard default + 'U16' => 'HEX16' # custom value + } + + config1 = CMockConfig.new({:treat_as => user_treat_as1}) + config2 = CMockConfig.new({:treat_as => user_treat_as2}) + + # ----- USER SET 1 + # standard defaults + assert_equal('INT', config1.treat_as['BOOL_T']) + assert_equal('HEX32', config1.treat_as['unsigned int']) + assert_equal('HEX8_ARRAY',config1.treat_as['void*']) + assert_equal('STRING', config1.treat_as['CSTRING']) + assert_equal('STRING', config1.treat_as['char*']) + assert_equal('HEX8', config1.treat_as['unsigned char']) + assert_equal('INT', config1.treat_as['long']) + assert_equal('INT16', config1.treat_as['short']) + + # overrides + assert_equal('UINT8', config1.treat_as['BOOL']) + assert_equal('INT', config1.treat_as['unsigned long']) + + # added custom values + assert_equal('UINT8', config1.treat_as['U8']) + assert_equal('UINT16', config1.treat_as['U16']) + + # standard_treat_as_map: unchanged + assert_equal('INT', config1.standard_treat_as_map['BOOL']) + assert_equal('HEX32', config1.standard_treat_as_map['unsigned long']) + assert_equal('STRING', config1.standard_treat_as_map['char*']) + + # ----- USER SET 2 + # standard defaults + assert_equal('INT', config2.treat_as['BOOL_T']) + assert_equal('HEX32', config2.treat_as['unsigned int']) + assert_equal('HEX8_ARRAY',config2.treat_as['void*']) + assert_equal('STRING', config2.treat_as['CSTRING']) + assert_equal('STRING', config2.treat_as['char*']) + assert_equal('HEX8', config2.treat_as['unsigned char']) + assert_equal('INT', config2.treat_as['long']) + assert_equal('INT16', config2.treat_as['short']) + assert_equal('HEX32', config2.treat_as['unsigned long']) + + # overrides + assert_equal('INT16', config2.treat_as['BOOL']) + + # added custom values + assert_equal('HEX16', config2.treat_as['U16']) + + # standard_treat_as_map: unchanged + assert_equal('INT', config2.standard_treat_as_map['BOOL']) + assert_equal('HEX32', config2.standard_treat_as_map['unsigned long']) + assert_equal('STRING', config2.standard_treat_as_map['char*']) + end + + it "standard treat_as map should be incorruptable" do + config = CMockConfig.new({}) + + assert_equal('INT', config.standard_treat_as_map['BOOL_T']) + + local = config.standard_treat_as_map + local['BOOL_T'] = "U8" + + assert_equal('INT', config.standard_treat_as_map['BOOL_T']) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.yml b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.yml new file mode 100644 index 000000000..eaee14b77 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_config_test.yml @@ -0,0 +1,7 @@ +:cmock: + :plugins: + - 'soda' + - 'pizza' + :treat_externs: :include + :treat_inlines: :include + :inline_function_patterns: ['MY_INLINE_FUNCTION_DECLARATION_PATTERN'] diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_file_writer_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_file_writer_test.rb new file mode 100644 index 000000000..932e7dcc8 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_file_writer_test.rb @@ -0,0 +1,27 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_file_writer' + +describe CMockFileWriter, "Verify CMockFileWriter Module" do + + before do + create_mocks :config + @cmock_file_writer = CMockFileWriter.new(@config) + end + + after do + end + + it "complain if a block was not specified when calling create" do + begin + @cmock_file_writer.create_file("test.txt") + assert false, "Should Have Thrown An Error When Calling Without A Block" + rescue + end + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_main_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_main_test.rb new file mode 100644 index 000000000..846241250 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_main_test.rb @@ -0,0 +1,686 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +$ThisIsOnlyATest = true + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator' + +class MockedPluginHelper + def initialize return_this + @return_this = return_this + end + + def include_files + return @return_this + end + + def instance_structure( name, args, rettype ) + return " #{@return_this}_#{name}(#{args}, #{rettype})" + end + + def mock_verify( name ) + return " #{@return_this}_#{name}" + end + + def mock_destroy( name, args, rettype ) + return " #{@return_this}_#{name}(#{args}, #{rettype})" + end + + def mock_implementation(name, args) + return " Mock#{name}#{@return_this}(#{args.join(", ")})" + end +end + +describe CMockGenerator, "Verify CMockGenerator Module" do + + before do + create_mocks :config, :file_writer, :utils, :plugins + @module_name = "PoutPoutFish" + + #no strict handling + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + @config.expect :enforce_strict_ordering, nil + @config.expect :framework, :unity + @config.expect :includes, ["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"] + #@config.expect :includes_h_pre_orig_header, nil #not called because includes called + @config.expect :includes_h_post_orig_header, nil + @config.expect :includes_c_pre_header, nil + @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil + @config.expect :fail_on_unexpected_calls, true + @config.expect :treat_inlines, :exclude + @cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins) + @cmock_generator.module_name = @module_name + @cmock_generator.module_ext = '.h' + @cmock_generator.mock_name = "Mock#{@module_name}" + @cmock_generator.clean_mock_name = "Mock#{@module_name}" + + #strict handling + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + @config.expect :enforce_strict_ordering, true + @config.expect :framework, :unity + @config.expect :includes, nil + @config.expect :includes_h_pre_orig_header, nil + @config.expect :includes_h_post_orig_header, nil + @config.expect :includes_c_pre_header, nil + @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil + @config.expect :fail_on_unexpected_calls, true + @config.expect :treat_inlines, :exclude + @cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins) + @cmock_generator_strict.module_name = @module_name + @cmock_generator_strict.module_ext = '.h' + @cmock_generator_strict.mock_name = "Mock#{@module_name}" + @cmock_generator_strict.clean_mock_name = "Mock#{@module_name}" + end + + after do + end + + def helper_create_header_top_with_opt_incldues_form_config_and_plugin(ext) + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + @cmock_generator.module_ext = ext + orig_filename = "PoutPoutFish#{ext}" + define_name = "MOCKPOUTPOUTFISH_H" + output = [] + expected = [ + "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", + "#ifndef _#{define_name}\n", + "#define _#{define_name}\n\n", + "#include \"unity.h\"\n", + "#include \"ConfigRequiredHeader1.h\"\n", + "#include \"ConfigRequiredHeader2.h\"\n", + "#include \"#{orig_filename}\"\n", + "#include \"PluginRequiredHeader.h\"\n", + "\n", + "/* Ignore the following warnings, since we are copying code */\n", + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n", + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n", + "#pragma GCC diagnostic push\n", + "#endif\n", + "#if !defined(__clang__)\n", + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n", + "#endif\n", + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n", + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n", + "#endif\n", + "\n", + ] + + @config.expect :orig_header_include_fmt, "#include \"%s\"" + @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] + + @cmock_generator.create_mock_header_header(output, "Mock#{orig_filename}") + + assert_equal(expected, output) + end + + it "create the top of a header file with optional include files from config and include file from plugin" do + ['.h','.hh','.hpp'].each do |ext| + helper_create_header_top_with_opt_incldues_form_config_and_plugin(ext) + end + end + + it "handle dashes and spaces in the module name" do + #no strict handling + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + @config.expect :enforce_strict_ordering, nil + @config.expect :framework, :unity + @config.expect :includes, ["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"] + @config.expect :includes_h_post_orig_header, nil + @config.expect :includes_c_pre_header, nil + @config.expect :includes_c_post_header, nil + @config.expect :subdir, nil + @config.expect :fail_on_unexpected_calls, true + @config.expect :treat_inlines, :exclude + @cmock_generator2 = CMockGenerator.new(@config, @file_writer, @utils, @plugins) + @cmock_generator2.module_name = "Pout-Pout Fish" + @cmock_generator2.module_ext = '.h' + @cmock_generator2.mock_name = "MockPout-Pout Fish" + @cmock_generator2.clean_mock_name = "MockPout_Pout_Fish" + + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + orig_filename = "Pout-Pout Fish.h" + define_name = "MOCKPOUT_POUT_FISH_H" + output = [] + expected = [ + "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", + "#ifndef _#{define_name}\n", + "#define _#{define_name}\n\n", + "#include \"unity.h\"\n", + "#include \"ConfigRequiredHeader1.h\"\n", + "#include \"ConfigRequiredHeader2.h\"\n", + "#include \"#{orig_filename}\"\n", + "#include \"PluginRequiredHeader.h\"\n", + "\n", + "/* Ignore the following warnings, since we are copying code */\n", + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n", + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n", + "#pragma GCC diagnostic push\n", + "#endif\n", + "#if !defined(__clang__)\n", + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n", + "#endif\n", + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n", + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n", + "#endif\n", + "\n", + ] + + @config.expect :orig_header_include_fmt, "#include \"%s\"" + @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] + + @cmock_generator2.create_mock_header_header(output, "MockPout-Pout Fish.h") + + assert_equal(expected, output) + end + + it "create the top of a header file with optional include files from config" do + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + orig_filename = "PoutPoutFish.h" + define_name = "MOCKPOUTPOUTFISH_H" + output = [] + expected = [ + "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", + "#ifndef _#{define_name}\n", + "#define _#{define_name}\n\n", + "#include \"unity.h\"\n", + "#include \"ConfigRequiredHeader1.h\"\n", + "#include \"ConfigRequiredHeader2.h\"\n", + "#include \"#{orig_filename}\"\n", + "\n", + "/* Ignore the following warnings, since we are copying code */\n", + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n", + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n", + "#pragma GCC diagnostic push\n", + "#endif\n", + "#if !defined(__clang__)\n", + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n", + "#endif\n", + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n", + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n", + "#endif\n", + "\n", + ] + + @config.expect :orig_header_include_fmt, "#include \"%s\"" + @plugins.expect :run, '', [:include_files] + + @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") + + assert_equal(expected, output) + end + + it "create the top of a header file with include file from plugin" do + @config.expect :mock_prefix, "Mock" + @config.expect :mock_suffix, "" + @config.expect :weak, "" + orig_filename = "PoutPoutFish.h" + define_name = "MOCKPOUTPOUTFISH_H" + output = [] + expected = [ + "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", + "#ifndef _#{define_name}\n", + "#define _#{define_name}\n\n", + "#include \"unity.h\"\n", + "#include \"ConfigRequiredHeader1.h\"\n", + "#include \"ConfigRequiredHeader2.h\"\n", + "#include \"#{orig_filename}\"\n", + "#include \"PluginRequiredHeader.h\"\n", + "\n", + "/* Ignore the following warnings, since we are copying code */\n", + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n", + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n", + "#pragma GCC diagnostic push\n", + "#endif\n", + "#if !defined(__clang__)\n", + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n", + "#endif\n", + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n", + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n", + "#endif\n", + "\n", + ] + + @config.expect :orig_header_include_fmt, "#include \"%s\"" + @plugins.expect :run, "#include \"PluginRequiredHeader.h\"\n", [:include_files] + + @cmock_generator.create_mock_header_header(output, "MockPoutPoutFish.h") + + assert_equal(expected, output) + end + + it "write typedefs" do + typedefs = [ 'typedef unsigned char U8;', + 'typedef char S8;', + 'typedef unsigned long U32;' + ] + output = [] + expected = [ "\n", + "typedef unsigned char U8;\n", + "typedef char S8;\n", + "typedef unsigned long U32;\n", + "\n\n" + ] + + @cmock_generator.create_typedefs(output, typedefs) + + assert_equal(expected, output.flatten) + end + + it "create the header file service call declarations" do + mock_name = "MockPoutPoutFish" + + output = [] + expected = [ "void #{mock_name}_Init(void);\n", + "void #{mock_name}_Destroy(void);\n", + "void #{mock_name}_Verify(void);\n\n" + ] + + @cmock_generator.create_mock_header_service_call_declarations(output) + + assert_equal(expected, output) + end + + it "append the proper footer to the header file" do + output = [] + expected = ["\n", + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n", + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n", + "#pragma GCC diagnostic pop\n", + "#endif\n", + "#endif\n", + "\n", + "#endif\n" + ] + + @cmock_generator.create_mock_header_footer(output) + + assert_equal(expected, output) + end + + it "create a proper heading for a source file" do + output = [] + functions = [ { :name => "uno", :args => [ { :name => "arg1" }, { :name => "arg2" } ] }, + { :name => "dos", :args => [ { :name => "arg3" }, { :name => "arg2" } ] }, + { :name => "tres", :args => [] } + ] + expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n", + "#include <string.h>\n", + "#include <stdlib.h>\n", + "#include <setjmp.h>\n", + "#include \"cmock.h\"\n", + "#include \"MockPoutPoutFish.h\"\n", + "\n", + "static const char* CMockString_arg1 = \"arg1\";\n", + "static const char* CMockString_arg2 = \"arg2\";\n", + "static const char* CMockString_arg3 = \"arg3\";\n", + "static const char* CMockString_dos = \"dos\";\n", + "static const char* CMockString_tres = \"tres\";\n", + "static const char* CMockString_uno = \"uno\";\n", + "\n" + ] + + @cmock_generator.create_source_header_section(output, "MockPoutPoutFish.c", functions) + + assert_equal(expected, output) + end + + it "create the instance structure where it is needed when no functions" do + output = [] + functions = [] + expected = [ "static struct MockPoutPoutFishInstance\n", + "{\n", + " unsigned char placeHolder;\n", + "} Mock;\n\n" + ].join + + @cmock_generator.create_instance_structure(output, functions) + + assert_equal(expected, output.join) + end + + it "create the instance structure where it is needed when functions required" do + output = [] + functions = [ { :name => "First", :args => "int Candy", :return => test_return[:int] }, + { :name => "Second", :args => "bool Smarty", :return => test_return[:string] } + ] + expected = [ "typedef struct _CMOCK_First_CALL_INSTANCE\n{\n", + " UNITY_LINE_TYPE LineNumber;\n", + " b1 b2", + "\n} CMOCK_First_CALL_INSTANCE;\n\n", + "typedef struct _CMOCK_Second_CALL_INSTANCE\n{\n", + " UNITY_LINE_TYPE LineNumber;\n", + "\n} CMOCK_Second_CALL_INSTANCE;\n\n", + "static struct MockPoutPoutFishInstance\n{\n", + " d1", + " CMOCK_MEM_INDEX_TYPE First_CallInstance;\n", + " e1 e2 e3", + " CMOCK_MEM_INDEX_TYPE Second_CallInstance;\n", + "} Mock;\n\n" + ].join + @plugins.expect :run, [" b1"," b2"], [:instance_typedefs, functions[0]] + @plugins.expect :run, [], [:instance_typedefs, functions[1]] + + @plugins.expect :run, [" d1"], [:instance_structure, functions[0]] + @plugins.expect :run, [" e1"," e2"," e3"], [:instance_structure, functions[1]] + + @cmock_generator.create_instance_structure(output, functions) + + assert_equal(expected, output.join) + end + + it "create extern declarations for source file" do + output = [] + expected = [ "extern jmp_buf AbortFrame;\n", + "\n" ] + + @cmock_generator.create_extern_declarations(output) + + assert_equal(expected, output.flatten) + end + + it "create extern declarations for source file when using strict ordering" do + output = [] + expected = [ "extern jmp_buf AbortFrame;\n", + "extern int GlobalExpectCount;\n", + "extern int GlobalVerifyOrder;\n", + "\n" ] + + @cmock_generator_strict.create_extern_declarations(output) + + assert_equal(expected, output.flatten) + end + + it "create mock verify functions in source file when no functions specified" do + functions = [] + output = [] + expected = "void MockPoutPoutFish_Verify(void)\n{\n}\n\n" + + @cmock_generator.create_mock_verify_function(output, functions) + + assert_equal(expected, output.join) + end + + it "create mock verify functions in source file when extra functions specified" do + functions = [ { :name => "First", :args => "int Candy", :return => test_return[:int] }, + { :name => "Second", :args => "bool Smarty", :return => test_return[:string] } + ] + output = [] + expected = [ "void MockPoutPoutFish_Verify(void)\n{\n", + " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " CMOCK_MEM_INDEX_TYPE call_instance;\n", + " call_instance = Mock.First_CallInstance;\n" + + " Uno_First" + + " Dos_First" + + " call_instance = Mock.Second_CallInstance;\n" + + " Uno_Second" + + " Dos_Second", + "}\n\n" + ] + @plugins.expect :run, [" Uno_First"," Dos_First"], [:mock_verify, functions[0]] + @plugins.expect :run, [" Uno_Second"," Dos_Second"], [:mock_verify, functions[1]] + + @cmock_generator.ordered = true + @cmock_generator.create_mock_verify_function(output, functions) + + assert_equal(expected, output.flatten) + end + + it "create mock init functions in source file" do + output = [] + expected = [ "void MockPoutPoutFish_Init(void)\n{\n", + " MockPoutPoutFish_Destroy();\n", + "}\n\n" + ] + + @cmock_generator.create_mock_init_function(output) + + assert_equal(expected.join, output.join) + end + + it "create mock destroy functions in source file" do + functions = [] + output = [] + expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n", + " CMock_Guts_MemFreeAll();\n", + " memset(&Mock, 0, sizeof(Mock));\n", + "}\n\n" + ] + + @cmock_generator.create_mock_destroy_function(output, functions) + + assert_equal(expected.join, output.join) + end + + it "create mock destroy functions in source file when specified with strict ordering" do + functions = [ { :name => "First", :args => "int Candy", :return => test_return[:int] }, + { :name => "Second", :args => "bool Smarty", :return => test_return[:string] } + ] + output = [] + expected = [ "void MockPoutPoutFish_Destroy(void)\n{\n", + " CMock_Guts_MemFreeAll();\n", + " memset(&Mock, 0, sizeof(Mock));\n", + " uno", + " GlobalExpectCount = 0;\n", + " GlobalVerifyOrder = 0;\n", + "}\n\n" + ] + @plugins.expect :run, [], [:mock_destroy, functions[0]] + @plugins.expect :run, [" uno"], [:mock_destroy, functions[1]] + + @cmock_generator_strict.create_mock_destroy_function(output, functions) + + assert_equal(expected.join, output.join) + end + + it "create mock implementation functions in source file" do + function = { :modifier => "static", + :return => test_return[:int], + :args_string => "uint32 sandwiches, const char* named", + :args => ["uint32 sandwiches", "const char* named"], + :var_arg => nil, + :name => "SupaFunction", + :unscoped_name => "SupaFunction", + :namespace => [], + :class => nil, + :attributes => "__inline" + } + output = [] + expected = [ "static int SupaFunction(uint32 sandwiches, const char* named)\n", + "{\n", + " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance;\n", + " UNITY_SET_DETAIL(CMockString_SupaFunction);\n", + " cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", + " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", + " uno", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", + " cmock_line = cmock_call_instance->LineNumber;\n", + " dos", + " tres", + " UNITY_CLR_DETAILS();\n", + " return cmock_call_instance->ReturnVal;\n", + "}\n\n" + ] + @plugins.expect :run, [" uno"], [:mock_implementation_precheck, function] + @plugins.expect :run, [" dos"," tres"], [:mock_implementation, function] + + @cmock_generator.create_mock_implementation(output, function) + + assert_equal(expected.join, output.join) + end + + it "create mock implementation functions in source file with different options" do + function = { :modifier => "", + :c_calling_convention => "__stdcall", + :return => test_return[:int], + :args_string => "uint32 sandwiches", + :args => ["uint32 sandwiches"], + :var_arg => "corn ...", + :name => "SupaFunction", + :unscoped_name => "SupaFunction", + :namespace => [], + :class => nil, + :attributes => nil + } + output = [] + expected = [ "int __stdcall SupaFunction(uint32 sandwiches, corn ...)\n", + "{\n", + " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " CMOCK_SupaFunction_CALL_INSTANCE* cmock_call_instance;\n", + " UNITY_SET_DETAIL(CMockString_SupaFunction);\n", + " cmock_call_instance = (CMOCK_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaFunction_CallInstance);\n", + " Mock.SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaFunction_CallInstance);\n", + " uno", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", + " cmock_line = cmock_call_instance->LineNumber;\n", + " dos", + " tres", + " UNITY_CLR_DETAILS();\n", + " return cmock_call_instance->ReturnVal;\n", + "}\n\n" + ] + @plugins.expect :run, [" uno"], [:mock_implementation_precheck, function] + @plugins.expect :run, [" dos"," tres"], [:mock_implementation, function] + + @cmock_generator.create_mock_implementation(output, function) + + assert_equal(expected.join, output.join) + end + + it "create mock implementation function in source file for C++ static member" do + function = { :modifier => "static", + :return => test_return[:int], + :args_string => "uint32 sandwiches, const char* named", + :args => ["uint32 sandwiches", "const char* named"], + :var_arg => nil, + :unscoped_name => "SupaFunction", + :namespace => ["ns1", "ns2"], + :class => "SupaClass", + :name => "ns1_ns2_SupaClass_SupaFunction", + :attributes => nil + } + output = [] + expected = [ "namespace ns1 {\n", + "namespace ns2 {\n", + "static int SupaClass::SupaFunction(uint32 sandwiches, const char* named)\n", + "{\n", + " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " CMOCK_ns1_ns2_SupaClass_SupaFunction_CALL_INSTANCE* cmock_call_instance;\n", + " UNITY_SET_DETAIL(CMockString_ns1_ns2_SupaClass_SupaFunction);\n", + " cmock_call_instance = (CMOCK_ns1_ns2_SupaClass_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.ns1_ns2_SupaClass_SupaFunction_CallInstance);\n", + " Mock.ns1_ns2_SupaClass_SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.ns1_ns2_SupaClass_SupaFunction_CallInstance);\n", + " uno", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", + " cmock_line = cmock_call_instance->LineNumber;\n", + " dos", + " tres", + " UNITY_CLR_DETAILS();\n", + " return cmock_call_instance->ReturnVal;\n", + "}\n", + "}\n", + "}\n\n", + ] + @plugins.expect :run, [" uno"], [:mock_implementation_precheck, function] + @plugins.expect :run, [" dos"," tres"], [:mock_implementation, function] + + @cmock_generator.create_mock_implementation(output, function) + + assert_equal(expected.join, output.join) + end + + it "create mock implementation functions in source file with different options for C++ static member" do + function = { :modifier => "static", + :c_calling_convention => "__stdcall", + :return => test_return[:int], + :args_string => "uint32 sandwiches", + :args => ["uint32 sandwiches"], + :var_arg => "corn ...", + :name => "SupaClass_SupaFunction", + :unscoped_name => "SupaFunction", + :namespace => [], + :class => "SupaClass", + :attributes => nil + } + output = [] + expected = [ "static int __stdcall SupaClass::SupaFunction(uint32 sandwiches, corn ...)\n", + "{\n", + " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n", + " CMOCK_SupaClass_SupaFunction_CALL_INSTANCE* cmock_call_instance;\n", + " UNITY_SET_DETAIL(CMockString_SupaClass_SupaFunction);\n", + " cmock_call_instance = (CMOCK_SupaClass_SupaFunction_CALL_INSTANCE*)CMock_Guts_GetAddressFor(Mock.SupaClass_SupaFunction_CallInstance);\n", + " Mock.SupaClass_SupaFunction_CallInstance = CMock_Guts_MemNext(Mock.SupaClass_SupaFunction_CallInstance);\n", + " uno", + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringCalledMore);\n", + " cmock_line = cmock_call_instance->LineNumber;\n", + " dos", + " tres", + " UNITY_CLR_DETAILS();\n", + " return cmock_call_instance->ReturnVal;\n", + "}\n\n" + ] + @plugins.expect :run, [" uno"], [:mock_implementation_precheck, function] + @plugins.expect :run, [" dos"," tres"], [:mock_implementation, function] + + @cmock_generator.create_mock_implementation(output, function) + + assert_equal(expected.join, output.join) + end + + it "creates using statements for C++ static member in namespace" do + function = { :modifier => "static", + :return => test_return[:int], + :args_string => "uint32 sandwiches", + :args => ["uint32 sandwiches"], + :var_arg => nil, + :unscoped_name => "SupaFunction", + :namespace => ["ns1"], + :class => "SupaClass", + :name => "ns1_SupaClass_SupaFunction", + :attributes => nil + } + output = [] + expected = "using namespace ns1;\n" + + @cmock_generator.create_using_statement(output, function) + + assert_equal(expected, output.join) + end + + it "creates using statements for C++ static member in nested namespace" do + function = { :modifier => "static", + :return => test_return[:int], + :args_string => "uint32 sandwiches", + :args => ["uint32 sandwiches"], + :var_arg => nil, + :unscoped_name => "SupaFunction", + :namespace => ["ns1", "ns2"], + :class => "SupaClass", + :name => "ns1_ns2_SupaClass_SupaFunction", + :attributes => nil + } + output = [] + expected = "using namespace ns1::ns2;\n" + + @cmock_generator.create_using_statement(output, function) + + assert_equal(expected, output.join) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_array_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_array_test.rb new file mode 100644 index 000000000..64c0b2838 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_array_test.rb @@ -0,0 +1,141 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_array' +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_utils' + +class UtilsStub + def helpers + {} + end + def arg_type_with_const(arg) + CMockGeneratorUtils.arg_type_with_const(arg) + end + def code_add_base_expectation(func) + "mock_retval_0" + end +end + +describe CMockGeneratorPluginArray, "Verify CMockPGeneratorluginArray Module" do + before do + #no strict ordering + @config = create_stub( + :when_ptr => :compare_data, + :enforce_strict_ordering => false, + :respond_to? => true ) + + @utils = UtilsStub.new + + @cmock_generator_plugin_array = CMockGeneratorPluginArray.new(@config, @utils) + end + + after do + end + + it "have set up internal priority" do + assert_nil(@cmock_generator_plugin_array.unity_helper) + assert_equal(8, @cmock_generator_plugin_array.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_array.respond_to?(:include_files)) + end + + it "not add to typedef structure for functions of style 'int* func(void)'" do + function = {:name => "Oak", :args => [], :return => test_return[:int_ptr]} + returned = @cmock_generator_plugin_array.instance_typedefs(function) + assert_equal("", returned) + end + + it "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do + function = {:name => "Cedar", :args => [{ :name => "chicken", :type => "int", :ptr? => false}, { :name => "pork", :type => "int*", :ptr? => true}], :return => test_return[:void]} + expected = " int Expected_pork_Depth;\n" + returned = @cmock_generator_plugin_array.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "not add an additional mock interface for functions not containing pointers" do + function = {:name => "Maple", :args_string => "int blah", :return => test_return[:string], :contains_ptr? => false} + returned = @cmock_generator_plugin_array.mock_function_declarations(function) + assert_nil(returned) + end + + it "add another mock function declaration for functions of style 'void func(int* tofu)'" do + function = {:name => "Pine", + :args => [{ :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:void], + :contains_ptr? => true } + + expected = "#define #{function[:name]}_ExpectWithArray(tofu, tofu_Depth) #{function[:name]}_CMockExpectWithArray(__LINE__, tofu, tofu_Depth)\n" + + "void #{function[:name]}_CMockExpectWithArray(UNITY_LINE_TYPE cmock_line, int* tofu, int tofu_Depth);\n" + returned = @cmock_generator_plugin_array.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add another mock function declaration for functions of style 'const char* func(int* tofu)'" do + function = {:name => "Pine", + :args => [{ :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:string], + :contains_ptr? => true } + + expected = "#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, tofu_Depth, cmock_retval)\n" + + "void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, int* tofu, int tofu_Depth, const char* cmock_to_return);\n" + returned = @cmock_generator_plugin_array.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add another mock function declaration for functions of style 'const char* func(const int* tofu)'" do + function = {:name => "Pine", + :args => [{ :type => "const int*", + :name => "tofu", + :ptr? => true, + :const? => true, + }], + :return => test_return[:string], + :contains_ptr? => true } + + expected = "#define #{function[:name]}_ExpectWithArrayAndReturn(tofu, tofu_Depth, cmock_retval) #{function[:name]}_CMockExpectWithArrayAndReturn(__LINE__, tofu, tofu_Depth, cmock_retval)\n" + + "void #{function[:name]}_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, const int* tofu, int tofu_Depth, const char* cmock_to_return);\n" + returned = @cmock_generator_plugin_array.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "not have a mock function implementation" do + assert(!@cmock_generator_plugin_array.respond_to?(:mock_implementation)) + end + + it "not have a mock interfaces for functions of style 'int* func(void)'" do + function = {:name => "Pear", :args => [], :args_string => "void", :return => test_return[:int_ptr]} + returned = @cmock_generator_plugin_array.mock_interfaces(function) + assert_nil(returned) + end + + it "add mock interfaces for functions of style 'int* func(int* pescado, int pes)'" do + function = {:name => "Lemon", + :args => [{ :type => "int*", :name => "pescado", :ptr? => true}, { :type => "int", :name => "pes", :ptr? => false}], + :args_string => "int* pescado, int pes", + :return => test_return[:int_ptr], + :contains_ptr? => true } + + expected = ["void Lemon_CMockExpectWithArrayAndReturn(UNITY_LINE_TYPE cmock_line, int* pescado, int pescado_Depth, int pes, int* cmock_to_return)\n", + "{\n", + "mock_retval_0", + " CMockExpectParameters_Lemon(cmock_call_instance, pescado, pescado_Depth, pes);\n", + " cmock_call_instance->ReturnVal = cmock_to_return;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_array.mock_interfaces(function).join + assert_equal(expected, returned) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_callback_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_callback_test.rb new file mode 100644 index 000000000..0b5ce1eb8 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_callback_test.rb @@ -0,0 +1,281 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_callback' + +describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Module" do + + before do + create_mocks :config, :utils + + @config.expect :callback_include_count, true + @config.expect :callback_after_arg_check, false + @config.expect :plugins, [:ignore] + + @cmock_generator_plugin_callback = CMockGeneratorPluginCallback.new(@config, @utils) + end + + after do + end + + it "have set up internal priority" do + assert_equal(6, @cmock_generator_plugin_callback.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_callback.respond_to?(:include_files)) + end + + it "add to instance structure" do + function = {:name => "Oak", :args => [:type => "int*", :name => "blah", :ptr? => true], :return => test_return[:int_ptr]} + expected = " char Oak_CallbackBool;\n" + + " CMOCK_Oak_CALLBACK Oak_CallbackFunctionPointer;\n" + + " int Oak_CallbackCalls;\n" + returned = @cmock_generator_plugin_callback.instance_structure(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for function without arguments" do + function = {:name => "Maple", :args_string => "void", :args => [], :return => test_return[:void]} + expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int cmock_num_calls);\n", + "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n", + "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n", + "#define Maple_StubWithCallback Maple_Stub\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for function without arguments when count is also turned off" do + function = {:name => "Maple", :args_string => "void", :args => [], :return => test_return[:void]} + expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(void);\n", + "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n", + "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n", + "#define Maple_StubWithCallback Maple_Stub\n" ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for function with arguments" do + function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:void]} + expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n", + "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n", + "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n", + "#define Maple_StubWithCallback Maple_Stub\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for function with return values" do + function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:string]} + expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n", + "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n", + "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n", + "#define Maple_StubWithCallback Maple_Stub\n" ].join + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for function with return values and count is turned off" do + function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:string]} + expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu);\n", + "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n", + "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n", + "#define Maple_StubWithCallback Maple_Stub\n" ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'void func(void)'" do + function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'void func(void)' when count turned off" do + function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer();\n", + " }\n" + ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'int func(void)'" do + function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:int]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " cmock_call_instance->ReturnVal = Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'void func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return=> test_return[:void]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'void func(int* steak, uint8_t flag)' when count turned off" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return=> test_return[:void]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer(steak, flag);\n", + " }\n" + ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions with no arg check and of style 'int16_t func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return => test_return[:int]} + expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " cmock_call_instance->ReturnVal = Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions without arg check and of style 'void func(void)' when count turned off" do + function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]} + expected = [" if (!Mock.Apple_CallbackBool &&\n", + " Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer();\n", + " UNITY_CLR_DETAILS();\n", + " return;\n", + " }\n" + ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions without arg check and of style 'int func(void)'" do + function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:int]} + expected = [" if (!Mock.Apple_CallbackBool &&\n", + " Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " int cmock_cb_ret = Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n", + " UNITY_CLR_DETAILS();\n", + " return cmock_cb_ret;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions without arg check and of style 'void func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return=> test_return[:void]} + expected = [" if (!Mock.Apple_CallbackBool &&\n", + " Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n", + " UNITY_CLR_DETAILS();\n", + " return;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions without arg check and of style 'void func(int* steak, uint8_t flag)' when count turned off" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return=> test_return[:void]} + expected = [" if (!Mock.Apple_CallbackBool &&\n", + " Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " Mock.Apple_CallbackFunctionPointer(steak, flag);\n", + " UNITY_CLR_DETAILS();\n", + " return;\n", + " }\n" + ].join + @cmock_generator_plugin_callback.include_count = false + returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions without arg check and of style 'int16_t func(int* steak, uint8_t flag)'" do + function = {:name => "Apple", + :args => [ { :type => 'int*', :name => 'steak', :ptr? => true}, + { :type => 'uint8_t', :name => 'flag', :ptr? => false} ], + :args_string => "int* steak, uint8_t flag", + :return => test_return[:int]} + expected = [" if (!Mock.Apple_CallbackBool &&\n", + " Mock.Apple_CallbackFunctionPointer != NULL)\n", + " {\n", + " int cmock_cb_ret = Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n", + " UNITY_CLR_DETAILS();\n", + " return cmock_cb_ret;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions " do + function = {:name => "Lemon", + :args => [{ :type => "char*", :name => "pescado"}], + :args_string => "char* pescado", + :return => test_return[:int] + } + + expected = ["void Lemon_AddCallback(CMOCK_Lemon_CALLBACK Callback)\n", + "{\n", + " Mock.Lemon_IgnoreBool = (char)0;\n", + " Mock.Lemon_CallbackBool = (char)1;\n", + " Mock.Lemon_CallbackFunctionPointer = Callback;\n", + "}\n\n", + "void Lemon_Stub(CMOCK_Lemon_CALLBACK Callback)\n", + "{\n", + " Mock.Lemon_IgnoreBool = (char)0;\n", + " Mock.Lemon_CallbackBool = (char)0;\n", + " Mock.Lemon_CallbackFunctionPointer = Callback;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_callback.mock_interfaces(function) + assert_equal(expected, returned) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_cexception_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_cexception_test.rb new file mode 100644 index 000000000..19699ea35 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_cexception_test.rb @@ -0,0 +1,96 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_cexception' + +describe CMockGeneratorPluginCexception, "Verify CMockGeneratorPluginCexception Module" do + + before do + create_mocks :config, :utils + @cmock_generator_plugin_cexception = CMockGeneratorPluginCexception.new(@config, @utils) + end + + after do + end + + it "have set up internal priority" do + assert_equal(7, @cmock_generator_plugin_cexception.priority) + end + + it "include the cexception library" do + expected = "#include \"CException.h\"\n" + returned = @cmock_generator_plugin_cexception.include_files + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs" do + function = { :name => "Oak", :args => [], :return => test_return[:void] } + expected = " CEXCEPTION_T ExceptionToThrow;\n" + returned = @cmock_generator_plugin_cexception.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add mock function declarations for functions without arguments" do + function = { :name => "Spruce", :args_string => "void", :return => test_return[:void] } + expected = "#define Spruce_ExpectAndThrow(cmock_to_throw) Spruce_CMockExpectAndThrow(__LINE__, cmock_to_throw)\n"+ + "void Spruce_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, CEXCEPTION_T cmock_to_throw);\n" + returned = @cmock_generator_plugin_cexception.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declarations for functions with arguments" do + function = { :name => "Spruce", :args_string => "const char* Petunia, uint32_t Lily", :args_call => "Petunia, Lily", :return => test_return[:void] } + expected = "#define Spruce_ExpectAndThrow(Petunia, Lily, cmock_to_throw) Spruce_CMockExpectAndThrow(__LINE__, Petunia, Lily, cmock_to_throw)\n" + + "void Spruce_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, const char* Petunia, uint32_t Lily, CEXCEPTION_T cmock_to_throw);\n" + returned = @cmock_generator_plugin_cexception.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add a mock implementation" do + function = {:name => "Cherry", :args => [], :return => test_return[:void]} + expected = " if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n" + + " {\n" + + " UNITY_CLR_DETAILS();\n" + + " Throw(cmock_call_instance->ExceptionToThrow);\n" + + " }\n" + returned = @cmock_generator_plugin_cexception.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions without arguments" do + function = {:name => "Pear", :args_string => "void", :args => [], :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0", ["Pear"] + @utils.expect :code_call_argument_loader, "", [function] + + expected = ["void Pear_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, CEXCEPTION_T cmock_to_throw)\n", + "{\n", + "mock_retval_0", + "", + " cmock_call_instance->ExceptionToThrow = cmock_to_throw;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_cexception.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add a mock interfaces for functions with arguments" do + function = {:name => "Pear", :args_string => "int blah", :args => [{ :type => "int", :name => "blah" }], :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0", ["Pear"] + @utils.expect :code_call_argument_loader, "mock_return_1", [function] + + expected = ["void Pear_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, int blah, CEXCEPTION_T cmock_to_throw)\n", + "{\n", + "mock_retval_0", + "mock_return_1", + " cmock_call_instance->ExceptionToThrow = cmock_to_throw;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_cexception.mock_interfaces(function) + assert_equal(expected, returned) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_a_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_a_test.rb new file mode 100644 index 000000000..dd86689a0 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_a_test.rb @@ -0,0 +1,185 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_expect' + +describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module Without Global Ordering" do + + before do + create_mocks :config, :utils + + @config = create_stub( + :when_ptr => :compare_data, + :enforce_strict_ordering => false, + :respond_to? => true, + :plugins => [ :expect ] ) + + @utils.expect :helpers, {} + @cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils) + end + + after do + end + + it "have set up internal priority on init" do + assert_nil(@cmock_generator_plugin_expect.unity_helper) + assert_equal(5, @cmock_generator_plugin_expect.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_expect.respond_to?(:include_files)) + end + + it "add to typedef structure mock needs of functions of style 'void func(void)'" do + function = {:name => "Oak", :args => [], :return => test_return[:void]} + expected = "" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'int func(void)'" do + function = {:name => "Elm", :args => [], :return => test_return[:int]} + expected = " int ReturnVal;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'void func(int chicken, char* pork)'" do + function = {:name => "Cedar", :args => [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}], :return => test_return[:void]} + expected = " int Expected_chicken;\n char* Expected_pork;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'int func(float beef)'" do + function = {:name => "Birch", :args => [{ :name => "beef", :type => "float"}], :return => test_return[:int]} + expected = " int ReturnVal;\n float Expected_beef;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'void func(void)'" do + function = {:name => "Maple", :args => [], :return => test_return[:void]} + expected = "#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" + + "void Maple_CMockExpect(UNITY_LINE_TYPE cmock_line);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'int func(void)'" do + function = {:name => "Spruce", :args => [], :return => test_return[:int]} + expected = "#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" + + "void Spruce_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'const char* func(int tofu)'" do + function = {:name => "Pine", :args => ["int tofu"], :args_string => "int tofu", :args_call => 'tofu', :return => test_return[:string]} + expected = "#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" + + "void Pine_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int tofu, const char* cmock_to_return);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions of style 'void func(void)'" do + function = {:name => "Apple", :args => [], :return => test_return[:void]} + expected = "" + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions of style 'int func(int veal, unsigned int sushi)'" do + function = {:name => "Cherry", :args => [ { :type => "int", :name => "veal" }, { :type => "unsigned int", :name => "sushi" } ], :return => test_return[:int]} + + @utils.expect :code_verify_an_arg_expectation, " mocked_retval_1", [function, function[:args][0]] + @utils.expect :code_verify_an_arg_expectation, " mocked_retval_2", [function, function[:args][1]] + expected = " mocked_retval_1 mocked_retval_2" + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation using ordering if needed" do + function = {:name => "Apple", :args => [], :return => test_return[:void]} + expected = "" + @cmock_generator_plugin_expect.ordered = true + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'void func(void)'" do + function = {:name => "Pear", :args => [], :args_string => "void", :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0 ", ["Pear"] + @utils.expect :code_call_argument_loader, "mock_retval_1 ", [function] + expected = ["void Pear_CMockExpect(UNITY_LINE_TYPE cmock_line)\n", + "{\n", + "mock_retval_0 ", + "mock_retval_1 ", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'int func(void)'" do + function = {:name => "Orange", :args => [], :args_string => "void", :return => test_return[:int]} + @utils.expect :code_add_base_expectation, "mock_retval_0 ", ["Orange"] + @utils.expect :code_call_argument_loader, "mock_retval_1 ", [function] + @utils.expect :code_assign_argument_quickly, "mock_retval_2", ["cmock_call_instance->ReturnVal", function[:return]] + expected = ["void Orange_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return)\n", + "{\n", + "mock_retval_0 ", + "mock_retval_1 ", + "mock_retval_2", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'int func(char* pescado)'" do + function = {:name => "Lemon", :args => [{ :type => "char*", :name => "pescado"}], :args_string => "char* pescado", :return => test_return[:int]} + @utils.expect :code_add_base_expectation, "mock_retval_0 ", ["Lemon"] + @utils.expect :code_call_argument_loader, "mock_retval_1 ", [function] + @utils.expect :code_assign_argument_quickly, "mock_retval_2", ["cmock_call_instance->ReturnVal", function[:return]] + expected = ["void Lemon_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, char* pescado, int cmock_to_return)\n", + "{\n", + "mock_retval_0 ", + "mock_retval_1 ", + "mock_retval_2", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions when using ordering" do + function = {:name => "Pear", :args => [], :args_string => "void", :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0 ", ["Pear"] + @utils.expect :code_call_argument_loader, "mock_retval_1 ", [function] + expected = ["void Pear_CMockExpect(UNITY_LINE_TYPE cmock_line)\n", + "{\n", + "mock_retval_0 ", + "mock_retval_1 ", + "}\n\n" + ].join + @cmock_generator_plugin_expect.ordered = true + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock verify lines" do + function = {:name => "Banana" } + expected = " if (CMOCK_GUTS_NONE != call_instance)\n" \ + " {\n" \ + " UNITY_SET_DETAIL(CMockString_Banana);\n" \ + " UNITY_TEST_FAIL(cmock_line, CMockStringCalledLess);\n" \ + " }\n" + returned = @cmock_generator_plugin_expect.mock_verify(function) + assert_equal(expected, returned) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_any_args_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_any_args_test.rb new file mode 100644 index 000000000..5a014d6e3 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_any_args_test.rb @@ -0,0 +1,67 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_expect_any_args.rb' + +describe CMockGeneratorPluginExpectAnyArgs, "Verify CMockGeneratorPluginExpectAnyArgs Module" do + + before do + create_mocks :config, :utils + @config = create_stub(:respond_to? => true) + @cmock_generator_plugin_expect_any_args = CMockGeneratorPluginExpectAnyArgs.new(@config, @utils) + end + + after do + end + + it "have set up internal priority" do + assert_equal(3, @cmock_generator_plugin_expect_any_args.priority) + end + + it "not have any additional include file requirements" do + assert(!@cmock_generator_plugin_expect_any_args.respond_to?(:include_files)) + end + + it "ignore functions without arguments" do + function = {:name => "Mold", :args_string => "void", :args => [], :return => test_return[:void]} + expected = "" + returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "handle function declarations for functions without return values" do + function = {:name => "Mold", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:void]} + expected = "#define Mold_ExpectAnyArgs() Mold_CMockExpectAnyArgs(__LINE__)\nvoid Mold_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line);\n" + returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "handle function declarations for functions that returns something" do + function = {:name => "Fungus", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:string]} + expected = "#define Fungus_ExpectAnyArgsAndReturn(cmock_retval) Fungus_CMockExpectAnyArgsAndReturn(__LINE__, cmock_retval)\n"+ + "void Fungus_CMockExpectAnyArgsAndReturn(UNITY_LINE_TYPE cmock_line, const char* cmock_to_return);\n" + returned = @cmock_generator_plugin_expect_any_args.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "should not respond to implementation requests" do + assert(!@cmock_generator_plugin_expect_any_args.respond_to?(:mock_implementation)) + end + + it "add a new mock interface for ignoring when function had no return value" do + function = {:name => "Slime", :args_string => "int meh", :args => [ :stuff ], :return => test_return[:void]} + expected = ["void Slime_CMockExpectAnyArgs(UNITY_LINE_TYPE cmock_line)\n", + "{\n", + "mock_return_1", + " cmock_call_instance->ExpectAnyArgsBool = (char)1;\n", + "}\n\n" + ].join + @utils.expect :code_add_base_expectation, "mock_return_1", ["Slime", true] + returned = @cmock_generator_plugin_expect_any_args.mock_interfaces(function) + assert_equal(expected, returned) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_b_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_b_test.rb new file mode 100644 index 000000000..35d48715c --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_expect_b_test.rb @@ -0,0 +1,201 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_expect' + +describe CMockGeneratorPluginExpect, "Verify CMockGeneratorPluginExpect Module with Global Ordering" do + + before do + create_mocks :config, :utils + + @config = create_stub( + :when_ptr => :compare_data, + :enforce_strict_ordering => true, + :respond_to? => true, + :plugins => [ :expect, :expect_any_args ] ) + + @utils.expect :helpers, {} + @cmock_generator_plugin_expect = CMockGeneratorPluginExpect.new(@config, @utils) + end + + after do + end + + it "have set up internal priority on init" do + assert_nil(@cmock_generator_plugin_expect.unity_helper) + assert_equal(5, @cmock_generator_plugin_expect.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_expect.respond_to?(:include_files)) + end + + it "add to typedef structure mock needs of functions of style 'void func(void)' and global ordering" do + function = {:name => "Oak", :args => [], :return => test_return[:void]} + expected = " int CallOrder;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'int func(void)'" do + function = {:name => "Elm", :args => [], :return => test_return[:int]} + expected = " int ReturnVal;\n int CallOrder;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'void func(int chicken, char* pork)'" do + function = {:name => "Cedar", :args => [{ :name => "chicken", :type => "int"}, { :name => "pork", :type => "char*"}], :return => test_return[:void]} + expected = " int CallOrder;\n int Expected_chicken;\n char* Expected_pork;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add to typedef structure mock needs of functions of style 'int func(float beef)'" do + function = {:name => "Birch", :args => [{ :name => "beef", :type => "float"}], :return => test_return[:int]} + expected = " int ReturnVal;\n int CallOrder;\n float Expected_beef;\n" + returned = @cmock_generator_plugin_expect.instance_typedefs(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'void func(void)'" do + function = {:name => "Maple", :args => [], :return => test_return[:void]} + expected = "#define Maple_Expect() Maple_CMockExpect(__LINE__)\n" + + "void Maple_CMockExpect(UNITY_LINE_TYPE cmock_line);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'int func(void)'" do + function = {:name => "Spruce", :args => [], :return => test_return[:int]} + expected = "#define Spruce_ExpectAndReturn(cmock_retval) Spruce_CMockExpectAndReturn(__LINE__, cmock_retval)\n" + + "void Spruce_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function declaration for functions of style 'const char* func(int tofu)'" do + function = {:name => "Pine", :args => ["int tofu"], :args_string => "int tofu", :args_call => 'tofu', :return => test_return[:string]} + expected = "#define Pine_ExpectAndReturn(tofu, cmock_retval) Pine_CMockExpectAndReturn(__LINE__, tofu, cmock_retval)\n" + + "void Pine_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int tofu, const char* cmock_to_return);\n" + returned = @cmock_generator_plugin_expect.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions of style 'void func(void)'" do + function = {:name => "Apple", :args => [], :return => test_return[:void]} + expected = "" + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions of style 'int func(int veal, unsigned int sushi)'" do + function = {:name => "Cherry", :args => [ { :type => "int", :name => "veal" }, { :type => "unsigned int", :name => "sushi" } ], :return => test_return[:int]} + + @utils.expect :code_verify_an_arg_expectation, "mocked_retval_1\n", [function, function[:args][0]] + @utils.expect :code_verify_an_arg_expectation, "mocked_retval_2\n", [function, function[:args][1]] + expected = " if (!cmock_call_instance->ExpectAnyArgsBool)\n" + + " {\n" + + "mocked_retval_1\n" + + "mocked_retval_2\n" + + " }\n" + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation using ordering if needed" do + function = {:name => "Apple", :args => [], :return => test_return[:void]} + expected = "" + @cmock_generator_plugin_expect.ordered = true + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock function implementation for functions of style 'void func(int worm)' and strict ordering" do + function = {:name => "Apple", :args => [{ :type => "int", :name => "worm" }], :return => test_return[:void]} + @utils.expect :code_verify_an_arg_expectation, "mocked_retval_0\n", [function, function[:args][0]] + expected = " if (!cmock_call_instance->ExpectAnyArgsBool)\n" + + " {\n" + + "mocked_retval_0\n" + + " }\n" + @cmock_generator_plugin_expect.ordered = true + returned = @cmock_generator_plugin_expect.mock_implementation(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'void func(void)'" do + function = {:name => "Pear", :args => [], :args_string => "void", :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0\n", ["Pear"] + @utils.expect :code_call_argument_loader, "mock_retval_1\n", [function] + expected = ["void Pear_CMockExpect(UNITY_LINE_TYPE cmock_line)\n", + "{\n", + "mock_retval_0\n", + "mock_retval_1\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'int func(void)'" do + function = {:name => "Orange", :args => [], :args_string => "void", :return => test_return[:int]} + @utils.expect :code_add_base_expectation, "mock_retval_0\n", ["Orange"] + @utils.expect :code_call_argument_loader, "mock_retval_1\n", [function] + @utils.expect :code_assign_argument_quickly, "mock_retval_2\n", ["cmock_call_instance->ReturnVal", function[:return]] + expected = ["void Orange_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return)\n", + "{\n", + "mock_retval_0\n", + "mock_retval_1\n", + "mock_retval_2\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions of style 'int func(char* pescado)'" do + function = {:name => "Lemon", :args => [{ :type => "char*", :name => "pescado"}], :args_string => "char* pescado", :return => test_return[:int]} + @utils.expect :code_add_base_expectation, "mock_retval_0\n", ["Lemon"] + @utils.expect :code_call_argument_loader, "mock_retval_1\n", [function] + @utils.expect :code_assign_argument_quickly, "mock_retval_2\n", ["cmock_call_instance->ReturnVal", function[:return]] + expected = ["void Lemon_CMockExpectAndReturn(UNITY_LINE_TYPE cmock_line, char* pescado, int cmock_to_return)\n", + "{\n", + "mock_retval_0\n", + "mock_retval_1\n", + "mock_retval_2\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock interfaces for functions when using ordering" do + function = {:name => "Pear", :args => [], :args_string => "void", :return => test_return[:void]} + @utils.expect :code_add_base_expectation, "mock_retval_0\n", ["Pear"] + @utils.expect :code_call_argument_loader, "mock_retval_1\n", [function] + expected = ["void Pear_CMockExpect(UNITY_LINE_TYPE cmock_line)\n", + "{\n", + "mock_retval_0\n", + "mock_retval_1\n", + "}\n\n" + ].join + @cmock_generator_plugin_expect.ordered = true + returned = @cmock_generator_plugin_expect.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add mock verify lines" do + function = {:name => "Banana" } + expected = " if (CMOCK_GUTS_NONE != call_instance)\n" \ + " {\n" \ + " UNITY_SET_DETAIL(CMockString_Banana);\n" \ + " UNITY_TEST_FAIL(cmock_line, CMockStringCalledLess);\n" \ + " }\n" + returned = @cmock_generator_plugin_expect.mock_verify(function) + assert_equal(expected, returned) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_arg_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_arg_test.rb new file mode 100644 index 000000000..5fa18e8c1 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_arg_test.rb @@ -0,0 +1,116 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_ignore_arg' + +describe CMockGeneratorPluginIgnoreArg, "Verify CMockGeneratorPluginIgnoreArg Module" do + + before do + create_mocks :config, :utils + + # int *Oak(void)" + @void_func = {:name => "Oak", :args => [], :return => test_return[:int_ptr]} + + # void Pine(int chicken, const int beef, int *tofu) + @complex_func = {:name => "Pine", + :args => [{ :type => "int", + :name => "chicken", + :ptr? => false, + }, + { :type => "const int*", + :name => "beef", + :ptr? => true, + :const? => true, + }, + { :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:void], + :contains_ptr? => true } + + #no strict ordering + @cmock_generator_plugin_ignore_arg = CMockGeneratorPluginIgnoreArg.new(@config, @utils) + end + + after do + end + + it "have set up internal priority correctly on init" do + assert_equal(10, @cmock_generator_plugin_ignore_arg.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_ignore_arg.respond_to?(:include_files)) + end + + it "not add to typedef structure for functions with no args" do + returned = @cmock_generator_plugin_ignore_arg.instance_typedefs(@void_func) + assert_equal("", returned) + end + + it "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do + expected = " char IgnoreArg_chicken;\n" + + " char IgnoreArg_beef;\n" + + " char IgnoreArg_tofu;\n" + returned = @cmock_generator_plugin_ignore_arg.instance_typedefs(@complex_func) + assert_equal(expected, returned) + end + + it "add mock function declarations for all arguments" do + expected = + "#define Pine_IgnoreArg_chicken()" + + " Pine_CMockIgnoreArg_chicken(__LINE__)\n" + + "void Pine_CMockIgnoreArg_chicken(UNITY_LINE_TYPE cmock_line);\n" + + + "#define Pine_IgnoreArg_beef()" + + " Pine_CMockIgnoreArg_beef(__LINE__)\n" + + "void Pine_CMockIgnoreArg_beef(UNITY_LINE_TYPE cmock_line);\n" + + + "#define Pine_IgnoreArg_tofu()" + + " Pine_CMockIgnoreArg_tofu(__LINE__)\n" + + "void Pine_CMockIgnoreArg_tofu(UNITY_LINE_TYPE cmock_line);\n" + + returned = @cmock_generator_plugin_ignore_arg.mock_function_declarations(@complex_func) + assert_equal(expected, returned) + end + + it "add mock interfaces for all arguments" do + expected = + "void Pine_CMockIgnoreArg_chicken(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + + " cmock_call_instance->IgnoreArg_chicken = 1;\n" + + "}\n\n" + + + "void Pine_CMockIgnoreArg_beef(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + + " cmock_call_instance->IgnoreArg_beef = 1;\n" + + "}\n\n" + + + "void Pine_CMockIgnoreArg_tofu(UNITY_LINE_TYPE cmock_line)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringIgnPreExp);\n" + + " cmock_call_instance->IgnoreArg_tofu = 1;\n" + + "}\n\n" + + returned = @cmock_generator_plugin_ignore_arg.mock_interfaces(@complex_func).join("") + assert_equal(expected, returned) + end + + it "not add a mock implementation" do + assert(!@cmock_generator_plugin_ignore_arg.respond_to?(:mock_implementation)) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_test.rb new file mode 100644 index 000000000..c0c28b84d --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_ignore_test.rb @@ -0,0 +1,119 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_ignore' + +describe CMockGeneratorPluginIgnore, "Verify CMockGeneratorPluginIgnore Module" do + + before do + create_mocks :config, :utils + @config = create_stub(:respond_to? => true) + @cmock_generator_plugin_ignore = CMockGeneratorPluginIgnore.new(@config, @utils) + end + + after do + end + + it "have set up internal priority" do + assert_equal(2, @cmock_generator_plugin_ignore.priority) + end + + it "not have any additional include file requirements" do + assert(!@cmock_generator_plugin_ignore.respond_to?(:include_files)) + end + + it "add a required variable to the instance structure" do + function = {:name => "Grass", :args => [], :return => test_return[:void]} + expected = " char Grass_IgnoreBool;\n" + returned = @cmock_generator_plugin_ignore.instance_structure(function) + assert_equal(expected, returned) + end + + it "handle function declarations for functions without return values" do + function = {:name => "Mold", :args_string => "void", :return => test_return[:void]} + expected = "#define Mold_Ignore() Mold_CMockIgnore()\nvoid Mold_CMockIgnore(void);\n" + + "#define Mold_StopIgnore() Mold_CMockStopIgnore()\nvoid Mold_CMockStopIgnore(void);\n" + returned = @cmock_generator_plugin_ignore.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "handle function declarations for functions that returns something" do + function = {:name => "Fungus", :args_string => "void", :return => test_return[:string]} + expected = "#define Fungus_IgnoreAndReturn(cmock_retval) Fungus_CMockIgnoreAndReturn(__LINE__, cmock_retval)\n"+ + "void Fungus_CMockIgnoreAndReturn(UNITY_LINE_TYPE cmock_line, const char* cmock_to_return);\n" + + "#define Fungus_StopIgnore() Fungus_CMockStopIgnore()\n"+ + "void Fungus_CMockStopIgnore(void);\n" + returned = @cmock_generator_plugin_ignore.mock_function_declarations(function) + assert_equal(expected, returned) + end + + it "add required code to implementation precheck with void function" do + function = {:name => "Mold", :args_string => "void", :return => test_return[:void]} + expected = [" if (Mock.Mold_IgnoreBool)\n", + " {\n", + " UNITY_CLR_DETAILS();\n", + " return;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_ignore.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add required code to implementation precheck with return functions" do + function = {:name => "Fungus", :args_string => "void", :return => test_return[:int]} + retval = test_return[:int].merge({ :name => "cmock_call_instance->ReturnVal"}) + @utils.expect :code_assign_argument_quickly, ' mock_retval_0', ["Mock.Fungus_FinalReturn", retval] + expected = [" if (Mock.Fungus_IgnoreBool)\n", + " {\n", + " UNITY_CLR_DETAILS();\n", + " if (cmock_call_instance == NULL)\n", + " return Mock.Fungus_FinalReturn;\n", + " mock_retval_0", + " return cmock_call_instance->ReturnVal;\n", + " }\n" + ].join + returned = @cmock_generator_plugin_ignore.mock_implementation_precheck(function) + assert_equal(expected, returned) + end + + it "add a new mock interface for ignoring when function had no return value" do + function = {:name => "Slime", :args => [], :args_string => "void", :return => test_return[:void]} + expected = ["void Slime_CMockIgnore(void)\n", + "{\n", + " Mock.Slime_IgnoreBool = (char)1;\n", + "}\n\n", + + "void Slime_CMockStopIgnore(void)\n", + "{\n", + " Mock.Slime_IgnoreBool = (char)0;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_ignore.mock_interfaces(function) + assert_equal(expected, returned) + end + + it "add a new mock interface for ignoring when function has return value" do + function = {:name => "Slime", :args => [], :args_string => "void", :return => test_return[:int]} + @utils.expect :code_add_base_expectation, "mock_return_1", ["Slime", false] + expected = ["void Slime_CMockIgnoreAndReturn(UNITY_LINE_TYPE cmock_line, int cmock_to_return)\n", + "{\n", + "mock_return_1", + " cmock_call_instance->ReturnVal = cmock_to_return;\n", + " Mock.Slime_IgnoreBool = (char)1;\n", + "}\n\n", + + "void Slime_CMockStopIgnore(void)\n{\n", + " if(Mock.Slime_IgnoreBool)\n", + " Mock.Slime_CallInstance = CMock_Guts_MemNext(Mock.Slime_CallInstance);\n", + " Mock.Slime_IgnoreBool = (char)0;\n", + "}\n\n" + ].join + returned = @cmock_generator_plugin_ignore.mock_interfaces(function) + assert_equal(expected, returned) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb new file mode 100644 index 000000000..3c8b075da --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_plugin_return_thru_ptr_test.rb @@ -0,0 +1,136 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_return_thru_ptr' + +describe CMockGeneratorPluginReturnThruPtr, "Verify CMockGeneratorPluginReturnThruPtr Module" do + + before do + create_mocks :config, :utils + + # int *Oak(void)" + @void_func = {:name => "Oak", :args => [], :return => test_return[:int_ptr]} + + # char *Maple(int blah) + @simple_func = {:name => "Maple", + :args => [{:name => "blah", :type => "int", :ptr? => false}], + :return => test_return[:string], + :contains_ptr? => false} + + # void Pine(int chicken, const int beef, int *tofu) + @complex_func = {:name => "Pine", + :args => [{ :type => "int", + :name => "chicken", + :ptr? => false, + }, + { :type => "const int*", + :name => "beef", + :ptr? => true, + :const? => true, + }, + { :type => "int*", + :name => "tofu", + :ptr? => true, + }], + :return => test_return[:void], + :contains_ptr? => true } + + #no strict ordering + @cmock_generator_plugin_return_thru_ptr = CMockGeneratorPluginReturnThruPtr.new(@config, @utils) + end + + after do + end + + def simple_func_expect + @utils.expect :ptr_or_str?, false, ['int'] + end + + def complex_func_expect + @utils.expect :ptr_or_str?, false, ['int'] + @utils.expect :ptr_or_str?, true, ['const int*'] + @utils.expect :ptr_or_str?, true, ['int*'] + end + + it "have set up internal priority correctly on init" do + assert_equal(9, @cmock_generator_plugin_return_thru_ptr.priority) + end + + it "not include any additional include files" do + assert(!@cmock_generator_plugin_return_thru_ptr.respond_to?(:include_files)) + end + + it "not add to typedef structure for functions of style 'int* func(void)'" do + returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@void_func) + assert_equal("", returned) + end + + it "add to tyepdef structure mock needs of functions of style 'void func(int chicken, int* pork)'" do + complex_func_expect() + expected = " char ReturnThruPtr_tofu_Used;\n" + + " int* ReturnThruPtr_tofu_Val;\n" + + " int ReturnThruPtr_tofu_Size;\n" + returned = @cmock_generator_plugin_return_thru_ptr.instance_typedefs(@complex_func) + assert_equal(expected, returned) + end + + it "not add an additional mock interface for functions not containing pointers" do + simple_func_expect() + returned = @cmock_generator_plugin_return_thru_ptr.mock_function_declarations(@simple_func) + assert_equal("", returned) + end + + it "add a mock function declaration only for non-const pointer arguments" do + complex_func_expect(); + + expected = + "#define Pine_ReturnThruPtr_tofu(tofu)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, sizeof(int))\n" + + "#define Pine_ReturnArrayThruPtr_tofu(tofu, cmock_len)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, (int)(cmock_len * (int)sizeof(*tofu)))\n" + + "#define Pine_ReturnMemThruPtr_tofu(tofu, cmock_size)" + + " Pine_CMockReturnMemThruPtr_tofu(__LINE__, tofu, cmock_size)\n" + + "void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, int cmock_size);\n" + + returned = @cmock_generator_plugin_return_thru_ptr.mock_function_declarations(@complex_func) + assert_equal(expected, returned) + end + + it "add mock interfaces only for non-const pointer arguments" do + complex_func_expect(); + + expected = + "void Pine_CMockReturnMemThruPtr_tofu(UNITY_LINE_TYPE cmock_line, int* tofu, int cmock_size)\n" + + "{\n" + + " CMOCK_Pine_CALL_INSTANCE* cmock_call_instance = " + + "(CMOCK_Pine_CALL_INSTANCE*)CMock_Guts_GetAddressFor(CMock_Guts_MemEndOfChain(Mock.Pine_CallInstance));\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringPtrPreExp);\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Used = 1;\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Val = tofu;\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Size = cmock_size;\n" + + "}\n\n" + + returned = @cmock_generator_plugin_return_thru_ptr.mock_interfaces(@complex_func).join("") + assert_equal(expected, returned) + end + + it "add mock implementations only for non-const pointer arguments" do + complex_func_expect() + + expected = + " if (cmock_call_instance->ReturnThruPtr_tofu_Used)\n" + + " {\n" + + " UNITY_TEST_ASSERT_NOT_NULL(tofu, cmock_line, CMockStringPtrIsNULL);\n" + + " memcpy((void*)tofu, (void*)cmock_call_instance->ReturnThruPtr_tofu_Val,\n" + + " cmock_call_instance->ReturnThruPtr_tofu_Size);\n" + + " }\n" + + returned = @cmock_generator_plugin_return_thru_ptr.mock_implementation(@complex_func).join("") + assert_equal(expected, returned) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_utils_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_utils_test.rb new file mode 100644 index 000000000..ab9df2a2c --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_generator_utils_test.rb @@ -0,0 +1,398 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_utils' + +describe CMockGeneratorUtils, "Verify CMockGeneratorUtils Module" do + + before do + create_mocks :config, :unity_helper, :unity_helper + + @config.expect :when_ptr, :compare_ptr + @config.expect :enforce_strict_ordering, false + @config.expect :plugins, [] + @config.expect :plugins, [] + @config.expect :plugins, [] + @config.expect :plugins, [] + @config.expect :plugins, [] + @config.expect :plugins, [] + @config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','const char*' => 'STRING'} + @cmock_generator_utils_simple = CMockGeneratorUtils.new(@config, {:unity_helper => @unity_helper}) + + @config.expect :when_ptr, :smart + @config.expect :enforce_strict_ordering, true + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :plugins, [:array, :cexception, :return_thru_ptr, :ignore_arg, :ignore] + @config.expect :treat_as, {'int' => 'INT','short' => 'INT16','long' => 'INT','char' => 'INT8','uint32_t' => 'HEX32','const char*' => 'STRING'} + @cmock_generator_utils_complex = CMockGeneratorUtils.new(@config, {:unity_helper => @unity_helper, :A=>1, :B=>2}) + end + + after do + end + + it "have set up internal accessors correctly on init" do + assert_equal(false, @cmock_generator_utils_simple.arrays) + assert_equal(false, @cmock_generator_utils_simple.cexception) + end + + it "have set up internal accessors correctly on init, complete with passed helpers" do + assert_equal(true, @cmock_generator_utils_complex.arrays) + assert_equal(true, @cmock_generator_utils_complex.cexception) + end + + it "detect pointers and strings" do + assert_equal(false, @cmock_generator_utils_simple.ptr_or_str?('int')) + assert_equal(true, @cmock_generator_utils_simple.ptr_or_str?('int*')) + assert_equal(true, @cmock_generator_utils_simple.ptr_or_str?('char*')) + end + + it "add code for a base expectation with no plugins" do + expected = + " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + + " cmock_call_instance->LineNumber = cmock_line;\n" + output = @cmock_generator_utils_simple.code_add_base_expectation("Apple") + assert_equal(expected, output) + end + + it "add code for a base expectation with all plugins" do + expected = + " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + + " Mock.Apple_IgnoreBool = (char)0;\n" + + " cmock_call_instance->LineNumber = cmock_line;\n" + + " cmock_call_instance->CallOrder = ++GlobalExpectCount;\n" + + " cmock_call_instance->ExceptionToThrow = CEXCEPTION_NONE;\n" + output = @cmock_generator_utils_complex.code_add_base_expectation("Apple", true) + assert_equal(expected, output) + end + + it "add code for a base expectation with all plugins and ordering not supported" do + expected = + " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_Apple_CALL_INSTANCE));\n" + + " CMOCK_Apple_CALL_INSTANCE* cmock_call_instance = (CMOCK_Apple_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n" + + " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n" + + " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n" + + " Mock.Apple_CallInstance = CMock_Guts_MemChain(Mock.Apple_CallInstance, cmock_guts_index);\n" + + " Mock.Apple_IgnoreBool = (char)0;\n" + + " cmock_call_instance->LineNumber = cmock_line;\n" + + " cmock_call_instance->ExceptionToThrow = CEXCEPTION_NONE;\n" + output = @cmock_generator_utils_complex.code_add_base_expectation("Apple", false) + assert_equal(expected, output) + end + + it "add argument expectations for values when no array plugin" do + arg1 = { :name => "Orange", :const? => false, :type => 'int', :ptr? => false } + expected1 = " cmock_call_instance->Expected_Orange = Orange;\n" + + arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => false } + expected2 = " cmock_call_instance->Expected_Lemon = Lemon;\n" + + arg3 = { :name => "Kiwi", :const? => false, :type => 'KIWI_T*', :ptr? => true } + expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n" + + arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false } + expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + + " sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" + + assert_equal(expected1, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg1)) + assert_equal(expected2, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg2)) + assert_equal(expected3, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg3)) + assert_equal(expected4, @cmock_generator_utils_simple.code_add_an_arg_expectation(arg4)) + end + + it "add argument expectations for values when array plugin enabled" do + arg1 = { :name => "Orange", :const? => false, :type => 'int', :ptr? => false } + expected1 = " cmock_call_instance->Expected_Orange = Orange;\n" + + " cmock_call_instance->IgnoreArg_Orange = 0;\n" + + arg2 = { :name => "Lemon", :const? => true, :type => 'const char*', :ptr? => false } + expected2 = " cmock_call_instance->Expected_Lemon = Lemon;\n" + + " cmock_call_instance->Expected_Lemon_Depth = Lemon_Depth;\n" + + " cmock_call_instance->IgnoreArg_Lemon = 0;\n" + + arg3 = { :name => "Kiwi", :const? => false, :type => 'KIWI_T*', :ptr? => true } + expected3 = " cmock_call_instance->Expected_Kiwi = Kiwi;\n" + + " cmock_call_instance->Expected_Kiwi_Depth = Kiwi_Depth;\n" + + " cmock_call_instance->IgnoreArg_Kiwi = 0;\n" + + " cmock_call_instance->ReturnThruPtr_Kiwi_Used = 0;\n" + + arg4 = { :name => "Lime", :const? => false, :type => 'LIME_T', :ptr? => false } + expected4 = " memcpy((void*)(&cmock_call_instance->Expected_Lime), (void*)(&Lime),\n" + + " sizeof(LIME_T[sizeof(Lime) == sizeof(LIME_T) ? 1 : -1])); /* add LIME_T to :treat_as_array if this causes an error */\n" + + " cmock_call_instance->IgnoreArg_Lime = 0;\n" + + assert_equal(expected1, @cmock_generator_utils_complex.code_add_an_arg_expectation(arg1)) + assert_equal(expected2, @cmock_generator_utils_complex.code_add_an_arg_expectation(arg2, 'Lemon_Depth')) + assert_equal(expected3, @cmock_generator_utils_complex.code_add_an_arg_expectation(arg3, 'Lemon_Depth')) + assert_equal(expected4, @cmock_generator_utils_complex.code_add_an_arg_expectation(arg4)) + end + + it 'not have an argument loader when the function has no arguments' do + function = { :name => "Melon", :args_string => "void" } + + assert_equal("", @cmock_generator_utils_complex.code_add_argument_loader(function)) + end + + it 'create an argument loader when the function has arguments' do + function = { :name => "Melon", + :args_string => "stuff", + :args => [test_arg[:int_ptr], test_arg[:mytype], test_arg[:string]] + } + expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, stuff)\n{\n" + + " cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" + + " memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + + " sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" + + " cmock_call_instance->Expected_MyStr = MyStr;\n" + + "}\n\n" + assert_equal(expected, @cmock_generator_utils_simple.code_add_argument_loader(function)) + end + + it 'create an argument loader when the function has arguments supporting arrays' do + function = { :name => "Melon", + :args_string => "stuff", + :args => [test_arg[:int_ptr], test_arg[:mytype], test_arg[:string]] + } + expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, int* MyIntPtr, int MyIntPtr_Depth, const MY_TYPE MyMyType, const char* MyStr)\n{\n" + + " cmock_call_instance->Expected_MyIntPtr = MyIntPtr;\n" + + " cmock_call_instance->Expected_MyIntPtr_Depth = MyIntPtr_Depth;\n" + + " cmock_call_instance->IgnoreArg_MyIntPtr = 0;\n" + + " cmock_call_instance->ReturnThruPtr_MyIntPtr_Used = 0;\n" + + " memcpy((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType),\n" + + " sizeof(MY_TYPE[sizeof(MyMyType) == sizeof(MY_TYPE) ? 1 : -1])); /* add MY_TYPE to :treat_as_array if this causes an error */\n" + + " cmock_call_instance->IgnoreArg_MyMyType = 0;\n" + + " cmock_call_instance->Expected_MyStr = MyStr;\n" + + " cmock_call_instance->IgnoreArg_MyStr = 0;\n" + + "}\n\n" + assert_equal(expected, @cmock_generator_utils_complex.code_add_argument_loader(function)) + end + + it 'create an argument loader when the function has pointer arguments supporting arrays' do + function = { :name => "Melon", + :args_string => "stuff", + :args => [test_arg[:const_ptr], test_arg[:double_ptr]] + } + expected = "void CMockExpectParameters_Melon(CMOCK_Melon_CALL_INSTANCE* cmock_call_instance, int* const MyConstPtr, int MyConstPtr_Depth, int const** MyDoublePtr, int MyDoublePtr_Depth)\n{\n" + + " cmock_call_instance->Expected_MyConstPtr = MyConstPtr;\n" + + " cmock_call_instance->Expected_MyConstPtr_Depth = MyConstPtr_Depth;\n" + + " cmock_call_instance->IgnoreArg_MyConstPtr = 0;\n" + + " cmock_call_instance->ReturnThruPtr_MyConstPtr_Used = 0;\n" + + " cmock_call_instance->Expected_MyDoublePtr = MyDoublePtr;\n" + + " cmock_call_instance->Expected_MyDoublePtr_Depth = MyDoublePtr_Depth;\n" + + " cmock_call_instance->IgnoreArg_MyDoublePtr = 0;\n" + + "}\n\n" + assert_equal(expected, @cmock_generator_utils_complex.code_add_argument_loader(function)) + end + + it "not call argument loader if there are no arguments to actually use for this function" do + function = { :name => "Pineapple", :args_string => "void" } + + assert_equal("", @cmock_generator_utils_complex.code_call_argument_loader(function)) + end + + it 'call an argument loader when the function has arguments' do + function = { :name => "Pineapple", + :args_string => "stuff", + :args => [test_arg[:int_ptr], test_arg[:mytype], test_arg[:string]] + } + expected = " CMockExpectParameters_Pineapple(cmock_call_instance, MyIntPtr, MyMyType, MyStr);\n" + assert_equal(expected, @cmock_generator_utils_simple.code_call_argument_loader(function)) + end + + it 'call an argument loader when the function has arguments with arrays' do + function = { :name => "Pineapple", + :args_string => "stuff", + :args => [test_arg[:int_ptr], test_arg[:mytype], test_arg[:string]] + } + expected = " CMockExpectParameters_Pineapple(cmock_call_instance, MyIntPtr, 1, MyMyType, MyStr);\n" + assert_equal(expected, @cmock_generator_utils_complex.code_call_argument_loader(function)) + end + + it 'handle a simple assert when requested' do + function = { :name => 'Pear' } + arg = test_arg[:int] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyInt);\n" + + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT', ''], ['int'] + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle a pointer comparison when configured to do so' do + function = { :name => 'Pear' } + arg = test_arg[:int_ptr] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyIntPtr);\n" + + " UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, CMockStringMismatch);\n" + + " }\n" + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle const char as string compares ' do + function = { :name => 'Pear' } + arg = test_arg[:string] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyStr);\n" + + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['const char*'] + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types as memory compares when we have no better way to do it' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(&cmock_call_instance->Expected_MyMyType), (void*)(&MyMyType), sizeof(MY_TYPE), cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MEMORY','&'], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types with custom handlers when available, even if they do not support the extra message' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE',''], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle pointers to custom types with array handlers, even if the array extension is turned off' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY','&'], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_simple.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle a simple assert when requested with array plugin enabled' do + function = { :name => 'Pear' } + arg = test_arg[:int] + expected = " if (!cmock_call_instance->IgnoreArg_MyInt)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyInt);\n" + + " UNITY_TEST_ASSERT_EQUAL_INT(cmock_call_instance->Expected_MyInt, MyInt, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT',''], ['int'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle an array comparison with array plugin enabled' do + function = { :name => 'Pear' } + arg = test_arg[:int_ptr] + expected = " if (!cmock_call_instance->IgnoreArg_MyIntPtr)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyIntPtr);\n" + + " if (cmock_call_instance->Expected_MyIntPtr == NULL)\n" + + " { UNITY_TEST_ASSERT_NULL(MyIntPtr, cmock_line, CMockStringExpNULL); }\n" + + " else if (cmock_call_instance->Expected_MyIntPtr_Depth == 0)\n" + + " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_line, CMockStringMismatch); }\n" + + " else\n" + + " { UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(cmock_call_instance->Expected_MyIntPtr, MyIntPtr, cmock_call_instance->Expected_MyIntPtr_Depth, cmock_line, CMockStringMismatch); }\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_INT_ARRAY',''], ['int*'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle const char as string compares with array plugin enabled' do + function = { :name => 'Pear' } + arg = test_arg[:string] + expected = " if (!cmock_call_instance->IgnoreArg_MyStr)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyStr);\n" + + " UNITY_TEST_ASSERT_EQUAL_STRING(cmock_call_instance->Expected_MyStr, MyStr, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_STRING',''], ['const char*'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types as memory compares when we have no better way to do it with array plugin enabled' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " if (cmock_call_instance->Expected_MyMyType == NULL)\n" + + " { UNITY_TEST_ASSERT_NULL(MyMyType, cmock_line, CMockStringExpNULL); }\n" + + " else\n" + + " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(cmock_call_instance->Expected_MyMyType), (void*)(MyMyType), sizeof(MY_TYPE), 1, cmock_line, CMockStringMismatch); }\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY', ''], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types with custom handlers when available, even if they do not support the extra message with array plugin enabled' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE(cmock_call_instance->Expected_MyMyType, MyMyType, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE', ''], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types with array handlers when array plugin is enabled' do + function = { :name => 'Pear' } + arg = test_arg[:mytype_ptr] + expected = " if (!cmock_call_instance->IgnoreArg_MyMyTypePtr)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyTypePtr);\n" + + " if (cmock_call_instance->Expected_MyMyTypePtr == NULL)\n" + + " { UNITY_TEST_ASSERT_NULL(MyMyTypePtr, cmock_line, CMockStringExpNULL); }\n" + + " else if (cmock_call_instance->Expected_MyMyTypePtr_Depth == 0)\n" + + " { UNITY_TEST_ASSERT_EQUAL_PTR(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_line, CMockStringMismatch); }\n" + + " else\n" + + " { UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(cmock_call_instance->Expected_MyMyTypePtr, MyMyTypePtr, cmock_call_instance->Expected_MyMyTypePtr_Depth, cmock_line, CMockStringMismatch); }\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY', ''], ['MY_TYPE*'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end + + it 'handle custom types with array handlers when array plugin is enabled for non-array types' do + function = { :name => 'Pear' } + arg = test_arg[:mytype] + expected = " if (!cmock_call_instance->IgnoreArg_MyMyType)\n" + + " {\n" + + " UNITY_SET_DETAILS(CMockString_Pear,CMockString_MyMyType);\n" + + " UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY(&cmock_call_instance->Expected_MyMyType, &MyMyType, 1, cmock_line, CMockStringMismatch);\n" + + " }\n" + @unity_helper.expect :nil?, false + @unity_helper.expect :get_helper, ['UNITY_TEST_ASSERT_EQUAL_MY_TYPE_ARRAY', '&'], ['MY_TYPE'] + assert_equal(expected, @cmock_generator_utils_complex.code_verify_an_arg_expectation(function, arg)) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_header_parser_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_header_parser_test.rb new file mode 100644 index 000000000..5d5d2fcca --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_header_parser_test.rb @@ -0,0 +1,2717 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +$ThisIsOnlyATest = true + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_header_parser' + +describe CMockHeaderParser, "Verify CMockHeaderParser Module" do + + before do + create_mocks :config + @test_name = 'test_file.h' + @config.expect :strippables, ["STRIPPABLE"] + @config.expect :attributes, ['__ramfunc', 'funky_attrib', 'SQLITE_API'] + @config.expect :c_calling_conventions, ['__stdcall'] + @config.expect :treat_as_void, ['MY_FUNKY_VOID'] + @config.expect :treat_as, { "BANJOS" => "INT", "TUBAS" => "HEX16"} + @config.expect :treat_as_array, {"IntArray" => "int", "Book" => "Page"} + @config.expect :when_no_prototypes, :error + @config.expect :verbosity, 1 + @config.expect :treat_externs, :exclude + @config.expect :treat_inlines, :exclude + @config.expect :inline_function_patterns, ['(static\s+inline|inline\s+static)\s*', '(\bstatic\b|\binline\b)\s*'] + @config.expect :array_size_type, ['int', 'size_t'] + @config.expect :array_size_name, 'size|len' + + @parser = CMockHeaderParser.new(@config) + end + + after do + end + + it "create and initialize variables to defaults appropriately" do + assert_equal([], @parser.funcs) + assert_equal(['const', '__ramfunc', 'funky_attrib', 'SQLITE_API'], @parser.c_attributes) + assert_equal(['void','MY_FUNKY_VOID'], @parser.treat_as_void) + end + + it "strip out line comments" do + source = + " abcd;\n" + + "// hello;\n" + + "who // is you\n" + + expected = + [ + "abcd", + "who" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove block comments" do + source = + " no_comments;\n" + + "// basic_line_comment;\n" + + "/* basic_block_comment;*/\n" + + "pre_block; /* start_of_block_comment;\n" + + "// embedded_line_comment_in_block_comment; */\n" + + "// /* commented_out_block_comment_line\n" + + "shown_because_block_comment_invalid_from_line_comment;\n" + + "// */\n" + + "//* shorter_commented_out_block_comment_line; \n" + + "shown_because_block_comment_invalid_from_shorter_line_comment;\n" + + "/*/\n" + + "not_shown_because_line_above_started_comment;\n" + + "//*/\n" + + "/* \n" + + "not_shown_because_block_comment_started_this_time;\n" + + "/*/\n" + + "shown_because_line_above_ended_comment_this_time;\n" + + "//*/\n" + + expected = + [ + "no_comments", + "pre_block", + "shown_because_block_comment_invalid_from_line_comment", + "shown_because_block_comment_invalid_from_shorter_line_comment", + "shown_because_line_above_ended_comment_this_time" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove strippables from the beginning or end of function declarations" do + source = + "void* my_calloc(size_t, size_t) STRIPPABLE;\n" + + "void\n" + + " my_realloc(void*, size_t) STRIPPABLE;\n" + + "extern int\n" + + " my_printf (void *my_object, const char *my_format, ...)\n" + + " STRIPPABLE;\n" + + " void STRIPPABLE universal_handler ();\n" + + expected = + [ + "void* my_calloc(size_t, size_t)", + "void my_realloc(void*, size_t)", + "void universal_handler()" + ] + + assert_equal(expected, @parser.import_source(source)) + end + + it "remove gcc's function __attribute__'s" do + source = + "void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2)));\n" + + "void\n" + + " my_realloc(void*, size_t) __attribute__((alloc_size(2)));\n" + + "extern int\n" + + " my_printf (void *my_object, const char *my_format, ...)\n" + + " __attribute__ ((format (printf, 2, 3)));\n" + + " void __attribute__ ((interrupt)) universal_handler ();\n" + + expected = + [ + "void* my_calloc(size_t, size_t)", + "void my_realloc(void*, size_t)", + "void universal_handler()" + ] + + assert_equal(expected, @parser.import_source(source)) + end + + it "remove preprocessor directives" do + source = + "#when stuff_happens\n" + + "#ifdef _TEST\n" + + "#pragma stack_switch" + + expected = [] + + assert_equal(expected, @parser.import_source(source)) + end + + + it "remove assembler pragma sections" do + source = + " #pragma\tasm\n" + + " .foo\n" + + " lda %m\n" + + " nop\n" + + "# pragma endasm \n" + + "foo" + + expected = ["foo"] + + assert_equal(expected, @parser.import_source(source)) + end + + + it "smush lines together that contain continuation characters" do + source = + "hoo hah \\\n" + + "when \\ \n" + + expected = + [ + "hoo hah when" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + + it "remove C macro definitions" do + source = + "#define this is the first line\\\n" + + "and the second\\\n" + + "and the third that should be removed\n" + + "but I'm here\n" + + expected = ["but I'm here"] + + assert_equal(expected, @parser.import_source(source)) + end + + + it "remove typedef statements" do + source = + "typedef uint32 (unsigned int);\n" + + "const typedef int INT;\n" + + "int notatypedef;\n" + + "int typedef_isnt_me;\n" + + " typedef who cares what really comes here \n" + # exercise multiline typedef + " continuation;\n" + + "this should remain!;\n" + + "typedef blah bleh;\n" + + "typedef struct shell_command_struct {\n" + + " char_ptr COMMAND;\n" + + " int_32 (*SHELL_FUNC)(int_32 argc);\n" + + "} SHELL_COMMAND_STRUCT, * SHELL_COMMAND_PTR;\n" + + "typedef struct shell_command_struct {\n" + + " char_ptr COMMAND;\n" + + " int_32 (*SHELL_FUNC)(int_32 argc, char_ptr argv[]);\n" + + "} SHELL_COMMAND_STRUCT, * SHELL_COMMAND_PTR;\n" + + "typedef struct shell_command_struct {\n" + + " char_ptr COMMAND;\n" + + " int_32 (*SHELL_FUNC)(int_32 argc);\n" + + "};\n" + + expected = + [ + "int notatypedef", + "int typedef_isnt_me", + "this should remain!" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + + it "remove enum statements" do + source = + "enum _NamedEnum {\n" + + " THING1 = (0x0001),\n" + + " THING2 = (0x0001 << 5),\n" + + "}ListOValues;\n\n" + + "don't delete me!!\n" + + " modifier_str enum _NamedEnum {THING1 = (0x0001), THING2 = (0x0001 << 5)} ListOValues;\n\n" + + "typedef enum {\n" + + " THING1,\n" + + " THING2,\n" + + "} Thinger;\n" + + "or me!!\n" + + assert_equal(["don't delete me!! or me!!"], @parser.import_source(source).map!{|s|s.strip}) + end + + + it "remove union statements" do + source = + "union _NamedDoohicky {\n" + + " unsigned int a;\n" + + " char b;\n" + + "} Doohicky;\n\n" + + "I want to live!!\n" + + "some_modifier union { unsigned int a; char b;} Whatever;\n" + + "typedef union {\n" + + " unsigned int a;\n" + + " char b;\n" + + "} Whatever;\n" + + "me too!!\n" + + assert_equal(["I want to live!! me too!!"], @parser.import_source(source).map!{|s|s.strip}) + end + + + it "remove struct statements" do + source = + "struct _NamedStruct1 {\n" + + " unsigned int a;\n" + + " signed long int b;\n" + + "} Thing ;\n\n" + + "extern struct ForwardDeclared_t TestDataType1;\n" + + "void foo(void);\n" + + "struct\n"+ + " MultilineForwardDeclared_t\n" + + " TestDataType2;\n" + + "struct THINGER foo(void);\n" + + "typedef struct {\n" + + " unsigned int a;\n" + + " signed char b;\n" + + "}Thinger;\n" + + "I want to live!!\n" + + assert_equal(["void foo(void)", "struct THINGER foo(void)", "I want to live!!"], + @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove externed and inline functions" do + source = + " extern uint32 foobar(unsigned int);\n" + + "uint32 extern_name_func(unsigned int);\n" + + "uint32 funcinline(unsigned int);\n" + + "extern void bar(unsigned int);\n" + + "inline void bar(unsigned int);\n" + + "extern\n" + + "void kinda_ugly_on_the_next_line(unsigned int);\n" + + expected = + [ + "uint32 extern_name_func(unsigned int)", + "uint32 funcinline(unsigned int)" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove function definitions but keep function declarations" do + source = + "uint32 func_with_decl_a(unsigned int);\n" + + "uint32 func_with_decl_a(unsigned int a) { return a; }\n" + + "uint32 func_with_decl_b(unsigned int);\n" + + "uint32 func_with_decl_b(unsigned int a)\n" + + "{\n" + + " bar((unsigned int) a);\n" + + " stripme(a);\n" + + "}\n" + + expected = + [ + "uint32 func_with_decl_a(unsigned int)", + "uint32 func_with_decl_a", #okay. it's not going to be interpretted as another function + "uint32 func_with_decl_b(unsigned int)", + "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove function definitions with nested braces but keep function declarations" do + source = + "uint32 func_with_decl_a(unsigned int);\n" + + "uint32 func_with_decl_a(unsigned int a) {\n" + + " while (stuff) {\n" + + " not_a_definition1(void);\n" + + " }\n" + + " not_a_definition2(blah, bleh);\n" + + " return a;\n" + + "}\n" + + "uint32 func_with_decl_b(unsigned int);\n" + + "uint32 func_with_decl_b(unsigned int a)\n" + + "{\n" + + " bar((unsigned int) a);\n" + + " stripme(a);\n" + + "}\n" + + "uint32 func_with_decl_c(unsigned int);\n" + + "uint32 func_with_decl_c(unsigned int a)\n" + + "{\n" + + " if(a > 0)\n" + + " {\n" + + " return 1;\n" + + " }\n" + + " else\n"+ + " {\n" + + " return 2;\n" + + " }\n" + + "}\n" + + expected = + [ + "uint32 func_with_decl_a(unsigned int)", + "uint32 func_with_decl_a", #okay. it's not going to be interpretted as another function + "uint32 func_with_decl_b(unsigned int)", + "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function + "uint32 func_with_decl_c(unsigned int)", + "uint32 func_with_decl_c", #okay. it's not going to be interpretted as another function + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove a fully defined inline function" do + source = + "inline void foo(unsigned int a) { oranges = a; }\n" + + "inline void bar(unsigned int a) { apples = a; };\n" + + "inline void bar(unsigned int a)\n" + + "{" + + " bananas = a;\n" + + "}" + + # ensure it's expected type of exception + assert_raises RuntimeError do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + + it "remove a fully defined inline function that is multiple lines" do + source = + "inline void bar(unsigned int a)\n" + + "{" + + " bananas = a;\n" + + " grapes = a;\n" + + " apples(bananas, grapes);\n" + + "}" + + # ensure it's expected type of exception + assert_raises RuntimeError do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + + it "remove a fully defined inline function that contains nested braces" do + source = + "inline void bar(unsigned int a)\n" + + "{" + + " apples(bananas, grapes);\n" + + " if (bananas == a)\n" + + " {\n" + + " oranges(a);\n" + + " grapes = a;\n" + + " }\n" + + " grapefruit(bananas, grapes);\n" + + "}" + + # ensure it's expected type of exception + assert_raises RuntimeError do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + + it "remove just inline functions if externs to be included" do + source = + " extern uint32 foobar(unsigned int);\n" + + "uint32 extern_name_func(unsigned int);\n" + + "uint32 funcinline(unsigned int);\n" + + "extern void bar(unsigned int);\n" + + "inline void bar(unsigned int);\n" + + "extern\n" + + "void kinda_ugly_on_the_next_line(unsigned int);\n" + + expected = + [ "extern uint32 foobar(unsigned int)", + "uint32 extern_name_func(unsigned int)", + "uint32 funcinline(unsigned int)", + "extern void bar(unsigned int)", + "extern void kinda_ugly_on_the_next_line(unsigned int)" + ] + + @parser.treat_externs = :include + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "leave inline functions if inline to be included" do + source = + "extern uint32 foobar(unsigned int);\n" + + "uint32 extern_name_func(unsigned int);\n" + + "uint32 funcinline(unsigned int);\n" + + "inline void inlineBar(unsigned int);\n" + + "extern int extern_bar(void);\n" + + "static inline void staticinlineBar(unsigned int);\n" + + "static inline void bar(unsigned int);\n" + + "static inline void bar(unsigned int)\n" + + "{\n" + + " // NOP\n" + + "}\n" + + expected = + [ "uint32 extern_name_func(unsigned int)", + "uint32 funcinline(unsigned int)", + "void inlineBar(unsigned int)", + "void staticinlineBar(unsigned int)", + "void bar(unsigned int)" + ] + + @parser.treat_inlines = :include + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "leave inline and extern functions if inline and extern to be included" do + source = + "extern uint32 foobar(unsigned int);\n" + + "uint32 extern_name_func(unsigned int);\n" + + "uint32 funcinline(unsigned int);\n" + + "inline void inlineBar(unsigned int);\n" + + "extern int extern_bar(void);\n" + + "static inline void staticinlineBar(unsigned int);\n" + + "static inline void bar(unsigned int);\n" + + "static inline void bar(unsigned int)\n" + + "{\n" + + " // NOP\n" + + "}\n" + + expected = + [ "extern uint32 foobar(unsigned int)", + "uint32 extern_name_func(unsigned int)", + "uint32 funcinline(unsigned int)", + "void inlineBar(unsigned int)", + "extern int extern_bar(void)", + "void staticinlineBar(unsigned int)", + "void bar(unsigned int)" + ] + + @parser.treat_externs = :include + @parser.treat_inlines = :include + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "Include inline functions that contain user defined inline function formats" do + source = + "uint32 foo(unsigned int);\n" + + "uint32 bar(unsigned int);\n" + + "inline void inlineBar(void)\n" + + "{\n" + + " return 43;\n" + + "}\n" + + "static __inline__ __attribute__ ((always_inline)) int alwaysinlinefunc(int a)\n" + + "{\n" + + " return a + inlineBar();\n" + + "}\n" + + "static __inline__ void inlinebar(unsigned int)\n" + + "{\n" + + " int a = alwaysinlinefunc()\n" + + "}\n" + + expected = + [ + "uint32 foo(unsigned int)", + "uint32 bar(unsigned int)", + "void inlineBar(void)", + "int alwaysinlinefunc(int a)", + "void inlinebar(unsigned int)" + ] + + @parser.treat_inlines = :include + @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__', '\binline\b'] + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + it "remove defines" do + source = + "#define whatever you feel like defining\n" + + "void hello(void);\n" + + "#DEFINE I JUST DON'T CARE\n" + + "#deFINE\n" + + "#define get_foo() \\\n ((Thing)foo.bar)" # exercise multiline define + + expected = + [ + "void hello(void)", + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + + it "remove keywords that would keep things from going smoothly in the future" do + source = + "const int TheMatrix(register int Trinity, unsigned int *restrict Neo)" + + expected = + [ + "const int TheMatrix(int Trinity, unsigned int * Neo)", + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + + # some code actually typedef's void even though it's not ANSI C and is, frankly, weird + # since cmock treats void specially, we can't let void be obfuscated + it "handle odd case of typedef'd void returned" do + source = "MY_FUNKY_VOID FunkyVoidReturned(int a)" + expected = { :var_arg=>nil, + :name=>"FunkyVoidReturned", + :unscoped_name=>"FunkyVoidReturned", + :namespace=>[], + :class=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :modifier=>"", + :contains_ptr? => false, + :args=>[{:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false}], + :args_string=>"int a", + :args_call=>"a"} + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "handle odd case of typedef'd void as arg" do + source = "int FunkyVoidAsArg(MY_FUNKY_VOID)" + expected = { :var_arg=>nil, + :name=>"FunkyVoidAsArg", + :unscoped_name=>"FunkyVoidAsArg", + :namespace=>[], + :class=>nil, + :return=>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :modifier=>"", + :contains_ptr? => false, + :args=>[], + :args_string=>"void", + :args_call=>"" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "handle odd case of typedef'd void as arg pointer" do + source = "char FunkyVoidPointer(MY_FUNKY_VOID* bluh)" + expected = { :var_arg=>nil, + :name=>"FunkyVoidPointer", + :unscoped_name=>"FunkyVoidPointer", + :namespace=>[], + :class=>nil, + :return=>{ :type => "char", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "char cmock_to_return", + :void? => false + }, + :modifier=>"", + :contains_ptr? => true, + :args=>[{:type=>"MY_FUNKY_VOID*", :name=>"bluh", :ptr? => true, :const? => false, :const_ptr? => false}], + :args_string=>"MY_FUNKY_VOID* bluh", + :args_call=>"bluh" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + + it "strip default values from function parameter lists" do + source = + "void Foo(int a = 57, float b=37.52, char c= 'd', char* e=\"junk\");\n" + + expected = + [ + "void Foo(int a, float b, char c, char* e)" + ] + + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) + end + + + it "raise upon empty file" do + source = '' + + # ensure it's expected type of exception + assert_raises RuntimeError do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + + it "clean up module names that contain spaces, dashes, and such" do + source = 'void meh(int (*func)(int));' + + retval = @parser.parse("C:\Ugly Module-Name", source) + assert (retval[:typedefs][0] =~ /CUglyModuleName/) + end + + it "raise upon no function prototypes found in file" do + source = + "typedef void SILLY_VOID_TYPE1;\n" + + "typedef (void) SILLY_VOID_TYPE2 ;\n" + + "typedef ( void ) (*FUNCPTR)(void);\n\n" + + "#define get_foo() \\\n ((Thing)foo.bar)" + + # ensure it's expected type of exception + assert_raises(RuntimeError) do + @parser.parse("module", source) + end + + assert_equal([], @parser.funcs) + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert_equal("ERROR: No function prototypes found!", e.message) + end + end + + + it "raise upon prototype parsing failure" do + source = "void (int, )" + + # ensure it's expected type of exception + assert_raises(RuntimeError) do + @parser.parse("module", source) + end + + # verify exception message + begin + @parser.parse("module", source) + rescue RuntimeError => e + assert(e.message.include?("Failed Parsing Declaration Prototype!")) + end + end + + it "extract and return function declarations with retval and args" do + + source = "int Foo(int a, unsigned int b)" + expected = { :var_arg=>nil, + :name=>"Foo", + :unscoped_name=>"Foo", + :namespace=>[], + :class=>nil, + :return=>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int a, unsigned int b", + :args_call=>"a, b" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "extract and return function declarations with no retval" do + + source = "void FunkyChicken( uint la, int de, bool da)" + expected = { :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyChicken", + :unscoped_name=>"FunkyChicken", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"uint", :name=>"la", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"de", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"bool", :name=>"da", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"uint la, int de, bool da", + :args_call=>"la, de, da" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "extract and return function declarations with implied voids" do + + source = "void tat()" + expected = { :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"tat", + :unscoped_name=>"tat", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ ], + :args_string=>"void", + :args_call=>"" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "extract modifiers properly" do + + source = "const int TheMatrix(int Trinity, unsigned int * Neo)" + expected = { :var_arg=>nil, + :return=>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => true, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"TheMatrix", + :unscoped_name=>"TheMatrix", + :namespace=>[], + :class=>nil, + :modifier=>"const", + :contains_ptr? => true, + :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Trinity, unsigned int* Neo", + :args_call=>"Trinity, Neo" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "extract c calling conventions properly" do + + source = "const int __stdcall TheMatrix(int Trinity, unsigned int * Neo)" + expected = { :var_arg=>nil, + :return=>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => true, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"TheMatrix", + :unscoped_name=>"TheMatrix", + :namespace=>[], + :class=>nil, + :modifier=>"const", + :c_calling_convention=>"__stdcall", + :contains_ptr? => true, + :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Trinity, unsigned int* Neo", + :args_call=>"Trinity, Neo" } + assert_equal(expected, @parser.parse_declaration(source)) + end + + it "extract and return function declarations inside namespace and class" do + source = "int Foo(int a, unsigned int b)" + expected = { :var_arg=>nil, + :name=>"ns1_ns2_Bar_Foo", + :unscoped_name=>"Foo", + :class=>"Bar", + :namespace=>["ns1", "ns2"], + :return=>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int a, unsigned int b", + :args_call=>"a, b" } + assert_equal(expected, @parser.parse_declaration(source, ["ns1", "ns2"], "Bar")) + end + + it "fully parse multiple prototypes" do + + source = "const int TheMatrix(int Trinity, unsigned int * Neo);\n" + + "int Morpheus(int, unsigned int*);\n" + + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => true, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"TheMatrix", + :unscoped_name=>"TheMatrix", + :namespace=>[], + :class=>nil, + :modifier=>"const", + :contains_ptr? => true, + :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Trinity, unsigned int* Neo", + :args_call=>"Trinity, Neo" }, + { :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"Morpheus", + :unscoped_name=>"Morpheus", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"int", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"int cmock_arg1, unsigned int* cmock_arg2", + :args_call=>"cmock_arg1, cmock_arg2" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "not extract for mocking multiply defined prototypes" do + + source = "const int TheMatrix(int Trinity, unsigned int * Neo);\n" + + "const int TheMatrix(int, unsigned int*);\n" + + expected = [{ :var_arg=>nil, + :name=>"TheMatrix", + :unscoped_name=>"TheMatrix", + :namespace=>[], + :class=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => true, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :modifier=>"const", + :contains_ptr? => true, + :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Trinity, unsigned int* Neo", + :args_call=>"Trinity, Neo" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "should properly handle const before return type" do + sources = [ + "const int * PorkRoast(void);\n", + "const int* PorkRoast(void);\n", + "const int *PorkRoast(void);\n" + ] + + expected = [{ :var_arg => nil, + :name => "PorkRoast", + :unscoped_name => "PorkRoast", + :namespace=>[], + :class=>nil, + :return => { :type => "const int*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => true, + :const_ptr? => false, + :str => "const int* cmock_to_return", + :void? => false + }, + :modifier => "", + :contains_ptr? => false, + :args => [], + :args_string => "void", + :args_call => "" + }] + + sources.each do |source| + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + end + + it "should properly handle const before return type" do + sources = [ + "int const * PorkRoast(void);\n", + "int const* PorkRoast(void);\n", + "int const *PorkRoast(void);\n" + ] + + expected = [{ :var_arg => nil, + :name => "PorkRoast", + :unscoped_name => "PorkRoast", + :namespace=>[], + :class=>nil, + :return => { :type => "int const*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => true, + :const_ptr? => false, + :str => "int const* cmock_to_return", + :void? => false + }, + :modifier => "", + :contains_ptr? => false, + :args => [], + :args_string => "void", + :args_call => "" + }] + + sources.each do |source| + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + end + + it "should properly handle const applied after asterisk in return type (not legal C, but sometimes used)" do + + source = "int * const PorkRoast(void);\n" + + expected = [{ :var_arg=>nil, + :name=>"PorkRoast", + :unscoped_name=>"PorkRoast", + :namespace=>[], + :class=>nil, + :return=> { :type => "int*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => false, + :const_ptr? => true, + :str => "int* cmock_to_return", + :void? => false + }, + :modifier=>"const", + :contains_ptr? => false, + :args=>[], + :args_string=>"void", + :args_call=>"" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "properly parse const and pointer argument types with no arg names" do + + source = "void foo(int const*, int*const, const int*, const int*const, int const*const, int*, int, const int);\n" + + expected = [{ :name => "foo", + :unscoped_name => "foo", + :namespace=>[], + :class=>nil, + :modifier => "", + :return => { :type => "void", + :name => "cmock_to_return", + :str => "void cmock_to_return", + :void? => true, + :ptr? => false, + :const? => false, + :const_ptr? => false + }, + :var_arg => nil, + :args_string => "int const* cmock_arg1, int* const cmock_arg2, const int* cmock_arg3, const int* const cmock_arg4, " + + "int const* const cmock_arg5, int* cmock_arg6, int cmock_arg7, const int cmock_arg8", + :args => [{ :type=>"int const*", :name => "cmock_arg1", :ptr? => true, :const? => true, :const_ptr? => false }, + { :type=>"int*", :name => "cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => true }, + { :type=>"const int*", :name => "cmock_arg3", :ptr? => true, :const? => true, :const_ptr? => false }, + { :type=>"const int*", :name => "cmock_arg4", :ptr? => true, :const? => true, :const_ptr? => true }, + { :type=>"int const*", :name => "cmock_arg5", :ptr? => true, :const? => true, :const_ptr? => true }, + { :type=>"int*", :name => "cmock_arg6", :ptr? => true, :const? => false, :const_ptr? => false }, + { :type=>"int", :name => "cmock_arg7", :ptr? => false, :const? => false, :const_ptr? => false }, + { :type=>"int", :name => "cmock_arg8", :ptr? => false, :const? => true, :const_ptr? => false }], + :args_call => "cmock_arg1, cmock_arg2, cmock_arg3, cmock_arg4, cmock_arg5, cmock_arg6, cmock_arg7, cmock_arg8", + :contains_ptr? => true + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "properly parse const and pointer argument types with arg names" do + + source = "void bar(int const* param1, int*const param2, const int* param3, const int*const param4,\n" + + " int const*const param5, int*param6, int param7, const int param8);\n" + + expected = [{ :name => "bar", + :unscoped_name => "bar", + :namespace=>[], + :class=>nil, + :modifier => "", + :return => { :type => "void", + :name => "cmock_to_return", + :str => "void cmock_to_return", + :void? => true, + :ptr? => false, + :const? => false, + :const_ptr? => false + }, + :var_arg => nil, + :args_string => "int const* param1, int* const param2, const int* param3, const int* const param4, " + + "int const* const param5, int* param6, int param7, const int param8", + :args => [{ :type=>"int const*", :name => "param1", :ptr? => true, :const? => true, :const_ptr? => false }, + { :type=>"int*", :name => "param2", :ptr? => true, :const? => false, :const_ptr? => true }, + { :type=>"const int*", :name => "param3", :ptr? => true, :const? => true, :const_ptr? => false }, + { :type=>"const int*", :name => "param4", :ptr? => true, :const? => true, :const_ptr? => true }, + { :type=>"int const*", :name => "param5", :ptr? => true, :const? => true, :const_ptr? => true }, + { :type=>"int*", :name => "param6", :ptr? => true, :const? => false, :const_ptr? => false }, + { :type=>"int", :name => "param7", :ptr? => false, :const? => false, :const_ptr? => false }, + { :type=>"int", :name => "param8", :ptr? => false, :const? => true, :const_ptr? => false }], + :args_call => "param1, param2, param3, param4, param5, param6, param7, param8", + :contains_ptr? => true + }].freeze + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "converts typedef'd array arguments to pointers" do + + source = "Book AddToBook(Book book, const IntArray values);\n" + + expected = [{ :name => "AddToBook", + :unscoped_name => "AddToBook", + :namespace=>[], + :class=>nil, + :modifier=>"", + :return => { :type => "Book", + :name => "cmock_to_return", + :str => "Book cmock_to_return", + :void? => false, + :ptr? => false, + :const? => false, + :const_ptr? => false + }, + :var_arg => nil, + :args => [{ :type => "Page*", :name => "book", :ptr? => true, :const? => false, :const_ptr? => false }, + { :type => "const int*", :name => "values", :ptr? => true, :const? => true, :const_ptr? => false }], + :args_string => "Book book, const IntArray values", + :args_call => "book, values", + :contains_ptr? => true + }] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + + end + + it "properly detect typedef'd variants of void and use those" do + + source = "typedef (void) FUNKY_VOID_T;\n" + + "typedef void CHUNKY_VOID_T;\n" + + "FUNKY_VOID_T DrHorrible(int SingAlong);\n" + + "int CaptainHammer(CHUNKY_VOID_T);\n" + + expected = [{ :var_arg=>nil, + :name=>"DrHorrible", + :unscoped_name=>"DrHorrible", + :namespace=>[], + :class=>nil, + :return => { :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"SingAlong", :ptr? => false, :const? => false, :const_ptr? => false} ], + :args_string=>"int SingAlong", + :args_call=>"SingAlong" + }, + { :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"CaptainHammer", + :unscoped_name=>"CaptainHammer", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ ], + :args_string=>"void", + :args_call=>"" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "be ok with structs inside of function declarations" do + + source = "int DrHorrible(struct SingAlong Blog);\n" + + "void Penny(struct const _KeepYourHeadUp_ * const BillyBuddy);\n" + + "struct TheseArentTheHammer CaptainHammer(void);\n" + + expected = [{ :var_arg=>nil, + :return =>{ :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"DrHorrible", + :unscoped_name=>"DrHorrible", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"struct SingAlong", :name=>"Blog", :ptr? => false, :const? => false, :const_ptr? => false} ], + :args_string=>"struct SingAlong Blog", + :args_call=>"Blog" + }, + { :var_arg=>nil, + :return=> { :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"Penny", + :unscoped_name=>"Penny", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"struct const _KeepYourHeadUp_*", :name=>"BillyBuddy", :ptr? => true, :const? => true, :const_ptr? => true} ], + :args_string=>"struct const _KeepYourHeadUp_* const BillyBuddy", + :args_call=>"BillyBuddy" + }, + { :var_arg=>nil, + :return=> { :type => "struct TheseArentTheHammer", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "struct TheseArentTheHammer cmock_to_return", + :void? => false + }, + :name=>"CaptainHammer", + :unscoped_name=>"CaptainHammer", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ ], + :args_string=>"void", + :args_call=>"" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "extract functions containing unions with union specifier" do + source = "void OrangePeel(union STARS_AND_STRIPES * a, union AFL_CIO b)" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"OrangePeel", + :unscoped_name=>"OrangePeel", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"union STARS_AND_STRIPES*", :name=>"a", :ptr? => true, :const? => false, :const_ptr? => false}, + {:type=>"union AFL_CIO", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"union STARS_AND_STRIPES* a, union AFL_CIO b", + :args_call=>"a, b" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "not be thwarted by variables named with primitive types as part of the name" do + source = "void ApplePeel(const unsigned int const_param, int int_param, int integer, char character, int* const constant)" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"ApplePeel", + :unscoped_name=>"ApplePeel", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=> "unsigned int", :name=>"const_param", :ptr? => false, :const? => true, :const_ptr? => false}, + {:type=>"int", :name=>"int_param", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"integer", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"char", :name=>"character", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int*", :name=>"constant", :ptr? => true, :const? => false, :const_ptr? => true} + ], + :args_string=>"const unsigned int const_param, int int_param, int integer, char character, int* const constant", + :args_call=>"const_param, int_param, integer, character, constant" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "not be thwarted by custom types named similarly to primitive types" do + source = "void LemonPeel(integer param, character thing, longint * junk, constant value, int32_t const number)" + expected = [{:var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"LemonPeel", + :unscoped_name=>"LemonPeel", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"integer", :name=>"param", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"character", :name=>"thing", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"longint*", :name=>"junk", :ptr? => true, :const? => false, :const_ptr? => false}, + {:type=>"constant", :name=>"value", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int32_t", :name=>"number", :ptr? => false, :const? => true, :const_ptr? => false} + ], + :args_string=>"integer param, character thing, longint* junk, constant value, int32_t const number", + :args_call=>"param, thing, junk, value, number" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "handle some of those chains of C name specifiers naturally" do + source = "void CoinOperated(signed char abc, const unsigned long int xyz_123, unsigned int const abc_123, long long arm_of_the_law)" + expected = [{:var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"CoinOperated", + :unscoped_name=>"CoinOperated", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"signed char", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned long int", :name=>"xyz_123", :ptr? => false, :const? => true, :const_ptr? => false}, + {:type=>"unsigned int", :name=>"abc_123", :ptr? => false, :const? => true, :const_ptr? => false}, + {:type=>"long long", :name=>"arm_of_the_law", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"signed char abc, const unsigned long int xyz_123, unsigned int const abc_123, long long arm_of_the_law", + :args_call=>"abc, xyz_123, abc_123, arm_of_the_law" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "handle custom types of various formats" do + source = "void CardOperated(CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const * const abc123)" + expected = [{:var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"CardOperated", + :unscoped_name=>"CardOperated", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"CUSTOM_TYPE", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"CUSTOM_TYPE*", :name=>"xyz_123", :ptr? => true, :const? => false, :const_ptr? => false}, + {:type=>"CUSTOM_TYPE", :name=>"abcxyz", :ptr? => false, :const? => true, :const_ptr? => false}, + {:type=>"struct CUSTOM_TYPE const*", :name=>"abc123", :ptr? => true, :const? => true, :const_ptr? => true} + ], + :args_string=>"CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const* const abc123", + :args_call=>"abc, xyz_123, abcxyz, abc123" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "handle arrays and treat them as pointers or strings" do + source = 'void KeyOperated(CUSTOM_TYPE thing1[], int thing2 [ ], ' \ + 'char thing3 [][2 ][ 3], int* thing4[4], u8 thing5[((u8)((5 + 5*2)/3))])' + expected_args = [ + { type: 'CUSTOM_TYPE*', name: 'thing1', ptr?: true, const?: false, const_ptr?: false }, + { type: 'int*', name: 'thing2', ptr?: true, const?: false, const_ptr?: false }, + # this one will likely change in the future when we improve multidimensional array support + { type: 'char*', name: 'thing3', ptr?: false, const?: false, const_ptr?: false }, + # this one will likely change in the future when we improve multidimensional array support + { type: 'int**', name: 'thing4', ptr?: true, const?: false, const_ptr?: false }, + { type: 'u8*', name: 'thing5', ptr?: true, const?: false, const_ptr?: false } + ] + expected = [{:var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"KeyOperated", + :unscoped_name=>"KeyOperated", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args => expected_args, + :args_string => 'CUSTOM_TYPE* thing1, int* thing2, ' \ + 'char* thing3, int** thing4, u8* thing5', + :args_call => 'thing1, thing2, thing3, thing4, thing5' }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "give a reasonable guess when dealing with weird combinations of custom types and modifiers" do + source = "void Cheese(unsigned CUSTOM_TYPE abc, unsigned xyz, CUSTOM_TYPE1 CUSTOM_TYPE2 pdq)" + expected = [{:var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"Cheese", + :unscoped_name=>"Cheese", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"unsigned CUSTOM_TYPE", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"unsigned", :name=>"xyz", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"CUSTOM_TYPE1 CUSTOM_TYPE2", :name=>"pdq", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"unsigned CUSTOM_TYPE abc, unsigned xyz, CUSTOM_TYPE1 CUSTOM_TYPE2 pdq", + :args_call=>"abc, xyz, pdq" }] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + end + + it "extract functions containing a function pointer" do + source = "void FunkyTurkey(unsigned int (*func_ptr)(int, char))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyTurkey", + :unscoped_name=>"FunkyTurkey", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 func_ptr", + :args_call=>"func_ptr" }] + typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions using a function pointer with shorthand notation" do + source = "void FunkyTurkey(unsigned int func_ptr(int, char))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyTurkey", + :unscoped_name=>"FunkyTurkey", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 func_ptr", + :args_call=>"func_ptr" }] + typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions containing a function pointer with a void" do + source = "void FunkyTurkey(void (*func_ptr)(void))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyTurkey", + :unscoped_name=>"FunkyTurkey", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 func_ptr", + :args_call=>"func_ptr" }] + typedefs = ["typedef void(*cmock_module_func_ptr1)(void);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions containing a function pointer with an implied void" do + source = "void FunkyTurkey(unsigned int (*func_ptr)())" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyTurkey", + :unscoped_name=>"FunkyTurkey", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 func_ptr", + :args_call=>"func_ptr" }] + typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)();"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions containing a constant function pointer and a pointer in the nested arg list" do + source = "void FunkyChicken(unsigned int (* const func_ptr)(unsigned long int * , char))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyChicken", + :unscoped_name=>"FunkyChicken", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => true, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 const func_ptr", + :args_call=>"func_ptr" }] + typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(unsigned long int* , char);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + # it "extract functions containing a function pointer taking a vararg" do + # source = "void FunkyParrot(unsigned int (*func_ptr)(int, char, ...))" + # expected = [{ :var_arg=>nil, + # :return=>{ :type => "void", + # :name => 'cmock_to_return', + # :ptr? => false, + # :const? => false, + # :const_ptr? => false, + # :str => "void cmock_to_return", + # :void? => true + # }, + # :name=>"FunkyParrot", + # :unscoped_name=>"FunkyParrot", + # :namespace=>[], + # :class=>nil, + # :modifier=>"", + # :contains_ptr? => false, + # :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false} + # ], + # :args_string=>"cmock_module_func_ptr1 func_ptr", + # :args_call=>"func_ptr" }] + # typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char, ...);"] + # result = @parser.parse("module", source) + # assert_equal(expected, result[:functions]) + # assert_equal(typedefs, result[:typedefs]) + # end + + it "extract functions containing a function pointer with extra parenthesis and two sets" do + source = "void FunkyBudgie(int (((* func_ptr1)(int, char))), void (*func_ptr2)(void))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyBudgie", + :unscoped_name=>"FunkyBudgie", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr1", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"cmock_module_func_ptr2", :name=>"func_ptr2", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 func_ptr1, cmock_module_func_ptr2 func_ptr2", + :args_call=>"func_ptr1, func_ptr2" }] + typedefs = ["typedef int(*cmock_module_func_ptr1)(int, char);", "typedef void(*cmock_module_func_ptr2)(void);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions containing a function pointers, structs and other things" do + source = "struct mytype *FunkyRobin(uint16_t num1, uint16_t num2, void (*func_ptr1)(uint16_t num3, struct mytype2 *s));" + expected = [{ :var_arg=>nil, + :return=>{ :type => "struct mytype*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => false, + :const_ptr? => false, + :str => "struct mytype* cmock_to_return", + :void? => false + }, + :name=>"FunkyRobin", + :unscoped_name=>"FunkyRobin", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"uint16_t", :name=>"num1", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"uint16_t", :name=>"num2", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"cmock_module_func_ptr1", :name=>"func_ptr1", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"uint16_t num1, uint16_t num2, cmock_module_func_ptr1 func_ptr1", + :args_call=>"num1, num2, func_ptr1" }] + typedefs = ["typedef void(*cmock_module_func_ptr1)(uint16_t num3, struct mytype2* s);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions containing an anonymous function pointer" do + source = "void FunkyFowl(unsigned int (* const)(int, char))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "void", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "void cmock_to_return", + :void? => true + }, + :name=>"FunkyFowl", + :unscoped_name=>"FunkyFowl", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => true, :const_ptr? => false} + ], + :args_string=>"cmock_module_func_ptr1 const cmock_arg1", + :args_call=>"cmock_arg1" }] + typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions returning a function pointer" do + source = "unsigned short (*FunkyPidgeon( const char op_code ))( int, long int )" + expected = [{ :var_arg=>nil, + :return=>{ :type => "cmock_module_func_ptr1", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "cmock_module_func_ptr1 cmock_to_return", + :void? => false + }, + :name=>"FunkyPidgeon", + :unscoped_name=>"FunkyPidgeon", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"char", :name=>"op_code", :ptr? => false, :const? => true, :const_ptr? => false} + ], + :args_string=>"const char op_code", + :args_call=>"op_code" }] + typedefs = ["typedef unsigned short(*cmock_module_func_ptr1)( int, long int );"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions returning a function pointer with implied void" do + source = "unsigned short (*FunkyTweetie())()" + expected = [{ :var_arg=>nil, + :return=>{ :type => "cmock_module_func_ptr1", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "cmock_module_func_ptr1 cmock_to_return", + :void? => false + }, + :name=>"FunkyTweetie", + :unscoped_name=>"FunkyTweetie", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[], + :args_string=>"void", + :args_call=>"" }] + typedefs = ["typedef unsigned short(*cmock_module_func_ptr1)();"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions returning a function pointer where everything is a void" do + source = "void (* FunkySeaGull(void))(void)" + expected = [{ :var_arg=>nil, + :return=>{ :type => "cmock_module_func_ptr1", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "cmock_module_func_ptr1 cmock_to_return", + :void? => false + }, + :name=>"FunkySeaGull", + :unscoped_name=>"FunkySeaGull", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[], + :args_string=>"void", + :args_call=>"" }] + typedefs = ["typedef void(*cmock_module_func_ptr1)(void);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions returning a function pointer with some pointer nonsense" do + source = "unsigned int * (* FunkyMacaw(double* foo, THING *bar))(unsigned int)" + expected = [{ :var_arg=>nil, + :return=>{ :type => "cmock_module_func_ptr1", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "cmock_module_func_ptr1 cmock_to_return", + :void? => false + }, + :name=>"FunkyMacaw", + :unscoped_name=>"FunkyMacaw", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"double*", :name=>"foo", :ptr? => true, :const? => false, :const_ptr? => false}, + {:type=>"THING*", :name=>"bar", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"double* foo, THING* bar", + :args_call=>"foo, bar" }] + typedefs = ["typedef unsigned int *(*cmock_module_func_ptr1)(unsigned int);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract this SQLite3 function with an anonymous function pointer arg (regression test)" do + source = "SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*))" + expected = [{ :var_arg=>nil, + :return=>{ :type => "int", + :name => "cmock_to_return", + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"sqlite3_bind_text", + :unscoped_name=>"sqlite3_bind_text", + :namespace=>[], + :class=>nil, + :modifier=>"SQLITE_API", + :contains_ptr? => true, + :args=>[ {:type=>"sqlite3_stmt*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"cmock_arg3", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"const char*", :name=>"cmock_arg4", :ptr? => false, :const? => true, :const_ptr? => false}, + {:type=>"int", :name=>"n", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"sqlite3_stmt* cmock_arg2, int cmock_arg3, const char* cmock_arg4, int n, cmock_module_func_ptr1 cmock_arg1", + :args_call=>"cmock_arg2, cmock_arg3, cmock_arg4, n, cmock_arg1" }] + typedefs = ["typedef void(*cmock_module_func_ptr1)(void*);"] + result = @parser.parse("module", source) + assert_equal(expected, result[:functions]) + assert_equal(typedefs, result[:typedefs]) + end + + it "extract functions with varargs" do + source = "int XFiles(int Scully, int Mulder, ...);\n" + expected = [{ :var_arg=>"...", + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"XFiles", + :unscoped_name=>"XFiles", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Scully", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"Mulder", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Scully, int Mulder", + :args_call=>"Scully, Mulder" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "extract functions with void pointers" do + source = "void* MoreSillySongs(void* stuff);\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "void*", + :name => 'cmock_to_return', + :ptr? => true, + :const? => false, + :const_ptr? => false, + :str => "void* cmock_to_return", + :void? => false + }, + :name=>"MoreSillySongs", + :unscoped_name=>"MoreSillySongs", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => true, + :args=>[ {:type=>"void*", :name=>"stuff", :ptr? => true, :const? => false, :const_ptr? => false} + ], + :args_string=>"void* stuff", + :args_call=>"stuff" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "extract functions with strippable confusing junk like gcc attributes" do + source = "int LaverneAndShirley(int Lenny, int Squiggy) __attribute__((weak)) __attribute__ ((deprecated));\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"LaverneAndShirley", + :unscoped_name=>"LaverneAndShirley", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Lenny", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"Squiggy", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Lenny, int Squiggy", + :args_call=>"Lenny, Squiggy" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "extract functions with strippable confusing junk like gcc attributes with parenthesis" do + source = "int TheCosbyShow(int Cliff, int Claire) __attribute__((weak, alias (\"__f\"));\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"TheCosbyShow", + :unscoped_name=>"TheCosbyShow", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Cliff", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"Claire", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Cliff, int Claire", + :args_call=>"Cliff, Claire" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "divines all permutations of ptr, const, and const_ptr correctly" do + truth_table = [ + # argument ptr const const_ptr + [ "constNOTconst constNOTconst", false, false, false ], + [ "const constNOTconst constNOTconst", false, true, false ], + [ "constNOTconst const constNOTconst", false, true, false ], + [ "constNOTconst *constNOTconst", true, false, false ], + [ "const constNOTconst *constNOTconst", true, true, false ], + [ "constNOTconst const *constNOTconst", true, true, false ], + [ "constNOTconst *const constNOTconst", true, false, true ], + [ "const constNOTconst *const constNOTconst", true, true, true ], + [ "constNOTconst const *const constNOTconst", true, true, true ], + [ "constNOTconst **constNOTconst", true, false, false ], + [ "const constNOTconst **constNOTconst", true, false, false ], + [ "constNOTconst const **constNOTconst", true, false, false ], + [ "constNOTconst *const *constNOTconst", true, true, false ], + [ "const constNOTconst *const *constNOTconst", true, true, false ], + [ "constNOTconst const *const *constNOTconst", true, true, false ], + [ "constNOTconst **const constNOTconst", true, false, true ], + [ "const constNOTconst **const constNOTconst", true, false, true ], + [ "constNOTconst const **const constNOTconst", true, false, true ], + [ "constNOTconst *const *const constNOTconst", true, true, true ], + [ "const constNOTconst *const *const constNOTconst", true, true, true ], + [ "constNOTconst const *const *const constNOTconst", true, true, true ] + ] + + truth_table.each do |entry| + assert_equal(@parser.divine_ptr(entry[0]), entry[1]) + assert_equal(@parser.divine_const(entry[0]), entry[2]) + assert_equal(@parser.divine_ptr_and_const(entry[0]), + { ptr?: entry[1], const?: entry[2], const_ptr?: entry[3] }) + end + end + + it "divines ptr correctly for string types" do + truth_table = [ + # argument ptr + [ "char s", false ], + [ "const char s", false ], + [ "char const s", false ], + [ "char *s", false ], + [ "const char *s", false ], + [ "char const *s", false ], + [ "char *const s", false ], + [ "const char *const s", false ], + [ "char const *const s", false ], + [ "char **s", true ], + [ "const char **s", true ], + [ "char const **s", true ], + [ "char *const *s", true ], + [ "const char *const *s", true ], + [ "char const *const *s", true ], + [ "char **const s", true ], + [ "const char **const s", true ], + [ "char const **const s", true ], + [ "char *const *const s", true ], + [ "const char *const *const s", true ], + [ "char const *const *const s", true ] + ] + + truth_table.each do |entry| + assert_equal(@parser.divine_ptr(entry[0]), entry[1]) + end + end + + it "Transform inline functions doesn't change a header with no inlines" do + source = + "#ifndef _NOINCLUDES\n" + + "#define _NOINCLUDES\n" + + "#include \"unity.h\"\n" + + "#include \"cmock.h\"\n" + + "#include \"YetAnotherHeader.h\"\n" + + "\n" + + "/* Ignore the following warnings since we are copying code */\n" + + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" + + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" + + "#pragma GCC diagnostic push\n" + + "#endif\n" + + "#if !defined(__clang__)\n" + + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" + + "#endif\n" + + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" + + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" + + "#endif\n" + + "\n" + + "struct my_struct {\n" + + "int a;\n" + + "int b;\n" + + "int b;\n" + + "char c;\n" + + "};\n" + + "int my_function(int a);\n" + + "int my_better_function(struct my_struct *s);\n" + + "\n" + + "#endif _NOINCLUDES\n" + + assert_equal(source, @parser.transform_inline_functions(source)) + end + + it "Transform inline functions changes inline functions to function declarations" do + source = + "#ifndef _NOINCLUDES\n" + + "#define _NOINCLUDES\n" + + "#include \"unity.h\"\n" + + "#include \"cmock.h\"\n" + + "#include \"YetAnotherHeader.h\"\n" + + "\n" + + "/* Ignore the following warnings since we are copying code */\n" + + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" + + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" + + "#pragma GCC diagnostic push\n" + + "#endif\n" + + "#if !defined(__clang__)\n" + + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" + + "#endif\n" + + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" + + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" + + "#endif\n" + + "\n" + + "struct my_struct {\n" + + "int a;\n" + + "int b;\n" + + "int b;\n" + + "char c;\n" + + "};\n" + + "int my_function(int a);\n" + + "int my_better_function(struct my_struct *s);\n" + + "static inline int staticinlinebar(struct my_struct *s)\n" + # This is a function with a lot of indentation levels, we should be able to handle it + "{\n" + + "\t{\n" + + "\t\t{\n" + + "\t\t\treturn s->a;\n" + + "\t\t}\n" + + "\t}\n" + + "}\n" + + "static inline int staticinlinefunc(struct my_struct *s)\n" + + "{\n" + + " return s->a;\n" + + "}\n" + + "int bar(struct my_struct *s);\n" + # A normal function to screw with our parser + "inline static int inlinestaticfunc(int a) {\n" + + " return a + 42;\n" + + "}\n" + + "inline int StaticInlineFunc(struct my_struct *s)\n" + + "{\n" + + " return get_member_a(s) + 42;\n" + + "}\n" + + "int inline StaticInlineBar(struct my_struct *s)\n" + + "{\n" + + " return get_member_a(s) + 42;\n" + + "}\n" + + "struct staticinlinestruct {\n" + # Add a structure declaration between the inline functions, just to make sure we don't touch it! + "int a;\n" + + "};\n" + + "\n" + + "struct staticinlinestruct fubarstruct(struct my_struct *s);\n" + # Another normal function to screw with our parser + "static inline struct staticinlinestruct inlinefubarfunction(struct my_struct *s)\n" + + "{\n" + + " return (struct staticinlinestruct)*s;\n" + + "}\n" + + "int fubar(struct my_struct *s);\n" + # Another normal function to screw with our parser + "inline int stuff(int num)" + + "{" + + " int reg = 0x12;" + + " if (num > 0)" + + " {" + + " reg |= (0x0Eu);" + + " }" + + " else" + + " {" + + " reg |= (0x07u);" + + " }" + + " return reg;" + + "}" + + "\n" + + "int inline static dummy_func_2(int a, char b, float c) {" + # This is a sneaky one, inline static is placed AFTER the return value + " c += 3.14;" + + " b -= 32;" + + " return a + (int)(b) + (int)c;" + + "}" + + "#endif _NOINCLUDES\n" + + expected = + "#ifndef _NOINCLUDES\n" + + "#define _NOINCLUDES\n" + + "#include \"unity.h\"\n" + + "#include \"cmock.h\"\n" + + "#include \"YetAnotherHeader.h\"\n" + + "\n" + + "/* Ignore the following warnings since we are copying code */\n" + + "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" + + "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" + + "#pragma GCC diagnostic push\n" + + "#endif\n" + + "#if !defined(__clang__)\n" + + "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" + + "#endif\n" + + "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" + + "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" + + "#endif\n" + + "\n" + + "struct my_struct {\n" + + "int a;\n" + + "int b;\n" + + "int b;\n" + + "char c;\n" + + "};\n" + + "int my_function(int a);\n" + + "int my_better_function(struct my_struct *s);\n" + + "int staticinlinebar(struct my_struct *s);\n" + + "int staticinlinefunc(struct my_struct *s);\n" + + "int bar(struct my_struct *s);\n" + + "int inlinestaticfunc(int a);\n" + + "int StaticInlineFunc(struct my_struct *s);\n" + + "int StaticInlineBar(struct my_struct *s);\n" + + "struct staticinlinestruct {\n" + + "int a;\n" + + "};\n" + + "\n" + + "struct staticinlinestruct fubarstruct(struct my_struct *s);\n" + + "struct staticinlinestruct inlinefubarfunction(struct my_struct *s);\n" + + "int fubar(struct my_struct *s);\n" + + "int stuff(int num);\n" + + "int dummy_func_2(int a, char b, float c);" + + "#endif _NOINCLUDES\n" + + assert_equal(expected, @parser.transform_inline_functions(source)) + end + + it "Count number of pairs of braces in function succesfully" do + source = + "int foo(struct my_struct *s)\n" + + "{\n" + + " return get_member_a(s) + 42;\n" + + "}\n" + complex_source = + "int bar(struct my_struct *s)\n" + + "{\n" + + "\tint a = 6;\n" + + "\tint res = foo(&(struct my_struct){.nr = a});\n" + + "\t{\n" + + "\t\tint a = 5;\n" + + "\t\tint res = foo(&(struct my_struct){.nr = a});\n" + + "\t\t{\n" + + "\t\t\tstruct my_struct a = {.nr = 1};\n" + + "\t\t}\n" + + "\t}\n" + + "\treturn 42;\n" + + "}\n" + + assert_equal(1, @parser.count_number_of_pairs_of_braces_in_function(source)) + assert_equal(6, @parser.count_number_of_pairs_of_braces_in_function(complex_source)) + end + + it "Count number of pairs of braces returns 0 if bad source is supplied" do + bad_source_0 = + "int foo(struct my_struct *s)\n" + + "{\n" + + " return get_member_a(s) + 42;\n" + + "\n" # Missing closing brace + bad_source_1 = + "int bar(struct my_struct *s)\n" + + "{\n" + + "\tint a = 6;\n" + + "\tint res = foo(&(struct my_struct){.nr = a});\n" + + "\t{\n" + + "\t\tint a = 5;\n" + + "\t\tint res = foo(&(struct my_struct){.nr = a});\n" + + "\t\t{\n" + + "\t\t\n" + # Missing closing brace + "\t}\n" + + "\treturn 42;\n" + + "}\n" + bad_source_2 = + "int foo(struct my_struct *s)\n" + + "\n" # No braces in source + + assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_0)) + assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_1)) + assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_2)) + end + + it "handles parsing multiline functions" do + source = "int\nLaverneAndShirley(int Lenny,\n int Squiggy);\n" + expected = [{ :var_arg=>nil, + :return=> { :type => "int", + :name => 'cmock_to_return', + :ptr? => false, + :const? => false, + :const_ptr? => false, + :str => "int cmock_to_return", + :void? => false + }, + :name=>"LaverneAndShirley", + :unscoped_name=>"LaverneAndShirley", + :namespace=>[], + :class=>nil, + :modifier=>"", + :contains_ptr? => false, + :args=>[ {:type=>"int", :name=>"Lenny", :ptr? => false, :const? => false, :const_ptr? => false}, + {:type=>"int", :name=>"Squiggy", :ptr? => false, :const? => false, :const_ptr? => false} + ], + :args_string=>"int Lenny, int Squiggy", + :args_call=>"Lenny, Squiggy" + }] + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "imports C++ differently when asked" do + source = + [ + "namespace ns1 {\n", + " namespace ns2 {\n", + "\n", + " class cls1 {\n", + " public:\n", + " int f_header_impl(int a, int b){\n", + " return a + b;\n", + " }\n", + "\n", + " static void f_void();\n", + " static int f_ret_simple();\n", + "\n", + " protected:\n", + " static void protected_f_void();\n", + "\n", + " public:\n", + " private:\n", + " static void private_f_void();\n", + " }; // cls1\n", + " } // ns2\n", + "} // ns1\n" + ].join + + expected = + [ + "namespace ns1 { namespace ns2 { class cls1 { public: int f_header_impl", + "static void f_void()", + "static int f_ret_simple()", + "protected: static void protected_f_void()", + "public: private: static void private_f_void()", + "}", + "} }" + ] + + assert_equal(expected, @parser.import_source(source, cpp=true)) + refute_equal(expected, @parser.import_source(source)) + end + + # only so parse_functions does not raise an error + def dummy_source + "void dummy(void);" + end + + # Expected result of above + def dummy_func + { :name => "dummy", + :unscoped_name => "dummy", + :class => nil, + :namespace => [], + :var_arg => nil, + :args_string => "void", + :args => [], + :args_call => "", + :contains_ptr? => false, + :modifier => "", + :return => { + :type => "void", + :name => "cmock_to_return", + :str => "void cmock_to_return", + :void? => true, + :ptr? => false, + :const? => false, + :const_ptr? => false}} + end + + # Commonly used example function + def voidvoid_func(namespace=[], name="Classic_functional") + { :name => name, + :unscoped_name => "functional", + :class => "Classic", + :namespace => namespace, + :var_arg => nil, + :args_string => "void", + :args => [], + :args_call => "", + :contains_ptr? => false, + :modifier => "", + :return => { + :type=>"void", + :name=>"cmock_to_return", + :str=>"void cmock_to_return", + :void? => true, + :ptr? => false, + :const? => false, + :const_ptr? => false}} + end + + it "parses public C++ functions" do + source = dummy_source + <<~SOURCE + class Classic { + public: + static void functional(void); + }; + SOURCE + + expected = [dummy_func, voidvoid_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "handles a namespace" do + source = dummy_source + <<~SOURCE + namespace ns1 { + class Classic { + public: + static void functional(void); + }; + } // ns1 + SOURCE + + expected = [dummy_func, + voidvoid_func(namespace=["ns1"], name="ns1_Classic_functional")] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "handles nested namespaces" do + source = dummy_source + <<~SOURCE + namespace ns1 { + namespace ns2 { + class Classic { + public: + static void functional(void); + }; + } // ns1 + } // ns1 + SOURCE + + expected = [dummy_func, + voidvoid_func(namespace=["ns1", "ns2"], name="ns1_ns2_Classic_functional")] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "ignores non-static C++ functions" do + source = dummy_source + <<~SOURCE + class Classic { + public: + void functional(void); + }; + SOURCE + + expected = [dummy_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "ignores private functions" do + source = dummy_source + <<~SOURCE + class Classic { + private: + static void functional(void); + }; + SOURCE + + expected = [dummy_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "parses public C++ functions after private functions" do + source = dummy_source + <<~SOURCE + class Classic { + private: + static void ignoreme(void); + public: + static void functional(void); + }; + SOURCE + + expected = [dummy_func, voidvoid_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "ignores protected functions" do + source = dummy_source + <<~SOURCE + class Classic { + protected: + static void functional(void); + }; + SOURCE + + expected = [dummy_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "parses public C++ functions after protected functions" do + source = dummy_source + <<~SOURCE + class Classic { + protected: + static void ignoreme(void); + public: + static void functional(void); + }; + SOURCE + + expected = [dummy_func, voidvoid_func] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "parses multiple classes in same file with uniquely named functions" do + source = dummy_source + <<~SOURCE + namespace ns1 { + class Classic { + public: + static void functional(void); + }; + + class Classical { + public: + static int functionality(int a); + }; + } // ns1 + + class Classy { + public: + static int* func(int* a); + }; + SOURCE + + expected = [dummy_func, + voidvoid_func(["ns1"], name="ns1_Classic_functional"), + { :name => "ns1_Classical_functionality", + :unscoped_name => "functionality", + :class => "Classical", + :namespace => ["ns1"], + :var_arg => nil, + :args_string => "int a", + :args => [ + { :ptr? => false, + :const? => false, + :const_ptr? => false, + :name => "a", + :type => "int"}], + :args_call => "a", + :contains_ptr? => false, + :modifier => "", + :return => { + :type=>"int", + :name=>"cmock_to_return", + :str=>"int cmock_to_return", + :void? => false, + :ptr? => false, + :const? => false, + :const_ptr? => false}}, + { :name => "Classy_func", + :unscoped_name => "func", + :class => "Classy", + :namespace => [], + :var_arg => nil, + :args_string => "int* a", + :args => [ + { :ptr? => true, + :const? => false, + :const_ptr? => false, + :name => "a", + :type => "int*"}], + :args_call => "a", + :contains_ptr? => true, + :modifier => "", + :return => { + :type=>"int*", + :name=>"cmock_to_return", + :str=>"int* cmock_to_return", + :void? => false, + :ptr? => true, + :const? => false, + :const_ptr? => false}}] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "handles multiple classes in same file with identically named functions" do + source = dummy_source + <<~SOURCE + namespace ns1 { + class Classic { + public: + static void functional(void); + }; + + class Classical { + public: + static int functional(int a); + }; + } // ns1 + + class Classy { + public: + static int* functional(int* a); + }; + SOURCE + + expected = [dummy_func, + voidvoid_func(["ns1"], name="ns1_Classic_functional"), + { :name => "ns1_Classical_functional", + :unscoped_name => "functional", + :class => "Classical", + :namespace => ["ns1"], + :var_arg => nil, + :args_string => "int a", + :args => [ + { :ptr? => false, + :const? => false, + :const_ptr? => false, + :name => "a", + :type => "int"}], + :args_call => "a", + :contains_ptr? => false, + :modifier => "", + :return => { + :type=>"int", + :name=>"cmock_to_return", + :str=>"int cmock_to_return", + :void? => false, + :ptr? => false, + :const? => false, + :const_ptr? => false}}, + { :name => "Classy_functional", + :unscoped_name => "functional", + :class => "Classy", + :namespace => [], + :var_arg => nil, + :args_string => "int* a", + :args => [ + { :ptr? => true, + :const? => false, + :const_ptr? => false, + :name => "a", + :type => "int*"}], + :args_call => "a", + :contains_ptr? => true, + :modifier => "", + :return => { + :type=>"int*", + :name=>"cmock_to_return", + :str=>"int* cmock_to_return", + :void? => false, + :ptr? => true, + :const? => false, + :const_ptr? => false}}] + + assert_equal(expected, @parser.parse("module", source)[:functions]) + end + + it "Transform inline functions can handle inline function declarations" do + source = + "static inline int dummy_func_decl(int a, char b, float c);\n" + # First declaration + "static inline int dummy_func_decl2(int a, char b, float c)\n\n\n\n\n\n;\n" + # Second declaration with a lot of newlines before the semicolon to mess with the parser + "static inline int staticinlinefunc(struct my_struct *s)\n" + # 'normal' inline pattern + "{\n" + + " return dummy_func_decl(1, 1, 1);\n" + + "}\n" + + "struct my_struct_with_inline_in_it\n" + # struct definition in between to mess with the parser + "{\n" + + " int a;\n" + + " char b;\n" + + " float inlineb;\n" + + "};\n" + + "static inline int dummy_func_decl(int a, char b, float c) {\n" + # Second user pattern + " return 42;\n" + + "}\n" + + "\n" + + expected = + "int dummy_func_decl(int a, char b, float c);\n" + + "int dummy_func_decl2(int a, char b, float c)\n\n\n\n\n\n;\n" + # Second declaration with a lot of newlines until the semicolon to mess with the parser + "int staticinlinefunc(struct my_struct *s);\n" + + "struct my_struct_with_inline_in_it\n" + + "{\n" + + " int a;\n" + + " char b;\n" + + " float inlineb;\n" + + "};\n" + + "int dummy_func_decl(int a, char b, float c);\n" + + "\n" + + @parser.treat_inlines = :include + assert_equal(expected, @parser.transform_inline_functions(source)) + end + + it "Transform inline functions can handle header with only inline function declarations" do + source = + "static inline int dummy_func_decl(int a, char b, float c);\n" + + "\n" + + expected = + "int dummy_func_decl(int a, char b, float c);\n" + + "\n" + + @parser.treat_inlines = :include + assert_equal(expected, @parser.transform_inline_functions(source)) + end + + it "Transform inline functions takes user provided patterns into account" do + source = + "static __inline__ __attribute__ ((always_inline)) uint16_t _somefunc (uint32_t a)\n" + + "{\n" + + " return _someotherfunc (a);\n" + + "}\n" + + "static __inline__ uint16_t _somefunc_0 (uint32_t a)\n" + + "{\n" + + " return (uint16_t) a;\n" + + "}\n" + + "\n" + + expected = + "uint16_t _somefunc (uint32_t a);\n" + + "uint16_t _somefunc_0 (uint32_t a);\n" + + "\n" + + @parser.treat_inlines = :include + @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__'] + assert_equal(expected, @parser.transform_inline_functions(source)) + end + + it "Transform inline functions limits deleting user macro to actual line/word" do + source = + "#if defined (FORCE_INLINE)\n" + + "#define MY_LIBRARY_INLINE static __inline__ __attribute__ ((always_inline))\n" + + "#else\n" + + "#define MY_LIBRARY_INLINE\n" + + "#endif\n" + + "#define INLINE static __inline__ __attribute__ ((always_inline))\n" + + "#define INLINE_TWO \\\nstatic\\\ninline\n" + + "INLINE uint16_t _somefunc (uint32_t a)\n" + + "{\n" + + " return _someotherfunc (a);\n" + + "}\n" + + "static __inline__ uint16_t _somefunc_0 (uint32_t a)\n" + + "{\n" + + " return (uint16_t) a;\n" + + "}\n" + + "static __inline__ __attribute__ \(\(always_inline\)\) uint16_t _somefunc_1 (uint32_t a)\n" + + "{\n" + + " return (uint16_t) a;\n" + + "}\n" + + "INLINE_TWO uint16_t _somefunc_2(uint32_t a)\n" + + "{\n" + + " return (uint16_t) a;\n" + + "}\n" + + "#define INLINE_THREE \\\nstatic\\\ninline" + + expected = + "#if defined (FORCE_INLINE)\n" + + "#else\n" + + "#endif\n" + + "uint16_t _somefunc (uint32_t a);\n" + + "uint16_t _somefunc_0 (uint32_t a);\n" + + "uint16_t _somefunc_1 (uint32_t a);\n" + + "uint16_t _somefunc_2(uint32_t a);\n" + + @parser.treat_inlines = :include + @parser.inline_function_patterns = ['MY_LIBRARY_INLINE', 'INLINE_THREE', 'INLINE_TWO', 'INLINE', 'static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__'] + assert_equal(expected, @parser.transform_inline_functions(source)) + end + +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_plugin_manager_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_plugin_manager_test.rb new file mode 100644 index 000000000..f9f0e640c --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_plugin_manager_test.rb @@ -0,0 +1,100 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_plugin_manager' +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_expect' +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_ignore' +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_cexception' + +describe CMockPluginManager, "Verify CMockPluginManager Module" do + + before do + create_mocks :utils, :pluginA, :pluginB + @config = create_stub( + :respond_to => true, + :when_ptr => :compare_data, + :enforce_strict_ordering => false, + :ignore => :args_and_calls + ) + + def @config.plugins + if instance_variable_defined?(:@plugins) + @plugins || [] + else + [] + end + end + + def @config.plugins=(val) + @plugins = val + end + end + + after do + end + + it "return all plugins by default" do + @config.plugins = ['cexception','ignore'] + @utils.expect :helpers, {} + + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + test_plugins = @cmock_plugins.plugins + contained = { :expect => false, :ignore => false, :cexception => false } + test_plugins.each do |plugin| + contained[:expect] = true if plugin.instance_of?(CMockGeneratorPluginExpect) + contained[:ignore] = true if plugin.instance_of?(CMockGeneratorPluginIgnore) + contained[:cexception] = true if plugin.instance_of?(CMockGeneratorPluginCexception) + end + assert_equal(true, contained[:expect]) + assert_equal(true, contained[:ignore]) + assert_equal(true, contained[:cexception]) + end + + it "return restricted plugins based on config" do + @utils.expect :helpers, {} + + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + test_plugins = @cmock_plugins.plugins + contained = { :expect => false, :ignore => false, :cexception => false } + test_plugins.each do |plugin| + contained[:expect] = true if plugin.instance_of?(CMockGeneratorPluginExpect) + contained[:ignore] = true if plugin.instance_of?(CMockGeneratorPluginIgnore) + contained[:cexception] = true if plugin.instance_of?(CMockGeneratorPluginCexception) + end + assert_equal(true, contained[:expect]) + assert_equal(false,contained[:ignore]) + assert_equal(false,contained[:cexception]) + end + + it "run a desired method over each plugin requested and return the results" do + @utils.expect :helpers, {} + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + @pluginA = create_stub(:test_method => ["This Is An Awesome Test-"]) + @pluginB = create_stub(:test_method => ["And This is Part 2-","Of An Awesome Test"]) + @cmock_plugins.plugins = [@pluginA, @pluginB] + + expected = "This Is An Awesome Test-And This is Part 2-Of An Awesome Test" + output = @cmock_plugins.run(:test_method) + assert_equal(expected, output) + end + + it "run a desired method and arg list over each plugin requested and return the results" do + @utils.expect :helpers, {} + @cmock_plugins = CMockPluginManager.new(@config, @utils) + + @pluginA = create_stub(:test_method => ["This Is An Awesome Test-"]) + @pluginB = create_stub(:test_method => ["And This is Part 2-","Of An Awesome Test"]) + @cmock_plugins.plugins = [@pluginA, @pluginB] + + expected = "This Is An Awesome Test-And This is Part 2-Of An Awesome Test" + output = @cmock_plugins.run(:test_method, "chickenpotpie") + assert_equal(expected, output) + end +end diff --git a/FreeRTOS-Plus/Test/CMock/test/unit/cmock_unityhelper_parser_test.rb b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_unityhelper_parser_test.rb new file mode 100644 index 000000000..987b0d697 --- /dev/null +++ b/FreeRTOS-Plus/Test/CMock/test/unit/cmock_unityhelper_parser_test.rb @@ -0,0 +1,223 @@ +# ========================================== +# CMock Project - Automatic Mock Generation for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +require File.expand_path(File.dirname(__FILE__)) + "/../test_helper" +require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_unityhelper_parser' + +describe CMockUnityHelperParser, "Verify CMockUnityHelperParser Module" do + + before do + create_mocks :config + end + + after do + end + + it "ignore lines that are commented out" do + source = + " abcd;\n" + + "// #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" + + "or maybe // #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n\n" + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, source + @parser = CMockUnityHelperParser.new(@config) + expected = {} + + assert_equal(expected, @parser.c_types) + end + + it "ignore stuff in block comments" do + source = + " abcd; /*\n" + + "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" + + "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n */\n" + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, source + @parser = CMockUnityHelperParser.new(@config) + expected = {} + + assert_equal(expected, @parser.c_types) + end + + it "notice equal helpers in the proper form and ignore others" do + source = + "abcd;\n" + + "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_T(a,b,line,msg) {...};\n" + + "abcd;\n" + + "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS(a,b,c,d,e) {...};\n" + + "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL(a,b,c,d) {...};\n" + + "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits(a,b,c,d) {...};\n" + + "abcd;\n" + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, source + @parser = CMockUnityHelperParser.new(@config) + expected = { + 'TURKEYS_T' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_T", + 'unsigned_funky_rabbits' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits" + } + + assert_equal(expected, @parser.c_types) + end + + it "notice equal helpers that contain arrays" do + source = + "abcd;\n" + + "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY(a,b,c,d,e) {...};\n" + + "abcd;\n" + + "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS_ARRAY(a,b,c,d,e,f) {...};\n" + + "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL_ARRAY(a,b,c,d,e) {...};\n" + + "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY(a,b,c,d,e) {...};\n" + + "abcd;\n" + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, source + @parser = CMockUnityHelperParser.new(@config) + expected = { + 'TURKEYS*' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY", + 'unsigned_funky_rabbits*' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY" + } + + assert_equal(expected, @parser.c_types) + end + + it "pull in the standard set of helpers and add them to my list" do + pairs = { + "UINT" => "HEX32", + "unsigned long" => "HEX64", + } + expected = { + "UINT" => "UNITY_TEST_ASSERT_EQUAL_HEX32", + "unsigned_long" => "UNITY_TEST_ASSERT_EQUAL_HEX64", + "UINT*" => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY", + "unsigned_long*"=> "UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY", + } + @config.expect :plugins, [] #not :array + @config.expect :treat_as, pairs + @config.expect :load_unity_helper, nil + @parser = CMockUnityHelperParser.new(@config) + + assert_equal(expected, @parser.c_types) + end + + it "pull in the user specified set of helpers and add them to my list" do + pairs = { + "char*" => "STRING", + "unsigned int" => "HEX32", + } + expected = { + "char*" => "UNITY_TEST_ASSERT_EQUAL_STRING", + "unsigned_int" => "UNITY_TEST_ASSERT_EQUAL_HEX32", + "char**" => "UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY", + "unsigned_int*" => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY", + } + @config.expect :plugins, [] #not :array + @config.expect :treat_as, pairs + @config.expect :load_unity_helper, nil + @parser = CMockUnityHelperParser.new(@config) + + assert_equal(expected, @parser.c_types) + end + + it "be able to fetch helpers on my list" do + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + 'LONG_LONG' => "UNITY_TEST_ASSERT_EQUAL_LONG_LONG" + } + + [["UINT8","UINT8"], + ["UINT16*","UINT16_ARRAY"], + ["const SPINACH","SPINACH"], + ["LONG LONG","LONG_LONG"] ].each do |ctype, exptype| + assert_equal(["UNITY_TEST_ASSERT_EQUAL_#{exptype}",''], @parser.get_helper(ctype)) + end + end + + it "return memory comparison when asked to fetch helper of types not on my list" do + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + } + + ["UINT32","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype| + @config.expect :memcmp_if_unknown, true + assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY",'&'], @parser.get_helper(ctype)) + end + end + + it "return memory array comparison when asked to fetch helper of types not on my list" do + @config.expect :plugins, [:array] + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + } + + ["UINT32*","SPINACH_T*"].each do |ctype| + @config.expect :memcmp_if_unknown, true + assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY",''], @parser.get_helper(ctype)) + end + end + + it "return the array handler if we cannot find the normal handler" do + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + } + + assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",'&'], @parser.get_helper("UINT16")) + end + + it "return the normal handler if we cannot find the array handler" do + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT16' => "UNITY_TEST_ASSERT_EQUAL_UINT16", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + } + + assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT8",'*'], @parser.get_helper("UINT8*")) + end + + it "raise error when asked to fetch helper of type not on my list and not allowed to mem check" do + @config.expect :plugins, [] #not :array + @config.expect :treat_as, {} + @config.expect :load_unity_helper, "" + @config.expect :memcmp_if_unknown, false + @parser = CMockUnityHelperParser.new(@config) + @parser.c_types = { + 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8", + 'UINT32*' => "UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY", + 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH", + } + + assert_raises (RuntimeError) { @parser.get_helper("UINT16") } + end +end |