File: test_tables.py

package info (click to toggle)
python-wasabi 0.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 208 kB
  • sloc: python: 1,255; makefile: 4
file content (422 lines) | stat: -rw-r--r-- 17,567 bytes parent folder | download
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# coding: utf8
from __future__ import unicode_literals, print_function

import os
import pytest
from wasabi.tables import table, row
from wasabi.util import supports_ansi

SUPPORTS_ANSI = supports_ansi()


@pytest.fixture()
def data():
    return [("Hello", "World", "12344342"), ("This is a test", "World", "1234")]


@pytest.fixture()
def header():
    return ["COL A", "COL B", "COL 3"]


@pytest.fixture()
def footer():
    return ["", "", "2030203.00"]


@pytest.fixture()
def fg_colors():
    return ["", "yellow", "87"]


@pytest.fixture()
def bg_colors():
    return ["green", "23", ""]


def test_table_default(data):
    result = table(data)
    assert (
        result
        == "\nHello            World   12344342\nThis is a test   World   1234    \n"
    )


def test_table_header(data, header):
    result = table(data, header=header)
    assert (
        result
        == "\nCOL A            COL B   COL 3   \nHello            World   12344342\nThis is a test   World   1234    \n"
    )


def test_table_header_footer_divider(data, header, footer):
    result = table(data, header=header, footer=footer, divider=True)
    assert (
        result
        == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
    )


def test_table_aligns(data):
    result = table(data, aligns=("r", "c", "l"))
    assert (
        result
        == "\n         Hello   World   12344342\nThis is a test   World   1234    \n"
    )


def test_table_aligns_single(data):
    result = table(data, aligns="r")
    assert (
        result
        == "\n         Hello   World   12344342\nThis is a test   World       1234\n"
    )


def test_table_widths():
    data = [("a", "bb", "ccc"), ("d", "ee", "fff")]
    widths = (5, 2, 10)
    result = table(data, widths=widths)
    assert result == "\na       bb   ccc       \nd       ee   fff       \n"


def test_row_single_widths():
    data = ("a", "bb", "ccc")
    result = row(data, widths=10)
    assert result == "a            bb           ccc       "


def test_table_multiline(header):
    data = [
        ("hello", ["foo", "bar", "baz"], "world"),
        ("hello", "world", ["world 1", "world 2"]),
    ]
    result = table(data, header=header, divider=True, multiline=True)
    assert (
        result
        == "\nCOL A   COL B   COL 3  \n-----   -----   -------\nhello   foo     world  \n        bar            \n        baz            \n                       \nhello   world   world 1\n                world 2\n"
    )


def test_row_fg_colors(fg_colors):
    result = row(("Hello", "World", "12344342"), fg_colors=fg_colors)
    if SUPPORTS_ANSI:
        assert (
            result == "Hello   \x1b[38;5;3mWorld\x1b[0m   \x1b[38;5;87m12344342\x1b[0m"
        )
    else:
        assert result == "Hello   World   12344342"


def test_row_bg_colors(bg_colors):
    result = row(("Hello", "World", "12344342"), bg_colors=bg_colors)
    if SUPPORTS_ANSI:
        assert (
            result == "\x1b[48;5;2mHello\x1b[0m   \x1b[48;5;23mWorld\x1b[0m   12344342"
        )
    else:
        assert result == "Hello   World   12344342"


