Debian Codegraph @

test/instrumentation/test-if.js

raw
1 /*jslint nomen: true */
2 var helper = require('../helper'),
3 code,
4 verifier;
5
6 /*jshint maxlen: 500 */
7 module.exports = {
8 "with a simple if": {
9 "as a statement": {
10 setUp: function (cb) {
11 code = [
12 'output = -1;',
13 'if (args[0] > args [1])',
14 ' output = args[0];'
15 ];
16 verifier = helper.verifier(__filename, code);
17 cb(null);
18 },
19 "should cover then path": function (test) {
20 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
21 test.done();
22 },
23 "should cover else path": function (test) {
24 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 2: 1, 3: 0 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
25 test.done();
26 }
27 },
28 "as a block": {
29 setUp: function (cb) {
30 code = [
31 'output = -1;',
32 'if (args[0] > args [1]) {',
33 ' output = args[0];',
34 '}'
35 ];
36 verifier = helper.verifier(__filename, code);
37 cb();
38 },
39 "should cover then path": function (test) {
40 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
41 test.done();
42 },
43 "should cover else path": function (test) {
44 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 2: 1, 3: 0 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
45 test.done();
46 }
47 },
48 "on a single line": {
49 "as statement": {
50 setUp: function (cb) {
51 code = [
52 'output = -1;',
53 'if (args[0] > args [1]) output = args[0];'
54 ];
55 verifier = helper.verifier(__filename, code);
56 cb();
57 },
58 "should cover then path": function (test) {
59 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
60 test.done();
61 },
62 "should cover else path": function (test) {
63 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
64 test.done();
65 }
66 },
67 "as block": {
68 setUp: function (cb) {
69 code = [
70 'output = -1;',
71 'if (args[0] > args [1]) { output = args[0]; }'
72 ];
73 verifier = helper.verifier(__filename, code);
74 cb();
75 },
76 "should cover then path": function (test) {
77 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
78 test.done();
79 },
80 "should cover else path": function (test) {
81 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
82 test.done();
83 }
84 }
85 }
86 },
87 "with a simple if-else": {
88 "as a statement": {
89 setUp: function (cb) {
90 code = [
91 'if (args[0] > args [1])',
92 ' output = args[0];',
93 'else',
94 ' output = args[1];'
95 ];
96 verifier = helper.verifier(__filename, code);
97 cb(null);
98 },
99 "should cover then path": function (test) {
100 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
101 test.done();
102 },
103 "should cover else path": function (test) {
104 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1, 2: 0, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
105 test.done();
106 }
107 },
108 "as a block": {
109 setUp: function (cb) {
110 code = [
111 'if (args[0] > args [1]) {',
112 ' output = args[0];',
113 '} else {',
114 ' output = args[1];',
115 '}'
116 ];
117 verifier = helper.verifier(__filename, code);
118 cb();
119 },
120 "should cover then path": function (test) {
121 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
122 test.done();
123 },
124 "should cover else path": function (test) {
125 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1, 2: 0, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
126 test.done();
127 }
128 },
129 "on a single line": {
130 "as statement": {
131 setUp: function (cb) {
132 code = [
133 'if (args[0] > args [1]) output = args[0]; else output = args[1];'
134 ];
135 verifier = helper.verifier(__filename, code);
136 cb();
137 },
138 "should cover then path": function (test) {
139 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
140 test.done();
141 },
142 "should cover else path": function (test) {
143 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
144 test.done();
145 }
146 },
147 "as block": {
148 setUp: function (cb) {
149 code = [
150 'if (args[0] > args [1]) { output = args[0]; } else { output = args[1]; }'
151 ];
152 verifier = helper.verifier(__filename, code);
153 cb();
154 },
155 "should cover then path": function (test) {
156 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
157 test.done();
158 },
159 "should cover else path": function (test) {
160 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
161 test.done();
162 }
163 },
164 "as mixed with then-block": {
165 setUp: function (cb) {
166 code = [
167 'if (args[0] > args [1]) { output = args[0]; } else output = args[1];'
168 ];
169 verifier = helper.verifier(__filename, code);
170 cb();
171 },
172 "should cover then path": function (test) {
173 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
174 test.done();
175 },
176 "should cover else path": function (test) {
177 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
178 test.done();
179 }
180 },
181 "as mixed with else-block": {
182 setUp: function (cb) {
183 code = [
184 'if (args[0] > args [1]) output = args[0]; else { output = args[1]; }'
185 ];
186 verifier = helper.verifier(__filename, code);
187 cb();
188 },
189 "should cover then path": function (test) {
190 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
191 test.done();
192 },
193 "should cover else path": function (test) {
194 verifier.verify(test, [ 10, 20 ], 20, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
195 test.done();
196 }
197 }
198 }
199 },
200 "with nested ifs": {
201 "without an else": {
202 "and no blocks": {
203 setUp: function (cb) {
204 code = [
205 'output = -1;',
206 'if (args[0] > args[1]) if (args[1] > args[2]) output = args[2];'
207 ];
208 verifier = helper.verifier(__filename, code);
209 cb();
210 },
211 "should cover first else and nothing below": function (test) {
212 verifier.verify(test, [ 10, 20, 15 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0 } });
213 test.done();
214 },
215 "should cover first then": function (test) {
216 verifier.verify(test, [ 20, 10, 15 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 0 } });
217 test.done();
218 },
219 "should cover first then and second then": function (test) {
220 verifier.verify(test, [ 20, 10, 5 ], 5, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 1 } });
221 test.done();
222 }
223 },
224 "and blocks": {
225 setUp: function (cb) {
226 code = [
227 'output = -1;',
228 'if (args[0] > args[1]) { if (args[1] > args[2]) { output = args[2]; } }'
229 ];
230 verifier = helper.verifier(__filename, code);
231 cb();
232 },
233 "should cover first else and nothing below": function (test) {
234 verifier.verify(test, [ 10, 20, 15 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0 } });
235 test.done();
236 },
237 "should cover first then": function (test) {
238 verifier.verify(test, [ 20, 10, 15 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 0 } });
239 test.done();
240 },
241 "should cover first then and second then": function (test) {
242 verifier.verify(test, [ 20, 10, 5 ], 5, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 1 } });
243 test.done();
244 }
245 }
246 },
247 "with elses": {
248 "and no blocks": {
249 setUp: function (cb) {
250 code = [
251 'output = -1;',
252 'if (args[0] > args[1]) if (args[1] > args[2]) output = args[2]; else output = args[1];'
253 ];
254 verifier = helper.verifier(__filename, code);
255 cb();
256 },
257 "should cover first else and nothing below": function (test) {
258 verifier.verify(test, [ 10, 20, 15 ], -1, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0 } });
259 test.done();
260 },
261 "should cover first then": function (test) {
262 verifier.verify(test, [ 20, 10, 15 ], 10, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 0, '5': 1 } });
263 test.done();
264 },
265 "should cover first then and second then": function (test) {
266 verifier.verify(test, [ 20, 10, 5 ], 5, { lines: { 1: 1, 2: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 1, '5': 0 } });
267 test.done();
268 }
269 },
270 "including else ifs with blocks": {
271 setUp: function (cb) {
272 code = [
273 'if (args[0] < args[1]) {',
274 ' output = args[0];',
275 '} else if (args[1] < args[2]) {',
276 ' output = args[1];',
277 '} else if (args[2] < args[3]) {',
278 ' output = args[2];',
279 '} else {',
280 ' output = args[3];',
281 '}'
282 ];
283 verifier = helper.verifier(__filename, code);
284 cb();
285 },
286 "should cover all else paths": function (test) {
287 verifier.verify(test, [ 4, 3, 2, 1 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 8: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 0, '7': 1 } });
288 test.done();
289 },
290 "should cover one then path": function (test) {
291 verifier.verify(test, [ 4, 3, 1, 2 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 1, 8: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 1, '7': 0 } });
292 test.done();
293 },
294 "should cover upper then paths": function (test) {
295 verifier.verify(test, [ 4, 2, 3, 1 ], 2, { lines: { 1: 1, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0, 8: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 1, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 1, '5': 0, '6': 0, '7': 0 } });
296 test.done();
297 },
298 "should cover uppermost then paths": function (test) {
299 verifier.verify(test, [ 1, 2, 3, 1 ], 1, { lines: { 1: 1, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0, 8: 0 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0 } });
300 test.done();
301 }
302 },
303 "including else ifs without blocks": {
304 setUp: function (cb) {
305 code = [
306 'if (args[0] < args[1])',
307 ' output = args[0];',
308 'else if (args[1] < args[2])',
309 ' output = args[1];',
310 'else if (args[2] < args[3])',
311 ' output = args[2];',
312 'else',
313 ' output = args[3];'
314 ];
315 verifier = helper.verifier(__filename, code);
316 cb();
317 },
318 "should cover all else paths": function (test) {
319 verifier.verify(test, [ 4, 3, 2, 1 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0, 8: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 0, '7': 1 } });
320 test.done();
321 },
322 "should cover one then path": function (test) {
323 verifier.verify(test, [ 4, 3, 1, 2 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 1, 8: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 1, '7': 0 } });
324 test.done();
325 },
326 "should cover upper then paths": function (test) {
327 verifier.verify(test, [ 4, 2, 3, 1 ], 2, { lines: { 1: 1, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0, 8: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 1, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 1, '5': 0, '6': 0, '7': 0 } });
328 test.done();
329 },
330 "should cover uppermost then paths": function (test) {
331 verifier.verify(test, [ 1, 2, 3, 1 ], 1, { lines: { 1: 1, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0, 8: 0 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0 } });
332 test.done();
333 }
334 },
335 "including else ifs without blocks (compact)": {
336 setUp: function (cb) {
337 code = [
338 'if (args[0] < args[1]) output = args[0]; else if (args[1] < args[2]) output = args[1]; else if (args[2] < args[3]) output = args[2]; else output = args[3];'
339 ];
340 verifier = helper.verifier(__filename, code);
341 cb();
342 },
343 "should cover all else paths": function (test) {
344 verifier.verify(test, [ 4, 3, 2, 1 ], 1, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 0, '7': 1 } });
345 test.done();
346 },
347 "should cover one then path": function (test) {
348 verifier.verify(test, [ 4, 3, 1, 2 ], 1, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 0, '5': 1, '6': 1, '7': 0 } });
349 test.done();
350 },
351 "should cover upper then paths": function (test) {
352 verifier.verify(test, [ 4, 2, 3, 1 ], 2, { lines: { 1: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 1, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1, '4': 1, '5': 0, '6': 0, '7': 0 } });
353 test.done();
354 },
355 "should cover uppermost then paths": function (test) {
356 verifier.verify(test, [ 1, 2, 3, 1 ], 1, { lines: { 1: 1 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0 } });
357 test.done();
358 }
359 },
360 "including else ifs without blocks (modified)": {
361 setUp: function (cb) {
362 code = [
363 'output = args[3]; if (args[0] < args[1])',
364 ' output = args[0];',
365 'else if (args[1] < args[2])',
366 ' output = args[1];',
367 'else if (args[2] < args[3])',
368 ' output = args[2];'
369 ];
370 verifier = helper.verifier(__filename, code);
371 cb();
372 },
373 "should cover all else paths": function (test) {
374 verifier.verify(test, [ 4, 3, 2, 1 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 1, '5': 0, '6': 1, '7': 0 } });
375 test.done();
376 },
377 "should cover one then path": function (test) {
378 verifier.verify(test, [ 4, 3, 1, 2 ], 1, { lines: { 1: 1, 2: 0, 3: 1, 4: 0, 5: 1, 6: 1 }, branches: { '1': [ 0, 1 ], '2' : [ 0, 1 ], '3': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 1, '5': 0, '6': 1, '7': 1 } });
379 test.done();
380 },
381 "should cover upper then paths": function (test) {
382 verifier.verify(test, [ 4, 2, 3, 1 ], 2, { lines: { 1: 1, 2: 0, 3: 1, 4: 1, 5: 0, 6: 0 }, branches: { '1': [ 0, 1 ], '2' : [ 1, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0, '4': 1, '5': 1, '6': 0, '7': 0 } });
383 test.done();
384 },
385 "should cover uppermost then paths": function (test) {
386 verifier.verify(test, [ 1, 2, 3, 1 ], 1, { lines: { 1: 1, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0 }, branches: { '1': [ 1, 0 ], '2' : [ 0, 0 ], '3': [ 0, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1, '4': 0, '5': 0, '6': 0, '7': 0 } });
387 test.done();
388 }
389 }
390 }
391 }
392 };
393