diff --git a/lib/CLexer.rb b/lib/CLexer.rb index 1cc00bb2..06c5dcd4 100644 --- a/lib/CLexer.rb +++ b/lib/CLexer.rb @@ -123,7 +123,7 @@ def tokenize @input = $' @tokens << OPERATOR_SYMBOLS[operator] else - raise "Unexpected character: #{@input[0]}" + raise "Unexpected character: #{@input[0].inspect}" end end diff --git a/lib/cmock.rb b/lib/cmock.rb index c79d6db6..dc8a2028 100644 --- a/lib/cmock.rb +++ b/lib/cmock.rb @@ -44,12 +44,12 @@ def setup_skeletons(files) def generate_mock(src, folder) name = File.basename(src, '.*') ext = File.extname(src) - @cm_generator.create_mock(name, @cm_parser.parse(src, name, File.read(src)), ext, folder, src) + @cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src), src), ext, folder, src) end def generate_skeleton(src) name = File.basename(src, '.*') - @cm_generator.create_skeleton(name, @cm_parser.parse(src, name, File.read(src)), src) + @cm_generator.create_skeleton(name, @cm_parser.parse(name, File.read(src), src), src) end end diff --git a/lib/cmock_header_parser.rb b/lib/cmock_header_parser.rb index 3fd5cddc..56790328 100644 --- a/lib/cmock_header_parser.rb +++ b/lib/cmock_header_parser.rb @@ -10,7 +10,7 @@ class CMockHeaderParser attr_accessor :funcs, :c_attr_noconst, :c_attributes, :treat_as_void, :treat_externs, :treat_inlines, :inline_function_patterns attr_reader :noreturn_attributes, :process_gcc_attributes, :process_cpp_attributes, :c_calling_conventions - attr_reader :parse_project + attr_accessor :parse_project def initialize(cfg) @c_strippables = cfg.strippables @@ -43,11 +43,15 @@ def initialize(cfg) def raise_parse_error(message) # TODO: keep track of line number to be able to insert it in the error message. - raise "#{@parse_project[:source_path]}:1: Failed Parsing Declaration Prototype!" + "\n" + message + if @parse_project[:source_path].nil? + raise "Failed Parsing Declaration Prototype!" + "\n" + message + else + raise "#{@parse_project[:source_path]}:1: Failed Parsing Declaration Prototype!" + "\n" + message + end end - def parse(src_path, name, source) - $stderr.puts "Parsing #{src_path}" if @verbosity >= 1 + def parse(name, source, src_path = nil) + $stderr.puts "Parsing #{src_path}" if !src_path.nil? and @verbosity >= 1 @parse_project = { :source_path => src_path, :module_name => name.gsub(/\W/, ''), @@ -233,6 +237,7 @@ def transform_inline_functions(source) end def import_source(source, cpp = false) + # let's clean up the encoding in case they've done anything weird with the characters we might find source = source.force_encoding('ISO-8859-1').encode('utf-8', :replace => nil) @@ -287,9 +292,9 @@ def import_source(source, cpp = false) # scan for functions which return function pointers, because they are a pain source.gsub!(/([\w\s\*]+)\(*\(\s*\*([\w\s\*]+)\s*\(([\w\s\*,]*)\)\)\s*\(([\w\s\*,]*)\)\)*/) do |_m| - functype = "cmock_#{parse_project[:module_name]}_func_ptr#{parse_project[:typedefs].size + 1}" + functype = "cmock_#{@parse_project[:module_name]}_func_ptr#{@parse_project[:typedefs].size + 1}" unless cpp # only collect once - parse_project[:typedefs] << "typedef #{Regexp.last_match(1).strip}(*#{functype})(#{Regexp.last_match(4)});" + @parse_project[:typedefs] << "typedef #{Regexp.last_match(1).strip}(*#{functype})(#{Regexp.last_match(4)});" "#{functype} #{Regexp.last_match(2).strip}(#{Regexp.last_match(3)});" end end diff --git a/test/rakefile_helper.rb b/test/rakefile_helper.rb index dd0b6541..283021dd 100644 --- a/test/rakefile_helper.rb +++ b/test/rakefile_helper.rb @@ -21,7 +21,13 @@ module RakefileHelpers def load_configuration(config_file) $cfg_file = config_file - $cfg = YAML.load(File.read('./targets/' + $cfg_file),aliases: true) + yaml_version = YAML::VERSION[0].to_i # Extract major version number of YAML + options = if yaml_version > 3 + { aliases: true } + else + { } + end + $cfg = YAML.load(File.read('./targets/' + $cfg_file), **options) $colour_output = false unless $cfg['colour'] end diff --git a/test/unit/cmock_generator_main_test.rb b/test/unit/cmock_generator_main_test.rb index 1a66c0a5..316bf0eb 100644 --- a/test/unit/cmock_generator_main_test.rb +++ b/test/unit/cmock_generator_main_test.rb @@ -56,6 +56,7 @@ def mock_implementation(name, args) @config.expect :fail_on_unexpected_calls, true @config.expect :treat_inlines, :exclude @config.expect :exclude_setjmp_h, false + @config.expect :verbosity, 1 @cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @cmock_generator.module_name = @module_name @cmock_generator.module_ext = '.h' @@ -77,6 +78,7 @@ def mock_implementation(name, args) @config.expect :fail_on_unexpected_calls, true @config.expect :treat_inlines, :exclude @config.expect :exclude_setjmp_h, false + @config.expect :verbosity, 1 @cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins) @test_project = { diff --git a/test/unit/cmock_generator_plugin_callback_test.rb b/test/unit/cmock_generator_plugin_callback_test.rb index 0b5ce1eb..f4568eca 100644 --- a/test/unit/cmock_generator_plugin_callback_test.rb +++ b/test/unit/cmock_generator_plugin_callback_test.rb @@ -12,11 +12,14 @@ before do create_mocks :config, :utils + @config.expect :callback_kind, '*' + @config.expect :callback_kind, '*' @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 @@ -36,7 +39,7 @@ " CMOCK_Oak_CALLBACK Oak_CallbackFunctionPointer;\n" + " int Oak_CallbackCalls;\n" returned = @cmock_generator_plugin_callback.instance_structure(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function declaration for function without arguments" do @@ -46,7 +49,7 @@ "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) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function declaration for function without arguments when count is also turned off" do @@ -57,7 +60,7 @@ "#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) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function declaration for function with arguments" do @@ -99,7 +102,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions with no arg check and of style 'void func(void)' when count turned off" do @@ -111,7 +114,7 @@ ].join @cmock_generator_plugin_callback.include_count = false returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions with no arg check and of style 'int func(void)'" do @@ -122,7 +125,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions with no arg check and of style 'void func(int* steak, uint8_t flag)'" do @@ -137,7 +140,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{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 @@ -153,7 +156,7 @@ ].join @cmock_generator_plugin_callback.include_count = false returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{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 @@ -168,7 +171,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions without arg check and of style 'void func(void)' when count turned off" do @@ -183,7 +186,7 @@ ].join @cmock_generator_plugin_callback.include_count = false returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions without arg check and of style 'int func(void)'" do @@ -215,7 +218,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{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 @@ -234,7 +237,7 @@ ].join @cmock_generator_plugin_callback.include_count = false returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock function implementation for functions without arg check and of style 'int16_t func(int* steak, uint8_t flag)'" do @@ -252,7 +255,7 @@ " }\n" ].join returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end it "add mock interfaces for functions " do @@ -276,6 +279,6 @@ "}\n\n" ].join returned = @cmock_generator_plugin_callback.mock_interfaces(function) - assert_equal(expected, returned) + assert_equal(expected, returned, "Expected:\n#{expected}\nGot:\n#{returned}") end end diff --git a/test/unit/cmock_header_parser_test.rb b/test/unit/cmock_header_parser_test.rb index 57ad5be1..4cd66e52 100644 --- a/test/unit/cmock_header_parser_test.rb +++ b/test/unit/cmock_header_parser_test.rb @@ -31,7 +31,7 @@ @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) @test_project = { @@ -40,6 +40,9 @@ :functions => [], :normalized_source => nil } + + @parser.parse_project = @test_project + end after do @@ -63,7 +66,7 @@ "who" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove block comments" do @@ -96,7 +99,7 @@ "shown_because_line_above_ended_comment_this_time" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove strippables from the beginning or end of function declarations" do @@ -116,7 +119,7 @@ "void universal_handler()" ] - assert_equal(expected, @parser.import_source(source, @test_project)) + assert_equal(expected, @parser.import_source(source)) end it "keep gcc's function __attribute__'s" do @@ -136,7 +139,7 @@ "void __attribute__((interrupt))universal_handler()" ] - assert_equal(expected, @parser.import_source(source, @test_project)) + assert_equal(expected, @parser.import_source(source)) end it "remove preprocessor directives" do @@ -147,7 +150,7 @@ expected = [] - assert_equal(expected, @parser.import_source(source, @test_project)) + assert_equal(expected, @parser.import_source(source)) end @@ -162,7 +165,7 @@ expected = ["foo"] - assert_equal(expected, @parser.import_source(source, @test_project)) + assert_equal(expected, @parser.import_source(source)) end @@ -176,7 +179,7 @@ "hoo hah when" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end @@ -189,7 +192,7 @@ expected = ["but I'm here"] - assert_equal(expected, @parser.import_source(source, @test_project)) + assert_equal(expected, @parser.import_source(source)) end @@ -223,7 +226,7 @@ "this should remain!" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end @@ -241,7 +244,7 @@ "} Thinger;\n" + "or me!!\n" - assert_equal(["don't delete me!! or me!!"], @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(["don't delete me!! or me!!"], @parser.import_source(source).map!{|s|s.strip}) end @@ -259,7 +262,7 @@ "} Whatever;\n" + "me too!!\n" - assert_equal(["I want to live!! me too!!"], @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(["I want to live!! me too!!"], @parser.import_source(source).map!{|s|s.strip}) end @@ -282,7 +285,7 @@ "I want to live!!\n" assert_equal(["void foo(void)", "struct THINGER foo(void)", "I want to live!!"], - @parser.import_source(source, @test_project).map!{|s|s.strip}) + @parser.import_source(source).map!{|s|s.strip}) end it "remove externed and inline functions" do @@ -301,7 +304,7 @@ "uint32 funcinline(unsigned int)" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove function definitions but keep function declarations" do @@ -323,7 +326,7 @@ "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove function definitions with nested braces but keep function declarations" do @@ -365,7 +368,7 @@ "uint32 func_with_decl_c", #okay. it's not going to be interpretted as another function ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove a fully defined inline function" do @@ -463,7 +466,7 @@ ] @parser.treat_externs = :include - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "leave inline functions if inline to be included" do @@ -489,7 +492,7 @@ ] @parser.treat_inlines = :include - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + 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 @@ -518,7 +521,7 @@ @parser.treat_externs = :include @parser.treat_inlines = :include - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "Include inline functions that contain user defined inline function formats" do @@ -549,7 +552,7 @@ @parser.treat_inlines = :include @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__', '\binline\b'] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end it "remove defines" do @@ -565,7 +568,7 @@ "void hello(void)", ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end @@ -578,7 +581,7 @@ "const int TheMatrix(int Trinity, unsigned int * Neo)", ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end @@ -605,7 +608,7 @@ :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(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "handle odd case of typedef'd void as arg" do @@ -629,7 +632,7 @@ :args => [], :args_string => "void", :args_call => "" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "handle odd case of typedef'd void as arg pointer" do @@ -653,7 +656,7 @@ :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(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end @@ -666,7 +669,7 @@ "void Foo(int a, float b, char c, char* e)" ] - assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip}) + assert_equal(expected, @parser.import_source(source).map!{|s|s.strip}) end @@ -758,7 +761,7 @@ ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "extract and return function declarations with no retval" do @@ -786,7 +789,7 @@ ], :args_string => "uint la, int de, bool da", :args_call => "la, de, da" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "extract and return function declarations with implied voids" do @@ -811,7 +814,7 @@ :args => [ ], :args_string => "void", :args_call => "" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "extract modifiers properly" do @@ -838,7 +841,7 @@ ], :args_string => "int Trinity, unsigned int* Neo", :args_call => "Trinity, Neo" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "extract c calling conventions properly" do @@ -866,7 +869,7 @@ ], :args_string => "int Trinity, unsigned int* Neo", :args_call => "Trinity, Neo" } - assert_equal(expected, @parser.parse_declaration(@test_project, source)) + assert_equal(expected, @parser.parse_declaration(source)) end it "extract and return function declarations inside namespace and class" do @@ -892,7 +895,7 @@ ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, ["ns1", "ns2"], "Bar")) + assert_equal(expected, @parser.parse_declaration(source, ["ns1", "ns2"], "Bar")) end it "fully parse multiple prototypes" do @@ -2454,8 +2457,8 @@ "} }" ] - assert_equal(expected, @parser.import_source(source, @test_project, cpp=true)) - refute_equal(expected, @parser.import_source(source, @test_project)) + 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 @@ -3134,7 +3137,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end it "extract and return function declarations with noreturn" do @@ -3160,7 +3163,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end it "extract and return function declarations with [[noreturn]]" do @@ -3186,7 +3189,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end it "extract and return function declarations with __attribute__((noreturn))" do @@ -3212,7 +3215,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end it "extract and return function declarations with __attribute__ ((noreturn))" do @@ -3238,7 +3241,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end it "extract and return function declarations with __attribute__ ((__noreturn__))" do @@ -3264,7 +3267,7 @@ class Classy { ], :args_string => "int a, unsigned int b", :args_call => "a, b" } - assert_equal(expected, @parser.parse_declaration(@test_project, source, [], nil)) + assert_equal(expected, @parser.parse_declaration(source, [], nil)) end