def test_row_fg_colors_and_bg_colors(fg_colors, bg_colors):
    result = row(
        ("Hello", "World", "12344342"), fg_colors=fg_colors, bg_colors=bg_colors
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\x1b[48;5;2mHello\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m12344342\x1b[0m"
        )
    else:
        assert result == "Hello   World   12344342"


def test_row_fg_colors_and_bg_colors_log_friendly(fg_colors, bg_colors):
    ENV_LOG_FRIENDLY = "WASABI_LOG_FRIENDLY"
    os.environ[ENV_LOG_FRIENDLY] = "True"
    result = row(
        ("Hello", "World", "12344342"), fg_colors=fg_colors, bg_colors=bg_colors
    )
    assert result == "Hello   World   12344342"
    del os.environ[ENV_LOG_FRIENDLY]


def test_row_fg_colors_and_bg_colors_log_friendly_prefix(fg_colors, bg_colors):
    ENV_LOG_FRIENDLY = "CUSTOM_LOG_FRIENDLY"
    os.environ[ENV_LOG_FRIENDLY] = "True"
    result = row(
        ("Hello", "World", "12344342"),
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        env_prefix="CUSTOM",
    )
    assert result == "Hello   World   12344342"
    del os.environ[ENV_LOG_FRIENDLY]


def test_row_fg_colors_and_bg_colors_supports_ansi_false(fg_colors, bg_colors):
    os.environ["ANSI_COLORS_DISABLED"] = "True"
    result = row(
        ("Hello", "World", "12344342"), fg_colors=fg_colors, bg_colors=bg_colors
    )
    assert result == "Hello   World   12344342"
    del os.environ["ANSI_COLORS_DISABLED"]


def test_colors_whole_table_with_automatic_widths(
    data, header, footer, fg_colors, bg_colors
):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCOL A         \x1b[0m   \x1b[38;5;3;48;5;23mCOL B\x1b[0m   \x1b[38;5;87mCOL 3     \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2mHello         \x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m12344342  \x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m1234      \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m              \x1b[0m   \x1b[38;5;3;48;5;23m     \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
        )


def test_colors_whole_table_only_fg_colors(data, header, footer, fg_colors):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\nCOL A            \x1b[38;5;3mCOL B\x1b[0m   \x1b[38;5;87mCOL 3     \x1b[0m\n--------------   \x1b[38;5;3m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\nHello            \x1b[38;5;3mWorld\x1b[0m   \x1b[38;5;87m12344342  \x1b[0m\nThis is a test   \x1b[38;5;3mWorld\x1b[0m   \x1b[38;5;87m1234      \x1b[0m\n--------------   \x1b[38;5;3m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n                 \x1b[38;5;3m     \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
        )


def test_colors_whole_table_only_bg_colors(data, header, footer, bg_colors):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        bg_colors=bg_colors,
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCOL A         \x1b[0m   \x1b[48;5;23mCOL B\x1b[0m   COL 3     \n\x1b[48;5;2m--------------\x1b[0m   \x1b[48;5;23m-----\x1b[0m   ----------\n\x1b[48;5;2mHello         \x1b[0m   \x1b[48;5;23mWorld\x1b[0m   12344342  \n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[48;5;23mWorld\x1b[0m   1234      \n\x1b[48;5;2m--------------\x1b[0m   \x1b[48;5;23m-----\x1b[0m   ----------\n\x1b[48;5;2m              \x1b[0m   \x1b[48;5;23m     \x1b[0m   2030203.00\n"
        )
    else:
        assert (
            result
            == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
        )


def test_colors_whole_table_with_supplied_spacing(
    data, header, footer, fg_colors, bg_colors
):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        spacing=5,
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCOL A         \x1b[0m     \x1b[38;5;3;48;5;23mCOL B\x1b[0m     \x1b[38;5;87mCOL 3     \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m     \x1b[38;5;3;48;5;23m-----\x1b[0m     \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2mHello         \x1b[0m     \x1b[38;5;3;48;5;23mWorld\x1b[0m     \x1b[38;5;87m12344342  \x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m     \x1b[38;5;3;48;5;23mWorld\x1b[0m     \x1b[38;5;87m1234      \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m     \x1b[38;5;3;48;5;23m-----\x1b[0m     \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m              \x1b[0m     \x1b[38;5;3;48;5;23m     \x1b[0m     \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCOL A              COL B     COL 3     \n--------------     -----     ----------\nHello              World     12344342  \nThis is a test     World     1234      \n--------------     -----     ----------\n                             2030203.00\n"
        )


def test_colors_whole_table_with_supplied_widths(
    data, header, footer, fg_colors, bg_colors
):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        widths=(5, 2, 10),
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCOL A\x1b[0m   \x1b[38;5;3;48;5;23mCOL B\x1b[0m   \x1b[38;5;87mCOL 3     \x1b[0m\n\x1b[48;5;2m-----\x1b[0m   \x1b[38;5;3;48;5;23m--\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2mHello\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m12344342  \x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m1234      \x1b[0m\n\x1b[48;5;2m-----\x1b[0m   \x1b[38;5;3;48;5;23m--\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m     \x1b[0m   \x1b[38;5;3;48;5;23m  \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCOL A   COL B   COL 3     \n-----   --   ----------\nHello   World   12344342  \nThis is a test   World   1234      \n-----   --   ----------\n             2030203.00\n"
        )


def test_colors_whole_table_with_single_alignment(
    data, header, footer, fg_colors, bg_colors
):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        aligns="r",
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2m         COL A\x1b[0m   \x1b[38;5;3;48;5;23mCOL B\x1b[0m   \x1b[38;5;87m     COL 3\x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m         Hello\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m  12344342\x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m      1234\x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m              \x1b[0m   \x1b[38;5;3;48;5;23m     \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\n         COL A   COL B        COL 3\n--------------   -----   ----------\n         Hello   World     12344342\nThis is a test   World         1234\n--------------   -----   ----------\n                         2030203.00\n"
        )


def test_colors_whole_table_with_multiple_alignment(
    data, header, footer, fg_colors, bg_colors
):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        aligns=("c", "r", "l"),
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2m    COL A     \x1b[0m   \x1b[38;5;3;48;5;23mCOL B\x1b[0m   \x1b[38;5;87mCOL 3     \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m    Hello     \x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m12344342  \x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[38;5;3;48;5;23mWorld\x1b[0m   \x1b[38;5;87m1234      \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;3;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m              \x1b[0m   \x1b[38;5;3;48;5;23m     \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\n    COL A        COL B   COL 3     \n--------------   -----   ----------\n    Hello        World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
        )


def test_colors_whole_table_with_multiline(data, header, footer, fg_colors, bg_colors):
    result = table(
        data=((["Charles", "Quinton", "Murphy"], "my", "brother"), ("1", "2", "3")),
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        multiline=True,
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCharles\x1b[0m   \x1b[38;5;3;48;5;23mmy\x1b[0m   \x1b[38;5;87mbrother\x1b[0m\n\x1b[48;5;2mQuinton\x1b[0m   \x1b[38;5;3;48;5;23m  \x1b[0m   \x1b[38;5;87m       \x1b[0m\n\x1b[48;5;2mMurphy \x1b[0m   \x1b[38;5;3;48;5;23m  \x1b[0m   \x1b[38;5;87m       \x1b[0m\n\x1b[48;5;2m       \x1b[0m   \x1b[38;5;3;48;5;23m  \x1b[0m   \x1b[38;5;87m       \x1b[0m\n\x1b[48;5;2m1      \x1b[0m   \x1b[38;5;3;48;5;23m2 \x1b[0m   \x1b[38;5;87m3      \x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCharles   my   brother\nQuinton               \nMurphy                \n                      \n1         2    3      \n"
        )


def test_colors_whole_table_log_friendly(data, header, footer, fg_colors, bg_colors):
    ENV_LOG_FRIENDLY = "WASABI_LOG_FRIENDLY"
    os.environ[ENV_LOG_FRIENDLY] = "True"
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
    )
    assert (
        result
        == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
    )
    del os.environ[ENV_LOG_FRIENDLY]


def test_colors_whole_table_log_friendly_prefix(
    data, header, footer, fg_colors, bg_colors
):
    ENV_LOG_FRIENDLY = "CUSTOM_LOG_FRIENDLY"
    os.environ[ENV_LOG_FRIENDLY] = "True"
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        env_prefix="CUSTOM",
    )
    assert (
        result
        == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
    )
    del os.environ[ENV_LOG_FRIENDLY]


def test_colors_whole_table_supports_ansi_false(
    data, header, footer, fg_colors, bg_colors
):
    os.environ["ANSI_COLORS_DISABLED"] = "True"
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
    )
    assert (
        result
        == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
    )
    del os.environ["ANSI_COLORS_DISABLED"]


def test_colors_whole_table_color_values(data, header, footer, fg_colors, bg_colors):
    result = table(
        data,
        header=header,
        footer=footer,
        divider=True,
        fg_colors=fg_colors,
        bg_colors=bg_colors,
        color_values={"yellow": 11},
    )
    if SUPPORTS_ANSI:
        assert (
            result
            == "\n\x1b[48;5;2mCOL A         \x1b[0m   \x1b[38;5;11;48;5;23mCOL B\x1b[0m   \x1b[38;5;87mCOL 3     \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;11;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2mHello         \x1b[0m   \x1b[38;5;11;48;5;23mWorld\x1b[0m   \x1b[38;5;87m12344342  \x1b[0m\n\x1b[48;5;2mThis is a test\x1b[0m   \x1b[38;5;11;48;5;23mWorld\x1b[0m   \x1b[38;5;87m1234      \x1b[0m\n\x1b[48;5;2m--------------\x1b[0m   \x1b[38;5;11;48;5;23m-----\x1b[0m   \x1b[38;5;87m----------\x1b[0m\n\x1b[48;5;2m              \x1b[0m   \x1b[38;5;11;48;5;23m     \x1b[0m   \x1b[38;5;87m2030203.00\x1b[0m\n"
        )
    else:
        assert (
            result
            == "\nCOL A            COL B   COL 3     \n--------------   -----   ----------\nHello            World   12344342  \nThis is a test   World   1234      \n--------------   -----   ----------\n                         2030203.00\n"
        )