Debian Codegraph @

test/unit/cmock_generator_plugin_callback_test.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 require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
8 require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_generator_plugin_callback'
9
10 describe CMockGeneratorPluginCallback, "Verify CMockGeneratorPluginCallback Module" do
11
12 before do
13 create_mocks :config, :utils
14
15 @config.expect :callback_include_count, true
16 @config.expect :callback_after_arg_check, false
17 @config.expect :plugins, [:ignore]
18
19 @cmock_generator_plugin_callback = CMockGeneratorPluginCallback.new(@config, @utils)
20 end
21
22 after do
23 end
24
25 it "have set up internal priority" do
26 assert_equal(6, @cmock_generator_plugin_callback.priority)
27 end
28
29 it "not include any additional include files" do
30 assert(!@cmock_generator_plugin_callback.respond_to?(:include_files))
31 end
32
33 it "add to instance structure" do
34 function = {:name => "Oak", :args => [:type => "int*", :name => "blah", :ptr? => true], :return => test_return[:int_ptr]}
35 expected = " char Oak_CallbackBool;\n" +
36 " CMOCK_Oak_CALLBACK Oak_CallbackFunctionPointer;\n" +
37 " int Oak_CallbackCalls;\n"
38 returned = @cmock_generator_plugin_callback.instance_structure(function)
39 assert_equal(expected, returned)
40 end
41
42 it "add mock function declaration for function without arguments" do
43 function = {:name => "Maple", :args_string => "void", :args => [], :return => test_return[:void]}
44 expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int cmock_num_calls);\n",
45 "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
46 "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
47 "#define Maple_StubWithCallback Maple_Stub\n" ].join
48 returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
49 assert_equal(expected, returned)
50 end
51
52 it "add mock function declaration for function without arguments when count is also turned off" do
53 function = {:name => "Maple", :args_string => "void", :args => [], :return => test_return[:void]}
54 expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(void);\n",
55 "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
56 "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
57 "#define Maple_StubWithCallback Maple_Stub\n" ].join
58 @cmock_generator_plugin_callback.include_count = false
59 returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
60 assert_equal(expected, returned)
61 end
62
63 it "add mock function declaration for function with arguments" do
64 function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:void]}
65 expected = [ "typedef void (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n",
66 "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
67 "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
68 "#define Maple_StubWithCallback Maple_Stub\n" ].join
69 returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
70 assert_equal(expected, returned)
71 end
72
73 it "add mock function declaration for function with return values" do
74 function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:string]}
75 expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu, int cmock_num_calls);\n",
76 "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
77 "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
78 "#define Maple_StubWithCallback Maple_Stub\n" ].join
79 returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
80 assert_equal(expected, returned)
81 end
82
83 it "add mock function declaration for function with return values and count is turned off" do
84 function = {:name => "Maple", :args_string => "int* tofu", :args => [1], :return => test_return[:string]}
85 expected = [ "typedef const char* (* CMOCK_Maple_CALLBACK)(int* tofu);\n",
86 "void Maple_AddCallback(CMOCK_Maple_CALLBACK Callback);\n",
87 "void Maple_Stub(CMOCK_Maple_CALLBACK Callback);\n",
88 "#define Maple_StubWithCallback Maple_Stub\n" ].join
89 @cmock_generator_plugin_callback.include_count = false
90 returned = @cmock_generator_plugin_callback.mock_function_declarations(function)
91 assert_equal(expected, returned)
92 end
93
94 it "add mock function implementation for functions with no arg check and of style 'void func(void)'" do
95 function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]}
96 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
97 " {\n",
98 " Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n",
99 " }\n"
100 ].join
101 returned = @cmock_generator_plugin_callback.mock_implementation(function)
102 assert_equal(expected, returned)
103 end
104
105 it "add mock function implementation for functions with no arg check and of style 'void func(void)' when count turned off" do
106 function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]}
107 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
108 " {\n",
109 " Mock.Apple_CallbackFunctionPointer();\n",
110 " }\n"
111 ].join
112 @cmock_generator_plugin_callback.include_count = false
113 returned = @cmock_generator_plugin_callback.mock_implementation(function)
114 assert_equal(expected, returned)
115 end
116
117 it "add mock function implementation for functions with no arg check and of style 'int func(void)'" do
118 function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:int]}
119 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
120 " {\n",
121 " cmock_call_instance->ReturnVal = Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n",
122 " }\n"
123 ].join
124 returned = @cmock_generator_plugin_callback.mock_implementation(function)
125 assert_equal(expected, returned)
126 end
127
128 it "add mock function implementation for functions with no arg check and of style 'void func(int* steak, uint8_t flag)'" do
129 function = {:name => "Apple",
130 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
131 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
132 :args_string => "int* steak, uint8_t flag",
133 :return=> test_return[:void]}
134 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
135 " {\n",
136 " Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n",
137 " }\n"
138 ].join
139 returned = @cmock_generator_plugin_callback.mock_implementation(function)
140 assert_equal(expected, returned)
141 end
142
143 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
144 function = {:name => "Apple",
145 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
146 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
147 :args_string => "int* steak, uint8_t flag",
148 :return=> test_return[:void]}
149 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
150 " {\n",
151 " Mock.Apple_CallbackFunctionPointer(steak, flag);\n",
152 " }\n"
153 ].join
154 @cmock_generator_plugin_callback.include_count = false
155 returned = @cmock_generator_plugin_callback.mock_implementation(function)
156 assert_equal(expected, returned)
157 end
158
159 it "add mock function implementation for functions with no arg check and of style 'int16_t func(int* steak, uint8_t flag)'" do
160 function = {:name => "Apple",
161 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
162 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
163 :args_string => "int* steak, uint8_t flag",
164 :return => test_return[:int]}
165 expected = [" if (Mock.Apple_CallbackFunctionPointer != NULL)\n",
166 " {\n",
167 " cmock_call_instance->ReturnVal = Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n",
168 " }\n"
169 ].join
170 returned = @cmock_generator_plugin_callback.mock_implementation(function)
171 assert_equal(expected, returned)
172 end
173
174 it "add mock function implementation for functions without arg check and of style 'void func(void)' when count turned off" do
175 function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:void]}
176 expected = [" if (!Mock.Apple_CallbackBool &&\n",
177 " Mock.Apple_CallbackFunctionPointer != NULL)\n",
178 " {\n",
179 " Mock.Apple_CallbackFunctionPointer();\n",
180 " UNITY_CLR_DETAILS();\n",
181 " return;\n",
182 " }\n"
183 ].join
184 @cmock_generator_plugin_callback.include_count = false
185 returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function)
186 assert_equal(expected, returned)
187 end
188
189 it "add mock function implementation for functions without arg check and of style 'int func(void)'" do
190 function = {:name => "Apple", :args => [], :args_string => "void", :return => test_return[:int]}
191 expected = [" if (!Mock.Apple_CallbackBool &&\n",
192 " Mock.Apple_CallbackFunctionPointer != NULL)\n",
193 " {\n",
194 " int cmock_cb_ret = Mock.Apple_CallbackFunctionPointer(Mock.Apple_CallbackCalls++);\n",
195 " UNITY_CLR_DETAILS();\n",
196 " return cmock_cb_ret;\n",
197 " }\n"
198 ].join
199 returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function)
200 assert_equal(expected, returned)
201 end
202
203 it "add mock function implementation for functions without arg check and of style 'void func(int* steak, uint8_t flag)'" do
204 function = {:name => "Apple",
205 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
206 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
207 :args_string => "int* steak, uint8_t flag",
208 :return=> test_return[:void]}
209 expected = [" if (!Mock.Apple_CallbackBool &&\n",
210 " Mock.Apple_CallbackFunctionPointer != NULL)\n",
211 " {\n",
212 " Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n",
213 " UNITY_CLR_DETAILS();\n",
214 " return;\n",
215 " }\n"
216 ].join
217 returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function)
218 assert_equal(expected, returned)
219 end
220
221 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
222 function = {:name => "Apple",
223 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
224 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
225 :args_string => "int* steak, uint8_t flag",
226 :return=> test_return[:void]}
227 expected = [" if (!Mock.Apple_CallbackBool &&\n",
228 " Mock.Apple_CallbackFunctionPointer != NULL)\n",
229 " {\n",
230 " Mock.Apple_CallbackFunctionPointer(steak, flag);\n",
231 " UNITY_CLR_DETAILS();\n",
232 " return;\n",
233 " }\n"
234 ].join
235 @cmock_generator_plugin_callback.include_count = false
236 returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function)
237 assert_equal(expected, returned)
238 end
239
240 it "add mock function implementation for functions without arg check and of style 'int16_t func(int* steak, uint8_t flag)'" do
241 function = {:name => "Apple",
242 :args => [ { :type => 'int*', :name => 'steak', :ptr? => true},
243 { :type => 'uint8_t', :name => 'flag', :ptr? => false} ],
244 :args_string => "int* steak, uint8_t flag",
245 :return => test_return[:int]}
246 expected = [" if (!Mock.Apple_CallbackBool &&\n",
247 " Mock.Apple_CallbackFunctionPointer != NULL)\n",
248 " {\n",
249 " int cmock_cb_ret = Mock.Apple_CallbackFunctionPointer(steak, flag, Mock.Apple_CallbackCalls++);\n",
250 " UNITY_CLR_DETAILS();\n",
251 " return cmock_cb_ret;\n",
252 " }\n"
253 ].join
254 returned = @cmock_generator_plugin_callback.mock_implementation_precheck(function)
255 assert_equal(expected, returned)
256 end
257
258 it "add mock interfaces for functions " do
259 function = {:name => "Lemon",
260 :args => [{ :type => "char*", :name => "pescado"}],
261 :args_string => "char* pescado",
262 :return => test_return[:int]
263 }
264
265 expected = ["void Lemon_AddCallback(CMOCK_Lemon_CALLBACK Callback)\n",
266 "{\n",
267 " Mock.Lemon_IgnoreBool = (char)0;\n",
268 " Mock.Lemon_CallbackBool = (char)1;\n",
269 " Mock.Lemon_CallbackFunctionPointer = Callback;\n",
270 "}\n\n",
271 "void Lemon_Stub(CMOCK_Lemon_CALLBACK Callback)\n",
272 "{\n",
273 " Mock.Lemon_IgnoreBool = (char)0;\n",
274 " Mock.Lemon_CallbackBool = (char)0;\n",
275 " Mock.Lemon_CallbackFunctionPointer = Callback;\n",
276 "}\n\n"
277 ].join
278 returned = @cmock_generator_plugin_callback.mock_interfaces(function)
279 assert_equal(expected, returned)
280 end
281 end