/
test_wrap.py
170 lines (138 loc) · 6.21 KB
/
test_wrap.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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, 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()
def test_SequenceWrapper_drop_whitespace_subsequent_indent():
"""Test that text wrapping matches internal extra options."""
WIDTH = 10
@as_subprocess
def child():
# build a test paragraph, along with a very colorful version
t = TestTerminal()
pgraph = u' '.join(
('a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh',
'abcdefghi', 'abcdefghij', 'abcdefghijk', 'abcdefghijkl',
'abcdefghijklm', 'abcdefghijklmn', 'abcdefghijklmno ',)
* 4)
pgraph_colored = u''.join([
t.color(n % 7) + t.bold + ch if ch != ' ' else ' '
for n, ch in enumerate(pgraph)])
internal_wrapped = textwrap.wrap(pgraph, width=WIDTH,
break_long_words=False,
drop_whitespace=True,
subsequent_indent=u' '*3)
my_wrapped = t.wrap(pgraph, width=WIDTH,
drop_whitespace=True,
subsequent_indent=u' '*3)
my_wrapped_colored = t.wrap(pgraph_colored, width=WIDTH,
drop_whitespace=True,
subsequent_indent=u' '*3)
# 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)), (internal_wrapped,
my_wrapped_colored)
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()
@pytest.mark.skipif(platform.python_implementation() == 'PyPy',
reason='PyPy fails TIOCSWINSZ')
def test_SequenceWrapper(all_terms, many_columns):
"""Test that text wrapping accounts for sequences correctly."""
@as_subprocess
def child(kind, lines=25, cols=80):
# set the pty's virtual window size
val = struct.pack('HHHH', lines, cols, 0, 0)
fcntl.ioctl(sys.__stdout__.fileno(), termios.TIOCSWINSZ, val)
# build a test paragraph, along with a very colorful version
t = TestTerminal(kind=kind)
pgraph = u' '.join(
('a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh',
'abcdefghi', 'abcdefghij', 'abcdefghijk', 'abcdefghijkl',
'abcdefghijklm', 'abcdefghijklmn', 'abcdefghijklmno',) * 4)
pgraph_colored = u''.join([
t.color(n % 7) + t.bold + ch
for n, ch in enumerate(pgraph)])
internal_wrapped = textwrap.wrap(pgraph, t.width,
break_long_words=False)
my_wrapped = t.wrap(pgraph)
my_wrapped_colored = t.wrap(pgraph_colored)
# 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]))
child(kind=all_terms, lines=25, cols=many_columns)
def test_SequenceWrapper_27(all_terms):
"""Test that text wrapping accounts for sequences correctly."""
WIDTH = 27
@as_subprocess
def child(kind):
# build a test paragraph, along with a very colorful version
t = TestTerminal(kind=kind)
pgraph = u' '.join(
('a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh',
'abcdefghi', 'abcdefghij', 'abcdefghijk', 'abcdefghijkl',
'abcdefghijklm', 'abcdefghijklmn', 'abcdefghijklmno ',)
* 8)
pgraph_colored = u''.join([
t.color(n % 7) + t.bold + ch
for n, ch in enumerate(pgraph)])
internal_wrapped = textwrap.wrap(pgraph, width=WIDTH,
break_long_words=False,
drop_whitespace=False)
my_wrapped = t.wrap(pgraph, width=WIDTH,
break_long_words=False,
drop_whitespace=False)
my_wrapped_colored = t.wrap(pgraph_colored, width=WIDTH,
break_long_words=False,
drop_whitespace=False)
# 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(kind=all_terms)