Debian Codegraph @

test/unit/cmock_header_parser_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 $ThisIsOnlyATest = true
8
9 require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
10 require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_header_parser'
11
12 describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
13
14 before do
15 create_mocks :config
16 @config.expect :strippables, ["STRIPPABLE"]
17 @config.expect :attributes, ['__ramfunc', 'funky_attrib', 'SQLITE_API']
18 @config.expect :c_calling_conventions, ['__stdcall']
19 @config.expect :treat_as_void, ['MY_FUNKY_VOID']
20 @config.expect :treat_as, { "BANJOS" => "INT", "TUBAS" => "HEX16"}
21 @config.expect :treat_as_array, {"IntArray" => "int", "Book" => "Page"}
22 @config.expect :when_no_prototypes, :error
23 @config.expect :verbosity, 1
24 @config.expect :treat_externs, :exclude
25 @config.expect :treat_inlines, :exclude
26 @config.expect :inline_function_patterns, ['(static\s+inline|inline\s+static)\s*', '(\bstatic\b|\binline\b)\s*']
27 @config.expect :array_size_type, ['int', 'size_t']
28 @config.expect :array_size_name, 'size|len'
29
30 @parser = CMockHeaderParser.new(@config)
31
32 @test_project = {
33 :module_name => 'test_file.h',
34 :typedefs => [],
35 :functions => [],
36 :normalized_source => nil
37 }
38 end
39
40 after do
41 end
42
43 it "create and initialize variables to defaults appropriately" do
44 assert_nil(@parser.funcs)
45 assert_equal(['const', '__ramfunc', 'funky_attrib', 'SQLITE_API'], @parser.c_attributes)
46 assert_equal(['void','MY_FUNKY_VOID'], @parser.treat_as_void)
47 end
48
49 it "strip out line comments" do
50 source =
51 " abcd;\n" +
52 "// hello;\n" +
53 "who // is you\n"
54
55 expected =
56 [
57 "abcd",
58 "who"
59 ]
60
61 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
62 end
63
64 it "remove block comments" do
65 source =
66 " no_comments;\n" +
67 "// basic_line_comment;\n" +
68 "/* basic_block_comment;*/\n" +
69 "pre_block; /* start_of_block_comment;\n" +
70 "// embedded_line_comment_in_block_comment; */\n" +
71 "// /* commented_out_block_comment_line\n" +
72 "shown_because_block_comment_invalid_from_line_comment;\n" +
73 "// */\n" +
74 "//* shorter_commented_out_block_comment_line; \n" +
75 "shown_because_block_comment_invalid_from_shorter_line_comment;\n" +
76 "/*/\n" +
77 "not_shown_because_line_above_started_comment;\n" +
78 "//*/\n" +
79 "/* \n" +
80 "not_shown_because_block_comment_started_this_time;\n" +
81 "/*/\n" +
82 "shown_because_line_above_ended_comment_this_time;\n" +
83 "//*/\n"
84
85 expected =
86 [
87 "no_comments",
88 "pre_block",
89 "shown_because_block_comment_invalid_from_line_comment",
90 "shown_because_block_comment_invalid_from_shorter_line_comment",
91 "shown_because_line_above_ended_comment_this_time"
92 ]
93
94 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
95 end
96
97 it "remove strippables from the beginning or end of function declarations" do
98 source =
99 "void* my_calloc(size_t, size_t) STRIPPABLE;\n" +
100 "void\n" +
101 " my_realloc(void*, size_t) STRIPPABLE;\n" +
102 "extern int\n" +
103 " my_printf (void *my_object, const char *my_format, ...)\n" +
104 " STRIPPABLE;\n" +
105 " void STRIPPABLE universal_handler ();\n"
106
107 expected =
108 [
109 "void* my_calloc(size_t, size_t)",
110 "void my_realloc(void*, size_t)",
111 "void universal_handler()"
112 ]
113
114 assert_equal(expected, @parser.import_source(source, @test_project))
115 end
116
117 it "remove gcc's function __attribute__'s" do
118 source =
119 "void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2)));\n" +
120 "void\n" +
121 " my_realloc(void*, size_t) __attribute__((alloc_size(2)));\n" +
122 "extern int\n" +
123 " my_printf (void *my_object, const char *my_format, ...)\n" +
124 " __attribute__ ( (format (printf, 2, 3)) );\n" +
125 " void __attribute__ ((interrupt)) universal_handler ();\n"
126
127 expected =
128 [
129 "void* my_calloc(size_t, size_t)",
130 "void my_realloc(void*, size_t)",
131 "void universal_handler()"
132 ]
133
134 assert_equal(expected, @parser.import_source(source, @test_project))
135 end
136
137 it "remove preprocessor directives" do
138 source =
139 "#when stuff_happens\n" +
140 "#ifdef _TEST\n" +
141 "#pragma stack_switch"
142
143 expected = []
144
145 assert_equal(expected, @parser.import_source(source, @test_project))
146 end
147
148
149 it "remove assembler pragma sections" do
150 source =
151 " #pragma\tasm\n" +
152 " .foo\n" +
153 " lda %m\n" +
154 " nop\n" +
155 "# pragma endasm \n" +
156 "foo"
157
158 expected = ["foo"]
159
160 assert_equal(expected, @parser.import_source(source, @test_project))
161 end
162
163
164 it "smush lines together that contain continuation characters" do
165 source =
166 "hoo hah \\\n" +
167 "when \\ \n"
168
169 expected =
170 [
171 "hoo hah when"
172 ]
173
174 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
175 end
176
177
178 it "remove C macro definitions" do
179 source =
180 "#define this is the first line\\\n" +
181 "and the second\\\n" +
182 "and the third that should be removed\n" +
183 "but I'm here\n"
184
185 expected = ["but I'm here"]
186
187 assert_equal(expected, @parser.import_source(source, @test_project))
188 end
189
190
191 it "remove typedef statements" do
192 source =
193 "typedef uint32 (unsigned int);\n" +
194 "const typedef int INT;\n" +
195 "int notatypedef;\n" +
196 "int typedef_isnt_me;\n" +
197 " typedef who cares what really comes here \n" + # exercise multiline typedef
198 " continuation;\n" +
199 "this should remain!;\n" +
200 "typedef blah bleh;\n" +
201 "typedef struct shell_command_struct {\n" +
202 " char_ptr COMMAND;\n" +
203 " int_32 (*SHELL_FUNC)(int_32 argc);\n" +
204 "} SHELL_COMMAND_STRUCT, * SHELL_COMMAND_PTR;\n" +
205 "typedef struct shell_command_struct {\n" +
206 " char_ptr COMMAND;\n" +
207 " int_32 (*SHELL_FUNC)(int_32 argc, char_ptr argv[]);\n" +
208 "} SHELL_COMMAND_STRUCT, * SHELL_COMMAND_PTR;\n" +
209 "typedef struct shell_command_struct {\n" +
210 " char_ptr COMMAND;\n" +
211 " int_32 (*SHELL_FUNC)(int_32 argc);\n" +
212 "};\n"
213
214 expected =
215 [
216 "int notatypedef",
217 "int typedef_isnt_me",
218 "this should remain!"
219 ]
220
221 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
222 end
223
224
225 it "remove enum statements" do
226 source =
227 "enum _NamedEnum {\n" +
228 " THING1 = (0x0001),\n" +
229 " THING2 = (0x0001 << 5),\n" +
230 "}ListOValues;\n\n" +
231 "don't delete me!!\n" +
232 " modifier_str enum _NamedEnum {THING1 = (0x0001), THING2 = (0x0001 << 5)} ListOValues;\n\n" +
233 "typedef enum {\n" +
234 " THING1,\n" +
235 " THING2,\n" +
236 "} Thinger;\n" +
237 "or me!!\n"
238
239 assert_equal(["don't delete me!! or me!!"], @parser.import_source(source, @test_project).map!{|s|s.strip})
240 end
241
242
243 it "remove union statements" do
244 source =
245 "union _NamedDoohicky {\n" +
246 " unsigned int a;\n" +
247 " char b;\n" +
248 "} Doohicky;\n\n" +
249 "I want to live!!\n" +
250 "some_modifier union { unsigned int a; char b;} Whatever;\n" +
251 "typedef union {\n" +
252 " unsigned int a;\n" +
253 " char b;\n" +
254 "} Whatever;\n" +
255 "me too!!\n"
256
257 assert_equal(["I want to live!! me too!!"], @parser.import_source(source, @test_project).map!{|s|s.strip})
258 end
259
260
261 it "remove struct statements" do
262 source =
263 "struct _NamedStruct1 {\n" +
264 " unsigned int a;\n" +
265 " signed long int b;\n" +
266 "} Thing ;\n\n" +
267 "extern struct ForwardDeclared_t TestDataType1;\n" +
268 "void foo(void);\n" +
269 "struct\n"+
270 " MultilineForwardDeclared_t\n" +
271 " TestDataType2;\n" +
272 "struct THINGER foo(void);\n" +
273 "typedef struct {\n" +
274 " unsigned int a;\n" +
275 " signed char b;\n" +
276 "}Thinger;\n" +
277 "I want to live!!\n"
278
279 assert_equal(["void foo(void)", "struct THINGER foo(void)", "I want to live!!"],
280 @parser.import_source(source, @test_project).map!{|s|s.strip})
281 end
282
283 it "remove externed and inline functions" do
284 source =
285 " extern uint32 foobar(unsigned int);\n" +
286 "uint32 extern_name_func(unsigned int);\n" +
287 "uint32 funcinline(unsigned int);\n" +
288 "extern void bar(unsigned int);\n" +
289 "inline void bar(unsigned int);\n" +
290 "extern\n" +
291 "void kinda_ugly_on_the_next_line(unsigned int);\n"
292
293 expected =
294 [
295 "uint32 extern_name_func(unsigned int)",
296 "uint32 funcinline(unsigned int)"
297 ]
298
299 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
300 end
301
302 it "remove function definitions but keep function declarations" do
303 source =
304 "uint32 func_with_decl_a(unsigned int);\n" +
305 "uint32 func_with_decl_a(unsigned int a) { return a; }\n" +
306 "uint32 func_with_decl_b(unsigned int);\n" +
307 "uint32 func_with_decl_b(unsigned int a)\n" +
308 "{\n" +
309 " bar((unsigned int) a);\n" +
310 " stripme(a);\n" +
311 "}\n"
312
313 expected =
314 [
315 "uint32 func_with_decl_a(unsigned int)",
316 "uint32 func_with_decl_a", #okay. it's not going to be interpretted as another function
317 "uint32 func_with_decl_b(unsigned int)",
318 "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function
319 ]
320
321 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
322 end
323
324 it "remove function definitions with nested braces but keep function declarations" do
325 source =
326 "uint32 func_with_decl_a(unsigned int);\n" +
327 "uint32 func_with_decl_a(unsigned int a) {\n" +
328 " while (stuff) {\n" +
329 " not_a_definition1(void);\n" +
330 " }\n" +
331 " not_a_definition2(blah, bleh);\n" +
332 " return a;\n" +
333 "}\n" +
334 "uint32 func_with_decl_b(unsigned int);\n" +
335 "uint32 func_with_decl_b(unsigned int a)\n" +
336 "{\n" +
337 " bar((unsigned int) a);\n" +
338 " stripme(a);\n" +
339 "}\n" +
340 "uint32 func_with_decl_c(unsigned int);\n" +
341 "uint32 func_with_decl_c(unsigned int a)\n" +
342 "{\n" +
343 " if(a > 0)\n" +
344 " {\n" +
345 " return 1;\n" +
346 " }\n" +
347 " else\n"+
348 " {\n" +
349 " return 2;\n" +
350 " }\n" +
351 "}\n"
352
353 expected =
354 [
355 "uint32 func_with_decl_a(unsigned int)",
356 "uint32 func_with_decl_a", #okay. it's not going to be interpretted as another function
357 "uint32 func_with_decl_b(unsigned int)",
358 "uint32 func_with_decl_b", #okay. it's not going to be interpretted as another function
359 "uint32 func_with_decl_c(unsigned int)",
360 "uint32 func_with_decl_c", #okay. it's not going to be interpretted as another function
361 ]
362
363 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
364 end
365
366 it "remove a fully defined inline function" do
367 source =
368 "inline void foo(unsigned int a) { oranges = a; }\n" +
369 "inline void bar(unsigned int a) { apples = a; };\n" +
370 "inline void bar(unsigned int a)\n" +
371 "{" +
372 " bananas = a;\n" +
373 "}"
374
375 # ensure it's expected type of exception
376 assert_raises RuntimeError do
377 @parser.parse("module", source)
378 end
379
380 assert_nil(@parser.funcs)
381
382 # verify exception message
383 begin
384 @parser.parse("module", source)
385 rescue RuntimeError => e
386 assert_equal("ERROR: No function prototypes found!", e.message)
387 end
388 end
389
390 it "remove a fully defined inline function that is multiple lines" do
391 source =
392 "inline void bar(unsigned int a)\n" +
393 "{" +
394 " bananas = a;\n" +
395 " grapes = a;\n" +
396 " apples(bananas, grapes);\n" +
397 "}"
398
399 # ensure it's expected type of exception
400 assert_raises RuntimeError do
401 @parser.parse("module", source)
402 end
403
404 assert_nil(@parser.funcs)
405
406 # verify exception message
407 begin
408 @parser.parse("module", source)
409 rescue RuntimeError => e
410 assert_equal("ERROR: No function prototypes found!", e.message)
411 end
412 end
413
414 it "remove a fully defined inline function that contains nested braces" do
415 source =
416 "inline void bar(unsigned int a)\n" +
417 "{" +
418 " apples(bananas, grapes);\n" +
419 " if (bananas == a)\n" +
420 " {\n" +
421 " oranges(a);\n" +
422 " grapes = a;\n" +
423 " }\n" +
424 " grapefruit(bananas, grapes);\n" +
425 "}"
426
427 # ensure it's expected type of exception
428 assert_raises RuntimeError do
429 @parser.parse("module", source)
430 end
431
432 assert_nil(@parser.funcs)
433
434 # verify exception message
435 begin
436 @parser.parse("module", source)
437 rescue RuntimeError => e
438 assert_equal("ERROR: No function prototypes found!", e.message)
439 end
440 end
441
442 it "remove just inline functions if externs to be included" do
443 source =
444 " extern uint32 foobar(unsigned int);\n" +
445 "uint32 extern_name_func(unsigned int);\n" +
446 "uint32 funcinline(unsigned int);\n" +
447 "extern void bar(unsigned int);\n" +
448 "inline void bar(unsigned int);\n" +
449 "extern\n" +
450 "void kinda_ugly_on_the_next_line(unsigned int);\n"
451
452 expected =
453 [ "extern uint32 foobar(unsigned int)",
454 "uint32 extern_name_func(unsigned int)",
455 "uint32 funcinline(unsigned int)",
456 "extern void bar(unsigned int)",
457 "extern void kinda_ugly_on_the_next_line(unsigned int)"
458 ]
459
460 @parser.treat_externs = :include
461 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
462 end
463
464 it "leave inline functions if inline to be included" do
465 source =
466 "extern uint32 foobar(unsigned int);\n" +
467 "uint32 extern_name_func(unsigned int);\n" +
468 "uint32 funcinline(unsigned int);\n" +
469 "inline void inlineBar(unsigned int);\n" +
470 "extern int extern_bar(void);\n" +
471 "static inline void staticinlineBar(unsigned int);\n" +
472 "static inline void bar(unsigned int);\n" +
473 "static inline void bar(unsigned int)\n" +
474 "{\n" +
475 " // NOP\n" +
476 "}\n"
477
478 expected =
479 [ "uint32 extern_name_func(unsigned int)",
480 "uint32 funcinline(unsigned int)",
481 "void inlineBar(unsigned int)",
482 "void staticinlineBar(unsigned int)",
483 "void bar(unsigned int)"
484 ]
485
486 @parser.treat_inlines = :include
487 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
488 end
489
490 it "leave inline and extern functions if inline and extern to be included" do
491 source =
492 "extern uint32 foobar(unsigned int);\n" +
493 "uint32 extern_name_func(unsigned int);\n" +
494 "uint32 funcinline(unsigned int);\n" +
495 "inline void inlineBar(unsigned int);\n" +
496 "extern int extern_bar(void);\n" +
497 "static inline void staticinlineBar(unsigned int);\n" +
498 "static inline void bar(unsigned int);\n" +
499 "static inline void bar(unsigned int)\n" +
500 "{\n" +
501 " // NOP\n" +
502 "}\n"
503
504 expected =
505 [ "extern uint32 foobar(unsigned int)",
506 "uint32 extern_name_func(unsigned int)",
507 "uint32 funcinline(unsigned int)",
508 "void inlineBar(unsigned int)",
509 "extern int extern_bar(void)",
510 "void staticinlineBar(unsigned int)",
511 "void bar(unsigned int)"
512 ]
513
514 @parser.treat_externs = :include
515 @parser.treat_inlines = :include
516 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
517 end
518
519 it "Include inline functions that contain user defined inline function formats" do
520 source =
521 "uint32 foo(unsigned int);\n" +
522 "uint32 bar(unsigned int);\n" +
523 "inline void inlineBar(void)\n" +
524 "{\n" +
525 " return 43;\n" +
526 "}\n" +
527 "static __inline__ __attribute__ ((always_inline)) int alwaysinlinefunc(int a)\n" +
528 "{\n" +
529 " return a + inlineBar();\n" +
530 "}\n" +
531 "static __inline__ void inlinebar(unsigned int)\n" +
532 "{\n" +
533 " int a = alwaysinlinefunc()\n" +
534 "}\n"
535
536 expected =
537 [
538 "uint32 foo(unsigned int)",
539 "uint32 bar(unsigned int)",
540 "void inlineBar(void)",
541 "int alwaysinlinefunc(int a)",
542 "void inlinebar(unsigned int)"
543 ]
544
545 @parser.treat_inlines = :include
546 @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__', '\binline\b']
547 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
548 end
549
550 it "remove defines" do
551 source =
552 "#define whatever you feel like defining\n" +
553 "void hello(void);\n" +
554 "#DEFINE I JUST DON'T CARE\n" +
555 "#deFINE\n" +
556 "#define get_foo() \\\n ((Thing)foo.bar)" # exercise multiline define
557
558 expected =
559 [
560 "void hello(void)",
561 ]
562
563 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
564 end
565
566
567 it "remove keywords that would keep things from going smoothly in the future" do
568 source =
569 "const int TheMatrix(register int Trinity, unsigned int *restrict Neo)"
570
571 expected =
572 [
573 "const int TheMatrix(int Trinity, unsigned int * Neo)",
574 ]
575
576 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
577 end
578
579
580 # some code actually typedef's void even though it's not ANSI C and is, frankly, weird
581 # since cmock treats void specially, we can't let void be obfuscated
582 it "handle odd case of typedef'd void returned" do
583 source = "MY_FUNKY_VOID FunkyVoidReturned(int a)"
584 expected = { :var_arg=>nil,
585 :name=>"FunkyVoidReturned",
586 :unscoped_name=>"FunkyVoidReturned",
587 :namespace=>[],
588 :class=>nil,
589 :return=>{ :type => "void",
590 :name => 'cmock_to_return',
591 :ptr? => false,
592 :const? => false,
593 :const_ptr? => false,
594 :str => "void cmock_to_return",
595 :void? => true
596 },
597 :modifier=>"",
598 :contains_ptr? => false,
599 :args=>[{:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false}],
600 :args_string=>"int a",
601 :args_call=>"a"}
602 assert_equal(expected, @parser.parse_declaration(@test_project, source))
603 end
604
605 it "handle odd case of typedef'd void as arg" do
606 source = "int FunkyVoidAsArg(MY_FUNKY_VOID)"
607 expected = { :var_arg=>nil,
608 :name=>"FunkyVoidAsArg",
609 :unscoped_name=>"FunkyVoidAsArg",
610 :namespace=>[],
611 :class=>nil,
612 :return=>{ :type => "int",
613 :name => 'cmock_to_return',
614 :ptr? => false,
615 :const? => false,
616 :const_ptr? => false,
617 :str => "int cmock_to_return",
618 :void? => false
619 },
620 :modifier=>"",
621 :contains_ptr? => false,
622 :args=>[],
623 :args_string=>"void",
624 :args_call=>"" }
625 assert_equal(expected, @parser.parse_declaration(@test_project, source))
626 end
627
628 it "handle odd case of typedef'd void as arg pointer" do
629 source = "char FunkyVoidPointer(MY_FUNKY_VOID* bluh)"
630 expected = { :var_arg=>nil,
631 :name=>"FunkyVoidPointer",
632 :unscoped_name=>"FunkyVoidPointer",
633 :namespace=>[],
634 :class=>nil,
635 :return=>{ :type => "char",
636 :name => 'cmock_to_return',
637 :ptr? => false,
638 :const? => false,
639 :const_ptr? => false,
640 :str => "char cmock_to_return",
641 :void? => false
642 },
643 :modifier=>"",
644 :contains_ptr? => true,
645 :args=>[{:type=>"MY_FUNKY_VOID*", :name=>"bluh", :ptr? => true, :const? => false, :const_ptr? => false}],
646 :args_string=>"MY_FUNKY_VOID* bluh",
647 :args_call=>"bluh" }
648 assert_equal(expected, @parser.parse_declaration(@test_project, source))
649 end
650
651
652 it "strip default values from function parameter lists" do
653 source =
654 "void Foo(int a = 57, float b=37.52, char c= 'd', char* e=\"junk\");\n"
655
656 expected =
657 [
658 "void Foo(int a, float b, char c, char* e)"
659 ]
660
661 assert_equal(expected, @parser.import_source(source, @test_project).map!{|s|s.strip})
662 end
663
664
665 it "raise upon empty file" do
666 source = ''
667
668 # ensure it's expected type of exception
669 assert_raises RuntimeError do
670 @parser.parse("module", source)
671 end
672
673 assert_nil(@parser.funcs)
674
675 # verify exception message
676 begin
677 @parser.parse("module", source)
678 rescue RuntimeError => e
679 assert_equal("ERROR: No function prototypes found!", e.message)
680 end
681 end
682
683 it "clean up module names that contain spaces, dashes, and such" do
684 source = 'void meh(int (*func)(int));'
685
686 retval = @parser.parse("C:\Ugly Module-Name", source)
687 assert (retval[:typedefs][0] =~ /CUglyModuleName/)
688 end
689
690 it "raise upon no function prototypes found in file" do
691 source =
692 "typedef void SILLY_VOID_TYPE1;\n" +
693 "typedef (void) SILLY_VOID_TYPE2 ;\n" +
694 "typedef ( void ) (*FUNCPTR)(void);\n\n" +
695 "#define get_foo() \\\n ((Thing)foo.bar)"
696
697 # ensure it's expected type of exception
698 assert_raises(RuntimeError) do
699 @parser.parse("module", source)
700 end
701
702 assert_nil(@parser.funcs)
703
704 # verify exception message
705 begin
706 @parser.parse("module", source)
707 rescue RuntimeError => e
708 assert_equal("ERROR: No function prototypes found!", e.message)
709 end
710 end
711
712
713 it "raise upon prototype parsing failure" do
714 source = "void (int, )"
715
716 # ensure it's expected type of exception
717 assert_raises(RuntimeError) do
718 @parser.parse("module", source)
719 end
720
721 # verify exception message
722 begin
723 @parser.parse("module", source)
724 rescue RuntimeError => e
725 assert(e.message.include?("Failed Parsing Declaration Prototype!"))
726 end
727 end
728
729 it "extract and return function declarations with retval and args" do
730
731 source = "int Foo(int a, unsigned int b)"
732 expected = { :var_arg=>nil,
733 :name=>"Foo",
734 :unscoped_name=>"Foo",
735 :namespace=>[],
736 :class=>nil,
737 :return=>{ :type => "int",
738 :name => 'cmock_to_return',
739 :ptr? => false,
740 :const? => false,
741 :const_ptr? => false,
742 :str => "int cmock_to_return",
743 :void? => false
744 },
745 :modifier=>"",
746 :contains_ptr? => false,
747 :args=>[ {:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false},
748 {:type=>"unsigned int", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false}
749 ],
750 :args_string=>"int a, unsigned int b",
751 :args_call=>"a, b" }
752 assert_equal(expected, @parser.parse_declaration(@test_project, source))
753 end
754
755 it "extract and return function declarations with no retval" do
756
757 source = "void FunkyChicken( uint la, int de, bool da)"
758 expected = { :var_arg=>nil,
759 :return=>{ :type => "void",
760 :name => 'cmock_to_return',
761 :ptr? => false,
762 :const? => false,
763 :const_ptr? => false,
764 :str => "void cmock_to_return",
765 :void? => true
766 },
767 :name=>"FunkyChicken",
768 :unscoped_name=>"FunkyChicken",
769 :namespace=>[],
770 :class=>nil,
771 :modifier=>"",
772 :contains_ptr? => false,
773 :args=>[ {:type=>"uint", :name=>"la", :ptr? => false, :const? => false, :const_ptr? => false},
774 {:type=>"int", :name=>"de", :ptr? => false, :const? => false, :const_ptr? => false},
775 {:type=>"bool", :name=>"da", :ptr? => false, :const? => false, :const_ptr? => false}
776 ],
777 :args_string=>"uint la, int de, bool da",
778 :args_call=>"la, de, da" }
779 assert_equal(expected, @parser.parse_declaration(@test_project, source))
780 end
781
782 it "extract and return function declarations with implied voids" do
783
784 source = "void tat()"
785 expected = { :var_arg=>nil,
786 :return=>{ :type => "void",
787 :name => 'cmock_to_return',
788 :ptr? => false,
789 :const? => false,
790 :const_ptr? => false,
791 :str => "void cmock_to_return",
792 :void? => true
793 },
794 :name=>"tat",
795 :unscoped_name=>"tat",
796 :namespace=>[],
797 :class=>nil,
798 :modifier=>"",
799 :contains_ptr? => false,
800 :args=>[ ],
801 :args_string=>"void",
802 :args_call=>"" }
803 assert_equal(expected, @parser.parse_declaration(@test_project, source))
804 end
805
806 it "extract modifiers properly" do
807
808 source = "const int TheMatrix(int Trinity, unsigned int * Neo)"
809 expected = { :var_arg=>nil,
810 :return=>{ :type => "int",
811 :name => 'cmock_to_return',
812 :ptr? => false,
813 :const? => true,
814 :const_ptr? => false,
815 :str => "int cmock_to_return",
816 :void? => false
817 },
818 :name=>"TheMatrix",
819 :unscoped_name=>"TheMatrix",
820 :namespace=>[],
821 :class=>nil,
822 :modifier=>"const",
823 :contains_ptr? => true,
824 :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false},
825 {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false}
826 ],
827 :args_string=>"int Trinity, unsigned int* Neo",
828 :args_call=>"Trinity, Neo" }
829 assert_equal(expected, @parser.parse_declaration(@test_project, source))
830 end
831
832 it "extract c calling conventions properly" do
833
834 source = "const int __stdcall TheMatrix(int Trinity, unsigned int * Neo)"
835 expected = { :var_arg=>nil,
836 :return=>{ :type => "int",
837 :name => 'cmock_to_return',
838 :ptr? => false,
839 :const? => true,
840 :const_ptr? => false,
841 :str => "int cmock_to_return",
842 :void? => false
843 },
844 :name=>"TheMatrix",
845 :unscoped_name=>"TheMatrix",
846 :namespace=>[],
847 :class=>nil,
848 :modifier=>"const",
849 :c_calling_convention=>"__stdcall",
850 :contains_ptr? => true,
851 :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false},
852 {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false}
853 ],
854 :args_string=>"int Trinity, unsigned int* Neo",
855 :args_call=>"Trinity, Neo" }
856 assert_equal(expected, @parser.parse_declaration(@test_project, source))
857 end
858
859 it "extract and return function declarations inside namespace and class" do
860 source = "int Foo(int a, unsigned int b)"
861 expected = { :var_arg=>nil,
862 :name=>"ns1_ns2_Bar_Foo",
863 :unscoped_name=>"Foo",
864 :class=>"Bar",
865 :namespace=>["ns1", "ns2"],
866 :return=>{ :type => "int",
867 :name => 'cmock_to_return',
868 :ptr? => false,
869 :const? => false,
870 :const_ptr? => false,
871 :str => "int cmock_to_return",
872 :void? => false
873 },
874 :modifier=>"",
875 :contains_ptr? => false,
876 :args=>[ {:type=>"int", :name=>"a", :ptr? => false, :const? => false, :const_ptr? => false},
877 {:type=>"unsigned int", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false}
878 ],
879 :args_string=>"int a, unsigned int b",
880 :args_call=>"a, b" }
881 assert_equal(expected, @parser.parse_declaration(@test_project, source, ["ns1", "ns2"], "Bar"))
882 end
883
884 it "fully parse multiple prototypes" do
885
886 source = "const int TheMatrix(int Trinity, unsigned int * Neo);\n" +
887 "int Morpheus(int, unsigned int*);\n"
888
889 expected = [{ :var_arg=>nil,
890 :return=> { :type => "int",
891 :name => 'cmock_to_return',
892 :ptr? => false,
893 :const? => true,
894 :const_ptr? => false,
895 :str => "int cmock_to_return",
896 :void? => false
897 },
898 :name=>"TheMatrix",
899 :unscoped_name=>"TheMatrix",
900 :namespace=>[],
901 :class=>nil,
902 :modifier=>"const",
903 :contains_ptr? => true,
904 :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false},
905 {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false}
906 ],
907 :args_string=>"int Trinity, unsigned int* Neo",
908 :args_call=>"Trinity, Neo" },
909 { :var_arg=>nil,
910 :return=> { :type => "int",
911 :name => 'cmock_to_return',
912 :ptr? => false,
913 :const? => false,
914 :const_ptr? => false,
915 :str => "int cmock_to_return",
916 :void? => false
917 },
918 :name=>"Morpheus",
919 :unscoped_name=>"Morpheus",
920 :namespace=>[],
921 :class=>nil,
922 :modifier=>"",
923 :contains_ptr? => true,
924 :args=>[ {:type=>"int", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false},
925 {:type=>"unsigned int*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false}
926 ],
927 :args_string=>"int cmock_arg1, unsigned int* cmock_arg2",
928 :args_call=>"cmock_arg1, cmock_arg2"
929 }]
930 assert_equal(expected, @parser.parse("module", source)[:functions])
931 end
932
933 it "not extract for mocking multiply defined prototypes" do
934
935 source = "const int TheMatrix(int Trinity, unsigned int * Neo);\n" +
936 "const int TheMatrix(int, unsigned int*);\n"
937
938 expected = [{ :var_arg=>nil,
939 :name=>"TheMatrix",
940 :unscoped_name=>"TheMatrix",
941 :namespace=>[],
942 :class=>nil,
943 :return=> { :type => "int",
944 :name => 'cmock_to_return',
945 :ptr? => false,
946 :const? => true,
947 :const_ptr? => false,
948 :str => "int cmock_to_return",
949 :void? => false
950 },
951 :modifier=>"const",
952 :contains_ptr? => true,
953 :args=>[ {:type=>"int", :name=>"Trinity", :ptr? => false, :const? => false, :const_ptr? => false},
954 {:type=>"unsigned int*", :name=>"Neo", :ptr? => true, :const? => false, :const_ptr? => false}
955 ],
956 :args_string=>"int Trinity, unsigned int* Neo",
957 :args_call=>"Trinity, Neo"
958 }]
959 assert_equal(expected, @parser.parse("module", source)[:functions])
960 end
961
962 it "should properly handle const before return type" do
963 sources = [
964 "const int * PorkRoast(void);\n",
965 "const int* PorkRoast(void);\n",
966 "const int *PorkRoast(void);\n"
967 ]
968
969 expected = [{ :var_arg => nil,
970 :name => "PorkRoast",
971 :unscoped_name => "PorkRoast",
972 :namespace=>[],
973 :class=>nil,
974 :return => { :type => "const int*",
975 :name => 'cmock_to_return',
976 :ptr? => true,
977 :const? => true,
978 :const_ptr? => false,
979 :str => "const int* cmock_to_return",
980 :void? => false
981 },
982 :modifier => "",
983 :contains_ptr? => false,
984 :args => [],
985 :args_string => "void",
986 :args_call => ""
987 }]
988
989 sources.each do |source|
990 assert_equal(expected, @parser.parse("module", source)[:functions])
991 end
992 end
993
994 it "should properly handle const before return type" do
995 sources = [
996 "int const * PorkRoast(void);\n",
997 "int const* PorkRoast(void);\n",
998 "int const *PorkRoast(void);\n"
999 ]
1000
1001 expected = [{ :var_arg => nil,
1002 :name => "PorkRoast",
1003 :unscoped_name => "PorkRoast",
1004 :namespace=>[],
1005 :class=>nil,
1006 :return => { :type => "int const*",
1007 :name => 'cmock_to_return',
1008 :ptr? => true,
1009 :const? => true,
1010 :const_ptr? => false,
1011 :str => "int const* cmock_to_return",
1012 :void? => false
1013 },
1014 :modifier => "",
1015 :contains_ptr? => false,
1016 :args => [],
1017 :args_string => "void",
1018 :args_call => ""
1019 }]
1020
1021 sources.each do |source|
1022 assert_equal(expected, @parser.parse("module", source)[:functions])
1023 end
1024 end
1025
1026 it "should properly handle const applied after asterisk in return type (not legal C, but sometimes used)" do
1027
1028 source = "int * const PorkRoast(void);\n"
1029
1030 expected = [{ :var_arg=>nil,
1031 :name=>"PorkRoast",
1032 :unscoped_name=>"PorkRoast",
1033 :namespace=>[],
1034 :class=>nil,
1035 :return=> { :type => "int*",
1036 :name => 'cmock_to_return',
1037 :ptr? => true,
1038 :const? => false,
1039 :const_ptr? => true,
1040 :str => "int* cmock_to_return",
1041 :void? => false
1042 },
1043 :modifier=>"const",
1044 :contains_ptr? => false,
1045 :args=>[],
1046 :args_string=>"void",
1047 :args_call=>""
1048 }]
1049 assert_equal(expected, @parser.parse("module", source)[:functions])
1050 end
1051
1052 it "properly parse const and pointer argument types with no arg names" do
1053
1054 source = "void foo(int const*, int*const, const int*, const int*const, int const*const, int*, int, const int);\n"
1055
1056 expected = [{ :name => "foo",
1057 :unscoped_name => "foo",
1058 :namespace=>[],
1059 :class=>nil,
1060 :modifier => "",
1061 :return => { :type => "void",
1062 :name => "cmock_to_return",
1063 :str => "void cmock_to_return",
1064 :void? => true,
1065 :ptr? => false,
1066 :const? => false,
1067 :const_ptr? => false
1068 },
1069 :var_arg => nil,
1070 :args_string => "int const* cmock_arg1, int* const cmock_arg2, const int* cmock_arg3, const int* const cmock_arg4, " +
1071 "int const* const cmock_arg5, int* cmock_arg6, int cmock_arg7, const int cmock_arg8",
1072 :args => [{ :type=>"int const*", :name => "cmock_arg1", :ptr? => true, :const? => true, :const_ptr? => false },
1073 { :type=>"int*", :name => "cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => true },
1074 { :type=>"const int*", :name => "cmock_arg3", :ptr? => true, :const? => true, :const_ptr? => false },
1075 { :type=>"const int*", :name => "cmock_arg4", :ptr? => true, :const? => true, :const_ptr? => true },
1076 { :type=>"int const*", :name => "cmock_arg5", :ptr? => true, :const? => true, :const_ptr? => true },
1077 { :type=>"int*", :name => "cmock_arg6", :ptr? => true, :const? => false, :const_ptr? => false },
1078 { :type=>"int", :name => "cmock_arg7", :ptr? => false, :const? => false, :const_ptr? => false },
1079 { :type=>"int", :name => "cmock_arg8", :ptr? => false, :const? => true, :const_ptr? => false }],
1080 :args_call => "cmock_arg1, cmock_arg2, cmock_arg3, cmock_arg4, cmock_arg5, cmock_arg6, cmock_arg7, cmock_arg8",
1081 :contains_ptr? => true
1082 }]
1083 assert_equal(expected, @parser.parse("module", source)[:functions])
1084 end
1085
1086 it "properly parse const and pointer argument types with arg names" do
1087
1088 source = "void bar(int const* param1, int*const param2, const int* param3, const int*const param4,\n" +
1089 " int const*const param5, int*param6, int param7, const int param8);\n"
1090
1091 expected = [{ :name => "bar",
1092 :unscoped_name => "bar",
1093 :namespace=>[],
1094 :class=>nil,
1095 :modifier => "",
1096 :return => { :type => "void",
1097 :name => "cmock_to_return",
1098 :str => "void cmock_to_return",
1099 :void? => true,
1100 :ptr? => false,
1101 :const? => false,
1102 :const_ptr? => false
1103 },
1104 :var_arg => nil,
1105 :args_string => "int const* param1, int* const param2, const int* param3, const int* const param4, " +
1106 "int const* const param5, int* param6, int param7, const int param8",
1107 :args => [{ :type=>"int const*", :name => "param1", :ptr? => true, :const? => true, :const_ptr? => false },
1108 { :type=>"int*", :name => "param2", :ptr? => true, :const? => false, :const_ptr? => true },
1109 { :type=>"const int*", :name => "param3", :ptr? => true, :const? => true, :const_ptr? => false },
1110 { :type=>"const int*", :name => "param4", :ptr? => true, :const? => true, :const_ptr? => true },
1111 { :type=>"int const*", :name => "param5", :ptr? => true, :const? => true, :const_ptr? => true },
1112 { :type=>"int*", :name => "param6", :ptr? => true, :const? => false, :const_ptr? => false },
1113 { :type=>"int", :name => "param7", :ptr? => false, :const? => false, :const_ptr? => false },
1114 { :type=>"int", :name => "param8", :ptr? => false, :const? => true, :const_ptr? => false }],
1115 :args_call => "param1, param2, param3, param4, param5, param6, param7, param8",
1116 :contains_ptr? => true
1117 }].freeze
1118 assert_equal(expected, @parser.parse("module", source)[:functions])
1119 end
1120
1121 it "converts typedef'd array arguments to pointers" do
1122
1123 source = "Book AddToBook(Book book, const IntArray values);\n"
1124
1125 expected = [{ :name => "AddToBook",
1126 :unscoped_name => "AddToBook",
1127 :namespace=>[],
1128 :class=>nil,
1129 :modifier=>"",
1130 :return => { :type => "Book",
1131 :name => "cmock_to_return",
1132 :str => "Book cmock_to_return",
1133 :void? => false,
1134 :ptr? => false,
1135 :const? => false,
1136 :const_ptr? => false
1137 },
1138 :var_arg => nil,
1139 :args => [{ :type => "Page*", :name => "book", :ptr? => true, :const? => false, :const_ptr? => false },
1140 { :type => "const int*", :name => "values", :ptr? => true, :const? => true, :const_ptr? => false }],
1141 :args_string => "Book book, const IntArray values",
1142 :args_call => "book, values",
1143 :contains_ptr? => true
1144 }]
1145
1146 assert_equal(expected, @parser.parse("module", source)[:functions])
1147
1148 end
1149
1150 it "properly detect typedef'd variants of void and use those" do
1151
1152 source = "typedef (void) FUNKY_VOID_T;\n" +
1153 "typedef void CHUNKY_VOID_T;\n" +
1154 "FUNKY_VOID_T DrHorrible(int SingAlong);\n" +
1155 "int CaptainHammer(CHUNKY_VOID_T);\n"
1156
1157 expected = [{ :var_arg=>nil,
1158 :name=>"DrHorrible",
1159 :unscoped_name=>"DrHorrible",
1160 :namespace=>[],
1161 :class=>nil,
1162 :return => { :type => "void",
1163 :name => 'cmock_to_return',
1164 :ptr? => false,
1165 :const? => false,
1166 :const_ptr? => false,
1167 :str => "void cmock_to_return",
1168 :void? => true
1169 },
1170 :modifier=>"",
1171 :contains_ptr? => false,
1172 :args=>[ {:type=>"int", :name=>"SingAlong", :ptr? => false, :const? => false, :const_ptr? => false} ],
1173 :args_string=>"int SingAlong",
1174 :args_call=>"SingAlong"
1175 },
1176 { :var_arg=>nil,
1177 :return=> { :type => "int",
1178 :name => 'cmock_to_return',
1179 :ptr? => false,
1180 :const? => false,
1181 :const_ptr? => false,
1182 :str => "int cmock_to_return",
1183 :void? => false
1184 },
1185 :name=>"CaptainHammer",
1186 :unscoped_name=>"CaptainHammer",
1187 :namespace=>[],
1188 :class=>nil,
1189 :modifier=>"",
1190 :contains_ptr? => false,
1191 :args=>[ ],
1192 :args_string=>"void",
1193 :args_call=>""
1194 }]
1195 assert_equal(expected, @parser.parse("module", source)[:functions])
1196 end
1197
1198 it "be ok with structs inside of function declarations" do
1199
1200 source = "int DrHorrible(struct SingAlong Blog);\n" +
1201 "void Penny(struct const _KeepYourHeadUp_ * const BillyBuddy);\n" +
1202 "struct TheseArentTheHammer CaptainHammer(void);\n"
1203
1204 expected = [{ :var_arg=>nil,
1205 :return =>{ :type => "int",
1206 :name => 'cmock_to_return',
1207 :ptr? => false,
1208 :const? => false,
1209 :const_ptr? => false,
1210 :str => "int cmock_to_return",
1211 :void? => false
1212 },
1213 :name=>"DrHorrible",
1214 :unscoped_name=>"DrHorrible",
1215 :namespace=>[],
1216 :class=>nil,
1217 :modifier=>"",
1218 :contains_ptr? => false,
1219 :args=>[ {:type=>"struct SingAlong", :name=>"Blog", :ptr? => false, :const? => false, :const_ptr? => false} ],
1220 :args_string=>"struct SingAlong Blog",
1221 :args_call=>"Blog"
1222 },
1223 { :var_arg=>nil,
1224 :return=> { :type => "void",
1225 :name => 'cmock_to_return',
1226 :ptr? => false,
1227 :const? => false,
1228 :const_ptr? => false,
1229 :str => "void cmock_to_return",
1230 :void? => true
1231 },
1232 :name=>"Penny",
1233 :unscoped_name=>"Penny",
1234 :namespace=>[],
1235 :class=>nil,
1236 :modifier=>"",
1237 :contains_ptr? => true,
1238 :args=>[ {:type=>"struct const _KeepYourHeadUp_*", :name=>"BillyBuddy", :ptr? => true, :const? => true, :const_ptr? => true} ],
1239 :args_string=>"struct const _KeepYourHeadUp_* const BillyBuddy",
1240 :args_call=>"BillyBuddy"
1241 },
1242 { :var_arg=>nil,
1243 :return=> { :type => "struct TheseArentTheHammer",
1244 :name => 'cmock_to_return',
1245 :ptr? => false,
1246 :const? => false,
1247 :const_ptr? => false,
1248 :str => "struct TheseArentTheHammer cmock_to_return",
1249 :void? => false
1250 },
1251 :name=>"CaptainHammer",
1252 :unscoped_name=>"CaptainHammer",
1253 :namespace=>[],
1254 :class=>nil,
1255 :modifier=>"",
1256 :contains_ptr? => false,
1257 :args=>[ ],
1258 :args_string=>"void",
1259 :args_call=>""
1260 }]
1261 assert_equal(expected, @parser.parse("module", source)[:functions])
1262 end
1263
1264 it "extract functions containing unions with union specifier" do
1265 source = "void OrangePeel(union STARS_AND_STRIPES * a, union AFL_CIO b)"
1266 expected = [{ :var_arg=>nil,
1267 :return=>{ :type => "void",
1268 :name => 'cmock_to_return',
1269 :ptr? => false,
1270 :const? => false,
1271 :const_ptr? => false,
1272 :str => "void cmock_to_return",
1273 :void? => true
1274 },
1275 :name=>"OrangePeel",
1276 :unscoped_name=>"OrangePeel",
1277 :namespace=>[],
1278 :class=>nil,
1279 :modifier=>"",
1280 :contains_ptr? => true,
1281 :args=>[ {:type=>"union STARS_AND_STRIPES*", :name=>"a", :ptr? => true, :const? => false, :const_ptr? => false},
1282 {:type=>"union AFL_CIO", :name=>"b", :ptr? => false, :const? => false, :const_ptr? => false}
1283 ],
1284 :args_string=>"union STARS_AND_STRIPES* a, union AFL_CIO b",
1285 :args_call=>"a, b" }]
1286 result = @parser.parse("module", source)
1287 assert_equal(expected, result[:functions])
1288 end
1289
1290 it "not be thwarted by variables named with primitive types as part of the name" do
1291 source = "void ApplePeel(const unsigned int const_param, int int_param, int integer, char character, int* const constant)"
1292 expected = [{ :var_arg=>nil,
1293 :return=>{ :type => "void",
1294 :name => 'cmock_to_return',
1295 :ptr? => false,
1296 :const? => false,
1297 :const_ptr? => false,
1298 :str => "void cmock_to_return",
1299 :void? => true
1300 },
1301 :name=>"ApplePeel",
1302 :unscoped_name=>"ApplePeel",
1303 :namespace=>[],
1304 :class=>nil,
1305 :modifier=>"",
1306 :contains_ptr? => true,
1307 :args=>[ {:type=> "unsigned int", :name=>"const_param", :ptr? => false, :const? => true, :const_ptr? => false},
1308 {:type=>"int", :name=>"int_param", :ptr? => false, :const? => false, :const_ptr? => false},
1309 {:type=>"int", :name=>"integer", :ptr? => false, :const? => false, :const_ptr? => false},
1310 {:type=>"char", :name=>"character", :ptr? => false, :const? => false, :const_ptr? => false},
1311 {:type=>"int*", :name=>"constant", :ptr? => true, :const? => false, :const_ptr? => true}
1312 ],
1313 :args_string=>"const unsigned int const_param, int int_param, int integer, char character, int* const constant",
1314 :args_call=>"const_param, int_param, integer, character, constant" }]
1315 result = @parser.parse("module", source)
1316 assert_equal(expected, result[:functions])
1317 end
1318
1319 it "not be thwarted by custom types named similarly to primitive types" do
1320 source = "void LemonPeel(integer param, character thing, longint * junk, constant value, int32_t const number)"
1321 expected = [{:var_arg=>nil,
1322 :return=>{ :type => "void",
1323 :name => 'cmock_to_return',
1324 :ptr? => false,
1325 :const? => false,
1326 :const_ptr? => false,
1327 :str => "void cmock_to_return",
1328 :void? => true
1329 },
1330 :name=>"LemonPeel",
1331 :unscoped_name=>"LemonPeel",
1332 :namespace=>[],
1333 :class=>nil,
1334 :modifier=>"",
1335 :contains_ptr? => true,
1336 :args=>[ {:type=>"integer", :name=>"param", :ptr? => false, :const? => false, :const_ptr? => false},
1337 {:type=>"character", :name=>"thing", :ptr? => false, :const? => false, :const_ptr? => false},
1338 {:type=>"longint*", :name=>"junk", :ptr? => true, :const? => false, :const_ptr? => false},
1339 {:type=>"constant", :name=>"value", :ptr? => false, :const? => false, :const_ptr? => false},
1340 {:type=>"int32_t", :name=>"number", :ptr? => false, :const? => true, :const_ptr? => false}
1341 ],
1342 :args_string=>"integer param, character thing, longint* junk, constant value, int32_t const number",
1343 :args_call=>"param, thing, junk, value, number" }]
1344 result = @parser.parse("module", source)
1345 assert_equal(expected, result[:functions])
1346 end
1347
1348 it "handle some of those chains of C name specifiers naturally" do
1349 source = "void CoinOperated(signed char abc, const unsigned long int xyz_123, unsigned int const abc_123, long long arm_of_the_law)"
1350 expected = [{:var_arg=>nil,
1351 :return=>{ :type => "void",
1352 :name => 'cmock_to_return',
1353 :ptr? => false,
1354 :const? => false,
1355 :const_ptr? => false,
1356 :str => "void cmock_to_return",
1357 :void? => true
1358 },
1359 :name=>"CoinOperated",
1360 :unscoped_name=>"CoinOperated",
1361 :namespace=>[],
1362 :class=>nil,
1363 :modifier=>"",
1364 :contains_ptr? => false,
1365 :args=>[ {:type=>"signed char", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false},
1366 {:type=>"unsigned long int", :name=>"xyz_123", :ptr? => false, :const? => true, :const_ptr? => false},
1367 {:type=>"unsigned int", :name=>"abc_123", :ptr? => false, :const? => true, :const_ptr? => false},
1368 {:type=>"long long", :name=>"arm_of_the_law", :ptr? => false, :const? => false, :const_ptr? => false}
1369 ],
1370 :args_string=>"signed char abc, const unsigned long int xyz_123, unsigned int const abc_123, long long arm_of_the_law",
1371 :args_call=>"abc, xyz_123, abc_123, arm_of_the_law" }]
1372 result = @parser.parse("module", source)
1373 assert_equal(expected, result[:functions])
1374 end
1375
1376 it "handle custom types of various formats" do
1377 source = "void CardOperated(CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const * const abc123)"
1378 expected = [{:var_arg=>nil,
1379 :return=>{ :type => "void",
1380 :name => 'cmock_to_return',
1381 :ptr? => false,
1382 :const? => false,
1383 :const_ptr? => false,
1384 :str => "void cmock_to_return",
1385 :void? => true
1386 },
1387 :name=>"CardOperated",
1388 :unscoped_name=>"CardOperated",
1389 :namespace=>[],
1390 :class=>nil,
1391 :modifier=>"",
1392 :contains_ptr? => true,
1393 :args=>[ {:type=>"CUSTOM_TYPE", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false},
1394 {:type=>"CUSTOM_TYPE*", :name=>"xyz_123", :ptr? => true, :const? => false, :const_ptr? => false},
1395 {:type=>"CUSTOM_TYPE", :name=>"abcxyz", :ptr? => false, :const? => true, :const_ptr? => false},
1396 {:type=>"struct CUSTOM_TYPE const*", :name=>"abc123", :ptr? => true, :const? => true, :const_ptr? => true}
1397 ],
1398 :args_string=>"CUSTOM_TYPE abc, CUSTOM_TYPE* xyz_123, CUSTOM_TYPE const abcxyz, struct CUSTOM_TYPE const* const abc123",
1399 :args_call=>"abc, xyz_123, abcxyz, abc123" }]
1400 result = @parser.parse("module", source)
1401 assert_equal(expected, result[:functions])
1402 end
1403
1404 it "handle arrays and treat them as pointers or strings" do
1405 source = 'void KeyOperated(CUSTOM_TYPE thing1[], int thing2 [ ], ' \
1406 'char thing3 [][2 ][ 3], int* thing4[4], u8 thing5[((u8)((5 + 5*2)/3))])'
1407 expected_args = [
1408 { type: 'CUSTOM_TYPE*', name: 'thing1', ptr?: true, const?: false, const_ptr?: false },
1409 { type: 'int*', name: 'thing2', ptr?: true, const?: false, const_ptr?: false },
1410 # this one will likely change in the future when we improve multidimensional array support
1411 { type: 'char*', name: 'thing3', ptr?: false, const?: false, const_ptr?: false },
1412 # this one will likely change in the future when we improve multidimensional array support
1413 { type: 'int**', name: 'thing4', ptr?: true, const?: false, const_ptr?: false },
1414 { type: 'u8*', name: 'thing5', ptr?: true, const?: false, const_ptr?: false }
1415 ]
1416 expected = [{:var_arg=>nil,
1417 :return=>{ :type => "void",
1418 :name => 'cmock_to_return',
1419 :ptr? => false,
1420 :const? => false,
1421 :const_ptr? => false,
1422 :str => "void cmock_to_return",
1423 :void? => true
1424 },
1425 :name=>"KeyOperated",
1426 :unscoped_name=>"KeyOperated",
1427 :namespace=>[],
1428 :class=>nil,
1429 :modifier=>"",
1430 :contains_ptr? => true,
1431 :args => expected_args,
1432 :args_string => 'CUSTOM_TYPE* thing1, int* thing2, ' \
1433 'char* thing3, int** thing4, u8* thing5',
1434 :args_call => 'thing1, thing2, thing3, thing4, thing5' }]
1435 result = @parser.parse("module", source)
1436 assert_equal(expected, result[:functions])
1437 end
1438
1439 it "give a reasonable guess when dealing with weird combinations of custom types and modifiers" do
1440 source = "void Cheese(unsigned CUSTOM_TYPE abc, unsigned xyz, CUSTOM_TYPE1 CUSTOM_TYPE2 pdq)"
1441 expected = [{:var_arg=>nil,
1442 :return=>{ :type => "void",
1443 :name => 'cmock_to_return',
1444 :ptr? => false,
1445 :const? => false,
1446 :const_ptr? => false,
1447 :str => "void cmock_to_return",
1448 :void? => true
1449 },
1450 :name=>"Cheese",
1451 :unscoped_name=>"Cheese",
1452 :namespace=>[],
1453 :class=>nil,
1454 :modifier=>"",
1455 :contains_ptr? => false,
1456 :args=>[ {:type=>"unsigned CUSTOM_TYPE", :name=>"abc", :ptr? => false, :const? => false, :const_ptr? => false},
1457 {:type=>"unsigned", :name=>"xyz", :ptr? => false, :const? => false, :const_ptr? => false},
1458 {:type=>"CUSTOM_TYPE1 CUSTOM_TYPE2", :name=>"pdq", :ptr? => false, :const? => false, :const_ptr? => false}
1459 ],
1460 :args_string=>"unsigned CUSTOM_TYPE abc, unsigned xyz, CUSTOM_TYPE1 CUSTOM_TYPE2 pdq",
1461 :args_call=>"abc, xyz, pdq" }]
1462 result = @parser.parse("module", source)
1463 assert_equal(expected, result[:functions])
1464 end
1465
1466 it "extract functions containing a function pointer" do
1467 source = "void FunkyTurkey(unsigned int (*func_ptr)(int, char))"
1468 expected = [{ :var_arg=>nil,
1469 :return=>{ :type => "void",
1470 :name => 'cmock_to_return',
1471 :ptr? => false,
1472 :const? => false,
1473 :const_ptr? => false,
1474 :str => "void cmock_to_return",
1475 :void? => true
1476 },
1477 :name=>"FunkyTurkey",
1478 :unscoped_name=>"FunkyTurkey",
1479 :namespace=>[],
1480 :class=>nil,
1481 :modifier=>"",
1482 :contains_ptr? => false,
1483 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false}
1484 ],
1485 :args_string=>"cmock_module_func_ptr1 func_ptr",
1486 :args_call=>"func_ptr" }]
1487 typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"]
1488 result = @parser.parse("module", source)
1489 assert_equal(expected, result[:functions])
1490 assert_equal(typedefs, result[:typedefs])
1491 end
1492
1493 it "extract functions using a function pointer with shorthand notation" do
1494 source = "void FunkyTurkey(unsigned int func_ptr(int, char))"
1495 expected = [{ :var_arg=>nil,
1496 :return=>{ :type => "void",
1497 :name => 'cmock_to_return',
1498 :ptr? => false,
1499 :const? => false,
1500 :const_ptr? => false,
1501 :str => "void cmock_to_return",
1502 :void? => true
1503 },
1504 :name=>"FunkyTurkey",
1505 :unscoped_name=>"FunkyTurkey",
1506 :namespace=>[],
1507 :class=>nil,
1508 :modifier=>"",
1509 :contains_ptr? => false,
1510 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false}
1511 ],
1512 :args_string=>"cmock_module_func_ptr1 func_ptr",
1513 :args_call=>"func_ptr" }]
1514 typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"]
1515 result = @parser.parse("module", source)
1516 assert_equal(expected, result[:functions])
1517 assert_equal(typedefs, result[:typedefs])
1518 end
1519
1520 it "extract functions containing a function pointer with a void" do
1521 source = "void FunkyTurkey(void (*func_ptr)(void))"
1522 expected = [{ :var_arg=>nil,
1523 :return=>{ :type => "void",
1524 :name => 'cmock_to_return',
1525 :ptr? => false,
1526 :const? => false,
1527 :const_ptr? => false,
1528 :str => "void cmock_to_return",
1529 :void? => true
1530 },
1531 :name=>"FunkyTurkey",
1532 :unscoped_name=>"FunkyTurkey",
1533 :namespace=>[],
1534 :class=>nil,
1535 :modifier=>"",
1536 :contains_ptr? => false,
1537 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false}
1538 ],
1539 :args_string=>"cmock_module_func_ptr1 func_ptr",
1540 :args_call=>"func_ptr" }]
1541 typedefs = ["typedef void(*cmock_module_func_ptr1)(void);"]
1542 result = @parser.parse("module", source)
1543 assert_equal(expected, result[:functions])
1544 assert_equal(typedefs, result[:typedefs])
1545 end
1546
1547 it "extract functions containing a function pointer with an implied void" do
1548 source = "void FunkyTurkey(unsigned int (*func_ptr)())"
1549 expected = [{ :var_arg=>nil,
1550 :return=>{ :type => "void",
1551 :name => 'cmock_to_return',
1552 :ptr? => false,
1553 :const? => false,
1554 :const_ptr? => false,
1555 :str => "void cmock_to_return",
1556 :void? => true
1557 },
1558 :name=>"FunkyTurkey",
1559 :unscoped_name=>"FunkyTurkey",
1560 :namespace=>[],
1561 :class=>nil,
1562 :modifier=>"",
1563 :contains_ptr? => false,
1564 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false}
1565 ],
1566 :args_string=>"cmock_module_func_ptr1 func_ptr",
1567 :args_call=>"func_ptr" }]
1568 typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)();"]
1569 result = @parser.parse("module", source)
1570 assert_equal(expected, result[:functions])
1571 assert_equal(typedefs, result[:typedefs])
1572 end
1573
1574 it "extract functions containing a constant function pointer and a pointer in the nested arg list" do
1575 source = "void FunkyChicken(unsigned int (* const func_ptr)(unsigned long int * , char))"
1576 expected = [{ :var_arg=>nil,
1577 :return=>{ :type => "void",
1578 :name => 'cmock_to_return',
1579 :ptr? => false,
1580 :const? => false,
1581 :const_ptr? => false,
1582 :str => "void cmock_to_return",
1583 :void? => true
1584 },
1585 :name=>"FunkyChicken",
1586 :unscoped_name=>"FunkyChicken",
1587 :namespace=>[],
1588 :class=>nil,
1589 :modifier=>"",
1590 :contains_ptr? => false,
1591 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => true, :const_ptr? => false}
1592 ],
1593 :args_string=>"cmock_module_func_ptr1 const func_ptr",
1594 :args_call=>"func_ptr" }]
1595 typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(unsigned long int* , char);"]
1596 result = @parser.parse("module", source)
1597 assert_equal(expected, result[:functions])
1598 assert_equal(typedefs, result[:typedefs])
1599 end
1600
1601 # it "extract functions containing a function pointer taking a vararg" do
1602 # source = "void FunkyParrot(unsigned int (*func_ptr)(int, char, ...))"
1603 # expected = [{ :var_arg=>nil,
1604 # :return=>{ :type => "void",
1605 # :name => 'cmock_to_return',
1606 # :ptr? => false,
1607 # :const? => false,
1608 # :const_ptr? => false,
1609 # :str => "void cmock_to_return",
1610 # :void? => true
1611 # },
1612 # :name=>"FunkyParrot",
1613 # :unscoped_name=>"FunkyParrot",
1614 # :namespace=>[],
1615 # :class=>nil,
1616 # :modifier=>"",
1617 # :contains_ptr? => false,
1618 # :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr", :ptr? => false, :const? => false, :const_ptr? => false}
1619 # ],
1620 # :args_string=>"cmock_module_func_ptr1 func_ptr",
1621 # :args_call=>"func_ptr" }]
1622 # typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char, ...);"]
1623 # result = @parser.parse("module", source)
1624 # assert_equal(expected, result[:functions])
1625 # assert_equal(typedefs, result[:typedefs])
1626 # end
1627
1628 it "extract functions containing a function pointer with extra parenthesis and two sets" do
1629 source = "void FunkyBudgie(int (((* func_ptr1)(int, char))), void (*func_ptr2)(void))"
1630 expected = [{ :var_arg=>nil,
1631 :return=>{ :type => "void",
1632 :name => 'cmock_to_return',
1633 :ptr? => false,
1634 :const? => false,
1635 :const_ptr? => false,
1636 :str => "void cmock_to_return",
1637 :void? => true
1638 },
1639 :name=>"FunkyBudgie",
1640 :unscoped_name=>"FunkyBudgie",
1641 :namespace=>[],
1642 :class=>nil,
1643 :modifier=>"",
1644 :contains_ptr? => false,
1645 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"func_ptr1", :ptr? => false, :const? => false, :const_ptr? => false},
1646 {:type=>"cmock_module_func_ptr2", :name=>"func_ptr2", :ptr? => false, :const? => false, :const_ptr? => false}
1647 ],
1648 :args_string=>"cmock_module_func_ptr1 func_ptr1, cmock_module_func_ptr2 func_ptr2",
1649 :args_call=>"func_ptr1, func_ptr2" }]
1650 typedefs = ["typedef int(*cmock_module_func_ptr1)(int, char);", "typedef void(*cmock_module_func_ptr2)(void);"]
1651 result = @parser.parse("module", source)
1652 assert_equal(expected, result[:functions])
1653 assert_equal(typedefs, result[:typedefs])
1654 end
1655
1656 it "extract functions containing a function pointers, structs and other things" do
1657 source = "struct mytype *FunkyRobin(uint16_t num1, uint16_t num2, void (*func_ptr1)(uint16_t num3, struct mytype2 *s));"
1658 expected = [{ :var_arg=>nil,
1659 :return=>{ :type => "struct mytype*",
1660 :name => 'cmock_to_return',
1661 :ptr? => true,
1662 :const? => false,
1663 :const_ptr? => false,
1664 :str => "struct mytype* cmock_to_return",
1665 :void? => false
1666 },
1667 :name=>"FunkyRobin",
1668 :unscoped_name=>"FunkyRobin",
1669 :namespace=>[],
1670 :class=>nil,
1671 :modifier=>"",
1672 :contains_ptr? => false,
1673 :args=>[ {:type=>"uint16_t", :name=>"num1", :ptr? => false, :const? => false, :const_ptr? => false},
1674 {:type=>"uint16_t", :name=>"num2", :ptr? => false, :const? => false, :const_ptr? => false},
1675 {:type=>"cmock_module_func_ptr1", :name=>"func_ptr1", :ptr? => false, :const? => false, :const_ptr? => false}
1676 ],
1677 :args_string=>"uint16_t num1, uint16_t num2, cmock_module_func_ptr1 func_ptr1",
1678 :args_call=>"num1, num2, func_ptr1" }]
1679 typedefs = ["typedef void(*cmock_module_func_ptr1)(uint16_t num3, struct mytype2* s);"]
1680 result = @parser.parse("module", source)
1681 assert_equal(expected, result[:functions])
1682 assert_equal(typedefs, result[:typedefs])
1683 end
1684
1685 it "extract functions containing an anonymous function pointer" do
1686 source = "void FunkyFowl(unsigned int (* const)(int, char))"
1687 expected = [{ :var_arg=>nil,
1688 :return=>{ :type => "void",
1689 :name => 'cmock_to_return',
1690 :ptr? => false,
1691 :const? => false,
1692 :const_ptr? => false,
1693 :str => "void cmock_to_return",
1694 :void? => true
1695 },
1696 :name=>"FunkyFowl",
1697 :unscoped_name=>"FunkyFowl",
1698 :namespace=>[],
1699 :class=>nil,
1700 :modifier=>"",
1701 :contains_ptr? => false,
1702 :args=>[ {:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => true, :const_ptr? => false}
1703 ],
1704 :args_string=>"cmock_module_func_ptr1 const cmock_arg1",
1705 :args_call=>"cmock_arg1" }]
1706 typedefs = ["typedef unsigned int(*cmock_module_func_ptr1)(int, char);"]
1707 result = @parser.parse("module", source)
1708 assert_equal(expected, result[:functions])
1709 assert_equal(typedefs, result[:typedefs])
1710 end
1711
1712 it "extract functions returning a function pointer" do
1713 source = "unsigned short (*FunkyPidgeon( const char op_code ))( int, long int )"
1714 expected = [{ :var_arg=>nil,
1715 :return=>{ :type => "cmock_module_func_ptr1",
1716 :name => 'cmock_to_return',
1717 :ptr? => false,
1718 :const? => false,
1719 :const_ptr? => false,
1720 :str => "cmock_module_func_ptr1 cmock_to_return",
1721 :void? => false
1722 },
1723 :name=>"FunkyPidgeon",
1724 :unscoped_name=>"FunkyPidgeon",
1725 :namespace=>[],
1726 :class=>nil,
1727 :modifier=>"",
1728 :contains_ptr? => false,
1729 :args=>[ {:type=>"char", :name=>"op_code", :ptr? => false, :const? => true, :const_ptr? => false}
1730 ],
1731 :args_string=>"const char op_code",
1732 :args_call=>"op_code" }]
1733 typedefs = ["typedef unsigned short(*cmock_module_func_ptr1)( int, long int );"]
1734 result = @parser.parse("module", source)
1735 assert_equal(expected, result[:functions])
1736 assert_equal(typedefs, result[:typedefs])
1737 end
1738
1739 it "extract functions returning a function pointer with implied void" do
1740 source = "unsigned short (*FunkyTweetie())()"
1741 expected = [{ :var_arg=>nil,
1742 :return=>{ :type => "cmock_module_func_ptr1",
1743 :name => 'cmock_to_return',
1744 :ptr? => false,
1745 :const? => false,
1746 :const_ptr? => false,
1747 :str => "cmock_module_func_ptr1 cmock_to_return",
1748 :void? => false
1749 },
1750 :name=>"FunkyTweetie",
1751 :unscoped_name=>"FunkyTweetie",
1752 :namespace=>[],
1753 :class=>nil,
1754 :modifier=>"",
1755 :contains_ptr? => false,
1756 :args=>[],
1757 :args_string=>"void",
1758 :args_call=>"" }]
1759 typedefs = ["typedef unsigned short(*cmock_module_func_ptr1)();"]
1760 result = @parser.parse("module", source)
1761 assert_equal(expected, result[:functions])
1762 assert_equal(typedefs, result[:typedefs])
1763 end
1764
1765 it "extract functions returning a function pointer where everything is a void" do
1766 source = "void (* FunkySeaGull(void))(void)"
1767 expected = [{ :var_arg=>nil,
1768 :return=>{ :type => "cmock_module_func_ptr1",
1769 :name => 'cmock_to_return',
1770 :ptr? => false,
1771 :const? => false,
1772 :const_ptr? => false,
1773 :str => "cmock_module_func_ptr1 cmock_to_return",
1774 :void? => false
1775 },
1776 :name=>"FunkySeaGull",
1777 :unscoped_name=>"FunkySeaGull",
1778 :namespace=>[],
1779 :class=>nil,
1780 :modifier=>"",
1781 :contains_ptr? => false,
1782 :args=>[],
1783 :args_string=>"void",
1784 :args_call=>"" }]
1785 typedefs = ["typedef void(*cmock_module_func_ptr1)(void);"]
1786 result = @parser.parse("module", source)
1787 assert_equal(expected, result[:functions])
1788 assert_equal(typedefs, result[:typedefs])
1789 end
1790
1791 it "extract functions returning a function pointer with some pointer nonsense" do
1792 source = "unsigned int * (* FunkyMacaw(double* foo, THING *bar))(unsigned int)"
1793 expected = [{ :var_arg=>nil,
1794 :return=>{ :type => "cmock_module_func_ptr1",
1795 :name => 'cmock_to_return',
1796 :ptr? => false,
1797 :const? => false,
1798 :const_ptr? => false,
1799 :str => "cmock_module_func_ptr1 cmock_to_return",
1800 :void? => false
1801 },
1802 :name=>"FunkyMacaw",
1803 :unscoped_name=>"FunkyMacaw",
1804 :namespace=>[],
1805 :class=>nil,
1806 :modifier=>"",
1807 :contains_ptr? => true,
1808 :args=>[ {:type=>"double*", :name=>"foo", :ptr? => true, :const? => false, :const_ptr? => false},
1809 {:type=>"THING*", :name=>"bar", :ptr? => true, :const? => false, :const_ptr? => false}
1810 ],
1811 :args_string=>"double* foo, THING* bar",
1812 :args_call=>"foo, bar" }]
1813 typedefs = ["typedef unsigned int *(*cmock_module_func_ptr1)(unsigned int);"]
1814 result = @parser.parse("module", source)
1815 assert_equal(expected, result[:functions])
1816 assert_equal(typedefs, result[:typedefs])
1817 end
1818
1819 it "extract this SQLite3 function with an anonymous function pointer arg (regression test)" do
1820 source = "SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*))"
1821 expected = [{ :var_arg=>nil,
1822 :return=>{ :type => "int",
1823 :name => "cmock_to_return",
1824 :ptr? => false,
1825 :const? => false,
1826 :const_ptr? => false,
1827 :str => "int cmock_to_return",
1828 :void? => false
1829 },
1830 :name=>"sqlite3_bind_text",
1831 :unscoped_name=>"sqlite3_bind_text",
1832 :namespace=>[],
1833 :class=>nil,
1834 :modifier=>"SQLITE_API",
1835 :contains_ptr? => true,
1836 :args=>[ {:type=>"sqlite3_stmt*", :name=>"cmock_arg2", :ptr? => true, :const? => false, :const_ptr? => false},
1837 {:type=>"int", :name=>"cmock_arg3", :ptr? => false, :const? => false, :const_ptr? => false},
1838 {:type=>"const char*", :name=>"cmock_arg4", :ptr? => false, :const? => true, :const_ptr? => false},
1839 {:type=>"int", :name=>"n", :ptr? => false, :const? => false, :const_ptr? => false},
1840 {:type=>"cmock_module_func_ptr1", :name=>"cmock_arg1", :ptr? => false, :const? => false, :const_ptr? => false}
1841 ],
1842 :args_string=>"sqlite3_stmt* cmock_arg2, int cmock_arg3, const char* cmock_arg4, int n, cmock_module_func_ptr1 cmock_arg1",
1843 :args_call=>"cmock_arg2, cmock_arg3, cmock_arg4, n, cmock_arg1" }]
1844 typedefs = ["typedef void(*cmock_module_func_ptr1)(void*);"]
1845 result = @parser.parse("module", source)
1846 assert_equal(expected, result[:functions])
1847 assert_equal(typedefs, result[:typedefs])
1848 end
1849
1850 it "extract functions with varargs" do
1851 source = "int XFiles(int Scully, int Mulder, ...);\n"
1852 expected = [{ :var_arg=>"...",
1853 :return=> { :type => "int",
1854 :name => 'cmock_to_return',
1855 :ptr? => false,
1856 :const? => false,
1857 :const_ptr? => false,
1858 :str => "int cmock_to_return",
1859 :void? => false
1860 },
1861 :name=>"XFiles",
1862 :unscoped_name=>"XFiles",
1863 :namespace=>[],
1864 :class=>nil,
1865 :modifier=>"",
1866 :contains_ptr? => false,
1867 :args=>[ {:type=>"int", :name=>"Scully", :ptr? => false, :const? => false, :const_ptr? => false},
1868 {:type=>"int", :name=>"Mulder", :ptr? => false, :const? => false, :const_ptr? => false}
1869 ],
1870 :args_string=>"int Scully, int Mulder",
1871 :args_call=>"Scully, Mulder"
1872 }]
1873 assert_equal(expected, @parser.parse("module", source)[:functions])
1874 end
1875
1876 it "extract functions with void pointers" do
1877 source = "void* MoreSillySongs(void* stuff);\n"
1878 expected = [{ :var_arg=>nil,
1879 :return=> { :type => "void*",
1880 :name => 'cmock_to_return',
1881 :ptr? => true,
1882 :const? => false,
1883 :const_ptr? => false,
1884 :str => "void* cmock_to_return",
1885 :void? => false
1886 },
1887 :name=>"MoreSillySongs",
1888 :unscoped_name=>"MoreSillySongs",
1889 :namespace=>[],
1890 :class=>nil,
1891 :modifier=>"",
1892 :contains_ptr? => true,
1893 :args=>[ {:type=>"void*", :name=>"stuff", :ptr? => true, :const? => false, :const_ptr? => false}
1894 ],
1895 :args_string=>"void* stuff",
1896 :args_call=>"stuff"
1897 }]
1898 assert_equal(expected, @parser.parse("module", source)[:functions])
1899 end
1900
1901 it "extract functions with strippable confusing junk like gcc attributes" do
1902 source = "int LaverneAndShirley(int Lenny, int Squiggy) __attribute__((weak)) __attribute__ ((deprecated));\n"
1903 expected = [{ :var_arg=>nil,
1904 :return=> { :type => "int",
1905 :name => 'cmock_to_return',
1906 :ptr? => false,
1907 :const? => false,
1908 :const_ptr? => false,
1909 :str => "int cmock_to_return",
1910 :void? => false
1911 },
1912 :name=>"LaverneAndShirley",
1913 :unscoped_name=>"LaverneAndShirley",
1914 :namespace=>[],
1915 :class=>nil,
1916 :modifier=>"",
1917 :contains_ptr? => false,
1918 :args=>[ {:type=>"int", :name=>"Lenny", :ptr? => false, :const? => false, :const_ptr? => false},
1919 {:type=>"int", :name=>"Squiggy", :ptr? => false, :const? => false, :const_ptr? => false}
1920 ],
1921 :args_string=>"int Lenny, int Squiggy",
1922 :args_call=>"Lenny, Squiggy"
1923 }]
1924 assert_equal(expected, @parser.parse("module", source)[:functions])
1925 end
1926
1927 it "extract functions with strippable confusing junk like gcc attributes with parenthesis" do
1928 source = "int TheCosbyShow(int Cliff, int Claire) __attribute__((weak, alias (\"__f\"));\n"
1929 expected = [{ :var_arg=>nil,
1930 :return=> { :type => "int",
1931 :name => 'cmock_to_return',
1932 :ptr? => false,
1933 :const? => false,
1934 :const_ptr? => false,
1935 :str => "int cmock_to_return",
1936 :void? => false
1937 },
1938 :name=>"TheCosbyShow",
1939 :unscoped_name=>"TheCosbyShow",
1940 :namespace=>[],
1941 :class=>nil,
1942 :modifier=>"",
1943 :contains_ptr? => false,
1944 :args=>[ {:type=>"int", :name=>"Cliff", :ptr? => false, :const? => false, :const_ptr? => false},
1945 {:type=>"int", :name=>"Claire", :ptr? => false, :const? => false, :const_ptr? => false}
1946 ],
1947 :args_string=>"int Cliff, int Claire",
1948 :args_call=>"Cliff, Claire"
1949 }]
1950 assert_equal(expected, @parser.parse("module", source)[:functions])
1951 end
1952
1953 it "divines all permutations of ptr, const, and const_ptr correctly" do
1954 truth_table = [
1955 # argument ptr const const_ptr
1956 [ "constNOTconst constNOTconst", false, false, false ],
1957 [ "const constNOTconst constNOTconst", false, true, false ],
1958 [ "constNOTconst const constNOTconst", false, true, false ],
1959 [ "constNOTconst *constNOTconst", true, false, false ],
1960 [ "const constNOTconst *constNOTconst", true, true, false ],
1961 [ "constNOTconst const *constNOTconst", true, true, false ],
1962 [ "constNOTconst *const constNOTconst", true, false, true ],
1963 [ "const constNOTconst *const constNOTconst", true, true, true ],
1964 [ "constNOTconst const *const constNOTconst", true, true, true ],
1965 [ "constNOTconst **constNOTconst", true, false, false ],
1966 [ "const constNOTconst **constNOTconst", true, false, false ],
1967 [ "constNOTconst const **constNOTconst", true, false, false ],
1968 [ "constNOTconst *const *constNOTconst", true, true, false ],
1969 [ "const constNOTconst *const *constNOTconst", true, true, false ],
1970 [ "constNOTconst const *const *constNOTconst", true, true, false ],
1971 [ "constNOTconst **const constNOTconst", true, false, true ],
1972 [ "const constNOTconst **const constNOTconst", true, false, true ],
1973 [ "constNOTconst const **const constNOTconst", true, false, true ],
1974 [ "constNOTconst *const *const constNOTconst", true, true, true ],
1975 [ "const constNOTconst *const *const constNOTconst", true, true, true ],
1976 [ "constNOTconst const *const *const constNOTconst", true, true, true ]
1977 ]
1978
1979 truth_table.each do |entry|
1980 assert_equal(@parser.divine_ptr(entry[0]), entry[1])
1981 assert_equal(@parser.divine_const(entry[0]), entry[2])
1982 assert_equal(@parser.divine_ptr_and_const(entry[0]),
1983 { ptr?: entry[1], const?: entry[2], const_ptr?: entry[3] })
1984 end
1985 end
1986
1987 it "divines ptr correctly for string types" do
1988 truth_table = [
1989 # argument ptr
1990 [ "char s", false ],
1991 [ "const char s", false ],
1992 [ "char const s", false ],
1993 [ "char *s", false ],
1994 [ "const char *s", false ],
1995 [ "char const *s", false ],
1996 [ "char *const s", false ],
1997 [ "const char *const s", false ],
1998 [ "char const *const s", false ],
1999 [ "char **s", true ],
2000 [ "const char **s", true ],
2001 [ "char const **s", true ],
2002 [ "char *const *s", true ],
2003 [ "const char *const *s", true ],
2004 [ "char const *const *s", true ],
2005 [ "char **const s", true ],
2006 [ "const char **const s", true ],
2007 [ "char const **const s", true ],
2008 [ "char *const *const s", true ],
2009 [ "const char *const *const s", true ],
2010 [ "char const *const *const s", true ]
2011 ]
2012
2013 truth_table.each do |entry|
2014 assert_equal(@parser.divine_ptr(entry[0]), entry[1])
2015 end
2016 end
2017
2018 it "Transform inline functions only removes comments from a header with no inlines" do
2019 # We remove the comments from the header, this is to protect the parser from wrongfully recognizing inline functions
2020 source =
2021 "#ifndef _NOINCLUDES\n" +
2022 "#define _NOINCLUDES\n" +
2023 "#include \"unity.h\"\n" +
2024 "#include \"cmock.h\"\n" +
2025 "#include \"YetAnotherHeader.h\"\n" +
2026 "\n" +
2027 "/* Ignore the following warnings since we are copying code */\n" +
2028 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2029 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2030 "#pragma GCC diagnostic push\n" +
2031 "#endif\n" +
2032 "#if !defined(__clang__)\n" +
2033 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2034 "#endif\n" +
2035 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2036 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2037 "#endif\n" +
2038 "\n" +
2039 "struct my_struct {\n" +
2040 "int a;\n" +
2041 "int b;\n" +
2042 "int b;\n" +
2043 "char c;\n" +
2044 "};\n" +
2045 "int my_function(int a);\n" +
2046 "int my_better_function(struct my_struct *s);\n" +
2047 "\n" +
2048 "#endif _NOINCLUDES\n"
2049
2050 expected =
2051 "#ifndef _NOINCLUDES\n" +
2052 "#define _NOINCLUDES\n" +
2053 "#include \"unity.h\"\n" +
2054 "#include \"cmock.h\"\n" +
2055 "#include \"YetAnotherHeader.h\"\n" +
2056 "\n" +
2057 "\n" + # Removed comment
2058 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2059 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2060 "#pragma GCC diagnostic push\n" +
2061 "#endif\n" +
2062 "#if !defined(__clang__)\n" +
2063 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2064 "#endif\n" +
2065 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2066 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2067 "#endif\n" +
2068 "\n" +
2069 "struct my_struct {\n" +
2070 "int a;\n" +
2071 "int b;\n" +
2072 "int b;\n" +
2073 "char c;\n" +
2074 "};\n" +
2075 "int my_function(int a);\n" +
2076 "int my_better_function(struct my_struct *s);\n" +
2077 "\n" +
2078 "#endif _NOINCLUDES\n"
2079
2080 assert_equal(expected, @parser.transform_inline_functions(source))
2081 end
2082
2083 it "Transform inline functions changes inline functions to function declarations" do
2084 source =
2085 "#ifndef _NOINCLUDES\n" +
2086 "#define _NOINCLUDES\n" +
2087 "#include \"unity.h\"\n" +
2088 "#include \"cmock.h\"\n" +
2089 "#include \"YetAnotherHeader.h\"\n" +
2090 "\n" +
2091 "/* Ignore the following warnings since we are copying code */\n" +
2092 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2093 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2094 "#pragma GCC diagnostic push\n" +
2095 "#endif\n" +
2096 "#if !defined(__clang__)\n" +
2097 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2098 "#endif\n" +
2099 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2100 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2101 "#endif\n" +
2102 "\n" +
2103 "struct my_struct {\n" +
2104 "int a;\n" +
2105 "int b;\n" +
2106 "int b;\n" +
2107 "char c;\n" +
2108 "};\n" +
2109 "int my_function(int a);\n" +
2110 "int my_better_function(struct my_struct *s);\n" +
2111 "static inline int staticinlinebar(struct my_struct *s)\n" + # This is a function with a lot of indentation levels, we should be able to handle it
2112 "{\n" +
2113 "\t{\n" +
2114 "\t\t{\n" +
2115 "\t\t\treturn s->a;\n" +
2116 "\t\t}\n" +
2117 "\t}\n" +
2118 "}\n" +
2119 "static inline int staticinlinefunc(struct my_struct *s)\n" +
2120 "{\n" +
2121 " return s->a;\n" +
2122 "}\n" +
2123 "int bar(struct my_struct *s);\n" + # A normal function to screw with our parser
2124 "inline static int inlinestaticfunc(int a) {\n" +
2125 " return a + 42;\n" +
2126 "}\n" +
2127 "inline int StaticInlineFunc(struct my_struct *s)\n" +
2128 "{\n" +
2129 " return get_member_a(s) + 42;\n" +
2130 "}\n" +
2131 "int inline StaticInlineBar(struct my_struct *s)\n" +
2132 "{\n" +
2133 " return get_member_a(s) + 42;\n" +
2134 "}\n" +
2135 "struct staticinlinestruct {\n" + # Add a structure declaration between the inline functions, just to make sure we don't touch it!
2136 "int a;\n" +
2137 "};\n" +
2138 "\n" +
2139 "struct staticinlinestruct fubarstruct(struct my_struct *s);\n" + # Another normal function to screw with our parser
2140 "static inline struct staticinlinestruct inlinefubarfunction(struct my_struct *s)\n" +
2141 "{\n" +
2142 " return (struct staticinlinestruct)*s;\n" +
2143 "}\n" +
2144 "int fubar(struct my_struct *s);\n" + # Another normal function to screw with our parser
2145 "inline int stuff(int num)" +
2146 "{" +
2147 " int reg = 0x12;" +
2148 " if (num > 0)" +
2149 " {" +
2150 " reg |= (0x0Eu);" +
2151 " }" +
2152 " else" +
2153 " {" +
2154 " reg |= (0x07u);" +
2155 " }" +
2156 " return reg;" +
2157 "}" +
2158 "\n" +
2159 "int inline static dummy_func_2(int a, char b, float c) {" + # This is a sneaky one, inline static is placed AFTER the return value
2160 " c += 3.14;" +
2161 " b -= 32;" +
2162 " return a + (int)(b) + (int)c;" +
2163 "}" +
2164 "#endif _NOINCLUDES\n"
2165
2166 expected =
2167 "#ifndef _NOINCLUDES\n" +
2168 "#define _NOINCLUDES\n" +
2169 "#include \"unity.h\"\n" +
2170 "#include \"cmock.h\"\n" +
2171 "#include \"YetAnotherHeader.h\"\n" +
2172 "\n" +
2173 "\n" + # Removed comment
2174 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2175 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2176 "#pragma GCC diagnostic push\n" +
2177 "#endif\n" +
2178 "#if !defined(__clang__)\n" +
2179 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2180 "#endif\n" +
2181 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2182 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2183 "#endif\n" +
2184 "\n" +
2185 "struct my_struct {\n" +
2186 "int a;\n" +
2187 "int b;\n" +
2188 "int b;\n" +
2189 "char c;\n" +
2190 "};\n" +
2191 "int my_function(int a);\n" +
2192 "int my_better_function(struct my_struct *s);\n" +
2193 "int staticinlinebar(struct my_struct *s);\n" +
2194 "int staticinlinefunc(struct my_struct *s);\n" +
2195 "int bar(struct my_struct *s);\n" +
2196 "int inlinestaticfunc(int a);\n" +
2197 "int StaticInlineFunc(struct my_struct *s);\n" +
2198 "int StaticInlineBar(struct my_struct *s);\n" +
2199 "struct staticinlinestruct {\n" +
2200 "int a;\n" +
2201 "};\n" +
2202 "\n" +
2203 "struct staticinlinestruct fubarstruct(struct my_struct *s);\n" +
2204 "struct staticinlinestruct inlinefubarfunction(struct my_struct *s);\n" +
2205 "int fubar(struct my_struct *s);\n" +
2206 "int stuff(int num);\n" +
2207 "int dummy_func_2(int a, char b, float c);" +
2208 "#endif _NOINCLUDES\n"
2209
2210 assert_equal(expected, @parser.transform_inline_functions(source))
2211 end
2212
2213 it "Transform inline functions leaves static variables" do
2214 source =
2215 "#ifndef _NOINCLUDES\n" +
2216 "#define _NOINCLUDES\n" +
2217 "#include \"unity.h\"\n" +
2218 "#include \"cmock.h\"\n" +
2219 "#include \"YetAnotherHeader.h\"\n" +
2220 "\n" +
2221 "/* Ignore the following warnings since we are copying code */\n" +
2222 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2223 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2224 "#pragma GCC diagnostic push\n" +
2225 "#endif\n" +
2226 "#if !defined(__clang__)\n" +
2227 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2228 "#endif\n" +
2229 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2230 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2231 "#endif\n" +
2232 "\n" +
2233 "int my_function(int a);\n" +
2234 "static inline int staticinlinefunc(struct my_struct *s)\n" +
2235 "{\n" +
2236 " return s->a;\n" +
2237 "}\n" +
2238 "static const int my_variable = 5;\n" +
2239 "struct my_struct {\n" +
2240 "int a;\n" +
2241 "int b;\n" +
2242 "int b;\n" +
2243 "char c;\n" +
2244 "};\n" +
2245 "#endif _NOINCLUDES\n"
2246
2247 expected =
2248 "#ifndef _NOINCLUDES\n" +
2249 "#define _NOINCLUDES\n" +
2250 "#include \"unity.h\"\n" +
2251 "#include \"cmock.h\"\n" +
2252 "#include \"YetAnotherHeader.h\"\n" +
2253 "\n" +
2254 "\n" + #The comments are now removed
2255 "#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
2256 "#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
2257 "#pragma GCC diagnostic push\n" +
2258 "#endif\n" +
2259 "#if !defined(__clang__)\n" +
2260 "#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
2261 "#endif\n" +
2262 "#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
2263 "#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
2264 "#endif\n" +
2265 "\n" +
2266 "int my_function(int a);\n" +
2267 "int staticinlinefunc(struct my_struct *s);\n" +
2268 "static const int my_variable = 5;\n" +
2269 "struct my_struct {\n" +
2270 "int a;\n" +
2271 "int b;\n" +
2272 "int b;\n" +
2273 "char c;\n" +
2274 "};\n" +
2275 "#endif _NOINCLUDES\n"
2276
2277 assert_equal(expected, @parser.transform_inline_functions(source))
2278 end
2279
2280 it "Count number of pairs of braces in function succesfully" do
2281 source =
2282 "int foo(struct my_struct *s)\n" +
2283 "{\n" +
2284 " return get_member_a(s) + 42;\n" +
2285 "}\n"
2286 complex_source =
2287 "int bar(struct my_struct *s)\n" +
2288 "{\n" +
2289 "\tint a = 6;\n" +
2290 "\tint res = foo(&(struct my_struct){.nr = a});\n" +
2291 "\t{\n" +
2292 "\t\tint a = 5;\n" +
2293 "\t\tint res = foo(&(struct my_struct){.nr = a});\n" +
2294 "\t\t{\n" +
2295 "\t\t\tstruct my_struct a = {.nr = 1};\n" +
2296 "\t\t}\n" +
2297 "\t}\n" +
2298 "\treturn 42;\n" +
2299 "}\n"
2300
2301 assert_equal(1, @parser.count_number_of_pairs_of_braces_in_function(source))
2302 assert_equal(6, @parser.count_number_of_pairs_of_braces_in_function(complex_source))
2303 end
2304
2305 it "Count number of pairs of braces returns 0 if bad source is supplied" do
2306 bad_source_0 =
2307 "int foo(struct my_struct *s)\n" +
2308 "{\n" +
2309 " return get_member_a(s) + 42;\n" +
2310 "\n" # Missing closing brace
2311 bad_source_1 =
2312 "int bar(struct my_struct *s)\n" +
2313 "{\n" +
2314 "\tint a = 6;\n" +
2315 "\tint res = foo(&(struct my_struct){.nr = a});\n" +
2316 "\t{\n" +
2317 "\t\tint a = 5;\n" +
2318 "\t\tint res = foo(&(struct my_struct){.nr = a});\n" +
2319 "\t\t{\n" +
2320 "\t\t\n" + # Missing closing brace
2321 "\t}\n" +
2322 "\treturn 42;\n" +
2323 "}\n"
2324 bad_source_2 =
2325 "int foo(struct my_struct *s)\n" +
2326 "\n" # No braces in source
2327
2328 assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_0))
2329 assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_1))
2330 assert_equal(0, @parser.count_number_of_pairs_of_braces_in_function(bad_source_2))
2331 end
2332
2333 it "handles parsing multiline functions" do
2334 source = "int\nLaverneAndShirley(int Lenny,\n int Squiggy);\n"
2335 expected = [{ :var_arg=>nil,
2336 :return=> { :type => "int",
2337 :name => 'cmock_to_return',
2338 :ptr? => false,
2339 :const? => false,
2340 :const_ptr? => false,
2341 :str => "int cmock_to_return",
2342 :void? => false
2343 },
2344 :name=>"LaverneAndShirley",
2345 :unscoped_name=>"LaverneAndShirley",
2346 :namespace=>[],
2347 :class=>nil,
2348 :modifier=>"",
2349 :contains_ptr? => false,
2350 :args=>[ {:type=>"int", :name=>"Lenny", :ptr? => false, :const? => false, :const_ptr? => false},
2351 {:type=>"int", :name=>"Squiggy", :ptr? => false, :const? => false, :const_ptr? => false}
2352 ],
2353 :args_string=>"int Lenny, int Squiggy",
2354 :args_call=>"Lenny, Squiggy"
2355 }]
2356 assert_equal(expected, @parser.parse("module", source)[:functions])
2357 end
2358
2359 it "imports C++ differently when asked" do
2360 source =
2361 [
2362 "namespace ns1 {\n",
2363 " namespace ns2 {\n",
2364 "\n",
2365 " class cls1 {\n",
2366 " public:\n",
2367 " int f_header_impl(int a, int b){\n",
2368 " return a + b;\n",
2369 " }\n",
2370 "\n",
2371 " static void f_void();\n",
2372 " static int f_ret_simple();\n",
2373 "\n",
2374 " protected:\n",
2375 " static void protected_f_void();\n",
2376 "\n",
2377 " public:\n",
2378 " private:\n",
2379 " static void private_f_void();\n",
2380 " }; // cls1\n",
2381 " } // ns2\n",
2382 "} // ns1\n"
2383 ].join
2384
2385 expected =
2386 [
2387 "namespace ns1 { namespace ns2 { class cls1 { public: int f_header_impl",
2388 "static void f_void()",
2389 "static int f_ret_simple()",
2390 "protected: static void protected_f_void()",
2391 "public: private: static void private_f_void()",
2392 "}",
2393 "} }"
2394 ]
2395
2396 assert_equal(expected, @parser.import_source(source, @test_project, cpp=true))
2397 refute_equal(expected, @parser.import_source(source, @test_project))
2398 end
2399
2400 # only so parse_functions does not raise an error
2401 def dummy_source
2402 "void dummy(void);"
2403 end
2404
2405 # Expected result of above
2406 def dummy_func
2407 { :name => "dummy",
2408 :unscoped_name => "dummy",
2409 :class => nil,
2410 :namespace => [],
2411 :var_arg => nil,
2412 :args_string => "void",
2413 :args => [],
2414 :args_call => "",
2415 :contains_ptr? => false,
2416 :modifier => "",
2417 :return => {
2418 :type => "void",
2419 :name => "cmock_to_return",
2420 :str => "void cmock_to_return",
2421 :void? => true,
2422 :ptr? => false,
2423 :const? => false,
2424 :const_ptr? => false}}
2425 end
2426
2427 # Commonly used example function
2428 def voidvoid_func(namespace=[], name="Classic_functional")
2429 { :name => name,
2430 :unscoped_name => "functional",
2431 :class => "Classic",
2432 :namespace => namespace,
2433 :var_arg => nil,
2434 :args_string => "void",
2435 :args => [],
2436 :args_call => "",
2437 :contains_ptr? => false,
2438 :modifier => "",
2439 :return => {
2440 :type=>"void",
2441 :name=>"cmock_to_return",
2442 :str=>"void cmock_to_return",
2443 :void? => true,
2444 :ptr? => false,
2445 :const? => false,
2446 :const_ptr? => false}}
2447 end
2448
2449 it "parses public C++ functions" do
2450 source = dummy_source + <<~SOURCE
2451 class Classic {
2452 public:
2453 static void functional(void);
2454 };
2455 SOURCE
2456
2457 expected = [dummy_func, voidvoid_func]
2458
2459 assert_equal(expected, @parser.parse("module", source)[:functions])
2460 end
2461
2462 it "handles a namespace" do
2463 source = dummy_source + <<~SOURCE
2464 namespace ns1 {
2465 class Classic {
2466 public:
2467 static void functional(void);
2468 };
2469 } // ns1
2470 SOURCE
2471
2472 expected = [dummy_func,
2473 voidvoid_func(namespace=["ns1"], name="ns1_Classic_functional")]
2474
2475 assert_equal(expected, @parser.parse("module", source)[:functions])
2476 end
2477
2478 it "handles nested namespaces" do
2479 source = dummy_source + <<~SOURCE
2480 namespace ns1 {
2481 namespace ns2 {
2482 class Classic {
2483 public:
2484 static void functional(void);
2485 };
2486 } // ns1
2487 } // ns1
2488 SOURCE
2489
2490 expected = [dummy_func,
2491 voidvoid_func(namespace=["ns1", "ns2"], name="ns1_ns2_Classic_functional")]
2492
2493 assert_equal(expected, @parser.parse("module", source)[:functions])
2494 end
2495
2496 it "ignores non-static C++ functions" do
2497 source = dummy_source + <<~SOURCE
2498 class Classic {
2499 public:
2500 void functional(void);
2501 };
2502 SOURCE
2503
2504 expected = [dummy_func]
2505
2506 assert_equal(expected, @parser.parse("module", source)[:functions])
2507 end
2508
2509 it "ignores private functions" do
2510 source = dummy_source + <<~SOURCE
2511 class Classic {
2512 private:
2513 static void functional(void);
2514 };
2515 SOURCE
2516
2517 expected = [dummy_func]
2518
2519 assert_equal(expected, @parser.parse("module", source)[:functions])
2520 end
2521
2522 it "parses public C++ functions after private functions" do
2523 source = dummy_source + <<~SOURCE
2524 class Classic {
2525 private:
2526 static void ignoreme(void);
2527 public:
2528 static void functional(void);
2529 };
2530 SOURCE
2531
2532 expected = [dummy_func, voidvoid_func]
2533
2534 assert_equal(expected, @parser.parse("module", source)[:functions])
2535 end
2536
2537 it "ignores protected functions" do
2538 source = dummy_source + <<~SOURCE
2539 class Classic {
2540 protected:
2541 static void functional(void);
2542 };
2543 SOURCE
2544
2545 expected = [dummy_func]
2546
2547 assert_equal(expected, @parser.parse("module", source)[:functions])
2548 end
2549
2550 it "parses public C++ functions after protected functions" do
2551 source = dummy_source + <<~SOURCE
2552 class Classic {
2553 protected:
2554 static void ignoreme(void);
2555 public:
2556 static void functional(void);
2557 };
2558 SOURCE
2559
2560 expected = [dummy_func, voidvoid_func]
2561
2562 assert_equal(expected, @parser.parse("module", source)[:functions])
2563 end
2564
2565 it "parses multiple classes in same file with uniquely named functions" do
2566 source = dummy_source + <<~SOURCE
2567 namespace ns1 {
2568 class Classic {
2569 public:
2570 static void functional(void);
2571 };
2572
2573 class Classical {
2574 public:
2575 static int functionality(int a);
2576 };
2577 } // ns1
2578
2579 class Classy {
2580 public:
2581 static int* func(int* a);
2582 };
2583 SOURCE
2584
2585 expected = [dummy_func,
2586 voidvoid_func(["ns1"], name="ns1_Classic_functional"),
2587 { :name => "ns1_Classical_functionality",
2588 :unscoped_name => "functionality",
2589 :class => "Classical",
2590 :namespace => ["ns1"],
2591 :var_arg => nil,
2592 :args_string => "int a",
2593 :args => [
2594 { :ptr? => false,
2595 :const? => false,
2596 :const_ptr? => false,
2597 :name => "a",
2598 :type => "int"}],
2599 :args_call => "a",
2600 :contains_ptr? => false,
2601 :modifier => "",
2602 :return => {
2603 :type=>"int",
2604 :name=>"cmock_to_return",
2605 :str=>"int cmock_to_return",
2606 :void? => false,
2607 :ptr? => false,
2608 :const? => false,
2609 :const_ptr? => false}},
2610 { :name => "Classy_func",
2611 :unscoped_name => "func",
2612 :class => "Classy",
2613 :namespace => [],
2614 :var_arg => nil,
2615 :args_string => "int* a",
2616 :args => [
2617 { :ptr? => true,
2618 :const? => false,
2619 :const_ptr? => false,
2620 :name => "a",
2621 :type => "int*"}],
2622 :args_call => "a",
2623 :contains_ptr? => true,
2624 :modifier => "",
2625 :return => {
2626 :type=>"int*",
2627 :name=>"cmock_to_return",
2628 :str=>"int* cmock_to_return",
2629 :void? => false,
2630 :ptr? => true,
2631 :const? => false,
2632 :const_ptr? => false}}]
2633
2634 assert_equal(expected, @parser.parse("module", source)[:functions])
2635 end
2636
2637 it "handles multiple classes in same file with identically named functions" do
2638 source = dummy_source + <<~SOURCE
2639 namespace ns1 {
2640 class Classic {
2641 public:
2642 static void functional(void);
2643 };
2644
2645 class Classical {
2646 public:
2647 static int functional(int a);
2648 };
2649 } // ns1
2650
2651 class Classy {
2652 public:
2653 static int* functional(int* a);
2654 };
2655 SOURCE
2656
2657 expected = [dummy_func,
2658 voidvoid_func(["ns1"], name="ns1_Classic_functional"),
2659 { :name => "ns1_Classical_functional",
2660 :unscoped_name => "functional",
2661 :class => "Classical",
2662 :namespace => ["ns1"],
2663 :var_arg => nil,
2664 :args_string => "int a",
2665 :args => [
2666 { :ptr? => false,
2667 :const? => false,
2668 :const_ptr? => false,
2669 :name => "a",
2670 :type => "int"}],
2671 :args_call => "a",
2672 :contains_ptr? => false,
2673 :modifier => "",
2674 :return => {
2675 :type=>"int",
2676 :name=>"cmock_to_return",
2677 :str=>"int cmock_to_return",
2678 :void? => false,
2679 :ptr? => false,
2680 :const? => false,
2681 :const_ptr? => false}},
2682 { :name => "Classy_functional",
2683 :unscoped_name => "functional",
2684 :class => "Classy",
2685 :namespace => [],
2686 :var_arg => nil,
2687 :args_string => "int* a",
2688 :args => [
2689 { :ptr? => true,
2690 :const? => false,
2691 :const_ptr? => false,
2692 :name => "a",
2693 :type => "int*"}],
2694 :args_call => "a",
2695 :contains_ptr? => true,
2696 :modifier => "",
2697 :return => {
2698 :type=>"int*",
2699 :name=>"cmock_to_return",
2700 :str=>"int* cmock_to_return",
2701 :void? => false,
2702 :ptr? => true,
2703 :const? => false,
2704 :const_ptr? => false}}]
2705
2706 assert_equal(expected, @parser.parse("module", source)[:functions])
2707 end
2708
2709 it "Transform inline functions can handle inline function declarations" do
2710 source =
2711 "static inline int dummy_func_decl(int a, char b, float c);\n" + # First declaration
2712 "static inline int dummy_func_decl2(int a, char b, float c)\n\n\n\n\n\n;\n" + # Second declaration with a lot of newlines before the semicolon to mess with the parser
2713 "static inline int staticinlinefunc(struct my_struct *s)\n" + # 'normal' inline pattern
2714 "{\n" +
2715 " return dummy_func_decl(1, 1, 1);\n" +
2716 "}\n" +
2717 "struct my_struct_with_inline_in_it\n" + # struct definition in between to mess with the parser
2718 "{\n" +
2719 " int a;\n" +
2720 " char b;\n" +
2721 " float inlineb;\n" +
2722 "};\n" +
2723 "static inline int dummy_func_decl(int a, char b, float c) {\n" + # Second user pattern
2724 " return 42;\n" +
2725 "}\n" +
2726 "\n"
2727
2728 expected =
2729 "int dummy_func_decl(int a, char b, float c);\n" +
2730 "int dummy_func_decl2(int a, char b, float c)\n\n\n\n\n\n;\n" + # Second declaration with a lot of newlines until the semicolon to mess with the parser
2731 "int staticinlinefunc(struct my_struct *s);\n" +
2732 "struct my_struct_with_inline_in_it\n" +
2733 "{\n" +
2734 " int a;\n" +
2735 " char b;\n" +
2736 " float inlineb;\n" +
2737 "};\n" +
2738 "int dummy_func_decl(int a, char b, float c);\n" +
2739 "\n"
2740
2741 @parser.treat_inlines = :include
2742 assert_equal(expected, @parser.transform_inline_functions(source))
2743 end
2744
2745 it "Transform inline functions can handle header with only inline function declarations" do
2746 source =
2747 "static inline int dummy_func_decl(int a, char b, float c);\n" +
2748 "\n"
2749
2750 expected =
2751 "int dummy_func_decl(int a, char b, float c);\n" +
2752 "\n"
2753
2754 @parser.treat_inlines = :include
2755 assert_equal(expected, @parser.transform_inline_functions(source))
2756 end
2757
2758 it "Transform inline functions using gnu attribute notation" do
2759 source =
2760 "static __inline__ __attribute__ ((always_inline)) uint16_t _somefunc (uint32_t a)\n" +
2761 "{\n" +
2762 " return _someotherfunc (a);\n" +
2763 "}\n" +
2764 "static __attribute__ (( always_inline )) uint16_t _somefunc_0 (uint32_t a)\n" +
2765 "{\n" +
2766 " return (uint16_t) a;\n" +
2767 "}\n" +
2768 "\n"
2769
2770 expected =
2771 "uint16_t _somefunc (uint32_t a);\n" +
2772 "uint16_t _somefunc_0 (uint32_t a);\n" +
2773 "\n"
2774
2775 @parser.treat_inlines = :include
2776 @parser.inline_function_patterns = ['(?:static\s*)?(?:__inline__)?__attribute__\s*\([ (]*always_inline[ )]*\)', 'static __inline__']
2777 assert_equal(expected, @parser.transform_inline_functions(source))
2778 end
2779
2780 it "Transform inline functions takes user provided patterns into account" do
2781 source =
2782 "static __inline__ __attribute__ ((always_inline)) uint16_t _somefunc (uint32_t a)\n" +
2783 "{\n" +
2784 " return _someotherfunc (a);\n" +
2785 "}\n" +
2786 "static __inline__ uint16_t _somefunc_0 (uint32_t a)\n" +
2787 "{\n" +
2788 " return (uint16_t) a;\n" +
2789 "}\n" +
2790 "\n"
2791
2792 expected =
2793 "uint16_t _somefunc (uint32_t a);\n" +
2794 "uint16_t _somefunc_0 (uint32_t a);\n" +
2795 "\n"
2796
2797 @parser.treat_inlines = :include
2798 @parser.inline_function_patterns = ['static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__']
2799 assert_equal(expected, @parser.transform_inline_functions(source))
2800 end
2801
2802 it "Transform inline functions limits deleting user macro to actual line/word" do
2803 source =
2804 "#if defined (FORCE_INLINE)\n" +
2805 "#define MY_LIBRARY_INLINE static __inline__ __attribute__ ((always_inline))\n" +
2806 "#else\n" +
2807 "#define MY_LIBRARY_INLINE\n" +
2808 "#endif\n" +
2809 "#define INLINE static __inline__ __attribute__ ((always_inline))\n" +
2810 "#define INLINE_TWO \\\nstatic\\\ninline\n" +
2811 "INLINE uint16_t _somefunc (uint32_t a)\n" +
2812 "{\n" +
2813 " return _someotherfunc (a);\n" +
2814 "}\n" +
2815 "static __inline__ uint16_t _somefunc_0 (uint32_t a)\n" +
2816 "{\n" +
2817 " return (uint16_t) a;\n" +
2818 "}\n" +
2819 "static __inline__ __attribute__ \(\(always_inline\)\) uint16_t _somefunc_1 (uint32_t a)\n" +
2820 "{\n" +
2821 " return (uint16_t) a;\n" +
2822 "}\n" +
2823 "INLINE_TWO uint16_t _somefunc_2(uint32_t a)\n" +
2824 "{\n" +
2825 " return (uint16_t) a;\n" +
2826 "}\n" +
2827 "#define INLINE_THREE \\\nstatic\\\ninline"
2828
2829 expected =
2830 "#if defined (FORCE_INLINE)\n" +
2831 "#else\n" +
2832 "#endif\n" +
2833 "uint16_t _somefunc (uint32_t a);\n" +
2834 "uint16_t _somefunc_0 (uint32_t a);\n" +
2835 "uint16_t _somefunc_1 (uint32_t a);\n" +
2836 "uint16_t _somefunc_2(uint32_t a);\n"
2837
2838 @parser.treat_inlines = :include
2839 @parser.inline_function_patterns = ['MY_LIBRARY_INLINE', 'INLINE_THREE', 'INLINE_TWO', 'INLINE', 'static __inline__ __attribute__ \(\(always_inline\)\)', 'static __inline__']
2840 assert_equal(expected, @parser.transform_inline_functions(source))
2841 end
2842
2843 it "Transform inline functions will ignore comments containing static" do
2844 source =
2845 "typedef struct {\n" +
2846 "const uint32_t var1;\n" +
2847 "const uint16_t var2; // comment with the word static in it\n" +
2848 "} struct1;\n" +
2849 "\n" +
2850 "typedef struct {\n" +
2851 "const uint32_t var1;\n" +
2852 "const uint16_t var2;\n" +
2853 "} struct2;\n"
2854
2855 expected =
2856 "typedef struct {\n" +
2857 "const uint32_t var1;\n" +
2858 "const uint16_t var2; \n" +
2859 "} struct1;\n" +
2860 "\n" +
2861 "typedef struct {\n" +
2862 "const uint32_t var1;\n" +
2863 "const uint16_t var2;\n" +
2864 "} struct2;\n"
2865
2866 @parser.treat_inlines = :include
2867 assert_equal(expected, @parser.transform_inline_functions(source))
2868 end
2869
2870
2871 end