How to use the xdoctest.doctest_parser.DoctestParser function in xdoctest

To help you get started, we’ve selected a few xdoctest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_ps1_linenos_3():
    source_lines = ub.codeblock(
        '''
        >>> x = """
            x = 2
            """
        >>> y = (x.strip() + '1')
        'x = 21'
        ''').split('\n')[:-1]
    self = doctest_parser.DoctestParser()
    linenos, eval_final = self._locate_ps1_linenos(source_lines)
    assert not eval_final
    assert linenos == [0, 3]
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_parse_eval_single_want():
    string = ub.codeblock(
        '''
        >>> a = 1
        >>> 1 / 0
        We have a want
        ''')
    self = doctest_parser.DoctestParser()
    parts = self.parse(string)
    raw_source_lines = string.split('\n')[:-1]
    ps1_linenos, eval_final = self._locate_ps1_linenos(raw_source_lines)
    assert ps1_linenos == [0, 1]
    assert eval_final
    # Only one part because there is no want
    assert len(parts) == 2
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_parse_comment():
    string = ub.codeblock(
        '''
        >>> # nothing
        ''')
    self = doctest_parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    assert labeled == [('dsrc', '>>> # nothing')]
    source_lines = string.split('\n')[:]
    linenos, eval_final = self._locate_ps1_linenos(source_lines)
    parts = self.parse(string)
    assert parts[0].source.strip().startswith('#')
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_package_string_tup():
    """
    pytest testing/test_doctest_parser.py::test_package_string_tup
    """
    raw_source_lines = ['>>> "string"']
    raw_want_lines = ['string']
    self = doctest_parser.DoctestParser()
    parts = list(self._package_chunk(raw_source_lines, raw_want_lines))
    assert len(parts) == 1, 'should only want one string'
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
... a=b)
                ... dsrc
                >>> dsrc():
                ...     a
                ...     b = """
                        multiline
                        """
                want

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

            text
    '''
    self = doctest_parser.DoctestParser()
    labeled = self._label_docsrc_lines(string)
    expected = [
        ('text', ''),
        ('text', '            text'),
        ('dsrc', '            >>> dsrc()'),
        ('want', '            want'),
        ('text', ''),
        ('dsrc', '                >>> dsrc()'),
        ('dsrc', '                >>> cont('),
        ('dsrc', '                ... a=b)'),
        ('dsrc', '                ... dsrc'),
        ('dsrc', '                >>> dsrc():'),
        ('dsrc', '                ...     a'),
        ('dsrc', '                ...     b = """'),
        ('dsrc', '                        multiline'),
        ('dsrc', '                        """'),
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
'multiline strings are now kosher'

        >>> '''
            double multiline string
            '''.strip()
        ...
        >>> '''
            double multiline string
            '''.strip()
        'double multiline string'
        """)

    import doctest
    import ubelt as ub
    self1 = doctest.DocTestParser()
    self2 = doctest_parser.DoctestParser()
    self2._label_docsrc_lines(string)
    print('\n==== PARSER2 ====')
    for x, o in enumerate(self2.parse(string)):
        print('----')
        print(x)
        if not isinstance(o, str):
            print(ub.repr2(o.__dict__, sv=True))
            # print('o.source = {!r}'.format(o.source))
            # print('o.want = {!r}'.format(o.want))
        else:
            print('o = {!r}'.format(o))
    print('\n==== PARSER1 ====')
    for x, o in enumerate(self1.parse(string)):
        print('----')
        print(x)
        if not isinstance(o, str):
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_parse_multi_want():
    string = ub.codeblock(
        '''
        >>> x = 2
        >>> x
        2
        >>> 'string'
        'string'
        >>> print('string')
        string
        ''')
    self = doctest_parser.DoctestParser()
    parts = self.parse(string)

    self._label_docsrc_lines(string)
    assert parts[2].source == "'string'"
    assert len(parts) == 4
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
"""
        .. doctest::

            >>> '''
                multiline strings are now kosher
                '''
            multiline strings are now kosher
        """)

    string = ub.codeblock(
        '''
        >>> import os
        >>> os.environ["HELLO"]
        'WORLD'
        ''')
    self = doctest_parser.DoctestParser()
    self._label_docsrc_lines(string)
    ex = self.parse(string)[0]

    parts = self2.parse(string)
    print('parts = {!r}'.format(parts))
    for o in parts:
        if not isinstance(o, str):
            e = o
            print('e.source = {!r}'.format(e.source))
            print('e.want = {!r}'.format(e.want))

    source = ub.codeblock(
            '''
            a = b; b = c
            def foo():
                pass
github Erotemic / xdoctest / testing / test_doctest_parser.py View on Github external
def test_simulate_repl():
    """
    pytest testing/test_doctest_parser.py::test_package_string_tup
    """
    string = ub.codeblock(
        '''
        >>> x = 1
        >>> x = 2
        >>> x = 3
        ''')
    self = doctest_parser.DoctestParser()
    self.simulate_repl = False
    assert len(self.parse(string)) == 1
    self.simulate_repl = True
    assert len(self.parse(string)) == 3