コード例 #1
0
def test_literals():
    document = document_from_sample('doctest_literals.txt')
    regions = list(DocTestParser(FIX_BYTE_UNICODE_REPR)(document))
    assert len(regions) == 5
    assert evaluate_region(regions[0], {}) == ''
    assert evaluate_region(regions[1], {}) == ''
    assert evaluate_region(regions[2], {}) == ''
    assert evaluate_region(regions[3], {}) == ''
    assert evaluate_region(regions[4], {}) == ''
コード例 #2
0
def test_fail():
    document = document_from_sample('doctest_fail.txt')
    regions = list(DocTestParser()(document))
    assert len(regions) == 2
    assert evaluate_region(regions[0], {}) == ("Expected:\n"
                                               "    Not my output\n"
                                               "Got:\n"
                                               "    where's my output?\n")
    actual = evaluate_region(regions[1], {})
    assert actual.startswith('Exception raised:')
    assert actual.endswith('Exception: boom!\n')
コード例 #3
0
def test_basic():
    document = document_from_sample('codeblock.txt')
    regions = list(CodeBlockParser()(document))
    assert len(regions) == 7
    namespace = document.namespace
    namespace['y'] = namespace['z'] = 0
    assert evaluate_region(regions[0], namespace) is None
    assert namespace['y'] == 1
    assert namespace['z'] == 0
    with pytest.raises(Exception) as excinfo:
        evaluate_region(regions[1], namespace)
    assert str(excinfo.value) == 'boom!'
    assert evaluate_region(regions[2], namespace) is None
    assert namespace['y'] == 1
    assert namespace['z'] == 1
    assert evaluate_region(regions[3], namespace) is None
    assert namespace['bin'] == b'x'
    assert namespace['uni'] == u'x'
    assert evaluate_region(regions[4], namespace) is None
    assert 'NoVars' in namespace
    assert evaluate_region(regions[5], namespace) is None
    assert namespace['define_this'] == 1
    assert evaluate_region(regions[6], namespace) is None
    assert 'YesVars' in namespace
    assert '__builtins__' not in namespace
コード例 #4
0
def test_pass():
    document = document_from_sample('doctest.txt')
    regions = list(DocTestParser()(document))
    assert len(regions) == 5
    namespace = document.namespace
    assert evaluate_region(regions[0], namespace) == ''
    assert namespace['y'] == 1
    assert evaluate_region(regions[1], namespace) == ''
    assert namespace['y'] == 1
    assert evaluate_region(regions[2], namespace) == ''
    assert namespace['x'] == [1, 2, 3]
    assert evaluate_region(regions[3], namespace) == ''
    assert namespace['y'] == 2
    assert evaluate_region(regions[4], namespace) == ''
    assert namespace['y'] == 2
コード例 #5
0
def test_future_imports():
    document = document_from_sample('codeblock_future_imports.txt')
    regions = list(CodeBlockParser(['print_function'])(document))
    assert len(regions) == 2
    buffer = StringIO()
    namespace = {'buffer': buffer}
    assert evaluate_region(regions[0], namespace) is None
    assert buffer.getvalue() == ('pathalogical worst case for line numbers\n')
    # the future import line drops the firstlineno by 1
    assert regions[0].parsed.co_firstlineno == 2
    assert evaluate_region(regions[1], namespace) is None
    assert buffer.getvalue() == (
        'pathalogical worst case for line numbers\n'
        'still should work and have good line numbers\n')
    # the future import line drops the firstlineno by 1
    assert regions[1].parsed.co_firstlineno == 8
コード例 #6
0
def test_fail_with_options():
    document = document_from_sample('doctest_fail.txt')
    regions = list(DocTestParser(optionflags=REPORT_NDIFF|ELLIPSIS)(document))
    assert len(regions) == 2
    assert evaluate_region(regions[0], {}) == (
        "Differences (ndiff with -expected +actual):\n"
        "    - Not my output\n"
        "    + where's my output?\n"
    )
コード例 #7
0
def test_basic():
    document = document_from_sample('capture.txt')
    regions = list(parse_captures(document))
    namespace = document.namespace
    assert evaluate_region(regions[-1], namespace) is None
    assert namespace['expected_listing'] == ('root.txt\n'
                                             'subdir/\n'
                                             'subdir/file.txt\n'
                                             'subdir/logs/\n')
    assert evaluate_region(regions[-2], namespace) is None
    assert namespace['foo'] == 'Third level of indentation.\n'
    assert evaluate_region(regions[-3], namespace) is None
    assert namespace['bar'] == (
        'Second level of indentation.\n\n'
        '    Third level of indentation.\n\n.. -> foo\n')
    assert evaluate_region(regions[-4], namespace) is None
    assert namespace['another'] == ('example\n')
    assert len(regions) == 4
コード例 #8
0
def test_min_indent():
    document = document_from_sample('doctest_min_indent.txt')
    regions = list(DocTestParser()(document))
    assert len(regions) == 1
    namespace = document.namespace
    assert evaluate_region(regions[0], namespace) == ''
コード例 #9
0
def test_literals():
    document = document_from_sample('doctest_literals.txt')
    regions = list(DocTestParser(FIX_BYTE_UNICODE_REPR)(document))
    assert len(regions) == 5
    for region in regions:
        assert evaluate_region(region, {}) == ''
コード例 #10
0
def test_unicode():
    document = Document(u'>>> print("├─")\n├─', path='dummy.rst')
    example, = DocTestParser()(document)
    namespace = document.namespace
    assert evaluate_region(example, namespace) == ''