Skip to content
Navigation Menu
{{ message }}
forked from terryyin/lizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestNestedStructures.py
More file actions
486 lines (432 loc) · 12.7 KB
/
Copy pathtestNestedStructures.py
File metadata and controls
486 lines (432 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import unittest
from .testHelpers import get_cpp_function_list_with_extnesion, \
get_python_function_list_with_extnesion
from lizard_ext.lizardns import LizardExtension as NestedStructure
def process_cpp(source):
return get_cpp_function_list_with_extnesion(source, NestedStructure())
def process_python(source):
return get_python_function_list_with_extnesion(source, NestedStructure())
class TestCppNestedStructures(unittest.TestCase):
def test_no_structures(self):
result = process_cpp("int fun(){}")
self.assertEqual(0, result[0].max_nested_structures)
def test_if_structure(self):
result = process_cpp("int fun(){if(a){xx;}}")
self.assertEqual(1, result[0].max_nested_structures)
def test_ternary_operator(self):
"""The ternary operator is not a structure."""
result = process_cpp("int fun(){return (a)?b:c;}")
self.assertEqual(0, result[0].max_nested_structures)
def test_forever_loop(self):
result = process_cpp("int fun(){for(;;){dosomething();}}")
self.assertEqual(1, result[0].max_nested_structures)
def test_terminator_in_parentheses(self):
result = process_cpp("int fun(){for(int a;;){ if (a) return b;}}")
self.assertEqual(2, result[0].max_nested_structures)
def test_and_condition_in_if_structure(self):
result = process_cpp("int fun(){if(a&&b){xx;}}")
self.assertEqual(1, result[0].max_nested_structures)
def test_else_if(self):
result = process_cpp("""
x c() {
if (a && b)
baz();
else if (c)
foo();
}
""")
self.assertEqual(1, result[0].max_nested_structures)
def test_non_r_value_ref_in_body(self):
result = process_cpp("""
x c() {
if (a && b) {
baz();
} else {
foo();
}
}
x a() {
c = a && b;
}
""")
self.assertEqual(1, result[0].max_nested_structures)
self.assertEqual(0, result[1].max_nested_structures)
def test_nested_if_structures(self):
result = process_cpp("""
x c() {
if (a && b) {
if(a != 0) {
a = b;
}
}
}
x a() {
if (a && b)
if (a != 0)
a = b;
}
""")
self.assertEqual(2, result[0].max_nested_structures)
self.assertEqual(2, result[1].max_nested_structures)
def test_nested_loop_mixed_brackets(self):
result = process_cpp("""
x c() {
if (a && b) {
if (a != 0)
a = b;
}
}
x a() {
if (a && b)
if (a != 0) {
a = b;
}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
self.assertEqual(2, result[1].max_nested_structures)
def test_equal_metric_structures(self):
result = process_cpp("""
x c() {
if (a && b) {
if(a != 0){
a = b;
} else b = a;
}
if (a && b){
if(a != 0)
a = b;
else
b = a;
}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_while(self):
result = process_cpp("""
x c() {
while (a && b) {
if(a != 0){
a = b;
}
while (s) continue;
}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_do(self):
result = process_cpp("""
x c() {
do {
if(a != 0){
a = b;
} else {
b = a;
}
} while (a && b);
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_try_catch(self):
result = process_cpp("""
x c() {
try {
if (a != 0) {
foo();
} else {
bar();
}
} catch (auto& err) {}
}
x a() {
try {
foo();
} catch (auto& err) {
if (a != 0) {
bar();
} else {
baz();
}
}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
self.assertEqual(2, result[1].max_nested_structures)
def test_braceless_nested_if_try_structures(self):
result = process_cpp("""
x c() {
if (a)
try {
throw 42;
} catch(...) {
if (b) return 42;
}
}
""")
self.assertEqual(3, result[0].max_nested_structures)
def test_braceless_nested_for_try_structures(self):
result = process_cpp("""
x c() {
for (;;)
try {
throw 42;
} catch(...) {
if (b) return 42;
}
}
""")
self.assertEqual(3, result[0].max_nested_structures)
def test_switch_case(self):
"""Switch-Case is one control structure."""
result = process_cpp("""
x c() {
switch (a) {
case 1:
if (b && c) break;
case 0:
default:
return;
}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_scope(self):
result = process_cpp("""
x c() {
{{{{{{for (a : c) {
{{{{if(a != 0) {{{{
a = b;
}}}}}}}}
}}}}}}}
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_gotcha_if_else(self):
"""The last 'else' is associated with the innermost 'if'."""
result = process_cpp("""
x c() {
if (a)
if (b)
call();
else // Deceiving indentation!
for (auto c : d) // #3 control structure is here!
call(c);
}
""")
self.assertEqual(3, result[0].max_nested_structures)
def test_non_structure_braces(self):
"""Extra braces for initializer lists may confuse the nesting level."""
result = process_cpp("""
x c() {
if (a) return {{}}; // Initializer list with a braceless structure.
if (b)
if (c)
if (d) return 42;
}
""")
self.assertEqual(3, result[0].max_nested_structures)
def test_braceless_consecutive_if_structures(self):
"""Braceless structures one after another."""
result = process_cpp("""
x c() {
if (a)
if (b)
foobar();
if (c)
if (d)
baz();
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_braceless_consecutive_for_if_structures(self):
"""Braceless structures one after another."""
result = process_cpp("""
x c() {
for (;;)
for (;;)
foobar();
if (c)
if (d)
baz();
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_braceless_consecutive_if_structures_with_return(self):
"""Braceless structures one after another."""
result = process_cpp("""
x c() {
if (a)
if (b)
return true;
if (c)
if (d)
return false;
}
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_braceless_nested_if_else_structures(self):
result = process_cpp("""
x c() {
if (a)
if (b) {
return b;
} else {
if (b) return 42;
}
}
""")
self.assertEqual(3, result[0].max_nested_structures)
def test_braceless_nested_if_else_if_structures(self):
result = process_cpp("""
x c() {
if (a)
if (b) {
return b;
} else if (c) {
if (b) return 42;
}
}
""")
self.assertEqual(3, result[0].max_nested_structures)
@unittest.skip("Unspecified. Not Implemented. Convoluted.")
def test_struct_inside_declaration(self):
"""Extra complexity class/struct should be ignored."""
result = process_cpp("""
x c() {
for (struct {int s; int foo() { while(c) return s; }} a{42};;) break;
}
""")
self.assertEqual(1, result[0].max_nested_structures)
@unittest.skip("Unspecified. Not Implemented. Convoluted.")
def test_struct_inside_definition(self):
"""Extra complexity class/struct should be ignored."""
result = process_cpp("""
x c() {
for (;;) {
struct {int s; int foo() { while(c) return s; }} a{42};
}
}
""")
self.assertEqual(1, result[0].max_nested_structures)
class TestPythonNestedStructures(unittest.TestCase):
def test_no_structures(self):
result = process_python("def fun():\n pass")
self.assertEqual(0, result[0].max_nested_structures)
def test_if_structure(self):
result = process_python("def fun():\n if a:\n return")
self.assertEqual(1, result[0].max_nested_structures)
def test_for_structure(self):
result = process_python("def fun():\n for a in b:\n foo()")
self.assertEqual(1, result[0].max_nested_structures)
def test_condition_in_if_structure(self):
result = process_python("def fun():\n if a and b:\n return")
self.assertEqual(1, result[0].max_nested_structures)
def test_elif(self):
result = process_python("""
def c():
if a:
baz()
elif c:
foo()
""")
self.assertEqual(1, result[0].max_nested_structures)
def test_nested_if_structures(self):
result = process_python("""
def c():
if a:
if b:
baz()
else:
foo()
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_equal_metric_structures(self):
result = process_python("""
def c():
if a:
if b:
baz()
else:
foo()
for a in b:
if c:
bar()
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_while(self):
result = process_python("""
def c():
while a:
baz()
""")
self.assertEqual(1, result[0].max_nested_structures)
def test_try_catch(self):
result = process_python("""
def c():
try:
f.open()
catch Exception as err:
print(err)
finally:
f.close()
""")
self.assertEqual(1, result[0].max_nested_structures)
def test_two_functions(self):
result = process_python("""
def c():
try:
if a:
foo()
catch Exception as err:
print(err)
def d():
for a in b:
for x in y:
if i:
return j
""")
self.assertEqual(2, result[0].max_nested_structures)
self.assertEqual(3, result[1].max_nested_structures)
def test_nested_functions(self):
result = process_python("""
def c():
def d():
for a in b:
for x in y:
if i:
return j
try:
if a:
foo()
catch Exception as err:
print(err)
""")
self.assertEqual(3, result[0].max_nested_structures)
self.assertEqual(2, result[1].max_nested_structures)
def test_with_structure(self):
result = process_python("""
def c():
with open(f) as input_file:
foo(f)
""")
self.assertEqual(1, result[0].max_nested_structures)
def test_for_else(self):
result = process_python("""
def c():
for i in range(10):
break
else:
for j in range(i):
print(j)
""")
self.assertEqual(2, result[0].max_nested_structures)
def test_while_else(self):
result = process_python("""
def c(i):
while i < 10:
break
else:
for j in range(i):
print(j)
""")
self.assertEqual(2, result[0].max_nested_structures)
You can’t perform that action at this time.
