Esempio n. 1
0
def test_fail_inside_twofunc():
    """
        python ~/code/xdoctest/testing/test_traceback.py test_fail_inside_twofunc

    """
    import warnings
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        text = _run_case(
            utils.codeblock('''
            def func(a):
                """
                Example:
                    >>> print('not failed')
                    >>> # just a comment
                    >>> print(("foo"
                    ...        "bar"))
                    >>> a = []()
                    >>> func(a)
                """
                return a

            def func2(a):
                """
                Example:
                    >>> pass
                """
                pass
            '''))
        assert text
        assert '>>> a = []()' in text
        assert 'rel: 5, abs: 8' in text
Esempio n. 2
0
def test_lineno_failcase_doctest_code():
    """
        python ~/code/xdoctest/testing/test_linenos.py test_lineno_failcase_doctest_code

    """
    text = _run_case(
        utils.codeblock(r'''
        def bar():
            pass

        def func(a):
            """
            Example:
                >>> # Perform some passing tests before we call failing code
                >>> func(0)
                0
                >>> # call the failing code
                >>> assert 1 == 2
                >>> # Do stuff that wont be executed
                >>> func(0)
                0
                >>> func(1)
                1
            """
            return a
        '''))
    assert 'rel: 5, abs: 11,' in text
    assert text
Esempio n. 3
0
def test_fail_call_twofunc():
    """
        python ~/code/xdoctest/testing/test_traceback.py test_fail_call_twofunc

    """
    import warnings
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        text = _run_case(
            utils.codeblock('''
            def func(a):
                """
                Example:
                    >>> a = 1
                    >>> func(a)
                """
                a = []()
                return a

            def func2(a):
                """
                Example:
                    >>> pass
                """
                pass
            '''))
        assert text
        assert '>>> func(a)' in text
        assert 'rel: 2, abs: 5,' in text
Esempio n. 4
0
def test_fail_call_twofunc():
    """
        python ~/code/xdoctest/testing/test_traceback.py test_fail_call_twofunc

    """
    text = _run_case(
        utils.codeblock('''
        def func(a):
            """
            Example:
                >>> a = 1
                >>> func(a)
            """
            a = []()
            return a

        def func2(a):
            """
            Example:
                >>> pass
            """
            pass
        '''))
    assert text
    assert '>>> func(a)' in text
    assert 'rel: 2, abs: 5,' in text
Esempio n. 5
0
def test_lineno_failcase_called_code():
    """
        python ~/code/xdoctest/testing/test_linenos.py test_lineno_failcase_called_code
        python ~/code/xdoctest/testing/test_linenos.py

    """
    text = _run_case(
        utils.codeblock(r'''
        def func(a):
            """
            Example:
                >>> func(0)
                >>> # this doesnt do anything
                >>> print('this passes')
                this passes
                >>> # call the failing code
                >>> func(3)
            """
            if a > 0:
                nested_failure(a)
            return a

        def nested_failure(a):
            if a > 0:
                nested_failure(a - 1)
            else:
                raise Exception('fail case')
        '''))
    assert 'rel: 6, abs: 9,' in text
    assert text
Esempio n. 6
0
def test_pathological_property_case():
    """
    This case actually wont error, but it displays a limitation of static
    analysis. We trick the doctest node parser into thinking there is a setter
    property when there really isn't.
    """
    text = _run_case(
        utils.codeblock('''
        def property(x):
            class Foo():
                def setter(self, x):
                    return x
            return Foo()

        class Test(object):
            @property
            def test(self):
                """
                Example:
                    >>> print('not really a getter')
                """
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> print('not really a setter')
                """
                pass
        '''))
    # If there was a way to make this case fail, that would be ok
    assert 'Test.test:0' in text
Esempio n. 7
0
def test_parse_doctset_error():
    source = utils.codeblock('''
        def func_with_an_unparsable_google_docstr(a):
            """
            This function will have an unparsable google docstr

            Args:
                a (int): a number

            Example:
                >>> a = "\\''' + '''n"
                >>> func(a)
            """
            pass

          ''')
    text = _run_case(source, style='google')
    text = _run_case(source, style='freeform')
    del text
