| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class CMockConfig |
| 8 |
CMOCK_DEFAULT_OPTIONS = |
| 9 |
{ |
| 10 |
:framework => :unity, |
| 11 |
:mock_path => 'mocks', |
| 12 |
:mock_prefix => 'Mock', |
| 13 |
:mock_suffix => '', |
| 14 |
:skeleton_path => '', |
| 15 |
:weak => '', |
| 16 |
:subdir => nil, |
| 17 |
:plugins => [], |
| 18 |
:strippables => ['(?:__attribute__\s*\([ (]*.*?[ )]*\)+)'], |
| 19 |
:attributes => %w[__ramfunc __irq __fiq register extern], |
| 20 |
:c_calling_conventions => %w[__stdcall __cdecl __fastcall], |
| 21 |
:enforce_strict_ordering => false, |
| 22 |
:fail_on_unexpected_calls => true, |
| 23 |
:unity_helper_path => false, |
| 24 |
:treat_as => {}, |
| 25 |
:treat_as_array => {}, |
| 26 |
:treat_as_void => [], |
| 27 |
:memcmp_if_unknown => true, |
| 28 |
:when_no_prototypes => :warn, |
| 29 |
:when_ptr => :compare_data, |
| 30 |
:verbosity => 2, |
| 31 |
:treat_externs => :exclude, |
| 32 |
:treat_inlines => :exclude, |
| 33 |
:callback_include_count => true, |
| 34 |
:callback_after_arg_check => false, |
| 35 |
:includes => nil, |
| 36 |
:includes_h_pre_orig_header => nil, |
| 37 |
:includes_h_post_orig_header => nil, |
| 38 |
:includes_c_pre_header => nil, |
| 39 |
:includes_c_post_header => nil, |
| 40 |
:orig_header_include_fmt => '#include "%s"', |
| 41 |
:array_size_type => [], |
| 42 |
:array_size_name => 'size|len', |
| 43 |
:skeleton => false, |
| 44 |
:exclude_setjmp_h => false, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
:inline_function_patterns => ['(static\s+inline|inline\s+static)\s*', '(\bstatic\b|\binline\b)\s*', '(?:static\s*)?(?:__inline__)?__attribute__\s*\([ (]*always_inline[ )]*\)', 'static __inline__'] |
| 54 |
}.freeze |
| 55 |
|
| 56 |
def initialize(options = nil) |
| 57 |
case options |
| 58 |
when NilClass then options = CMOCK_DEFAULT_OPTIONS.dup |
| 59 |
when String then options = CMOCK_DEFAULT_OPTIONS.dup.merge(load_config_file_from_yaml(options)) |
| 60 |
when Hash then options = CMOCK_DEFAULT_OPTIONS.dup.merge(options) |
| 61 |
else raise 'If you specify arguments, it should be a filename or a hash of options' |
| 62 |
end |
| 63 |
|
| 64 |
|
| 65 |
%i[plugins attributes treat_as_void].each do |opt| |
| 66 |
unless options[opt].class == Array |
| 67 |
options[opt] = [] |
| 68 |
puts "WARNING: :#{opt} should be an array." unless options[:verbosity] < 1 |
| 69 |
end |
| 70 |
end |
| 71 |
%i[includes includes_h_pre_orig_header includes_h_post_orig_header includes_c_pre_header includes_c_post_header].each do |opt| |
| 72 |
unless options[opt].nil? || (options[opt].class == Array) |
| 73 |
options[opt] = [] |
| 74 |
puts "WARNING: :#{opt} should be an array." unless options[:verbosity] < 1 |
| 75 |
end |
| 76 |
end |
| 77 |
options[:unity_helper_path] ||= options[:unity_helper] |
| 78 |
options[:unity_helper_path] = [options[:unity_helper_path]] if options[:unity_helper_path].is_a? String |
| 79 |
|
| 80 |
if options[:unity_helper_path] |
| 81 |
require 'pathname' |
| 82 |
includes1 = options[:includes_c_post_header] || [] |
| 83 |
includes2 = options[:unity_helper_path].map do |path| |
| 84 |
Pathname(path).relative_path_from(Pathname(options[:mock_path])).to_s |
| 85 |
end |
| 86 |
options[:includes_c_post_header] = (includes1 + includes2).uniq |
| 87 |
end |
| 88 |
|
| 89 |
options[:plugins].compact! |
| 90 |
options[:plugins].map!(&:to_sym) |
| 91 |
@options = options |
| 92 |
|
| 93 |
treat_as_map = standard_treat_as_map |
| 94 |
treat_as_map.merge!(@options[:treat_as]) |
| 95 |
@options[:treat_as] = treat_as_map |
| 96 |
|
| 97 |
@options.each_key do |key| |
| 98 |
unless methods.include?(key) |
| 99 |
eval("def #{key}() return @options[:#{key}] end") |
| 100 |
end |
| 101 |
end |
| 102 |
end |
| 103 |
|
| 104 |
def load_config_file_from_yaml(yaml_filename) |
| 105 |
self.class.load_config_file_from_yaml yaml_filename |
| 106 |
end |
| 107 |
|
| 108 |
def self.load_config_file_from_yaml(yaml_filename) |
| 109 |
require 'yaml' |
| 110 |
require 'fileutils' |
| 111 |
YAML.load_file(yaml_filename)[:cmock] |
| 112 |
end |
| 113 |
|
| 114 |
def path(new_path) |
| 115 |
@src_path = new_path |
| 116 |
end |
| 117 |
|
| 118 |
def load_unity_helper |
| 119 |
return nil unless @options[:unity_helper_path] |
| 120 |
|
| 121 |
@options[:unity_helper_path].inject('') do |unity_helper, filename| |
| 122 |
unity_helper + "\n" + File.new(filename).read |
| 123 |
end |
| 124 |
end |
| 125 |
|
| 126 |
def standard_treat_as_map |
| 127 |
{ |
| 128 |
'int' => 'INT', |
| 129 |
'char' => 'INT8', |
| 130 |
'short' => 'INT16', |
| 131 |
'long' => 'INT', |
| 132 |
'int8' => 'INT8', |
| 133 |
'int16' => 'INT16', |
| 134 |
'int32' => 'INT', |
| 135 |
'int8_t' => 'INT8', |
| 136 |
'int16_t' => 'INT16', |
| 137 |
'int32_t' => 'INT', |
| 138 |
'INT8_T' => 'INT8', |
| 139 |
'INT16_T' => 'INT16', |
| 140 |
'INT32_T' => 'INT', |
| 141 |
'bool' => 'INT', |
| 142 |
'bool_t' => 'INT', |
| 143 |
'BOOL' => 'INT', |
| 144 |
'BOOL_T' => 'INT', |
| 145 |
'unsigned int' => 'HEX32', |
| 146 |
'unsigned long' => 'HEX32', |
| 147 |
'uint32' => 'HEX32', |
| 148 |
'uint32_t' => 'HEX32', |
| 149 |
'UINT32' => 'HEX32', |
| 150 |
'UINT32_T' => 'HEX32', |
| 151 |
'void*' => 'HEX8_ARRAY', |
| 152 |
'void const*' => 'HEX8_ARRAY', |
| 153 |
'const void*' => 'HEX8_ARRAY', |
| 154 |
'unsigned short' => 'HEX16', |
| 155 |
'uint16' => 'HEX16', |
| 156 |
'uint16_t' => 'HEX16', |
| 157 |
'UINT16' => 'HEX16', |
| 158 |
'UINT16_T' => 'HEX16', |
| 159 |
'unsigned char' => 'HEX8', |
| 160 |
'uint8' => 'HEX8', |
| 161 |
'uint8_t' => 'HEX8', |
| 162 |
'UINT8' => 'HEX8', |
| 163 |
'UINT8_T' => 'HEX8', |
| 164 |
'char*' => 'STRING', |
| 165 |
'char const*' => 'STRING', |
| 166 |
'const char*' => 'STRING', |
| 167 |
'pCHAR' => 'STRING', |
| 168 |
'cstring' => 'STRING', |
| 169 |
'CSTRING' => 'STRING', |
| 170 |
'float' => 'FLOAT', |
| 171 |
'double' => 'FLOAT' |
| 172 |
} |
| 173 |
end |
| 174 |
end |