Skip to content

Commit

Permalink
Corrections to fulfill the tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
informatimago committed Aug 1, 2023
1 parent 9266641 commit 888a6c0
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 64 deletions.
2 changes: 1 addition & 1 deletion lib/CLexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/cmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 11 additions & 6 deletions lib/cmock_header_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/, ''),
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/rakefile_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/unit/cmock_generator_main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 = {
Expand Down
31 changes: 17 additions & 14 deletions test/unit/cmock_generator_plugin_callback_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Loading

0 comments on commit 888a6c0

Please sign in to comment.