예제 #1
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
예제 #2
0
def test_directive_indent_equal_to_block():
    document = document_from_sample('capture_bad_indent2.txt')
    with pytest.raises(ValueError) as excinfo:
        list(parse_captures(document))
    assert str(excinfo.value) == (
            "couldn't find the start of the block to match'    .. -> foo' "
            "on line 5 of "+sample_path('capture_bad_indent2.txt')
        )
예제 #3
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"
    )
예제 #4
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], {}) == ''
예제 #5
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')
예제 #6
0
def test_directive_indent_beyond_block():
    document = document_from_sample('capture_bad_indent1.txt')
    with pytest.raises(ValueError) as excinfo:
        list(parse_captures(document))
    if PY3:
        block = "'        .. -> foo'"
    else:
        block = "u'        .. -> foo'"
    assert str(
        excinfo.value) == ("couldn't find the start of the block to match " +
                           block + " "
                           "on line 5 of " +
                           sample_path('capture_bad_indent1.txt'))
예제 #7
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
예제 #8
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
예제 #9
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
예제 #10
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) == ''
예제 #11
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, {}) == ''