summaryrefslogtreecommitdiff
path: root/blessed/tests/test_wrap.py
blob: 1e12060a7be4607226c40955ba587987c5f1984f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import platform
import textwrap
import termios
import struct
import fcntl
import sys

from .accessories import (
    as_subprocess,
    TestTerminal,
    many_columns,
    all_terms,
)

import pytest


def test_SequenceWrapper_invalid_width():
    """Test exception thrown from invalid width"""
    WIDTH = -3

    @as_subprocess
    def child():
        t = TestTerminal()
        try:
            my_wrapped = t.wrap(u'------- -------------', WIDTH)
        except ValueError as err:
            assert err.args[0] == (
                "invalid width %r(%s) (must be integer > 0)" % (
                    WIDTH, type(WIDTH)))
        else:
            assert False, 'Previous stmt should have raised exception.'
            del my_wrapped  # assigned but never used

    child()


@pytest.mark.parametrize("kwargs", [
    dict(break_long_words=False,
         drop_whitespace=False,
         subsequent_indent=''),
    dict(break_long_words=False,
         drop_whitespace=True,
         subsequent_indent=''),
    dict(break_long_words=False,
         drop_whitespace=False,
         subsequent_indent=' '),
    dict(break_long_words=False,
         drop_whitespace=True,
         subsequent_indent=' '),
    dict(break_long_words=True,
         drop_whitespace=False,
         subsequent_indent=''),
    dict(break_long_words=True,
         drop_whitespace=True,
         subsequent_indent=''),
    dict(break_long_words=True,
         drop_whitespace=False,
         subsequent_indent=' '),
    dict(break_long_words=True,
         drop_whitespace=True,
         subsequent_indent=' '),
])
def test_SequenceWrapper(all_terms, many_columns, kwargs):
    """Test that text wrapping matches internal extra options."""
    @as_subprocess
    def child(term, width, kwargs):
        # build a test paragraph, along with a very colorful version
        t = TestTerminal()
        pgraph = u' '.join((
            'a', 'bc', 'def', 'ghij', 'klmno', 'pqrstu', 'vwxyz012',
            '34567890A', 'BCDEFGHIJK', 'LMNOPQRSTUV', 'WXYZabcdefgh',
            'ijklmnopqrstu', 'vwxyz123456789', '0ABCDEFGHIJKLMN  '))

        pgraph_colored = u''.join([
            t.color(idx % 7)(char) if char != ' ' else ' '
            for idx, char in enumerate(pgraph)])

        internal_wrapped = textwrap.wrap(pgraph, width=width, **kwargs)
        my_wrapped = t.wrap(pgraph, width=width, **kwargs)
        my_wrapped_colored = t.wrap(pgraph_colored, width=width, **kwargs)

        # ensure we textwrap ascii the same as python
        assert (internal_wrapped == my_wrapped)

        # ensure our first and last line wraps at its ends
        first_l = internal_wrapped[0]
        last_l = internal_wrapped[-1]
        my_first_l = my_wrapped_colored[0]
        my_last_l = my_wrapped_colored[-1]
        assert (len(first_l) == t.length(my_first_l))
        assert (len(last_l) == t.length(my_last_l))
        assert (len(internal_wrapped[-1]) == t.length(my_wrapped_colored[-1]))

        # ensure our colored textwrap is the same line length
        assert (len(internal_wrapped) == len(my_wrapped_colored))

    child(all_terms, many_columns, kwargs)