-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·667 lines (543 loc) · 23.1 KB
/
test.py
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/usr/bin/env python3
"""
Unit tests for Yan.
"""
# pylint: disable=missing-docstring
import unittest
import yan
from yan import (lex,
NSyntaxError,
Position)
def _remove_notes_issues(issues):
return [i for i in issues if i.level != 'note']
def parse_generic(source, parse_func):
expr, issues = parse_func(source)
issues = _remove_notes_issues(issues)
if len(issues) > 0:
raise issues[0]
return expr
def parse(source):
return parse_generic(source, yan.parse)
def parse_expr(source):
return parse_generic(source, yan.parse_expr)
def parse_statement(source):
return parse_generic(source, yan.parse_statement)
class TestPosition(unittest.TestCase):
def test_begin_position(self):
pos = Position('abcd')
self.assertEqual(pos.file_name, 'abcd')
self.assertEqual(pos.index, 0)
self.assertEqual(pos.line, 1)
self.assertEqual(pos.column, 1)
self.assertEqual(str(pos), 'abcd:1:1')
def test_position(self):
pos = Position('abcd', 2, 3, 4)
self.assertEqual(pos.index, 2)
self.assertEqual(pos.line, 3)
self.assertEqual(pos.column, 4)
self.assertEqual(str(pos), 'abcd:3:4')
class TestLexer(unittest.TestCase):
def assertLexEqual(self, source, expected):
tokens = lex(source)
self.assertEqual(''.join(repr(t) for t in tokens), expected)
def assertTokenEqual(self, source, kind, string):
assert kind in yan.TOKEN_KINDS
tokens = lex(source)
self.assertEqual(len(tokens), 1)
self.assertEqual(tokens[0].kind, kind)
self.assertEqual(tokens[0].string, string)
def test_skip_spaces(self):
self.assertLexEqual('', '')
self.assertLexEqual(' ', '')
def test_new_line(self):
self.assertLexEqual(' \n"abc"',
'<Token kind=string, string=\'"abc"\', '
'begin=<unknown file>:2:1, '
'end=<unknown file>:2:5>')
def test_string(self):
self.assertLexEqual('"abc"',
'<Token kind=string, string=\'"abc"\', '
'begin=<unknown file>:1:1, '
'end=<unknown file>:1:5>')
self.assertLexEqual(' "abc"',
'<Token kind=string, string=\'"abc"\', '
'begin=<unknown file>:1:3, '
'end=<unknown file>:1:7>')
self.assertLexEqual('""',
'<Token kind=string, string=\'""\', '
'begin=<unknown file>:1:1, '
'end=<unknown file>:1:2>')
def test_multiple_strings(self):
self.assertLexEqual('"" ""',
'<Token kind=string, string=\'""\', '
'begin=<unknown file>:1:1, '
'end=<unknown file>:1:2>'
'<Token kind=string, string=\'""\', '
'begin=<unknown file>:1:4, '
'end=<unknown file>:1:5>')
def test_string_error(self):
with self.assertRaises(NSyntaxError):
lex('"abc')
with self.assertRaises(NSyntaxError):
lex('"\n"')
with self.assertRaises(NSyntaxError):
lex('"')
def test_string_escape(self):
self.assertTokenEqual(r'"\0"', 'string', r'"\0"')
self.assertTokenEqual(r'"\n"', 'string', r'"\n"')
with self.assertRaises(NSyntaxError):
lex(r'"\"')
def test_character(self):
self.assertTokenEqual("'a'", 'character', "'a'")
self.assertTokenEqual(r"'\0'", 'character', r"'\0'")
self.assertTokenEqual(r"'\n'", 'character', r"'\n'")
def test_keyword(self):
self.assertLexEqual('int',
'<Token kind=keyword, string=\'int\', '
'begin=<unknown file>:1:1, '
'end=<unknown file>:1:3>')
def test_identifier(self):
self.assertLexEqual('_Abcde_F',
'<Token kind=identifier, string=\'_Abcde_F\', '
'begin=<unknown file>:1:1, '
'end=<unknown file>:1:8>')
def test_integer_dec(self):
self.assertTokenEqual('0', 'integer', '0')
self.assertTokenEqual('0777', 'integer', '0777')
def test_integer_hex(self):
self.assertTokenEqual('0x10', 'integer', '0x10')
self.assertTokenEqual('0xcafebabe', 'integer', '0xcafebabe')
def test_integer_ul(self):
self.assertTokenEqual('0u', 'integer', '0u')
self.assertTokenEqual('0U', 'integer', '0U')
self.assertTokenEqual('0L', 'integer', '0L')
self.assertTokenEqual('0Ll', 'integer', '0Ll')
self.assertTokenEqual('0uL', 'integer', '0uL')
def test_float(self):
self.assertTokenEqual('.0', 'float', '.0')
self.assertTokenEqual('0.', 'float', '0.')
self.assertTokenEqual('.0l', 'float', '.0l')
self.assertTokenEqual('0.l', 'float', '0.l')
self.assertTokenEqual('0.0', 'float', '0.0')
def test_comment(self):
self.assertTokenEqual('/**/', 'comment', '/**/')
self.assertTokenEqual('/*\n*/', 'comment', '/*\n*/')
self.assertTokenEqual('/*abc*/', 'comment', '/*abc*/')
def test_comment_and_string(self):
self.assertTokenEqual('/*"abc"*/', 'comment', '/*"abc"*/')
self.assertTokenEqual('"/**/"', 'string', '"/**/"')
def test_sign(self):
self.assertTokenEqual('++', 'sign', '++')
self.assertTokenEqual('.', 'sign', '.')
self.assertTokenEqual('...', 'sign', '...')
self.assertTokenEqual('>>', 'sign', '>>')
self.assertTokenEqual('->', 'sign', '->')
self.assertTokenEqual('-', 'sign', '-')
def test_directive(self):
self.assertTokenEqual('#ifdef a', 'directive', '#ifdef a')
self.assertTokenEqual('\n#ifdef a', 'directive', '#ifdef a')
self.assertTokenEqual('\n # include <a> \n ',
'directive', ' # include <a> ')
self.assertTokenEqual('\n # include "a" \n ',
'directive', ' # include "a" ')
with self.assertRaises(NSyntaxError):
lex('#include "a>')
with self.assertRaises(NSyntaxError):
lex('#include <a"')
class TestIncludedFile(unittest.TestCase):
def test_eq(self):
self.assertEqual(yan.IncludedFile(False, 'a.h'),
yan.IncludedFile(False, 'a.h'))
class TestParser(unittest.TestCase):
def checkDecl(self, source, expected_result=None, parse_func=yan.parse):
if expected_result is None:
expected_result = source
tree = parse_generic(source, parse_func)
result = str(tree).rstrip('\n')
self.assertEqual(result, expected_result)
def checkExpr(self, source, expected_result=None):
self.checkDecl(source, expected_result, yan.parse_expr)
def checkStatement(self, source, expected_result=None):
self.checkDecl(source, expected_result, yan.parse_statement)
def test_string_concatenation(self):
self.checkExpr('"hello" "world" "!"')
def test_sizeof(self):
self.checkExpr('sizeof(int)')
self.checkExpr('sizeof(123)')
self.checkExpr('sizeof 123')
def test_cast(self):
self.checkExpr('(int)54.9')
self.checkExpr('(char **)17')
def test_call(self):
self.checkExpr('a()()()')
def test_binary_operation(self):
self.checkExpr('1 + 2', '(1 + 2)')
self.checkExpr('1 + 2 * 3', '(1 + (2 * 3))')
self.checkExpr('1 * 2 + 3', '((1 * 2) + 3)')
self.checkExpr('1 + 2 + 3', '((1 + 2) + 3)')
def test_assign(self):
self.checkStatement('a = 2;')
self.checkStatement('a += 2;')
def test_dot_operation(self):
self.checkExpr('a.b')
self.checkExpr('a->b')
self.checkExpr('a->b.c')
with self.assertRaises(NSyntaxError):
parse_expr('a.""')
with self.assertRaises(NSyntaxError):
parse_expr('a->""')
def test_precedence(self):
self.checkExpr('1 || 2 && 3 | 4 ^ 5 & 6 == '
'7 > 8 >> 9 + 10 % a.b',
'(1 || (2 && (3 | (4 ^ (5 & (6 == '
'(7 > (8 >> (9 + (10 % a.b))))))))))')
def test_statement(self):
self.checkStatement('0;')
self.checkStatement('1 + 2;', '(1 + 2);')
def test_array(self):
self.checkDecl('int b[4];')
self.checkDecl('int b[1][2];', 'int b[1][2];')
self.checkDecl('void f(int b[]);')
def test_decl(self):
self.checkDecl('long unsigned register int b, c;')
self.checkDecl('const volatile int b, c = 1;')
self.checkDecl('int **b, *c = 1;',)
def test_function_decl(self):
self.checkDecl('void f(long a);')
self.checkDecl('void f(long);')
self.checkDecl('void (*a)(char *, int, long *);')
def test_function_def(self):
self.checkDecl('void f(long a) {}',
'void f(long a)\n{\n}')
self.checkDecl('void f(long) {}', 'void f(long)\n{\n}')
self.checkDecl('int main()'
'{'
'if (a < b) a;'
'}',
'int main()\n'
'{\n'
'if ((a < b))\n'
'a;\n'
'}')
self.checkDecl('void f(short b)\n'
'{\n'
'char *a, c;\n'
'int c;\n'
'\n'
'(7 += a);\n'
'}')
self.checkDecl('char *strdup(const char *);')
self.checkDecl('char *strdup(const char *s);')
def test_function_pointer(self):
self.checkDecl('int (*getFunc())(int, int (*)(long));')
self.checkDecl('int (*getFunc())(int, int (*)(long)) {}',
'int (*getFunc())(int, int (*)(long))\n'
'{\n'
'}')
def test_unknown_header(self):
self.checkDecl('#include <_unknown_header_ieuaeiuaeiua_>\n'
'int n;',
'int n;')
def test_size_t(self):
with self.assertRaises(NSyntaxError):
parse('size_t n;')
self.checkDecl('#include <stdlib.h>\n'
'size_t n;',
'size_t n;')
self.checkDecl('#include <stddef.h>\n'
'size_t n;',
'size_t n;')
self.checkDecl('#include <unistd.h>\n'
'size_t n;',
'size_t n;')
def test_comment(self):
with self.assertRaises(NSyntaxError):
parse('// no C++ comment')
self.checkDecl('/* This is a coment */', '')
self.checkDecl('int /* hello */ n;', 'int n;')
def test_print_char(self):
self.checkDecl('#include <unistd.h>\n'
''
'void print_char(char c)'
'{'
' write(STDOUT_FILENO, &c, 1);'
'}',
'void print_char(char c)\n'
'{\n'
'write(STDOUT_FILENO, (&c), 1);\n'
'}')
def test_struct(self):
self.checkDecl('struct s;')
self.checkDecl('struct s s;')
self.checkDecl('union s;')
def test_struct_compound(self):
self.checkDecl('struct'
'{'
'int a;'
'};',
'struct\n'
'{\n'
'int a;\n'
'};')
self.checkDecl('struct s'
'{'
'int a;'
'};',
'struct s\n'
'{\n'
'int a;\n'
'};')
with self.assertRaises(NSyntaxError):
parse('struct s {a + a;}')
def test_compound_literal(self):
self.checkExpr('(int *){23, 45, 67}')
self.checkExpr('(int[]){23, 45, 67}')
self.checkExpr('(struct s *[12]){23, 45, 67}')
def test_initializer_list(self):
self.checkDecl('int a[] = {2, 3, 4};')
def test_designated_initializer(self):
self.checkDecl('struct a a = {.a = 0, .b = 1};')
self.checkDecl('int a[] = {[0] = 0, [1] = 1};')
def test_enum(self):
# TODO
# self.checkDecl('enum s;')
pass
def test_typedef(self):
self.checkDecl('typedef int a;\n\n'
'typedef int a;')
with self.assertRaises(NSyntaxError):
parse('a b;')
self.checkDecl('typedef int (*a);\n\n'
'typedef a b;\n\n'
'b c;')
self.checkDecl('typedef int a[32];')
self.checkDecl('typedef int (*a)();')
def test_typedef_struct(self):
self.checkDecl('typedef struct s_dir\n'
'{\n'
'int n;\n'
'int m;\n'
'} t_dir;\n\n'
't_dir d;')
self.checkDecl('typedef char c;\n'
'\n'
'typedef struct s_dir\n'
'{\n'
'void (*a)(c);\n'
'} t_dir;\n\n'
't_dir d;')
def test_if(self):
self.checkStatement('if (a)\n'
'b;')
self.checkStatement('{\n'
'if (a)\n'
'b;\n'
'c;\n'
'}')
def test_while(self):
self.checkStatement('while (a)\n'
'b;')
self.checkStatement('{\n'
'while (a)\n'
'b;\n'
'c;\n'
'}')
def test_bug_cpp_comments(self):
with self.assertRaises(NSyntaxError):
parse('int main() {//bug\n}')
def test_selection(self):
expr = parse_expr('1 + 1')
plus = expr.select('binary_operation')
assert len(plus) == 1
assert isinstance(list(plus)[0], yan.BinaryOperationExpr)
one = expr.select('literal')
assert len(one) == 2
for child in one:
assert isinstance(child, yan.LiteralExpr)
one = expr.select('binary_operation literal')
assert len(one) == 2
for child in one:
assert isinstance(child, yan.LiteralExpr)
with self.assertRaises(ValueError):
parse_expr('123').select('')
with self.assertRaises(ValueError):
parse_expr('123').select('eiaueiuaeiua')
def test_file(test_name, error_messages=None):
if error_messages is None:
error_messages = []
if isinstance(error_messages, str):
error_messages = [error_messages]
def handle_issue(issue):
if issue.message not in error_messages:
exp = ' or '.join(repr(msg) for msg in error_messages)
raise Exception('Expected {}, got {!r}'.format(exp,
issue.message))
error_messages.remove(issue.message)
checkers = yan.create_checkers(handle_issue)
file_path = 'test/' + test_name
if not file_path.endswith('.h'):
file_path += '.c'
_, issues = yan.check_file(file_path, checkers)
issues = [i for i in issues if i.level != 'note']
if len(issues) > 0:
raise Exception(str(issues[0]))
if len(error_messages) > 0:
raise Exception('Expected error messages {}'.format(error_messages))
class TestFiles(unittest.TestCase):
def test_comment(self):
test_file('comment_inside_function', 'Comment inside a function')
test_file('comment_invalid_0',
"The comment lines should start with '**'")
test_file('comment_invalid_1', "Expected a space after '**'")
test_file('comment_invalid_2', "A comment must start with '/*'")
def test_return(self):
test_file('return_no_paren', "Missing parentheses after 'return'")
test_file('return_no_space',
"Expected one space between 'return' and '('")
test_file('return_valid')
def test_if(self):
test_file('if_no_space',
"Expected one space between 'if' and '('")
def test_while(self):
test_file('while_no_space',
"Expected one space between 'while' and '('")
def test_binary_op_space(self):
test_file('binary_op_space_0',
"Expected one space between '+' and '2'")
test_file('binary_op_space_1',
"Expected one space between '1' and '+'")
test_file('binary_op_space_3',
"Expected 0 spaces or tabs between '.' and 'n'")
test_file('binary_op_space_4',
"Expected 0 spaces or tabs between 'a' and '->'")
def test_line_too_long(self):
test_file('line_too_long',
'Too long line (more than 80 columns)')
test_file('line_too_long_in_comment',
'Too long line (more than 80 columns)')
def test_79_columns_line(self):
test_file('79_columns_line')
def test_80_columns_line(self):
test_file('80_columns_line',
[
'The offical style checker seems to forbid lines '
'of 80 columns'
] * 2)
def test_81_columns_line(self):
test_file('81_columns_line',
['Too long line (more than 80 columns)'] * 2)
def test_multiple_statements_by_line(self):
test_file('many_statements_by_line_0',
[
"'return' on the same line than the previous ';'",
"Multiple statements on the same line",
])
test_file('many_statements_by_line_1',
[
"'return' on the same line than the previous ';'",
"Multiple statements on the same line",
])
def test_too_long_function(self):
test_file('26_line_function',
"Too long function (more than 25 lines)")
# This one raises just a warning, not an error
test_file('25_line_function', "Long function (25 lines)")
def test_too_many_functions(self):
test_file('too_many_functions',
"Too many functions in a file (more than 5)")
def test_header_file(self):
"""
Only includes, defines, declarations, prototypes and macros are
allowed in header files
"""
test_file('header_file_global_variable.h',
'This declaration is forbidden in a header file')
test_file('header_file_extern_variable.h',
'Global variable declaration')
test_file('header_file_function_def.h',
'This is forbidden in a header file')
def test_empty_file(self):
test_file('empty', 'Empty file, missing header comment')
def test_macro_in_source_file(self):
test_file('macro_in_source_file',
"The most of the '#define' directives are forbidden in "
"source files")
def test_function_decl_in_source_file(self):
test_file('function_decl_in_source_file',
'Declaration in source file')
def test_indentation(self):
test_file('indentation_invalid_0',
'Bad indent level, expected 1 more space')
test_file('indentation_invalid_1',
'Bad indent level, expected 2 fewer spaces')
test_file('indentation_invalid_2',
'Bad indent level, expected 1 more space')
test_file('indentation_invalid_if',
'Bad indent level, expected 5 fewer spaces')
test_file('indentation_if')
test_file('indentation_alternative_if')
def test_name(self):
test_file('bad_function_name', "'Bad' is an invalid name")
test_file('bad_function_name.h', "'Bad' is an invalid name")
test_file('bad_parameter_name', "'Bad' is an invalid name")
test_file('bad_struct_name.h', "Invalid struct name")
test_file('bad_union_name.h', "Invalid union name")
test_file('bad_variable_name', "'Bad' is an invalid name")
test_file('bad_global_variable_name',
[
'Invalid global variable name',
'Declaration in source file',
])
test_file('bad_typedef_name.h', "Invalid type name")
test_file('bad_typedef_struct_name.h', "Invalid type name")
test_file('bad_macro_name.h', "'bad' is an invalid macro name")
def test_declarator_alignment(self):
test_file('bad_declarator_alignment_0', 'Misaligned declarator')
test_file('bad_declarator_alignment_1', 'Misaligned declarator')
test_file('declarator_alignment')
def test_break(self):
test_file('break_valid')
test_file('break_invalid',
"Expected one space between 'break' and ';'")
def test_continue(self):
test_file('continue_valid')
test_file('continue_invalid',
"Expected one space between 'continue' and ';'")
def test_yan_parser_off(self):
test_file('yan_parser_off')
def test_yan_typedef(self):
test_file('yan_typedef')
def test_header_file_once_include_guard(self):
test_file('header_file_bad_once_include_guard.h',
"Bad once include guard directive (expected '#endif "
"/* !HEADER_FILE_BAD_ONCE_INCLUDE_GUARD_H_ */')")
def test_bug_invalid_name_in_initializer(self):
test_file('bug_invalid_name_in_initializer')
def test_space_between_func_declarator_and_args(self):
test_file('space_between_func_declarator_and_args',
"Expected 0 spaces or tabs between 'main' and '('")
def test_space_between_func_name_and_args(self):
test_file('space_between_func_name_and_args',
"Expected 0 spaces or tabs between 'some_function' and '('")
def test_space_between_type_and_declarator(self):
test_file('space_between_type_and_declarator',
"Expected one space between 'int' and 'a'")
def test_cplusplus(self):
test_file('cplusplus.h')
def test_attribute(self):
test_file('__attribute___valid.h')
def test_empty_line(self):
test_file('empty_line_0', 'Unexpected empty line')
test_file('empty_line_1', 'Unexpected empty line')
test_file('empty_line_2',
'Expected empty line between declarations and statements')
test_file('empty_line_3', 'Unexpected empty line')
test_file('empty_line_4', 'Unexpected empty line')
def test_sizeof(self):
test_file('sizeof_invalid_0',
"Expected 0 spaces or tabs between 'sizeof' and '('")
test_file('sizeof_invalid_1',
"Expected one space between 'sizeof' and 'a'")
test_file('sizeof_valid')
if __name__ == '__main__':
unittest.main()