Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/CMock_Summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,17 @@ from the defaults. We've tried to specify what the defaults are below.

* default: true

* `:mock_name_include_folder`:
Name a mock by the folder it is generated into as well as its filename. The
folder is the path below `:mock_path`, built from `:subdir` and any folder
given to `setup_mocks`. A mock of "config.h" generated into `uart/` gets an
include guard and Init, Verify and Destroy functions named `uart_Mockconfig`
rather than `Mockconfig`. This lets two mocks of the same filename coexist in
one test. A test should include such a mock by that same folder, and Unity's
test runner generator needs its option of the same name enabled to match.

* default: false

* `:mock_path`:
The directory where you would like the mock files generated to be
placed.
Expand Down
1 change: 1 addition & 0 deletions lib/cmock_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CMockConfig
:skeleton_path => '',
:weak => '',
:subdir => nil,
:mock_name_include_folder => false,
:plugins => [],
:strippables => ['(?:__attribute__\s*\([ (]*.*?[ )]*\)+)'],
:attributes => %w[__ramfunc __irq __fiq register extern],
Expand Down
6 changes: 5 additions & 1 deletion lib/cmock_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(config, file_writer, utils, plugins)
@fail_on_unexpected_calls = @config.fail_on_unexpected_calls
@exclude_setjmp_h = @config.exclude_setjmp_h
@subdir = @config.subdir
@name_include_folder = @config.mock_name_include_folder
@debug_output = @config.debug_output

@includes_h_pre_orig_header = ((@config.includes || []) + (@config.includes_h_pre_orig_header || [])).uniq.map { |h| h =~ /</ ? h : "\"#{h}\"" }
Expand Down Expand Up @@ -62,12 +63,15 @@ def create_mock(module_name, parsed_stuff, module_ext = nil, folder = nil)
# adds a trailing slash to the folder output
mock_folder = File.join(mock_folder, '') if mock_folder

# optionally fold the folder into the mock name to keep same-named headers distinct
identity = @name_include_folder && mock_folder ? mock_folder.sub(/\A(?:\.[\/\\])+/, '') + mock_name : mock_name

# create out mock project from incoming data
mock_project = {
:module_name => module_name,
:module_ext => (module_ext || '.h'),
:mock_name => mock_name,
:clean_name => TypeSanitizer.sanitize_c_identifier(mock_name),
:clean_name => TypeSanitizer.sanitize_c_identifier(identity),
:folder => mock_folder,
:parsed_stuff => parsed_stuff,
:skeleton => false
Expand Down
31 changes: 31 additions & 0 deletions test/unit/cmock_generator_main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def mock_implementation(name, args)
@config.expect :includes_c_pre_header, nil
@config.expect :includes_c_post_header, nil
@config.expect :subdir, nil
@config.expect :mock_name_include_folder, false
@config.expect :fail_on_unexpected_calls, true
@config.expect :treat_inlines, :exclude
@config.expect :exclude_setjmp_h, false
Expand All @@ -69,6 +70,7 @@ def mock_implementation(name, args)
@config.expect :includes_c_pre_header, nil
@config.expect :includes_c_post_header, nil
@config.expect :subdir, nil
@config.expect :mock_name_include_folder, false
@config.expect :fail_on_unexpected_calls, true
@config.expect :treat_inlines, :exclude
@config.expect :exclude_setjmp_h, false
Expand Down Expand Up @@ -426,6 +428,35 @@ def helper_create_header_top_with_opt_includes_form_config_and_plugin(ext)
assert_equal(expected, output.flatten)
end

def capture_mock_project(generator, folder)
captured = nil
generator.define_singleton_method(:create_mock_subdir) { |project| captured = project }
generator.define_singleton_method(:create_mock_header_file) { |_project| }
generator.define_singleton_method(:create_mock_source_file) { |_project| }
generator.create_mock("config", {}, ".h", folder)
captured
end

it "name a mock by its filename alone by default" do
project = capture_mock_project(@cmock_generator, "drivers/uart")

assert_equal("Mockconfig", project[:clean_name])
assert_equal("drivers/uart/", project[:folder])
end

it "name a mock by its folder when configured to" do
@cmock_generator.instance_variable_set(:@name_include_folder, true)

assert_equal("drivers_uart_Mockconfig", capture_mock_project(@cmock_generator, "drivers/uart")[:clean_name])
assert_equal("drivers_uart_Mockconfig", capture_mock_project(@cmock_generator, "./drivers/uart")[:clean_name])
end

it "name a mock by its filename when configured to use a folder but given none" do
@cmock_generator.instance_variable_set(:@name_include_folder, true)

assert_equal("Mockconfig", capture_mock_project(@cmock_generator, nil)[:clean_name])
end

it "create mock verify functions in source file when no functions specified" do
functions = []
output = []
Expand Down
Loading