Пример #1
0
def test_large_data_performance():
    data = [{'text': 'lorem ipsum dolor sit amet ' * 500}] * 200
    start = datetime.datetime.now()
    pformat(data)
    end = datetime.datetime.now()
    took = end - start
    # The bottleneck is in string to doc conversion,
    # specifically escaping strings many times.
    # There's probably more we can do here
    assert took < datetime.timedelta(seconds=10)
Пример #2
0
def test_bytes_pprint_equals_repr(bytestr):
    reprd = repr(bytestr)
    pformatted = pformat(bytestr)

    # This is not always the case. E.g.:
    # >>> print(repr(b"\"''"))
    # >>> b'"\'\''
    #
    # Where as peprint chooses
    # >>> print(pformat(b"\"''""))
    # >>> b"\"''"
    # For fewer escapes.
    used_same_quote_type = reprd[-1] == pformatted[-1]

    if used_same_quote_type:
        assert pformat(bytestr) == repr(bytestr)
Пример #3
0
def test_top_level_str():
    """Tests that top level strings are not indented or surrounded with parentheses"""

    pprint('ab' * 50)
    expected = (
        "'ababababababababababababababababababababababababababababababababababa'"
        "\n'bababababababababababababababab'")
    assert pformat('ab' * 50) == expected
Пример #4
0
def test_single_element_sequence_multiline_strategy():
    """Test that sequences with a single str-like element are not hang-indented
    in multiline mode."""
    expected = """\
[
    'ababababababababababababababababababababababababababababababababababa'
    'bababababababababababababababab'
]"""
    res = pformat(['ab' * 50])
    assert res == expected
Пример #5
0
def test_second_level_str():
    """Test that second level strs are indented"""
    expected = """\
[
    'ababababababababababababababababababababababababababababababababababa'
        'bababababababababababababababab',
    'ababababababababababababababababababababababababababababababababababa'
        'bababababababababababababababab'
]"""
    res = pformat(['ab' * 50] * 2)
    assert res == expected
Пример #6
0
def test_always_breaking():
    """A dictionary value that is broken into multiple lines must
    also break the whole dictionary to multiple lines."""
    data = {
        'okay': ''.join(islice(cycle(['ab' * 20, ' ' * 3]), 3)),
    }
    expected = """\
{
    'okay':
        'abababababababababababababababababababab   '
        'abababababababababababababababababababab'
}"""
    res = pformat(data)
    assert res == expected
Пример #7
0
def main():
    with open('tests/sample_json.json') as f:
        data = json.load(f)

    pformat(data)
Пример #8
0
def test_readable(value):
    formatted = pformat(value)

    _locals = {'datetime': datetime, 'pytz': pytz}
    evald = eval(formatted, None, _locals)
    assert evald == value
Пример #9
0
def test_recursive():
    d = {}
    d['self_recursion'] = d

    rendered = pformat(d)
    assert rendered == f"{{'self_recursion': <Recursion on dict with id={id(d)}>}}"