rope_py3k / ropetest / refactor / patchedasttest.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import unittest

from rope.base import ast
from rope.refactor import patchedast
from ropetest import testutils


class PatchedASTTest(unittest.TestCase):

    def setUp(self):
        super(PatchedASTTest, self).setUp()

    def tearDown(self):
        super(PatchedASTTest, self).tearDown()

    def test_integer_literals_and_region(self):
        source = 'a = 10\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.index('10')
        checker.check_region('Num', start, start + 2)

    def test_integer_literals_and_sorted_children(self):
        source = 'a = 10\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.index('10')
        checker.check_children('Num', ['10'])

    def test_ass_name_node(self):
        source = 'a = 10\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.index('a')
        checker.check_region('Name', start, start + 1)
        checker.check_children('Name', ['a'])

    def test_assign_node(self):
        source = 'a = 10\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.index('a')
        checker.check_region('Assign', 0, len(source) - 1)
        checker.check_children(
            'Assign', ['Name', ' ', '=', ' ', 'Num'])

    def test_add_node(self):
        source = '1 + 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' ', 'Num'])

    def test_lshift_node(self):
        source = '1 << 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['Num', ' ', '<<', ' ', 'Num'])

    def test_and_node(self):
        source = 'True and True\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BoolOp', 0, len(source) - 1)
        checker.check_children(
            'BoolOp', ['Name', ' ', 'and', ' ', 'Name'])

    def test_basic_closing_parens(self):
        source = '1 + (2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' (', 'Num', ')'])

    def test_basic_opening_parens(self):
        source = '(1) + 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['(', 'Num', ') ', '+', ' ', 'Num'])

    def test_basic_opening_biway(self):
        source = '(1) + (2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['(', 'Num', ') ', '+', ' (', 'Num', ')'])

    def test_basic_opening_double(self):
        source = '1 + ((2))\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' ((', 'Num', '))'])

    def test_handling_comments(self):
        source = '(1 + #(\n2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' #(\n', 'Num'])

    def test_handling_parens_with_spaces(self):
        source = '1 + (2\n    )\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' (', 'Num', '\n    )'])

    def test_handling_strings(self):
        source = '1 + "("\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num', ' ', '+', ' ', 'Str'])

    def test_handling_implicit_string_concatenation(self):
        source = "a = '1''2'"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Assign', ['Name' , ' ', '=', ' ', 'Str'])
        checker.check_children('Str', ["'1''2'"])

    def test_handling_implicit_string_concatenation_line_breaks(self):
        source = "a = '1' \\\n'2'"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Assign', ['Name' , ' ', '=', ' ', 'Str'])
        checker.check_children('Str', ["'1' \\\n'2'"])

    def test_handling_explicit_string_concatenation_line_breaks(self):
        source = "a = ('1' \n'2')"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Assign', ['Name' , ' ', '=', ' (', 'Str', ')'])
        checker.check_children('Str', ["'1' \n'2'"])

    def test_not_concatenating_strings_on_separate_lines(self):
        source = "'1'\n'2'\n"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Module', ['', 'Expr', '\n', 'Expr', '\n'])

    def test_long_integer_literals(self):
        source = "0x1L + a"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num' , ' ', '+', ' ', 'Name'])
        checker.check_children('Num', ['0x1L'])

    def test_complex_number_literals(self):
        source = "1.0e2j + a"
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num' , ' ', '+', ' ', 'Name'])
        checker.check_children('Num', ['1.0e2j'])

    def test_ass_attr_node(self):
        source = 'a.b = 1\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Attribute', 0, source.index('=') - 1)
        checker.check_children('Attribute', ['Name', '', '.', '', 'b'])

    def test_ass_list_node(self):
        source = '[a, b] = 1, 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('List', 0, source.index(']') + 1)
        checker.check_children('List', ['[', '', 'Name', '', ',',
                                        ' ', 'Name', '', ']'])

    def test_ass_tuple(self):
        source = 'a, b = range(2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Tuple', 0, source.index('=') - 1)
        checker.check_children(
            'Tuple', ['Name', '', ',', ' ', 'Name'])

    def test_ass_tuple2(self):
        source = '(a, b) = range(2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Tuple', 0, source.index('=') - 1)
        checker.check_children(
            'Tuple', ['(', '', 'Name', '', ',', ' ', 'Name', '', ')'])

    def test_assert(self):
        source = 'assert True\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Assert', 0, len(source) - 1)
        checker.check_children(
            'Assert', ['assert', ' ', 'Name'])

    def test_assert2(self):
        source = 'assert True, "error"\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Assert', 0, len(source) - 1)
        checker.check_children(
            'Assert', ['assert', ' ', 'Name', '', ',', ' ', 'Str'])

    def test_aug_assign_node(self):
        source = 'a += 1\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.index('a')
        checker.check_region('AugAssign', 0, len(source) - 1)
        checker.check_children(
            'AugAssign', ['Name', ' ', '+', '', '=', ' ', 'Num'])

    def test_back_quotenode(self):
        source = '`1`\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Repr', 0, len(source) - 1)
        checker.check_children(
            'Repr', ['`', '', 'Num', '', '`'])

    def test_bitand(self):
        source = '1 & 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children(
            'BinOp', ['Num', ' ', '&', ' ', 'Num'])

    def test_bitor(self):
        source = '1 | 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'BinOp', ['Num', ' ', '|', ' ', 'Num'])

    def test_call_func(self):
        source = 'f(1, 2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Call', 0, len(source) - 1)
        checker.check_children(
            'Call', ['Name', '', '(', '', 'Num', '', ',',
                     ' ', 'Num', '', ')'])

    def test_call_func_and_keywords(self):
        source = 'f(1, p=2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Call', ['Name', '', '(', '', 'Num', '', ',',
                     ' ', 'keyword', '', ')'])

    def test_call_func_and_start_args(self):
        source = 'f(1, *args)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Call', ['Name', '', '(', '', 'Num', '', ',',
                     ' ', '*', '', 'Name', '', ')'])

    def test_call_func_and_only_dstart_args(self):
        source = 'f(**kwds)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Call', ['Name', '', '(', '', '**', '', 'Name', '', ')'])

    def test_call_func_and_both_varargs_and_kwargs(self):
        source = 'f(*args, **kwds)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Call', ['Name', '', '(', '', '*', '', 'Name', '', ',',
                     ' ', '**', '', 'Name', '', ')'])

    def test_class_node(self):
        source = 'class A(object):\n    """class docs"""\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Class', 0, len(source) - 1)
        checker.check_children(
            'Class', ['class', ' ', 'A', '', '(', '', 'Name', '', ')',
                      '', ':', '\n    ', 'Expr', '\n    ', 'Pass'])

    def test_class_with_no_bases(self):
        source = 'class A:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Class', 0, len(source) - 1)
        checker.check_children(
            'Class', ['class', ' ', 'A', '', ':', '\n    ', 'Pass'])

    def test_simple_compare(self):
        source = '1 < 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Compare', 0, len(source) - 1)
        checker.check_children(
            'Compare', ['Num', ' ', '<', ' ', 'Num'])

    def test_multiple_compare(self):
        source = '1 < 2 <= 3\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Compare', 0, len(source) - 1)
        checker.check_children(
            'Compare', ['Num', ' ', '<', ' ', 'Num', ' ',
                        '<=', ' ', 'Num'])

    def test_decorators_node(self):
        source = '@d\ndef f():\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('FunctionDef', 0, len(source) - 1)
        checker.check_children(
            'FunctionDef',
            ['@', '', 'Name', '\n', 'def', ' ', 'f', '', '(', '', 'arguments',
             '', ')', '', ':', '\n    ', 'Pass'])

    @testutils.only_for('2.6')
    def test_decorators_for_classes(self):
        source = '@d\nclass C(object):\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('ClassDef', 0, len(source) - 1)
        checker.check_children(
            'ClassDef',
            ['@', '', 'Name', '\n', 'class', ' ', 'C', '', '(', '', 'Name',
             '', ')', '', ':', '\n    ', 'Pass'])

    def test_both_varargs_and_kwargs(self):
        source = 'def f(*args, **kwds):\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'arguments', ['*', '', 'args' , '', ',' , ' ', '**', '', 'kwds'])

    def test_function_node(self):
        source = 'def f():\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Function', 0, len(source) - 1)
        checker.check_children('Function', ['def', ' ', 'f', '', '(', '', 'arguments', '',
                                            ')', '', ':', '\n    ', 'Pass'])

    def test_function_node2(self):
        source = 'def f(p1, **p2):\n    """docs"""\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Function', 0, len(source) - 1)
        checker.check_children(
            'Function', ['def', ' ', 'f', '', '(', '', 'arguments',
                         '', ')' , '', ':', '\n    ', 'Expr', '\n    ', 'Pass'])
        checker.check_children(
            'arguments', ['Name', '', ',',
                          ' ', '**', '', 'p2'])

    def test_function_node_and_tuple_parameters(self):
        source = 'def f(a, (b, c)):\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Function', 0, len(source) - 1)
        checker.check_children(
            'Function', ['def', ' ', 'f', '', '(', '', 'arguments',
                         '', ')' , '', ':', '\n    ', 'Pass'])
        checker.check_children(
            'arguments', ['Name', '', ',', ' ', 'Tuple'])

    def test_dict_node(self):
        source = '{1: 2, 3: 4}\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Dict', 0, len(source) - 1)
        checker.check_children(
            'Dict', ['{', '', 'Num', '', ':', ' ', 'Num', '', ',',
                     ' ', 'Num', '', ':', ' ', 'Num', '', '}'])

    def test_div_node(self):
        source = '1 / 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('BinOp', 0, len(source) - 1)
        checker.check_children('BinOp', ['Num', ' ', '/', ' ', 'Num'])

    def test_simple_exec_node(self):
        source = 'exec ""\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Exec', 0, len(source) - 1)
        checker.check_children('Exec', ['exec', ' ', 'Str'])

    def test_exec_node(self):
        source = 'exec "" in locals(), globals()\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Exec', 0, len(source) - 1)
        checker.check_children(
            'Exec', ['exec', ' ', 'Str', ' ', 'in',
                     ' ', 'Call', '', ',', ' ', 'Call'])

    def test_for_node(self):
        source = 'for i in range(1):\n    pass\nelse:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('For', 0, len(source) - 1)
        checker.check_children(
            'For', ['for', ' ', 'Name', ' ', 'in', ' ', 'Call', '',
                    ':', '\n    ', 'Pass', '\n',
                    'else', '', ':', '\n    ', 'Pass'])

    def test_normal_from_node(self):
        source = 'from x import y\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('ImportFrom', 0, len(source) - 1)
        checker.check_children(
            'ImportFrom', ['from', ' ', 'x', ' ', 'import', ' ', 'alias'])
        checker.check_children('alias', ['y'])

    @testutils.run_only_for_25
    def test_from_node(self):
        source = 'from ..x import y as z\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('ImportFrom', 0, len(source) - 1)
        checker.check_children(
            'ImportFrom', ['from', ' ', '..', '', 'x', ' ',
                           'import', ' ', 'alias'])
        checker.check_children('alias', ['y', ' ', 'as', ' ', 'z'])

    def test_simple_gen_expr_node(self):
        source = 'zip(i for i in x)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('GeneratorExp', 4, len(source) - 2)
        checker.check_children(
            'GeneratorExp', ['Name', ' ', 'comprehension'])
        checker.check_children(
            'comprehension', ['for', ' ', 'Name', ' ', 'in', ' ', 'Name'])

    def test_gen_expr_node_handling_surrounding_parens(self):
        source = '(i for i in x)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('GeneratorExp', 0, len(source) - 1)
        checker.check_children(
            'GeneratorExp', ['(', '', 'Name', ' ', 'comprehension', '', ')'])

    def test_gen_expr_node2(self):
        source = 'zip(i for i in range(1) if i == 1)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'comprehension', ['for', ' ', 'Name', ' ', 'in', ' ', 'Call',
                              ' ', 'if', ' ', 'Compare'])

    def test_get_attr_node(self):
        source = 'a.b\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Attribute', 0, len(source) - 1)
        checker.check_children('Attribute', ['Name', '', '.', '', 'b'])

    def test_global_node(self):
        source = 'global a, b\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Global', 0, len(source) - 1)
        checker.check_children('Global', ['global', ' ', 'a', '', ',', ' ', 'b'])

    def test_if_node(self):
        source = 'if True:\n    pass\nelse:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('If', 0, len(source) - 1)
        checker.check_children(
            'If', ['if', ' ', 'Name', '', ':', '\n    ', 'Pass', '\n',
                   'else', '', ':', '\n    ', 'Pass'])

    def test_if_node2(self):
        source = 'if True:\n    pass\nelif False:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('If', 0, len(source) - 1)
        checker.check_children(
            'If', ['if', ' ', 'Name', '', ':', '\n    ', 'Pass', '\n',
                   'If'])

    def test_if_node3(self):
        source = 'if True:\n    pass\nelse:\n' \
                 '    if True:\n        pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('If', 0, len(source) - 1)
        checker.check_children(
            'If', ['if', ' ', 'Name', '', ':', '\n    ', 'Pass', '\n',
                   'else', '', ':', '\n    ', 'If'])

    def test_import_node(self):
        source = 'import a, b as c\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Import', 0, len(source) - 1)
        checker.check_children(
            'Import', ['import', ' ', 'alias', '', ',', ' ', 'alias'])

    def test_lambda_node(self):
        source = 'lambda a, b=1, *z: None\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Lambda', 0, len(source) - 1)
        checker.check_children(
            'Lambda', ['lambda', ' ', 'arguments', '', ':', ' ', 'Name'])
        checker.check_children(
            'arguments', ['Name', '', ',', ' ', 'Name', '', '=', '',
                          'Num', '', ',', ' ', '*', '', 'z'])

    def test_list_node(self):
        source = '[1, 2]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('List', 0, len(source) - 1)
        checker.check_children(
            'List', ['[', '', 'Num', '', ',', ' ', 'Num', '', ']'])

    def test_list_comp_node(self):
        source = '[i for i in range(1) if True]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('ListComp', 0, len(source) - 1)
        checker.check_children(
            'ListComp', ['[', '', 'Name', ' ', 'comprehension', '', ']'])
        checker.check_children(
            'comprehension', ['for', ' ', 'Name', ' ', 'in', ' ',
                              'Call', ' ', 'if', ' ', 'Name'])

    def test_list_comp_node_with_multiple_comprehensions(self):
        source = '[i for i in range(1) for j in range(1) if True]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('ListComp', 0, len(source) - 1)
        checker.check_children(
            'ListComp', ['[', '', 'Name', ' ', 'comprehension',
                         ' ', 'comprehension', '', ']'])
        checker.check_children(
            'comprehension', ['for', ' ', 'Name', ' ', 'in', ' ',
                              'Call', ' ', 'if', ' ', 'Name'])

    def test_simple_module_node(self):
        source = 'pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Module', 0, len(source))
        checker.check_children('Module', ['', 'Pass', '\n'])

    def test_module_node(self):
        source = '"""docs"""\npass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Module', 0, len(source))
        checker.check_children('Module', ['', 'Expr', '\n', 'Pass', '\n'])
        checker.check_children('Str', ['"""docs"""'])

    def test_not_and_or_nodes(self):
        source = 'not True or False\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Expr', ['BoolOp'])
        checker.check_children('BoolOp', ['UnaryOp', ' ', 'or', ' ', 'Name'])

    def test_print_node(self):
        source = 'print >>out, 1,\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Print', 0, len(source) - 1)
        checker.check_children('Print', ['print', ' ', '>>', '', 'Name', '',
                                         ',', ' ', 'Num', '', ','])

    def test_printnl_node(self):
        source = 'print 1\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Print', 0, len(source) - 1)
        checker.check_children('Print', ['print', ' ', 'Num'])

    def test_raise_node(self):
        source = 'raise x, y, z\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_region('Raise', 0, len(source) - 1)
        checker.check_children(
            'Raise', ['raise', ' ', 'Name', '', ',', ' ', 'Name', '', ',',
                      ' ', 'Name'])

    def test_return_node(self):
        source = 'def f():\n    return None\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Return', ['return', ' ', 'Name'])

    def test_empty_return_node(self):
        source = 'def f():\n    return\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Return', ['return'])

    def test_simple_slice_node(self):
        source = 'a[1:2]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Subscript', ['Name', '', '[', '', 'Slice', '', ']'])
        checker.check_children(
            'Slice', ['Num', '', ':', '', 'Num'])

    def test_slice_node2(self):
        source = 'a[:]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Subscript', ['Name', '', '[', '', 'Slice', '', ']'])
        checker.check_children('Slice', [':'])

    def test_simple_subscript(self):
        source = 'a[1]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Subscript', ['Name', '', '[', '', 'Index', '', ']'])
        checker.check_children('Index', ['Num'])

    def test_tuple_node(self):
        source = '(1, 2)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Tuple', ['(', '', 'Num', '', ',', ' ', 'Num', '', ')'])

    def test_tuple_node2(self):
        source = '#(\n1, 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Tuple', ['Num', '', ',', ' ', 'Num'])

    def test_one_item_tuple_node(self):
        source = '(1,)\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Tuple', ['(', '', 'Num', ',', ')'])

    def test_empty_tuple_node(self):
        source = '()\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Tuple', ['(', '', ')'])

    def test_yield_node(self):
        source = 'def f():\n    yield None\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children('Yield', ['yield', ' ', 'Name'])

    def test_while_node(self):
        source = 'while True:\n    pass\nelse:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'While', ['while', ' ', 'Name', '', ':', '\n    ', 'Pass', '\n',
                      'else', '', ':', '\n    ', 'Pass'])

    @testutils.run_only_for_25
    def test_with_node(self):
        source = 'from __future__ import with_statement\nwith a as b:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'With', ['with', ' ', 'Name', ' ', 'as', ' ', 'Name', '', ':',
                     '\n    ', 'Pass'])

    def test_try_finally_node(self):
        source = 'try:\n    pass\nfinally:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'TryFinally', ['try', '', ':', '\n    ', 'Pass', '\n', 'finally',
                           '', ':', '\n    ', 'Pass'])

    def test_try_except_node(self):
        source = 'try:\n    pass\nexcept Exception, e:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'TryExcept', ['try', '', ':', '\n    ', 'Pass', '\n',
                          ('excepthandler', 'ExceptHandler')])
        checker.check_children(
            ('excepthandler', 'ExceptHandler'),
            ['except', ' ', 'Name', '', ',', ' ', 'Name', '', ':',
             '\n    ', 'Pass'])

    @testutils.run_only_for_25
    def test_try_except_and_finally_node(self):
        source = 'try:\n    pass\nexcept:\n    pass\nfinally:\n    pass\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'TryFinally', ['TryExcept', '\n', 'finally',
                           '', ':', '\n    ', 'Pass'])

    def test_ignoring_comments(self):
        source = '#1\n1\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        start = source.rindex('1')
        checker.check_region('Num', start, start + 1)

    def test_simple_sliceobj(self):
        source = 'a[1::3]\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Slice', ['Num', '', ':', '', ':', '', 'Num'])

    def test_ignoring_strings_that_start_with_a_char(self):
        source = 'r"""("""\n1\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Module', ['', 'Expr', '\n', 'Expr', '\n'])

    def test_how_to_handle_old_not_equals(self):
        source = '1 <> 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Compare', ['Num', ' ', '<>', ' ', 'Num'])

    def test_semicolon(self):
        source = '1;\n'
        ast = patchedast.get_patched_ast(source, True)

    @testutils.run_only_for_25
    def test_if_exp_node(self):
        source = '1 if True else 2\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'IfExp', ['Num', ' ', 'if', ' ', 'Name', ' ', 'else',
                      ' ', 'Num'])

    def test_delete_node(self):
        source = 'del a, b\n'
        ast = patchedast.get_patched_ast(source, True)
        checker = _ResultChecker(self, ast)
        checker.check_children(
            'Delete', ['del', ' ', 'Name', '', ',', ' ', 'Name'])


