mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-09-13 21:59:58 +00:00
- protect against filenames with dashes for mocking
This commit is contained in:
+11
-9
@@ -8,7 +8,7 @@ $here = File.dirname __FILE__
|
||||
|
||||
class CMockGenerator
|
||||
|
||||
attr_accessor :config, :file_writer, :module_name, :mock_name, :utils, :plugins, :ordered
|
||||
attr_accessor :config, :file_writer, :module_name, :clean_mock_name, :mock_name, :utils, :plugins, :ordered
|
||||
|
||||
def initialize(config, file_writer, utils, plugins)
|
||||
@file_writer = file_writer
|
||||
@@ -28,6 +28,7 @@ class CMockGenerator
|
||||
def create_mock(module_name, parsed_stuff)
|
||||
@module_name = module_name
|
||||
@mock_name = @prefix + @module_name
|
||||
@clean_mock_name = @mock_name.gsub(/-/, "_")
|
||||
create_mock_header_file(parsed_stuff)
|
||||
create_mock_source_file(parsed_stuff)
|
||||
end
|
||||
@@ -63,6 +64,7 @@ class CMockGenerator
|
||||
|
||||
def create_mock_header_header(file, filename)
|
||||
define_name = filename.gsub(/\.h/, "_h").upcase
|
||||
define_name = define_name.gsub(/-/, "_")
|
||||
orig_filename = filename.gsub(@config.mock_prefix, "")
|
||||
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
|
||||
file << "#ifndef _#{define_name}\n"
|
||||
@@ -82,9 +84,9 @@ class CMockGenerator
|
||||
end
|
||||
|
||||
def create_mock_header_service_call_declarations(file)
|
||||
file << "void #{@mock_name}_Init(void);\n"
|
||||
file << "void #{@mock_name}_Destroy(void);\n"
|
||||
file << "void #{@mock_name}_Verify(void);\n\n"
|
||||
file << "void #{@clean_mock_name}_Init(void);\n"
|
||||
file << "void #{@clean_mock_name}_Destroy(void);\n"
|
||||
file << "void #{@clean_mock_name}_Verify(void);\n\n"
|
||||
end
|
||||
|
||||
def create_mock_header_footer(header)
|
||||
@@ -112,7 +114,7 @@ class CMockGenerator
|
||||
file << @plugins.run(:instance_typedefs, function)
|
||||
file << "\n} CMOCK_#{function[:name]}_CALL_INSTANCE;\n\n"
|
||||
end
|
||||
file << "static struct #{@mock_name}Instance\n{\n"
|
||||
file << "static struct #{@clean_mock_name}Instance\n{\n"
|
||||
if (functions.size == 0)
|
||||
file << " unsigned char placeHolder;\n"
|
||||
end
|
||||
@@ -133,7 +135,7 @@ class CMockGenerator
|
||||
end
|
||||
|
||||
def create_mock_verify_function(file, functions)
|
||||
file << "void #{@mock_name}_Verify(void)\n{\n"
|
||||
file << "void #{@clean_mock_name}_Verify(void)\n{\n"
|
||||
verifications = functions.collect {|function| @plugins.run(:mock_verify, function)}.join
|
||||
file << " UNITY_LINE_TYPE cmock_line = TEST_LINE_NUM;\n" unless verifications.empty?
|
||||
file << verifications
|
||||
@@ -141,13 +143,13 @@ class CMockGenerator
|
||||
end
|
||||
|
||||
def create_mock_init_function(file)
|
||||
file << "void #{@mock_name}_Init(void)\n{\n"
|
||||
file << " #{@mock_name}_Destroy();\n"
|
||||
file << "void #{@clean_mock_name}_Init(void)\n{\n"
|
||||
file << " #{@clean_mock_name}_Destroy();\n"
|
||||
file << "}\n\n"
|
||||
end
|
||||
|
||||
def create_mock_destroy_function(file, functions)
|
||||
file << "void #{@mock_name}_Destroy(void)\n{\n"
|
||||
file << "void #{@clean_mock_name}_Destroy(void)\n{\n"
|
||||
file << " CMock_Guts_MemFreeAll();\n"
|
||||
file << " memset(&Mock, 0, sizeof(Mock));\n"
|
||||
file << functions.collect {|function| @plugins.run(:mock_destroy, function)}.join
|
||||
|
||||
@@ -46,7 +46,8 @@ class CMockHeaderParser
|
||||
private if $ThisIsOnlyATest.nil? ################
|
||||
|
||||
def import_source(source)
|
||||
|
||||
source = source.force_encoding("ISO-8859-1").encode("utf-8", replace: nil)
|
||||
|
||||
# void must be void for cmock _ExpectAndReturn calls to process properly, not some weird typedef which equates to void
|
||||
# to a certain extent, this action assumes we're chewing on pre-processed header files, otherwise we'll most likely just get stuff from @treat_as_void
|
||||
@local_as_void = @treat_as_void
|
||||
|
||||
@@ -52,6 +52,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
|
||||
@cmock_generator = CMockGenerator.new(@config, @file_writer, @utils, @plugins)
|
||||
@cmock_generator.module_name = @module_name
|
||||
@cmock_generator.mock_name = "Mock#{@module_name}"
|
||||
@cmock_generator.clean_mock_name = "Mock#{@module_name}"
|
||||
|
||||
#strict handling
|
||||
@config.expect.mock_prefix.returns("Mock")
|
||||
@@ -65,6 +66,7 @@ class CMockGeneratorTest < Test::Unit::TestCase
|
||||
@cmock_generator_strict = CMockGenerator.new(@config, @file_writer, @utils, @plugins)
|
||||
@cmock_generator_strict.module_name = @module_name
|
||||
@cmock_generator_strict.mock_name = "Mock#{@module_name}"
|
||||
@cmock_generator_strict.clean_mock_name = "Mock#{@module_name}"
|
||||
end
|
||||
|
||||
def teardown
|
||||
@@ -100,6 +102,42 @@ class CMockGeneratorTest < Test::Unit::TestCase
|
||||
assert_equal(expected, output)
|
||||
end
|
||||
|
||||
should "handle dashes in the module name" do
|
||||
#no strict handling
|
||||
@config.expect.mock_prefix.returns("Mock")
|
||||
@config.expect.enforce_strict_ordering.returns(nil)
|
||||
@config.expect.framework.returns(:unity)
|
||||
@config.expect.includes.returns(["ConfigRequiredHeader1.h","ConfigRequiredHeader2.h"])
|
||||
@config.expect.includes_h_post_orig_header.returns(nil)
|
||||
@config.expect.includes_c_pre_header.returns(nil)
|
||||
@config.expect.includes_c_post_header.returns(nil)
|
||||
@cmock_generator2 = CMockGenerator.new(@config, @file_writer, @utils, @plugins)
|
||||
@cmock_generator2.module_name = "Pout-Pout-Fish"
|
||||
@cmock_generator2.mock_name = "MockPout-Pout-Fish"
|
||||
@cmock_generator2.clean_mock_name = "MockPout_Pout_Fish"
|
||||
|
||||
@config.expect.mock_prefix.returns("Mock")
|
||||
orig_filename = "Pout-Pout-Fish.h"
|
||||
define_name = "MOCKPOUT_POUT_FISH_H"
|
||||
mock_name = "MockPout_Pout_Fish"
|
||||
output = []
|
||||
expected = [ "/* AUTOGENERATED FILE. DO NOT EDIT. */\n",
|
||||
"#ifndef _#{define_name}\n",
|
||||
"#define _#{define_name}\n\n",
|
||||
"#include \"ConfigRequiredHeader1.h\"\n",
|
||||
"#include \"ConfigRequiredHeader2.h\"\n",
|
||||
"#include \"#{orig_filename}\"\n",
|
||||
"#include \"PluginRequiredHeader.h\"\n",
|
||||
"\n"
|
||||
]
|
||||
|
||||
@plugins.expect.run(:include_files).returns("#include \"PluginRequiredHeader.h\"\n")
|
||||
|
||||
@cmock_generator.create_mock_header_header(output, "MockPout-Pout-Fish.h")
|
||||
|
||||
assert_equal(expected, output)
|
||||
end
|
||||
|
||||
should "create the top of a header file with optional include files from config" do
|
||||
@config.expect.mock_prefix.returns("Mock")
|
||||
orig_filename = "PoutPoutFish.h"
|
||||
|
||||
Reference in New Issue
Block a user