Esempio n. 8
0
def test_fail_call_onefunc():
    text = _run_case(
        utils.codeblock('''
        def func(a):
            """
            Example:
                >>> a = 1
                >>> func(a)
            """
            a = []()
            return a
        '''))
    assert '>>> func(a)' in text
    assert 'rel: 2, abs: 5' in text
Esempio n. 9
0
def test_fail_call_onefunc():
    import warnings
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        text = _run_case(
            utils.codeblock('''
            def func(a):
                """
                Example:
                    >>> a = 1
                    >>> func(a)
                """
                a = []()
                return a
            '''))
        assert '>>> func(a)' in text
        assert 'rel: 2, abs: 5' in text
Esempio n. 10
0
def test_lineno_failcase_gotwant():
    """
        python ~/code/xdoctest/testing/test_linenos.py test_lineno_failcase_gotwant

    """
    text = _run_case(
        utils.codeblock('''
        def func(a):
            """
            Example:
                >>> got = func('foo')
                >>> print(got)
                bar
            """
            return a
        '''))
    assert text
    assert 'line 3' in text
    assert 'line 6' in text
Esempio n. 11
0
def test_extract_got_exception():
    """
    Make a repr that fails

    CommandLine:
        xdoctest -m ~/code/xdoctest/testing/test_errors.py test_extract_got_exception
    """

    source = utils.codeblock('''
        class MyObj(object):
            """
            Example:
                >>> a = MyObj()
                >>> a
                you cant always get what you want
            """
            def __repr__(self):
                raise Exception('this repr fails')
          ''')
    text = _run_case(source, style='google')
    assert 'ExtractGotReprException' in text
Esempio n. 12
0
def test_fail_inside_onefunc():
    """
        python ~/code/xdoctest/testing/test_traceback.py test_fail_inside_onefunc

    """
    text = _run_case(
        utils.codeblock('''
        def func(a):
            """
            Example:
                >>> x = 1
                >>> # just a comment
                >>> print(("foo"
                ...        "bar"))
                foobar
                >>> a = []()
                >>> func(a)
            """
            return a
        '''))
    assert text
    assert '>>> a = []()' in text
    assert 'rel: 6, abs: 9,' in text
Esempio n. 13
0
def test_failure_linenos():
    """
    Example:
        python ~/code/xdoctest/testing/test_linenos.py test_failure_linenos

    Example:
        >>> test_failure_linenos()
    """
    text = _run_case(
        utils.codeblock(r'''
        def bar(a):
            return a

        class Foo:
            @bar
            @staticmethod
            def func(a):
                """
                Example:
                    >>> # Perform some passing tests before we call failing code
                    >>> Foo.func(0)
                    0
                    >>> # call the failing code
                    >>> if True:
                    >>>     assert 1 == 2
                    >>> # Do stuff that wont be executed
                    >>> Foo.func(0)
                    0
                    >>> Foo.func(1)
                    1
                """
                return a
        '''))

    assert 'line 15' in text
    assert 'line 6' in text
    assert text
Esempio n. 14
0
def test_properties():
    """
    Test that all doctests are extracted from properties correctly.
    https://github.com/Erotemic/xdoctest/issues/73

    Credit: @trappitsch
    """
    text = _run_case(
        utils.codeblock('''
        class Test(object):
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14

            @test.setter
            def test(self, s):
                pass
        '''))
    assert 'running 1 test' in text

    text = _run_case(
        utils.codeblock('''
        class Test(object):
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14
        '''))
    assert 'running 1 test' in text

    text = _run_case(
        utils.codeblock('''
        class Test(object):
            @property
            def test(self):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test
                    3.14
                """
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 1 test' in text

    text = _run_case(
        utils.codeblock('''
        class Test(object):
            @property
            def test(self):
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 0 test' in text

    text = _run_case(
        utils.codeblock('''
        class Test(object):
            @property
            def test(self):
                return 3.14

            @test.setter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass

            @test.deleter
            def test(self, s):
                """
                Example:
                    >>> ini = Test()
                    >>> ini.test = 3
                """
                pass
        '''))
    assert 'running 0 test' in text