Debian Codegraph @

test/unit/cmock_unityhelper_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 require File.expand_path(File.dirname(__FILE__)) + "/../test_helper"
8 require File.expand_path(File.dirname(__FILE__)) + '/../../lib/cmock_unityhelper_parser'
9
10 describe CMockUnityHelperParser, "Verify CMockUnityHelperParser Module" do
11
12 before do
13 create_mocks :config
14 end
15
16 after do
17 end
18
19 it "ignore lines that are commented out" do
20 source =
21 " abcd;\n" +
22 "// #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" +
23 "or maybe // #define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n\n"
24 @config.expect :plugins, [] #not :array
25 @config.expect :treat_as, {}
26 @config.expect :load_unity_helper, source
27 @parser = CMockUnityHelperParser.new(@config)
28 expected = {}
29
30 assert_equal(expected, @parser.c_types)
31 end
32
33 it "ignore stuff in block comments" do
34 source =
35 " abcd; /*\n" +
36 "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n" +
37 "#define UNITY_TEST_ASSERT_EQUAL_CHICKENS(a,b,line,msg) {...};\n */\n"
38 @config.expect :plugins, [] #not :array
39 @config.expect :treat_as, {}
40 @config.expect :load_unity_helper, source
41 @parser = CMockUnityHelperParser.new(@config)
42 expected = {}
43
44 assert_equal(expected, @parser.c_types)
45 end
46
47 it "notice equal helpers in the proper form and ignore others" do
48 source =
49 "abcd;\n" +
50 "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_T(a,b,line,msg) {...};\n" +
51 "abcd;\n" +
52 "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS(a,b,c,d,e) {...};\n" +
53 "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL(a,b,c,d) {...};\n" +
54 "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits(a,b,c,d) {...};\n" +
55 "abcd;\n"
56 @config.expect :plugins, [] #not :array
57 @config.expect :treat_as, {}
58 @config.expect :load_unity_helper, source
59 @parser = CMockUnityHelperParser.new(@config)
60 expected = {
61 'TURKEYS_T' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_T",
62 'unsigned_funky_rabbits' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits"
63 }
64
65 assert_equal(expected, @parser.c_types)
66 end
67
68 it "notice equal helpers that contain arrays" do
69 source =
70 "abcd;\n" +
71 "#define UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY(a,b,c,d,e) {...};\n" +
72 "abcd;\n" +
73 "#define UNITY_TEST_ASSERT_EQUAL_WRONG_NUM_ARGS_ARRAY(a,b,c,d,e,f) {...};\n" +
74 "#define UNITY_TEST_ASSERT_WRONG_NAME_EQUAL_ARRAY(a,b,c,d,e) {...};\n" +
75 "#define UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY(a,b,c,d,e) {...};\n" +
76 "abcd;\n"
77 @config.expect :plugins, [] #not :array
78 @config.expect :treat_as, {}
79 @config.expect :load_unity_helper, source
80 @parser = CMockUnityHelperParser.new(@config)
81 expected = {
82 'TURKEYS*' => "UNITY_TEST_ASSERT_EQUAL_TURKEYS_ARRAY",
83 'unsigned_funky_rabbits*' => "UNITY_TEST_ASSERT_EQUAL_unsigned_funky_rabbits_ARRAY"
84 }
85
86 assert_equal(expected, @parser.c_types)
87 end
88
89 it "pull in the standard set of helpers and add them to my list" do
90 pairs = {
91 "UINT" => "HEX32",
92 "unsigned long" => "HEX64",
93 }
94 expected = {
95 "UINT" => "UNITY_TEST_ASSERT_EQUAL_HEX32",
96 "unsigned_long" => "UNITY_TEST_ASSERT_EQUAL_HEX64",
97 "UINT*" => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY",
98 "unsigned_long*"=> "UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY",
99 }
100 @config.expect :plugins, [] #not :array
101 @config.expect :treat_as, pairs
102 @config.expect :load_unity_helper, nil
103 @parser = CMockUnityHelperParser.new(@config)
104
105 assert_equal(expected, @parser.c_types)
106 end
107
108 it "pull in the user specified set of helpers and add them to my list" do
109 pairs = {
110 "char*" => "STRING",
111 "unsigned int" => "HEX32",
112 }
113 expected = {
114 "char*" => "UNITY_TEST_ASSERT_EQUAL_STRING",
115 "unsigned_int" => "UNITY_TEST_ASSERT_EQUAL_HEX32",
116 "char**" => "UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY",
117 "unsigned_int*" => "UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY",
118 }
119 @config.expect :plugins, [] #not :array
120 @config.expect :treat_as, pairs
121 @config.expect :load_unity_helper, nil
122 @parser = CMockUnityHelperParser.new(@config)
123
124 assert_equal(expected, @parser.c_types)
125 end
126
127 it "be able to fetch helpers on my list" do
128 @config.expect :plugins, [] #not :array
129 @config.expect :treat_as, {}
130 @config.expect :load_unity_helper, ""
131 @parser = CMockUnityHelperParser.new(@config)
132 @parser.c_types = {
133 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
134 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
135 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
136 'LONG_LONG' => "UNITY_TEST_ASSERT_EQUAL_LONG_LONG"
137 }
138
139 [["UINT8","UINT8"],
140 ["UINT16*","UINT16_ARRAY"],
141 ["const SPINACH","SPINACH"],
142 ["LONG LONG","LONG_LONG"] ].each do |ctype, exptype|
143 assert_equal(["UNITY_TEST_ASSERT_EQUAL_#{exptype}",''], @parser.get_helper(ctype))
144 end
145 end
146
147 it "return memory comparison when asked to fetch helper of types not on my list" do
148 @config.expect :plugins, [] #not :array
149 @config.expect :treat_as, {}
150 @config.expect :load_unity_helper, ""
151 @parser = CMockUnityHelperParser.new(@config)
152 @parser.c_types = {
153 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
154 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
155 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
156 }
157
158 ["UINT32","SPINACH_T","SALAD","PINEAPPLE"].each do |ctype|
159 @config.expect :memcmp_if_unknown, true
160 assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY",'&'], @parser.get_helper(ctype))
161 end
162 end
163
164 it "return memory array comparison when asked to fetch helper of types not on my list" do
165 @config.expect :plugins, [:array]
166 @config.expect :treat_as, {}
167 @config.expect :load_unity_helper, ""
168 @parser = CMockUnityHelperParser.new(@config)
169 @parser.c_types = {
170 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
171 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
172 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
173 }
174
175 ["UINT32*","SPINACH_T*"].each do |ctype|
176 @config.expect :memcmp_if_unknown, true
177 assert_equal(["UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY",''], @parser.get_helper(ctype))
178 end
179 end
180
181 it "return the array handler if we cannot find the normal handler" do
182 @config.expect :plugins, [] #not :array
183 @config.expect :treat_as, {}
184 @config.expect :load_unity_helper, ""
185 @parser = CMockUnityHelperParser.new(@config)
186 @parser.c_types = {
187 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
188 'UINT16*' => "UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",
189 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
190 }
191
192 assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT16_ARRAY",'&'], @parser.get_helper("UINT16"))
193 end
194
195 it "return the normal handler if we cannot find the array handler" do
196 @config.expect :plugins, [] #not :array
197 @config.expect :treat_as, {}
198 @config.expect :load_unity_helper, ""
199 @parser = CMockUnityHelperParser.new(@config)
200 @parser.c_types = {
201 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
202 'UINT16' => "UNITY_TEST_ASSERT_EQUAL_UINT16",
203 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
204 }
205
206 assert_equal(["UNITY_TEST_ASSERT_EQUAL_UINT8",'*'], @parser.get_helper("UINT8*"))
207 end
208
209 it "raise error when asked to fetch helper of type not on my list and not allowed to mem check" do
210 @config.expect :plugins, [] #not :array
211 @config.expect :treat_as, {}
212 @config.expect :load_unity_helper, ""
213 @config.expect :memcmp_if_unknown, false
214 @parser = CMockUnityHelperParser.new(@config)
215 @parser.c_types = {
216 'UINT8' => "UNITY_TEST_ASSERT_EQUAL_UINT8",
217 'UINT32*' => "UNITY_TEST_ASSERT_EQUAL_UINT32_ARRAY",
218 'SPINACH' => "UNITY_TEST_ASSERT_EQUAL_SPINACH",
219 }
220
221 assert_raises (RuntimeError) { @parser.get_helper("UINT16") }
222 end
223 end