forked from foutaise/texttable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
305 lines (285 loc) · 8.76 KB
/
tests.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#coding: utf-8
import re
import sys
from textwrap import dedent
from texttable import Texttable
if sys.version >= '3':
u_dedent = dedent
else:
def u_dedent(b):
return unicode(dedent(b), 'utf-8')
def clean(text):
return re.sub(r'( +)$', '', text, flags=re.MULTILINE) + '\n'
def test_texttable():
table = Texttable()
table.set_cols_align(["l", "r", "c"])
table.set_cols_valign(["t", "m", "b"])
table.add_rows([
["Name", "Age", "Nickname"],
["Mr\nXavier\nHuon", 32, "Xav'"],
["Mr\nBaptiste\nClement", 1, "Baby"],
["Mme\nLouise\nBourgeau", 28, "Lou\n \nLoue"],
])
assert clean(table.draw()) == dedent('''\
+----------+-----+----------+
| Name | Age | Nickname |
+==========+=====+==========+
| Mr | | |
| Xavier | 32 | |
| Huon | | Xav' |
+----------+-----+----------+
| Mr | | |
| Baptiste | 1 | |
| Clement | | Baby |
+----------+-----+----------+
| Mme | | Lou |
| Louise | 28 | |
| Bourgeau | | Loue |
+----------+-----+----------+
''')
def test_texttable_header():
table = Texttable()
table.set_deco(Texttable.HEADER)
table.set_cols_dtype([
't', # text
'f', # float (decimal)
'e', # float (exponent)
'i', # integer
'a', # automatic
])
table.set_cols_align(["l", "r", "r", "r", "l"])
table.add_rows([
["text", "float", "exp", "int", "auto"],
["abcd", "67", 654, 89, 128.001],
["efghijk", 67.5434, .654, 89.6, 12800000000000000000000.00023],
["lmn", 5e-78, 5e-78, 89.4, .000000000000128],
["opqrstu", .023, 5e+78, 92., 12800000000000000000000],
])
assert clean(table.draw()) == dedent('''\
text float exp int auto
==============================================
abcd 67.000 6.540e+02 89 128.001
efghijk 67.543 6.540e-01 90 1.280e+22
lmn 0.000 5.000e-78 89 0.000
opqrstu 0.023 5.000e+78 92 1.280e+22
''')
def test_set_cols_width():
table = Texttable()
table.set_deco(Texttable.HEADER)
table.set_cols_width([10, 10])
table.add_rows([
["key", "value"],
[1, "a"],
[2, "b"],
])
assert clean(table.draw()) == dedent('''\
key value
=======================
1 a
2 b
''')
def test_exceeding_max_width():
table = Texttable(max_width=35)
table.set_deco(Texttable.HEADER)
table.add_rows([
["key", "value"],
[1, "a"],
[2, "b"],
[3, "very long, very long, very long"],
])
assert clean(table.draw()) == dedent('''\
key value
===================================
1 a
2 b
3 very long, very long, very
long
''')
def test_exceeding_max_width2():
table = Texttable(max_width=14)
table.add_rows([
["a", "b"],
[1, "+"],
[22, "++++++++"],
])
assert clean(table.draw()) == dedent('''\
+----+-------+
| a | b |
+====+=======+
| 1 | + |
+----+-------+
| 22 | +++++ |
| | +++ |
+----+-------+
''')
def test_exceeding_max_width3():
table = Texttable()
table.set_max_width(35)
table.set_deco(Texttable.HEADER)
table.add_rows([
["key", "value"],
[1, "a"],
[2, "b"],
[3, "very long, very long, very long"],
])
assert clean(table.draw()) == dedent('''\
key value
===================================
1 a
2 b
3 very long, very long, very
long
''')
def test_exceeding_max_width4():
table = Texttable()
table.set_max_width(14)
table.add_rows([
["a", "b"],
[1, "+"],
[22, "++++++++"],
])
assert clean(table.draw()) == dedent('''\
+----+-------+
| a | b |
+====+=======+
| 1 | + |
+----+-------+
| 22 | +++++ |
| | +++ |
+----+-------+
''')
def test_obj2unicode():
table = Texttable()
table.set_deco(Texttable.HEADER)
table.add_rows([
["key", "value"],
[1, "a"],
[2, 1],
[3, None],
])
assert clean(table.draw()) == dedent('''\
key value
===========
1 a
2 1
3 None
''')
def test_combining_char():
table = Texttable()
table.set_cols_align(["l", "r", "r"])
table.add_rows([
["str", "code-point\nlength", "display\nwidth"],
["ā", 2, 1],
["a", 1, 1],
])
assert clean(table.draw()) == u_dedent('''\
+-----+------------+---------+
| str | code-point | display |
| | length | width |
+=====+============+=========+
| ā | 2 | 1 |
+-----+------------+---------+
| a | 1 | 1 |
+-----+------------+---------+
''')
def test_combining_char2():
table = Texttable()
table.add_rows([
["a", "b", "c"],
["诶诶诶", "bbb", "西西西"],
], False)
assert clean(table.draw()) == u_dedent('''\
+--------+-----+--------+
| a | b | c |
+--------+-----+--------+
| 诶诶诶 | bbb | 西西西 |
+--------+-----+--------+
''')
def test_user_dtype():
table = Texttable()
table.set_cols_align(["l", "r", "r"])
table.set_cols_dtype([
'a', # automatic
lambda s:str(s)+"s", # user-def
lambda s:('%s'%s) if s>=0 else '[%s]'%(-s), # user-def
])
table.add_rows([
["str", "code-point\nlength", "display\nwidth"],
["a", 2, 1],
["a", 1,-3],
])
assert clean(table.draw()) == u_dedent('''\
+-----+------------+---------+
| str | code-point | display |
| | length | width |
+=====+============+=========+
| a | 2s | 1 |
+-----+------------+---------+
| a | 1s | [3] |
+-----+------------+---------+
''')
def test_cjkwarp():
try:
import cjkwrap
table = Texttable()
table.set_cols_align(["r", "l"])
table.add_rows([
["Name", 'Discuz! 6.x/7.x 全局变量防御绕过导致命令执行'],
["Description", '由于php5.3.x版本里php.ini的设置里request_order默认值为GP,导致Discuz! 6.x/7.x 全局变量防御绕过漏洞'],
], header = False)
assert clean(table.draw()) == u_dedent('''\
+-------------+----------------------------------------------------------------+
| Name | Discuz! 6.x/7.x 全局变量防御绕过导致命令执行 |
+-------------+----------------------------------------------------------------+
| Description | 由于php5.3.x版本里php.ini的设置里request_order默认值为GP,导致 |
| | Discuz! 6.x/7.x 全局变量防御绕过漏洞 |
+-------------+----------------------------------------------------------------+
''')
except ImportError:
True
def test_chaining():
table = Texttable()
table.reset()
table.set_max_width(50)
table.set_chars(list('-|+='))
table.set_deco(Texttable.BORDER)
table.set_header_align(list('lll'))
table.set_cols_align(list('lll'))
table.set_cols_valign(list('mmm'))
table.set_cols_dtype(list('ttt'))
table.set_cols_width([3, 3, 3])
table.set_precision(3)
table.header(list('abc'))
table.add_row(list('def'))
table.add_rows([list('ghi')], False)
s1 = table.draw()
s2 = (Texttable()
.reset()
.set_max_width(50)
.set_chars(list('-|+='))
.set_deco(Texttable.BORDER)
.set_header_align(list('lll'))
.set_cols_align(list('lll'))
.set_cols_valign(list('mmm'))
.set_cols_dtype(list('ttt'))
.set_cols_width([3, 3, 3])
.set_precision(3)
.header(list('abc'))
.add_row(list('def'))
.add_rows([list('ghi')], False)
.draw())
assert s1 == s2
def test_nan():
table = Texttable()
table.set_cols_align(["l"])
table.add_rows([
["A NaN"],
["NaN"],
])
assert clean(table.draw()) == u_dedent('''\
+-------+
| A NaN |
+=======+
| NaN |
+-------+
''')