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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
"""Regression tests."""
from tabulate import tabulate, TableFormat, Line, DataRow
from common import assert_equal, skip
def test_ansi_color_in_table_cells():
"Regression: ANSI color in table cells (issue #5)."
colortable = [("test", "\x1b[31mtest\x1b[0m", "\x1b[32mtest\x1b[0m")]
colorlessheaders = ("test", "test", "test")
formatted = tabulate(colortable, colorlessheaders, "pipe")
expected = "\n".join(
[
"| test | test | test |",
"|:-------|:-------|:-------|",
"| test | \x1b[31mtest\x1b[0m | \x1b[32mtest\x1b[0m |",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_alignment_of_colored_cells():
"Regression: Align ANSI-colored values as if they were colorless."
colortable = [
("test", 42, "\x1b[31m42\x1b[0m"),
("test", 101, "\x1b[32m101\x1b[0m"),
]
colorheaders = ("test", "\x1b[34mtest\x1b[0m", "test")
formatted = tabulate(colortable, colorheaders, "grid")
expected = "\n".join(
[
"+--------+--------+--------+",
"| test | \x1b[34mtest\x1b[0m | test |",
"+========+========+========+",
"| test | 42 | \x1b[31m42\x1b[0m |",
"+--------+--------+--------+",
"| test | 101 | \x1b[32m101\x1b[0m |",
"+--------+--------+--------+",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_alignment_of_link_cells():
"Regression: Align links as if they were colorless."
linktable = [
("test", 42, "\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\"),
("test", 101, "\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\"),
]
linkheaders = ("test", "\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\", "test")
formatted = tabulate(linktable, linkheaders, "grid")
expected = "\n".join(
[
"+--------+--------+--------+",
"| test | \x1b]8;;target\x1b\\test\x1b]8;;\x1b\\ | test |",
"+========+========+========+",
"| test | 42 | \x1b]8;;target\x1b\\test\x1b]8;;\x1b\\ |",
"+--------+--------+--------+",
"| test | 101 | \x1b]8;;target\x1b\\test\x1b]8;;\x1b\\ |",
"+--------+--------+--------+",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_alignment_of_link_text_cells():
"Regression: Align links as if they were colorless."
linktable = [
("test", 42, "1\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\2"),
("test", 101, "3\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\4"),
]
linkheaders = ("test", "5\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\6", "test")
formatted = tabulate(linktable, linkheaders, "grid")
expected = "\n".join(
[
"+--------+----------+--------+",
"| test | 5\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\6 | test |",
"+========+==========+========+",
"| test | 42 | 1\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\2 |",
"+--------+----------+--------+",
"| test | 101 | 3\x1b]8;;target\x1b\\test\x1b]8;;\x1b\\4 |",
"+--------+----------+--------+",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_iter_of_iters_with_headers():
"Regression: Generator of generators with a gen. of headers (issue #9)."
def mk_iter_of_iters():
def mk_iter():
yield from range(3)
for r in range(3):
yield mk_iter()
def mk_headers():
yield from ["a", "b", "c"]
formatted = tabulate(mk_iter_of_iters(), headers=mk_headers())
expected = "\n".join(
[
" a b c",
"--- --- ---",
" 0 1 2",
" 0 1 2",
" 0 1 2",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_datetime_values():
"Regression: datetime, date, and time values in cells (issue #10)."
import datetime
dt = datetime.datetime(1991, 2, 19, 17, 35, 26)
d = datetime.date(1991, 2, 19)
t = datetime.time(17, 35, 26)
formatted = tabulate([[dt, d, t]])
expected = "\n".join(
[
"------------------- ---------- --------",
"1991-02-19 17:35:26 1991-02-19 17:35:26",
"------------------- ---------- --------",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_simple_separated_format():
"Regression: simple_separated_format() accepts any separator (issue #12)"
from tabulate import simple_separated_format
fmt = simple_separated_format("!")
expected = "spam!eggs"
formatted = tabulate([["spam", "eggs"]], tablefmt=fmt)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_simple_separated_format_with_headers():
"Regression: simple_separated_format() on tables with headers (issue #15)"
from tabulate import simple_separated_format
expected = " a| b\n 1| 2"
formatted = tabulate(
[[1, 2]], headers=["a", "b"], tablefmt=simple_separated_format("|")
)
assert_equal(expected, formatted)
def test_column_type_of_bytestring_columns():
"Regression: column type for columns of bytestrings (issue #16)"
from tabulate import _column_type
result = _column_type([b"foo", b"bar"])
expected = bytes
assert_equal(result, expected)
def test_numeric_column_headers():
"Regression: numbers as column headers (issue #22)"
result = tabulate([[1], [2]], [42])
expected = " 42\n----\n 1\n 2"
assert_equal(result, expected)
lod = [{p: i for p in range(5)} for i in range(5)]
result = tabulate(lod, "keys")
expected = "\n".join(
[
" 0 1 2 3 4",
"--- --- --- --- ---",
" 0 0 0 0 0",
" 1 1 1 1 1",
" 2 2 2 2 2",
" 3 3 3 3 3",
" 4 4 4 4 4",
]
)
assert_equal(result, expected)
def test_88_256_ANSI_color_codes():
"Regression: color codes for terminals with 88/256 colors (issue #26)"
colortable = [("\x1b[48;5;196mred\x1b[49m", "\x1b[38;5;196mred\x1b[39m")]
colorlessheaders = ("background", "foreground")
formatted = tabulate(colortable, colorlessheaders, "pipe")
expected = "\n".join(
[
"| background | foreground |",
"|:-------------|:-------------|",
"| \x1b[48;5;196mred\x1b[49m | \x1b[38;5;196mred\x1b[39m |",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)
def test_column_with_mixed_value_types():
"Regression: mixed value types in the same column (issue #31)"
expected = "\n".join(["-----", "", "a", "я", "0", "False", "-----"])
data = [[None], ["a"], ["\u044f"], [0], [False]]
table = tabulate(data)
assert_equal(table, expected)
def test_latex_escape_special_chars():
"Regression: escape special characters in LaTeX output (issue #32)"
expected = "\n".join(
[
r"\begin{tabular}{l}",
r"\hline",
r" foo\^{}bar \\",
r"\hline",
r" \&\%\^{}\_\$\#\{\}\ensuremath{<}\ensuremath{>}\textasciitilde{} \\",
r"\hline",
r"\end{tabular}",
]
)
result = tabulate([["&%^_$#{}<>~"]], ["foo^bar"], tablefmt="latex")
assert_equal(result, expected)
def test_isconvertible_on_set_values():
"Regression: don't fail with TypeError on set values (issue #35)"
expected = "\n".join(["a b", "--- -----", "Foo set()"])
result = tabulate([["Foo", set()]], headers=["a", "b"])
assert_equal(result, expected)
def test_ansi_color_for_decimal_numbers():
"Regression: ANSI colors for decimal numbers (issue #36)"
table = [["Magenta", "\033[95m" + "1.1" + "\033[0m"]]
expected = "\n".join(
["------- ---", "Magenta \x1b[95m1.1\x1b[0m", "------- ---"]
)
result = tabulate(table)
assert_equal(result, expected)
def test_alignment_of_decimal_numbers_with_ansi_color():
"Regression: alignment for decimal numbers with ANSI color (issue #42)"
v1 = "\033[95m" + "12.34" + "\033[0m"
v2 = "\033[95m" + "1.23456" + "\033[0m"
table = [[v1], [v2]]
expected = "\n".join(["\x1b[95m12.34\x1b[0m", " \x1b[95m1.23456\x1b[0m"])
result = tabulate(table, tablefmt="plain")
assert_equal(result, expected)
def test_alignment_of_decimal_numbers_with_commas():
"Regression: alignment for decimal numbers with comma separators"
skip("test is temporarily disable until the feature is reimplemented")
# table = [["c1r1", "14502.05"], ["c1r2", 105]]
# result = tabulate(table, tablefmt="grid", floatfmt=',.2f')
# expected = "\n".join(
# ['+------+-----------+', '| c1r1 | 14,502.05 |',
# '+------+-----------+', '| c1r2 | 105.00 |',
# '+------+-----------+']
# )
# assert_equal(result, expected)
def test_long_integers():
"Regression: long integers should be printed as integers (issue #48)"
table = [[18446744073709551614]]
result = tabulate(table, tablefmt="plain")
expected = "18446744073709551614"
assert_equal(result, expected)
def test_colorclass_colors():
"Regression: ANSI colors in a unicode/str subclass (issue #49)"
try:
import colorclass
s = colorclass.Color("{magenta}3.14{/magenta}")
result = tabulate([[s]], tablefmt="plain")
expected = "\x1b[35m3.14\x1b[39m"
assert_equal(result, expected)
except ImportError:
class textclass(str):
pass
s = textclass("\x1b[35m3.14\x1b[39m")
result = tabulate([[s]], tablefmt="plain")
expected = "\x1b[35m3.14\x1b[39m"
assert_equal(result, expected)
def test_mix_normal_and_wide_characters():
"Regression: wide characters in a grid format (issue #51)"
try:
import wcwidth # noqa
ru_text = "\u043f\u0440\u0438\u0432\u0435\u0442"
cn_text = "\u4f60\u597d"
result = tabulate([[ru_text], [cn_text]], tablefmt="grid")
expected = "\n".join(
[
"+--------+",
"| \u043f\u0440\u0438\u0432\u0435\u0442 |",
"+--------+",
"| \u4f60\u597d |",
"+--------+",
]
)
assert_equal(result, expected)
except ImportError:
skip("test_mix_normal_and_wide_characters is skipped (requires wcwidth lib)")
def test_multiline_with_wide_characters():
"Regression: multiline tables with varying number of wide characters (github issue #28)"
try:
import wcwidth # noqa
table = [["가나\n가ab", "가나", "가나"]]
result = tabulate(table, tablefmt="fancy_grid")
expected = "\n".join(
[
"╒══════╤══════╤══════╕",
"│ 가나 │ 가나 │ 가나 │",
"│ 가ab │ │ │",
"╘══════╧══════╧══════╛",
]
)
assert_equal(result, expected)
except ImportError:
skip("test_multiline_with_wide_characters is skipped (requires wcwidth lib)")
def test_align_long_integers():
"Regression: long integers should be aligned as integers (issue #61)"
table = [[int(1)], [int(234)]]
result = tabulate(table, tablefmt="plain")
expected = "\n".join([" 1", "234"])
assert_equal(result, expected)
def test_numpy_array_as_headers():
"Regression: NumPy array used as headers (issue #62)"
try:
import numpy as np
headers = np.array(["foo", "bar"])
result = tabulate([], headers, tablefmt="plain")
expected = "foo bar"
assert_equal(result, expected)
except ImportError:
raise skip("")
def test_boolean_columns():
"Regression: recognize boolean columns (issue #64)"
xortable = [[False, True], [True, False]]
expected = "\n".join(["False True", "True False"])
result = tabulate(xortable, tablefmt="plain")
assert_equal(result, expected)
def test_ansi_color_bold_and_fgcolor():
"Regression: set ANSI color and bold face together (issue #65)"
table = [["1", "2", "3"], ["4", "\x1b[1;31m5\x1b[1;m", "6"], ["7", "8", "9"]]
result = tabulate(table, tablefmt="grid")
expected = "\n".join(
[
"+---+---+---+",
"| 1 | 2 | 3 |",
"+---+---+---+",
"| 4 | \x1b[1;31m5\x1b[1;m | 6 |",
"+---+---+---+",
"| 7 | 8 | 9 |",
"+---+---+---+",
]
)
assert_equal(result, expected)
def test_empty_table_with_keys_as_header():
"Regression: headers='keys' on an empty table (issue #81)"
result = tabulate([], headers="keys")
expected = ""
assert_equal(result, expected)
def test_escape_empty_cell_in_first_column_in_rst():
"Regression: escape empty cells of the first column in RST format (issue #82)"
table = [["foo", 1], ["", 2], ["bar", 3]]
headers = ["", "val"]
expected = "\n".join(
[
"==== =====",
".. val",
"==== =====",
"foo 1",
".. 2",
"bar 3",
"==== =====",
]
)
result = tabulate(table, headers, tablefmt="rst")
assert_equal(result, expected)
def test_ragged_rows():
"Regression: allow rows with different number of columns (issue #85)"
table = [[1, 2, 3], [1, 2], [1, 2, 3, 4]]
expected = "\n".join(["- - - -", "1 2 3", "1 2", "1 2 3 4", "- - - -"])
result = tabulate(table)
assert_equal(result, expected)
def test_empty_pipe_table_with_columns():
"Regression: allow empty pipe tables with columns, like empty dataframes (github issue #15)"
table = []
headers = ["Col1", "Col2"]
expected = "\n".join(["| Col1 | Col2 |", "|--------|--------|"])
result = tabulate(table, headers, tablefmt="pipe")
assert_equal(result, expected)
def test_custom_tablefmt():
"Regression: allow custom TableFormat that specifies with_header_hide (github issue #20)"
tablefmt = TableFormat(
lineabove=Line("", "-", " ", ""),
linebelowheader=Line("", "-", " ", ""),
linebetweenrows=None,
linebelow=Line("", "-", " ", ""),
headerrow=DataRow("", " ", ""),
datarow=DataRow("", " ", ""),
padding=0,
with_header_hide=["lineabove", "linebelow"],
)
rows = [["foo", "bar"], ["baz", "qux"]]
expected = "\n".join(["A B", "--- ---", "foo bar", "baz qux"])
result = tabulate(rows, headers=["A", "B"], tablefmt=tablefmt)
assert_equal(result, expected)
def test_string_with_comma_between_digits_without_floatfmt_grouping_option():
"Regression: accept commas in numbers-as-text when grouping is not defined (github issue #110)"
table = [["126,000"]]
expected = "126,000"
result = tabulate(table, tablefmt="plain")
assert_equal(result, expected) # no exception
def test_iterable_row_index():
"Regression: accept 'infinite' row indices (github issue #175)"
table = [["a"], ["b"], ["c"]]
def count(start, step=1):
n = start
while True:
yield n
n += step
if n >= 10: # safety valve
raise IndexError("consuming too many values from the count iterator")
expected = "1 a\n2 b\n3 c"
result = tabulate(table, showindex=count(1), tablefmt="plain")
assert_equal(result, expected)
|