class _ResultChecker(object):

    def __init__(self, test_case, ast):
        self.test_case = test_case
        self.ast = ast

    def check_region(self, text, start, end):
        node = self._find_node(text)
        if node is None:
            self.test_case.fail('Node <%s> cannot be found' % text)
        self.test_case.assertEquals((start, end), node.region)

    def _find_node(self, text):
        goal = text
        if not isinstance(text, (tuple, list)):
            goal = [text]
        class Search(object):
            result = None
            def __call__(self, node):
                for text in goal:
                    if str(node).startswith(text):
                        self.result = node
                        break
                    if node.__class__.__name__.startswith(text):
                        self.result = node
                        break
                return self.result is not None
        search = Search()
        ast.call_for_nodes(self.ast, search, recursive=True)
        return search.result

    def check_children(self, text, children):
        node = self._find_node(text)
        if node is None:
            self.test_case.fail('Node <%s> cannot be found' % text)
        result = list(node.sorted_children)
        self.test_case.assertEquals(len(children), len(result))
        for expected, child in zip(children, result):
            goals = expected
            if not isinstance(expected, (tuple, list)):
                goals = [expected]
            for goal in goals:
                if goal == '' or isinstance(child, basestring):
                    self.test_case.assertEquals(goal, child)
                    break
            else:
                self.test_case.assertNotEquals(
                    '', text, 'probably ignoring some node')
                self.test_case.assertTrue(
                    child.__class__.__name__.startswith(expected),
                    msg='Expected <%s> but was <%s>' %
                    (expected, child.__class__.__name__))


if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.