forked from myint/cppclean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tokenize.py
executable file
·526 lines (433 loc) · 21.3 KB
/
test_tokenize.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
#!/usr/bin/env python
#
# Copyright 2007 Neal Norwitz
# Portions Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tokenize test."""
from __future__ import absolute_import
import unittest
from cpp import tokenize
__author__ = '[email protected] (Neal Norwitz)'
# For convenience, add factories and __eq__ to test the module.
def Syntax(name, start, end):
return tokenize.Token(tokenize.SYNTAX, name, start, end)
def Constant(name, start, end):
return tokenize.Token(tokenize.CONSTANT, name, start, end)
def Name(name, start, end):
return tokenize.Token(tokenize.NAME, name, start, end)
def Preprocessor(name, start, end):
return tokenize.Token(tokenize.PREPROCESSOR, name, start, end)
def __eq__(self, other):
assert isinstance(other, self.__class__)
return (self.token_type == other.token_type and
self.name == other.name and
self.start == other.start and
self.end == other.end)
tokenize.Token.__eq__ = __eq__
class TokenizeTest(unittest.TestCase):
def get_tokens(self, string):
return list(tokenize.get_tokens(string))
def test_get_tokens_empty_string(self):
self.assertEqual([], self.get_tokens(''))
def test_get_tokens_whitespace(self):
self.assertEqual([], self.get_tokens(' '))
self.assertEqual([], self.get_tokens(' \n\n\n'))
def test_get_tokens_cpp_comment(self):
self.assertEqual([], self.get_tokens('// comment'))
def test_get_tokens_multiline_comment(self):
self.assertEqual([], self.get_tokens('/* comment\n\n\nfoo */'))
def test_get_tokens_if0(self):
tokens = self.get_tokens('#if 0\n@\n#endif')
self.assertEqual(0, len(tokens), tokens)
def test_get_tokens_define(self):
tokens = self.get_tokens('#define PI 3.14')
self.assertEqual(1, len(tokens), tokens)
self.assertEqual(Preprocessor('#define PI 3.14', 0, 15), tokens[0])
def test_get_tokens_binary_operators(self):
for operator in '+-*/%&|^<>':
# 012 345
tokens = self.get_tokens('5 %s 3' % operator)
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Constant('5', 0, 1), tokens[0])
self.assertEqual(Syntax(operator, 2, 3), tokens[1])
self.assertEqual(Constant('3', 4, 5), tokens[2])
def test_get_tokens_multi_char_binary_operators(self):
# 0123456
tokens = self.get_tokens('5 << 3')
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Constant('5', 0, 1), tokens[0])
self.assertEqual(Syntax('<<', 2, 4), tokens[1])
self.assertEqual(Constant('3', 5, 6), tokens[2])
def test_get_tokens_addition_with_comment(self):
# 0123456789012 3 4 56789012345
tokens = self.get_tokens('5 /* comment\n\n\nfoo */ + 3')
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Constant('5', 0, 1), tokens[0])
self.assertEqual(Syntax('+', 22, 23), tokens[1])
self.assertEqual(Constant('3', 24, 25), tokens[2])
def test_get_tokens_logical_operators(self):
for operator in ('&&', '||'):
# 0123456
tokens = self.get_tokens('a %s b' % operator)
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Name('a', 0, 1), tokens[0])
self.assertEqual(Syntax(operator, 2, 4), tokens[1])
self.assertEqual(Name('b', 5, 6), tokens[2])
# 01234
tokens = self.get_tokens('!not')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Syntax('!', 0, 1), tokens[0])
self.assertEqual(Name('not', 1, 4), tokens[1])
def test_get_tokens_operators(self):
for operator in ('+=', '-=', '*=', '==', '!=', '/=', '%=', '^=', '|=',
'<<', '<=', '>='):
# 0123456
tokens = self.get_tokens('a %s b' % operator)
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Name('a', 0, 1), tokens[0])
self.assertEqual(Syntax(operator, 2, 4), tokens[1])
self.assertEqual(Name('b', 5, 6), tokens[2])
def test_get_tokens_ones_complement(self):
# 01234
tokens = self.get_tokens('~not')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Syntax('~', 0, 1), tokens[0])
self.assertEqual(Name('not', 1, 4), tokens[1])
def test_get_tokens_pre_increment_operators(self):
for operator in ('++', '--'):
# 012345
tokens = self.get_tokens('%sFOO' % operator)
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Syntax(operator, 0, 2), tokens[0])
self.assertEqual(Name('FOO', 2, 5), tokens[1])
# 012345
tokens = self.get_tokens('%s FOO' % operator)
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Syntax(operator, 0, 2), tokens[0])
self.assertEqual(Name('FOO', 3, 6), tokens[1])
def test_get_tokens_post_increment_operators(self):
for operator in ('++', '--'):
# 012345
tokens = self.get_tokens('FOO%s' % operator)
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Name('FOO', 0, 3), tokens[0])
self.assertEqual(Syntax(operator, 3, 5), tokens[1])
# 012345
tokens = self.get_tokens('FOO %s' % operator)
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Name('FOO', 0, 3), tokens[0])
self.assertEqual(Syntax(operator, 4, 6), tokens[1])
def test_get_tokens_semicolons(self):
# 0123456 789012
tokens = self.get_tokens(' foo;\n bar;')
self.assertEqual(4, len(tokens), tokens)
self.assertEqual(Name('foo', 2, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
self.assertEqual(Name('bar', 9, 12), tokens[2])
self.assertEqual(Syntax(';', 12, 13), tokens[3])
def test_get_tokens_pointers1(self):
# 0123456789
tokens = self.get_tokens('foo->bar;')
self.assertEqual(4, len(tokens), tokens)
self.assertEqual(Name('foo', 0, 3), tokens[0])
self.assertEqual(Syntax('->', 3, 5), tokens[1])
self.assertEqual(Name('bar', 5, 8), tokens[2])
self.assertEqual(Syntax(';', 8, 9), tokens[3])
def test_get_tokens_pointers2(self):
# 01234567890
tokens = self.get_tokens('(*foo).bar;')
self.assertEqual(7, len(tokens), tokens)
self.assertEqual(Syntax('(', 0, 1), tokens[0])
self.assertEqual(Syntax('*', 1, 2), tokens[1])
self.assertEqual(Name('foo', 2, 5), tokens[2])
self.assertEqual(Syntax(')', 5, 6), tokens[3])
self.assertEqual(Syntax('.', 6, 7), tokens[4])
self.assertEqual(Name('bar', 7, 10), tokens[5])
self.assertEqual(Syntax(';', 10, 11), tokens[6])
def test_get_tokens_block(self):
# 0123456
tokens = self.get_tokens('{ 0; }')
self.assertEqual(4, len(tokens), tokens)
self.assertEqual(Syntax('{', 0, 1), tokens[0])
self.assertEqual(Constant('0', 2, 3), tokens[1])
self.assertEqual(Syntax(';', 3, 4), tokens[2])
self.assertEqual(Syntax('}', 5, 6), tokens[3])
def test_get_tokens_bit_fields(self):
# 012345678901234567
tokens = self.get_tokens('unsigned foo : 1;')
self.assertEqual(5, len(tokens), tokens)
self.assertEqual(Name('unsigned', 0, 8), tokens[0])
self.assertEqual(Name('foo', 9, 12), tokens[1])
self.assertEqual(Syntax(':', 13, 14), tokens[2])
self.assertEqual(Constant('1', 15, 16), tokens[3])
self.assertEqual(Syntax(';', 16, 17), tokens[4])
def test_get_tokens_assignment(self):
# 012345678901234567
tokens = self.get_tokens('unsigned foo = 1;')
self.assertEqual(5, len(tokens), tokens)
self.assertEqual(Name('unsigned', 0, 8), tokens[0])
self.assertEqual(Name('foo', 9, 12), tokens[1])
self.assertEqual(Syntax('=', 13, 14), tokens[2])
self.assertEqual(Constant('1', 15, 16), tokens[3])
self.assertEqual(Syntax(';', 16, 17), tokens[4])
# 012345678901234 5678
tokens = self.get_tokens('unsigned foo =\n 1;')
self.assertEqual(5, len(tokens), tokens)
self.assertEqual(Name('unsigned', 0, 8), tokens[0])
self.assertEqual(Name('foo', 9, 12), tokens[1])
self.assertEqual(Syntax('=', 13, 14), tokens[2])
self.assertEqual(Constant('1', 16, 17), tokens[3])
self.assertEqual(Syntax(';', 17, 18), tokens[4])
# 012345678901234 5 6789
tokens = self.get_tokens('unsigned foo =\\\n 1;')
self.assertEqual(5, len(tokens), tokens)
self.assertEqual(Name('unsigned', 0, 8), tokens[0])
self.assertEqual(Name('foo', 9, 12), tokens[1])
self.assertEqual(Syntax('=', 13, 14), tokens[2])
self.assertEqual(Constant('1', 17, 18), tokens[3])
self.assertEqual(Syntax(';', 18, 19), tokens[4])
def test_get_tokens_int_constants(self):
# 01234
tokens = self.get_tokens('123;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('123', 0, 3), tokens[0])
self.assertEqual(Syntax(';', 3, 4), tokens[1])
for suffix in ('l', 'u', 'ul', 'll', 'ull'):
# 0123456
tokens = self.get_tokens('123%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '123%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
suffix = suffix.upper()
# 0123456
tokens = self.get_tokens('123%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '123%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
def test_get_tokens_octal_constants(self):
# 0123456789
tokens = self.get_tokens('01234567;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('01234567', 0, 8), tokens[0])
self.assertEqual(Syntax(';', 8, 9), tokens[1])
for suffix in ('l', 'u', 'ul', 'll', 'ull'):
# 012345678901
tokens = self.get_tokens('01234567%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '01234567%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
suffix = suffix.upper()
# 012345678901
tokens = self.get_tokens('01234567%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '01234567%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
def test_get_tokens_hex_constants(self):
# 012345678901
tokens = self.get_tokens('0xDeadBEEF;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('0xDeadBEEF', 0, 10), tokens[0])
self.assertEqual(Syntax(';', 10, 11), tokens[1])
for suffix in ('l', 'u', 'ul', 'll', 'ull'):
# 0123456789
tokens = self.get_tokens('0xBEEF%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '0xBEEF%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
suffix = suffix.upper()
# 0123456789
tokens = self.get_tokens('0xBEEF%s;' % suffix)
self.assertEqual(2, len(tokens), tokens)
value = '0xBEEF%s' % suffix
size = len(value)
self.assertEqual(Constant(value, 0, size), tokens[0])
self.assertEqual(Syntax(';', size, size + 1), tokens[1])
def test_get_tokens_float_constants(self):
# 012345678901
tokens = self.get_tokens('3.14;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14', 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14E;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14E', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14e;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14e', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('.14;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('.14', 0, 3), tokens[0])
self.assertEqual(Syntax(';', 3, 4), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14e+10;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14e+10', 0, 8), tokens[0])
self.assertEqual(Syntax(';', 8, 9), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14e-10;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14e-10', 0, 8), tokens[0])
self.assertEqual(Syntax(';', 8, 9), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14f;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14f', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14l;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14l', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14F;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14F', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('3.14L;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('3.14L', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 012345678901
tokens = self.get_tokens('.14f;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('.14f', 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens('.14l;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('.14l', 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens('.14F;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('.14F', 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens('.14L;')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('.14L', 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
def test_get_tokens_char_constants(self):
# 012345678901
tokens = self.get_tokens("'5';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant("'5'", 0, 3), tokens[0])
self.assertEqual(Syntax(';', 3, 4), tokens[1])
# 012345678901
tokens = self.get_tokens("u'5';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant("u'5'", 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens("U'5';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant("U'5'", 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens("L'5';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant("L'5'", 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 012345678901
tokens = self.get_tokens(r"'\005';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r"'\005'", 0, 6), tokens[0])
self.assertEqual(Syntax(';', 6, 7), tokens[1])
# 012345678901
tokens = self.get_tokens(r"'\\';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r"'\\'", 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 01 2345678901
tokens = self.get_tokens(r"'\'';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r"'\''", 0, 4), tokens[0])
self.assertEqual(Syntax(';', 4, 5), tokens[1])
# 01 2345678901
tokens = self.get_tokens(r"U'\'';")
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r"U'\''", 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
def test_get_tokens_string_constants(self):
# 0123456
tokens = self.get_tokens('"str";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('"str"', 0, 5), tokens[0])
self.assertEqual(Syntax(';', 5, 6), tokens[1])
# 01234567
tokens = self.get_tokens('u"str";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('u"str"', 0, 6), tokens[0])
self.assertEqual(Syntax(';', 6, 7), tokens[1])
# 01234567
tokens = self.get_tokens('U"str";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('U"str"', 0, 6), tokens[0])
self.assertEqual(Syntax(';', 6, 7), tokens[1])
# 012345678
tokens = self.get_tokens('u8"str";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant('u8"str"', 0, 7), tokens[0])
self.assertEqual(Syntax(';', 7, 8), tokens[1])
# 01234567890
tokens = self.get_tokens(r'"s\"t\"r";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r'"s\"t\"r"', 0, 9), tokens[0])
self.assertEqual(Syntax(';', 9, 10), tokens[1])
# 012345678
tokens = self.get_tokens(r'"str\\";')
self.assertEqual(2, len(tokens), tokens)
self.assertEqual(Constant(r'"str\\"', 0, 7), tokens[0])
self.assertEqual(Syntax(';', 7, 8), tokens[1])
def test_get_tokens_ternary_operator(self):
# 012345678901234567
tokens = self.get_tokens('cond ? foo : bar;')
self.assertEqual(6, len(tokens), tokens)
self.assertEqual(Name('cond', 0, 4), tokens[0])
self.assertEqual(Syntax('?', 5, 6), tokens[1])
self.assertEqual(Name('foo', 7, 10), tokens[2])
self.assertEqual(Syntax(':', 11, 12), tokens[3])
self.assertEqual(Name('bar', 13, 16), tokens[4])
self.assertEqual(Syntax(';', 16, 17), tokens[5])
def test_get_tokens_identifier(self):
# 0123456
tokens = self.get_tokens('U elt;')
self.assertEqual(3, len(tokens), tokens)
self.assertEqual(Name('U', 0, 1), tokens[0])
self.assertEqual(Name('elt', 2, 5), tokens[1])
self.assertEqual(Syntax(';', 5, 6), tokens[2])
# TODO(nnorwitz): test all the following
# Augmented assignments (lots)
# []
# () and function calls
# comma operator
# identifiers (e.g., _). what to do about dollar signs?
if __name__ == '__main__':
unittest.main()