Debian Codegraph @

lib/cmock_generator_utils.rb

raw symbols SCIP
1 # ==========================================
2 # CMock Project - Automatic Mock Generation for C
3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 # [Released under MIT License. Please refer to license.txt for details]
5 # ==========================================
6
7 class CMockGeneratorUtils
8 attr_accessor :config, :helpers, :ordered, :ptr_handling, :arrays, :cexception
9
10 def initialize(config, helpers = {})
11 @config = config
12 @ptr_handling = @config.when_ptr
13 @ordered = @config.enforce_strict_ordering
14 @arrays = @config.plugins.include? :array
15 @cexception = @config.plugins.include? :cexception
16 @expect_any = @config.plugins.include? :expect_any_args
17 @return_thru_ptr = @config.plugins.include? :return_thru_ptr
18 @ignore_arg = @config.plugins.include? :ignore_arg
19 @ignore = @config.plugins.include? :ignore
20 @ignore_stateless = @config.plugins.include? :ignore_stateless
21 @treat_as = @config.treat_as
22 @helpers = helpers
23 end
24
25 def self.arg_type_with_const(arg)
26 # Restore any "const" that was removed in header parsing
27 if arg[:type].include?('*')
28 arg[:const_ptr?] ? "#{arg[:type]} const" : arg[:type]
29 else
30 arg[:const?] ? "const #{arg[:type]}" : arg[:type]
31 end
32 end
33
34 def arg_type_with_const(arg)
35 self.class.arg_type_with_const(arg)
36 end
37
38 def code_verify_an_arg_expectation(function, arg)
39 if @arrays
40 case @ptr_handling
41 when :smart then code_verify_an_arg_expectation_with_smart_arrays(function, arg)
42 when :compare_data then code_verify_an_arg_expectation_with_normal_arrays(function, arg)
43 when :compare_ptr then raise "ERROR: the array plugin doesn't enjoy working with :compare_ptr only. Disable one option."
44 end
45 else
46 code_verify_an_arg_expectation_with_no_arrays(function, arg)
47 end
48 end
49
50 def code_add_base_expectation(func_name, global_ordering_supported = true)
51 lines = " CMOCK_MEM_INDEX_TYPE cmock_guts_index = CMock_Guts_MemNew(sizeof(CMOCK_#{func_name}_CALL_INSTANCE));\n"
52 lines << " CMOCK_#{func_name}_CALL_INSTANCE* cmock_call_instance = (CMOCK_#{func_name}_CALL_INSTANCE*)CMock_Guts_GetAddressFor(cmock_guts_index);\n"
53 lines << " UNITY_TEST_ASSERT_NOT_NULL(cmock_call_instance, cmock_line, CMockStringOutOfMemory);\n"
54 lines << " memset(cmock_call_instance, 0, sizeof(*cmock_call_instance));\n"
55 lines << " Mock.#{func_name}_CallInstance = CMock_Guts_MemChain(Mock.#{func_name}_CallInstance, cmock_guts_index);\n"
56 lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if @ignore || @ignore_stateless
57 lines << " cmock_call_instance->LineNumber = cmock_line;\n"
58 lines << " cmock_call_instance->CallOrder = ++GlobalExpectCount;\n" if @ordered && global_ordering_supported
59 lines << " cmock_call_instance->ExceptionToThrow = CEXCEPTION_NONE;\n" if @cexception
60 lines << " cmock_call_instance->ExpectAnyArgsBool = (char)0;\n" if @expect_any
61 lines
62 end
63
64 def code_add_an_arg_expectation(arg, depth = 1)
65 lines = code_assign_argument_quickly("cmock_call_instance->Expected_#{arg[:name]}", arg)
66 lines << " cmock_call_instance->Expected_#{arg[:name]}_Depth = #{arg[:name]}_Depth;\n" if @arrays && (depth.class == String)
67 lines << " cmock_call_instance->IgnoreArg_#{arg[:name]} = 0;\n" if @ignore_arg
68 lines << " cmock_call_instance->ReturnThruPtr_#{arg[:name]}_Used = 0;\n" if @return_thru_ptr && ptr_or_str?(arg[:type]) && !(arg[:const?])
69 lines
70 end
71
72 def code_assign_argument_quickly(dest, arg)
73 if arg[:ptr?] || @treat_as.include?(arg[:type])
74 " #{dest} = #{arg[:name]};\n"
75 else
76 assert_expr = "sizeof(#{arg[:name]}) == sizeof(#{arg[:type]}) ? 1 : -1"
77 comment = "/* add #{arg[:type]} to :treat_as_array if this causes an error */"
78 " memcpy((void*)(&#{dest}), (void*)(&#{arg[:name]}),\n" \
79 " sizeof(#{arg[:type]}[#{assert_expr}])); #{comment}\n"
80 end
81 end
82
83 def code_add_argument_loader(function)
84 if function[:args_string] != 'void'
85 if @arrays
86 args_string = function[:args].map do |m|
87 type = arg_type_with_const(m)
88 m[:ptr?] ? "#{type} #{m[:name]}, int #{m[:name]}_Depth" : "#{type} #{m[:name]}"
89 end.join(', ')
90 "void CMockExpectParameters_#{function[:name]}(CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance, #{args_string});\n" \
91 "void CMockExpectParameters_#{function[:name]}(CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance, #{args_string})\n{\n" +
92 function[:args].inject('') { |all, arg| all + code_add_an_arg_expectation(arg, (arg[:ptr?] ? "#{arg[:name]}_Depth" : 1)) } +
93 "}\n\n"
94 else
95 "void CMockExpectParameters_#{function[:name]}(CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance, #{function[:args_string]});\n" \
96 "void CMockExpectParameters_#{function[:name]}(CMOCK_#{function[:name]}_CALL_INSTANCE* cmock_call_instance, #{function[:args_string]})\n{\n" +
97 function[:args].inject('') { |all, arg| all + code_add_an_arg_expectation(arg) } +
98 "}\n\n"
99 end
100 else
101 ''
102 end
103 end
104
105 def code_call_argument_loader(function)
106 if function[:args_string] != 'void'
107 args = function[:args].map do |m|
108 if @arrays && m[:ptr?] && !(m[:array_data?])
109 "#{m[:name]}, 1"
110 elsif @arrays && m[:array_size?]
111 "#{m[:name]}, #{m[:name]}"
112 else
113 m[:name]
114 end
115 end
116 " CMockExpectParameters_#{function[:name]}(cmock_call_instance, #{args.join(', ')});\n"
117 else
118 ''
119 end
120 end
121
122 def ptr_or_str?(arg_type)
123 (arg_type.include?('*') ||
124 @treat_as.fetch(arg_type, '').include?('*'))
125 end
126
127 # private ######################
128
129 def lookup_expect_type(_function, arg)
130 c_type = arg[:type]
131 arg_name = arg[:name]
132 expected = "cmock_call_instance->Expected_#{arg_name}"
133 ignore = "cmock_call_instance->IgnoreArg_#{arg_name}"
134 unity_func = if (arg[:ptr?]) && ((c_type =~ /\*\*/) || (@ptr_handling == :compare_ptr))
135 ['UNITY_TEST_ASSERT_EQUAL_PTR', '']
136 else
137 @helpers.nil? || @helpers[:unity_helper].nil? ? ['UNITY_TEST_ASSERT_EQUAL', ''] : @helpers[:unity_helper].get_helper(c_type)
138 end
139 [c_type, arg_name, expected, ignore, unity_func[0], unity_func[1]]
140 end
141
142 def code_verify_an_arg_expectation_with_no_arrays(function, arg)
143 c_type, arg_name, expected, ignore, unity_func, pre = lookup_expect_type(function, arg)
144 lines = ''
145 lines << " if (!#{ignore})\n" if @ignore_arg
146 lines << " {\n"
147 lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n"
148 case unity_func
149 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY'
150 c_type_local = c_type.gsub(/\*$/, '')
151 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n"
152 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY'
153 if pre == '&'
154 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), cmock_line, CMockStringMismatch);\n"
155 else
156 lines << " if (#{pre}#{expected} == NULL)\n"
157 lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
158 lines << " else\n"
159 lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), cmock_line, CMockStringMismatch); }\n"
160 end
161 when /_ARRAY/
162 if pre == '&'
163 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, CMockStringMismatch);\n"
164 else
165 lines << " if (#{pre}#{expected} == NULL)\n"
166 lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
167 lines << " else\n"
168 lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, 1, cmock_line, CMockStringMismatch); }\n"
169 end
170 else
171 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n"
172 end
173 lines << " }\n"
174 lines
175 end
176
177 def code_verify_an_arg_expectation_with_normal_arrays(function, arg)
178 c_type, arg_name, expected, ignore, unity_func, pre = lookup_expect_type(function, arg)
179 depth_name = arg[:ptr?] ? "cmock_call_instance->Expected_#{arg_name}_Depth" : 1
180 lines = ''
181 lines << " if (!#{ignore})\n" if @ignore_arg
182 lines << " {\n"
183 lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n"
184 case unity_func
185 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY'
186 c_type_local = c_type.gsub(/\*$/, '')
187 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n"
188 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY'
189 if pre == '&'
190 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), cmock_line, CMockStringMismatch);\n"
191 else
192 lines << " if (#{pre}#{expected} == NULL)\n"
193 lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
194 lines << " else\n"
195 lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), #{depth_name}, cmock_line, CMockStringMismatch); }\n"
196 end
197 when /_ARRAY/
198 if pre == '&'
199 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch);\n"
200 else
201 lines << " if (#{pre}#{expected} == NULL)\n"
202 lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
203 lines << " else\n"
204 lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch); }\n"
205 end
206 else
207 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n"
208 end
209 lines << " }\n"
210 lines
211 end
212
213 def code_verify_an_arg_expectation_with_smart_arrays(function, arg)
214 c_type, arg_name, expected, ignore, unity_func, pre = lookup_expect_type(function, arg)
215 depth_name = arg[:ptr?] ? "cmock_call_instance->Expected_#{arg_name}_Depth" : 1
216 lines = ''
217 lines << " if (!#{ignore})\n" if @ignore_arg
218 lines << " {\n"
219 lines << " UNITY_SET_DETAILS(CMockString_#{function[:name]},CMockString_#{arg_name});\n"
220 case unity_func
221 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY'
222 c_type_local = c_type.gsub(/\*$/, '')
223 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type_local}), cmock_line, CMockStringMismatch);\n"
224 when 'UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY'
225 if pre == '&'
226 lines << " UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), #{depth_name}, cmock_line, CMockStringMismatch);\n"
227 else
228 lines << " if (#{pre}#{expected} == NULL)\n"
229 lines << " { UNITY_TEST_ASSERT_NULL(#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
230 lines << (depth_name != 1 ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch); }\n" : '')
231 lines << " else\n"
232 lines << " { UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY((void*)(#{pre}#{expected}), (void*)(#{pre}#{arg_name}), sizeof(#{c_type.sub('*', '')}), #{depth_name}, cmock_line, CMockStringMismatch); }\n"
233 end
234 when /_ARRAY/
235 if pre == '&'
236 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch);\n"
237 else
238 lines << " if (#{pre}#{expected} == NULL)\n"
239 lines << " { UNITY_TEST_ASSERT_NULL(#{pre}#{arg_name}, cmock_line, CMockStringExpNULL); }\n"
240 lines << (depth_name != 1 ? " else if (#{depth_name} == 0)\n { UNITY_TEST_ASSERT_EQUAL_PTR(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch); }\n" : '')
241 lines << " else\n"
242 lines << " { #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, #{depth_name}, cmock_line, CMockStringMismatch); }\n"
243 end
244 else
245 lines << " #{unity_func}(#{pre}#{expected}, #{pre}#{arg_name}, cmock_line, CMockStringMismatch);\n"
246 end
247 lines << " }\n"
248 lines
249 end
250 end