File: test_parser.py

package info (click to toggle)
xdoctest 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,512 kB
  • sloc: python: 10,963; sh: 815; cpp: 33; makefile: 19
file content (759 lines) | stat: -rw-r--r-- 19,575 bytes parent folder | download
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
import pytest
from xdoctest import parser
from xdoctest import utils
from xdoctest import exceptions


def test_final_eval_exec():
    """
    Ensure that if the line before a want is able to be evaled, it is so we can
    compare its value to the want value.

    CommandLine:
        xdoctest -m ~/code/xdoctest/tests/test_parser.py test_final_eval_exec
    """
    string = utils.codeblock(
        '''
        >>> x = 2
        >>> x + 1
        1
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    DEBUG = 0
    if DEBUG:
        print(string)
        print([p.compile_mode for p in parts])
    assert [p.compile_mode for p in parts] == ['exec', 'eval']

    string = utils.codeblock(
        '''
        >>> x = 2
        >>> x + 1
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    if DEBUG:
        print(string)
        print([p.compile_mode for p in parts])
    assert [p.compile_mode for p in parts] == ['exec']

    string = utils.codeblock(
        r'''
        >>> x = 2
        >>> x += 3
        >>> """
        ... foobar
        ... """
        '\nfoobar\n'
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    if DEBUG:
        print(string)
        print([p.compile_mode for p in parts])
    assert [p.compile_mode for p in parts] == ['exec', 'single']

    string = utils.codeblock(
        r'''
        >>> i = 0
        >>> 0 / i
        2
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    if DEBUG:
        print(string)
        print([p.compile_mode for p in parts])
    assert [p.compile_mode for p in parts] == ['exec', 'eval']

    string = utils.codeblock(
        r'''
        >>> if True:
        ...     2
        2
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    if DEBUG:
        print(string)
        print([p.compile_mode for p in parts])
    assert [p.compile_mode for p in parts] == ['single']


def test_compile_mode_print():
    string = utils.codeblock(
        r'''
        >>> x = 2
        >>> x += 3
        >>> print('foo')
        foo
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    assert [p.compile_mode for p in parts] == ['exec', 'eval']


def test_label_lines():
    string = utils.codeblock(
        r'''
        >>> i = 0
        >>> 0 / i
        2
        ''')
    self = parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    assert labeled == [
        ('dsrc', '>>> i = 0'),
        ('dsrc', '>>> 0 / i'),
        ('want', '2')
    ]


def test_label_indented_lines():
    string = '''
            text
            >>> dsrc1()
            want

                >>> dsrc2()
                >>> cont(
                ... a=b)
                ... dsrc
                >>> dsrc3():
                ...     a
                ...     b = """
                        multiline
                        """
                want

            text
            ... still text
            >>> "now its a doctest"

            text
    '''
    self = parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    # import sys
    # print('EXIT NOW')
    # sys.exit(1)
    expected = [
        ('text', ''),
        ('text', '            text'),
        ('dsrc', '            >>> dsrc1()'),
        ('want', '            want'),
        ('text', ''),
        ('dsrc', '                >>> dsrc2()'),
        ('dsrc', '                >>> cont('),
        ('dcnt', '                ... a=b)'),
        ('dcnt', '                ... dsrc'),
        ('dsrc', '                >>> dsrc3():'),
        ('dcnt', '                ...     a'),
        ('dcnt', '                ...     b = """'),
        ('dcnt', '                        multiline'),
        ('dcnt', '                        """'),
        ('want', '                want'),
        ('text', ''),
        ('text', '            text'),
        ('text', '            ... still text'),
        ('dsrc', '            >>> "now its a doctest"'),
        ('text', ''),
        ('text', '            text'),
        ('text', '    '),    # FIXME: weird that this space has an indent
    ]
    if labeled != expected:
        try:
            import itertools as it
            for got, want in it.zip_longest(labeled, expected):
                if got != want:
                    print(utils.color_text('GOT  = {!r}'.format(got), 'red'))
                    print(utils.color_text('WANT = {!r}'.format(want), 'blue'))
                else:
                    print('PASS = {!r}'.format(got))
        except ImportError:
            pass
        raise AssertionError
    assert labeled == expected


def test_ps1_linenos_1():
    """
    Test we can find the line numbers for every "evaluatable" statement
    """
    source_lines = utils.codeblock(
        '''
        >>> x = 2
        >>> x + 1
        1
        ''').split('\n')[:-1]
    self = parser.DoctestParser()
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    assert mode_hint == 'eval'
    assert linenos == [0, 1]


def test_ps1_linenos_2():
    source_lines = utils.codeblock(
        '''
        >>> x = """
            x = 2
            """
        >>> print(x.strip() + '1')
        x = 21
        ''').split('\n')[:-1]
    self = parser.DoctestParser()
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    assert mode_hint == 'eval'
    assert linenos == [0, 3]


def test_ps1_linenos_3():
    source_lines = utils.codeblock(
        '''
        >>> x = """
            x = 2
            """
        >>> y = (x.strip() + '1')
        'x = 21'
        ''').split('\n')[:-1]
    self = parser.DoctestParser()
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    assert mode_hint == 'exec'
    assert linenos == [0, 3]


def test_ps1_linenos_4():
    source_lines = utils.codeblock(
        '''
        >>> x = """
            x = 2
            """
        >>> def foo():
        ...     return 5
        >>> ms1 = """
        ... multistring2
        ... multistring2
        ... """
        >>> ms2 = """
        ... multistring2
        ... multistring2
        ... """
        >>> x = sum([
        >>>     foo()
        >>> ])
        >>> y = len(ms1) + len(ms2)
        >>> z = (
        >>>     x + y
        >>> )
        >>> z
        59
        ''').split('\n')[:-1]
    self = parser.DoctestParser()
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    assert mode_hint == 'eval'
    assert linenos == [0, 3, 5, 9, 13, 16, 17, 20]


def test_retain_source():
    """
    """
    source = utils.codeblock(
        '''
        >>> x = 2
        >>> print("foo")
        foo
        ''')
    source_lines = source.split('\n')[:-1]
    self = parser.DoctestParser()
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    assert mode_hint == 'eval'
    assert linenos == [0, 1]
    p1, p2 = self.parse(source)
    assert p1.source == 'x = 2'
    assert p2.source == 'print("foo")'


def test_package_string_tup():
    """
    pytest tests/test_parser.py::test_package_string_tup
    """
    raw_source_lines = ['>>> "string"']
    raw_want_lines = ['string']
    self = parser.DoctestParser()
    parts = list(self._package_chunk(raw_source_lines, raw_want_lines))
    assert len(parts) == 1, 'should only want one string'


def test_simulate_repl():
    """
    pytest tests/test_parser.py::test_package_string_tup
    """
    string = utils.codeblock(
        '''
        >>> x = 1
        >>> x = 2
        >>> x = 3
        ''')
    self = parser.DoctestParser()
    self.simulate_repl = False
    assert len(self.parse(string)) == 1
    self.simulate_repl = True
    assert len(self.parse(string)) == 3


def test_parse_multi_want():
    string = utils.codeblock(
        '''
        >>> x = 2
        >>> x
        2
        >>> 'string'
        'string'
        >>> print('string')
        string
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)

    self._label_docsrc_lines(string)
    assert parts[2].source == "'string'"
    assert len(parts) == 4


def test_parse_eval_nowant():
    string = utils.codeblock(
        '''
        >>> a = 1
        >>> 1 / 0
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    raw_source_lines = string.split('\n')[:]
    ps1_linenos, mode_hint = self._locate_ps1_linenos(raw_source_lines)
    assert ps1_linenos == [0, 1]
    assert mode_hint == 'eval'
    # Only one part because there is no want
    assert len(parts) == 1


def test_parse_eval_single_want():
    string = utils.codeblock(
        '''
        >>> a = 1
        >>> 1 / 0
        We have a want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    raw_source_lines = string.split('\n')[:-1]
    ps1_linenos, mode_hint = self._locate_ps1_linenos(raw_source_lines)
    assert ps1_linenos == [0, 1]
    assert mode_hint == 'eval'
    # Only one part because there is no want
    assert len(parts) == 2


def test_parse_comment():
    string = utils.codeblock(
        '''
        >>> # nothing
        ''')
    self = parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    assert labeled == [('dsrc', '>>> # nothing')]
    source_lines = string.split('\n')[:]
    linenos, mode_hint = self._locate_ps1_linenos(source_lines)
    parts = self.parse(string)
    assert parts[0].source.strip().startswith('#')


def test_text_after_want():
    string = utils.codeblock('''
        Example:
            >>> dsrc()
            want
        just some test
    ''')
    self = parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    expected = [
        ('text', 'Example:'),
        ('dsrc', '    >>> dsrc()'),
        ('want', '    want'),
        ('text', 'just some test'),
    ]
    assert labeled == expected


def test_want_ellipse_with_space():
    string = utils.codeblock('''
        Example:
            >>> dsrc()
            ...
    ''')
    # Add an extra space after the ellipses to be clear what we are testing
    # and because my editor automatically removes it when I try to save the
    # file ¯\_(ツ)_/¯
    string = string + ' '
    self = parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    expected = [
        ('text', 'Example:'),
        ('dsrc', '    >>> dsrc()'),
        ('want', '    ... '),
    ]
    assert labeled == expected


def test_syntax_error():
    string = utils.codeblock('''
        Example:
            >>> 03 = dsrc()
    ''')
    self = parser.DoctestParser()
    with pytest.raises(exceptions.DoctestParseError):
        self.parse(string)


def test_nonbalanced_statement():
    """
    xdoctest ~/code/xdoctest/tests/test_parser.py test_nonbalanced_statement

    from xdoctest import static_analysis as static
    lines = ['x = [']
    static.is_balanced_statement(lines, only_tokens=True)
    """
    string = utils.codeblock(
        '''
        >>> x = [
        # ] this bracket is to make my editor happy and is does not effect the test
        ''').splitlines()[0]

    self = parser.DoctestParser()
    with pytest.raises(exceptions.DoctestParseError) as exc_info:
        self.parse(string)
    msg = exc_info.value.orig_ex.msg.lower()
    assert msg.startswith('ill-formed doctest'.lower())


def test_bad_indent():
    """
    CommandLine:
        python tests/test_parser.py test_bad_indent
    """
    string = utils.codeblock(
        '''
        Example:
            >>> x = [
        # ] bad want indent
        ''')

    self = parser.DoctestParser()
    with pytest.raises(exceptions.DoctestParseError) as exc_info:
        self.parse(string)
    msg = exc_info.value.orig_ex.msg.lower()
    assert msg.startswith('Bad indentation in doctest'.lower())


def test_part_nice_no_lineoff():
    from xdoctest import doctest_part
    self = doctest_part.DoctestPart([], [], None)
    assert str(self) == '<DoctestPart(src="", want=None)>'


def test_repl_oneline():
    string = utils.codeblock(
        '''
        >>> x = 1
        ''')
    self = parser.DoctestParser(simulate_repl=True)
    parts = self.parse(string)
    assert [p.source for p in parts] == ['x = 1']


def test_repl_twoline():
    string = utils.codeblock(
        '''
        >>> x = 1
        >>> x = 2
        ''')
    self = parser.DoctestParser(simulate_repl=True)
    parts = self.parse(string)
    assert [p.source for p in parts] == ['x = 1', 'x = 2']


def test_repl_comment_in_string():
    source_lines = ['>>> x = """', '    # comment in a string', '    """']
    self = parser.DoctestParser()
    assert self._locate_ps1_linenos(source_lines) == ([0], 'exec')

    source_lines = [
        '>>> x = """',
        '    # comment in a string',
        '    """',
        '>>> x = """',
        '    # comment in a string',
        '    """',
    ]
    self = parser.DoctestParser()
    assert self._locate_ps1_linenos(source_lines) == ([0, 3], 'exec')


def test_inline_directive():
    """
        python ~/code/xdoctest/tests/test_parser.py test_inline_directive
    """
    string = utils.codeblock(
        '''
        >>> # doctest: +SKIP
        >>> func1(*
        >>>    [i for i in range(10)])
        >>> # not a directive
        >>> func2(  # not a directive
        >>>    a=b
        >>>    )
        >>> func3()  # xdoctest: +SKIP
        >>> func4()
        want1
        >>> func5()  # xdoctest: +SKIP
        want1
        >>> # xdoctest: +SKIP
        >>> func6()
        >>> func7(a=b,
        >>>            c=d) # xdoctest: +SKIP
        >>> # xdoctest: +SKIP
        >>> func8(' # doctest: not a directive')
        >>> func9("""
                  # doctest: still not a directive
                  """)
        finalwant
        ''')
    # source_lines = string.splitlines()
    self = parser.DoctestParser()
    # [0, 1, 3, 4, 7, 8, 10, 11, 12]
    # assert ps1_linenos == [0, 2, 5, 6, 8, 9, 10]
    parts = self.parse(string)
    # assert len(parts) ==
    for part in parts:
        print('----')
        print(part.source)
        print('----')
    try:
        print(parts)
    except ImportError:
        pass
    # TODO: finish me


def test_block_directive_nowant1():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_nowant1
    """
    string = utils.codeblock(
        '''
        >>> # doctest: +SKIP
        >>> func1()
        >>> func2()
        ''')
    # source_lines = string.splitlines()
    self = parser.DoctestParser()
    parts = self.parse(string)
    print('----')
    for part in parts:
        print(part.source)
        print('----')
    try:
        print(parts)
    except ImportError:
        pass
    assert len(parts) == 1


def test_block_directive_nowant2():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_nowant
    """
    string = utils.codeblock(
        '''
        >>> # doctest: +SKIP
        >>> func1()
        >>> func2()
        >>> # doctest: +SKIP
        >>> func1()
        >>> func2()
        ''')
    # source_lines = string.splitlines()
    self = parser.DoctestParser()
    parts = self.parse(string)
    # TODO: finish me
    assert len(parts) == 2


def test_block_directive_want1_assign():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_want1
    """
    string = utils.codeblock(
        '''
        >>> # doctest: +SKIP
        >>> func1()
        >>> _ = func2()  # assign this line so we dont break it off for eval
        want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    print('----')
    for part in parts:
        print(part.source)
        print('----')
    try:
        print(parts)
    except ImportError:
        pass
    assert len(parts) == 1


def test_block_directive_want1_eval():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_want1
    """
    string = utils.codeblock(
        '''
        >>> # doctest: +SKIP
        >>> func1()
        >>> func2()  # eval this line so it is broken off
        want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    assert len(parts) == 2


def test_block_directive_want2_assign():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_want2
    """
    string = utils.codeblock(
        '''
        >>> func1()
        >>> # doctest: +SKIP
        >>> func2()
        >>> _ = func3()
        want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    assert len(parts) == 2


def test_block_directive_want2_eval():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_want2_eval
    """
    string = utils.codeblock(
        '''
        >>> func1()
        >>> # doctest: +SKIP
        >>> func2()
        >>> func3()
        want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    print('----')
    for part in parts:
        print(part.source)
        print('----')
    try:
        print(parts)
    except ImportError:
        pass
    assert len(parts) == 3


def test_block_directive_want2_eval2():
    """
        python ~/code/xdoctest/tests/test_parser.py test_block_directive_want2_eval
    """
    string = utils.codeblock(
        '''
        >>> func1()
        >>> func1()
        >>> # doctest: +SKIP
        >>> func2()
        >>> func2()
        >>> # doctest: +SKIP
        >>> func3()
        >>> func3()
        >>> func3()
        >>> func4()
        want
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    assert len(parts) == 4


def test_gh_issue_25_parsing_failure():
    string = utils.codeblock(
        '''
        >>> _, o = 0, 1
        >>> A = B = C = D = 1
        >>> cc_mask = [        # Y
        >>>     [ _, _, _, o, _, _, ],  # 0
        >>>     [ _, _, o, o, o, _, ],  # 1
        >>>     [ _, o, o, o, o, o, ],  # 2
        >>>     [ o, o, o, o, o, _, ],  # 3
        >>>     [ _, o, o, o, _, _, ],  # 4
        >>>     [ _, _, o, o, _, _, ],  # 5
        >>> # X:  0  1  2  3  4  5  6
        >>> ]
        >>> # a regular comment
        >>> print(cc_mask)
        ''')
    source_lines = string.splitlines()
    self = parser.DoctestParser()
    ps1_linenos = self._locate_ps1_linenos(source_lines)[0]
    assert ps1_linenos ==  [0, 1, 2, 11, 12]
    parts = self.parse(string)
    assert len(parts) == 1


def test_parser_with_type_annot():
    string = utils.codeblock(
        '''
        >>> def foo(x: str) -> None:
        >>>     ...
        ''')
    source_lines = string.splitlines()
    self = parser.DoctestParser()
    ps1_linenos = self._locate_ps1_linenos(source_lines)[0]
    assert ps1_linenos ==  [0]
    parts = self.parse(string)
    assert len(parts) == 1


def test_parse_tabs():
    tab = '\t'
    string = utils.codeblock(
        f'''
        >>> text = "tab{tab}sep{tab}val"
        ''')
    self = parser.DoctestParser()
    parts = self.parse(string)
    doctest_part = parts[0]
    assert tab in doctest_part.source


if __name__ == '__main__':
    """
    CommandLine:
        python ~/code/xdoctest/tests/test_parser.py --help
        python ~/code/xdoctest/tests/test_parser.py test_inline_directive
        python ~/code/xdoctest/tests/test_parser.py zero-all
        python ~/code/xdoctest/tests/test_parser.py test_gh_issue_25_parsing_failure
        pytest tests/test_parser.py -vv
    """
    import xdoctest
    xdoctest.doctest_module(__file__)