Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,10 +535,10 @@ def escape_empty(val):
_multiline_codes = re.compile(r"\r|\n|\r\n")
_multiline_codes_bytes = re.compile(b"\r|\n|\r\n")
_invisible_codes = re.compile(
r"\x1b\[\d+[;\d]*m|\x1b\[\d*\;\d*\;\d*m"
r"\x1b\[\d+[;\d]*m|\x1b\[\d*\;\d*\;\d*m|\x1b\]8;;(.*?)\x1b\\"
) # ANSI color codes
_invisible_codes_bytes = re.compile(
b"\x1b\\[\\d+\\[;\\d]*m|\x1b\\[\\d*;\\d*;\\d*m"
b"\x1b\\[\\d+\\[;\\d]*m|\x1b\\[\\d*;\\d*;\\d*m|\\x1b\\]8;;(.*?)\\x1b\\\\"
) # ANSI color codes


Expand Down
71 changes: 71 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def test_plain_multiline():
assert_equal(expected, result)


def test_plain_multiline_with_links():
"Output: plain with multiline cells with links and headers"
table = [[2, "foo\nbar"]]
headers = ("more\nspam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\", "more spam\n& eggs")
expected = "\n".join(
[
" more more spam",
" spam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\ & eggs",
" 2 foo",
" bar",
]
)
result = tabulate(table, headers, tablefmt="plain")
assert_equal(expected, result)


def test_plain_multiline_with_empty_cells():
"Output: plain with multiline cells and empty cells with headers"
table = [
Expand Down Expand Up @@ -162,6 +178,23 @@ def test_simple_multiline():
assert_equal(expected, result)


def test_simple_multiline_with_links():
"Output: simple with multiline cells with links and headers"
table = [[2, "foo\nbar"]]
headers = ("more\nspam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\", "more spam\n& eggs")
expected = "\n".join(
[
" more more spam",
" spam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\ & eggs",
"----------- -----------",
" 2 foo",
" bar",
]
)
result = tabulate(table, headers, tablefmt="simple")
assert_equal(expected, result)


def test_simple_multiline_with_empty_cells():
"Output: simple with multiline cells and empty cells with headers"
table = [
Expand Down Expand Up @@ -766,6 +799,25 @@ def test_pretty_multiline():
assert_equal(expected, result)


def test_pretty_multiline_with_links():
"Output: pretty with multiline cells with headers"
table = [[2, "foo\nbar"]]
headers = ("more\nspam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\", "more spam\n& eggs")
expected = "\n".join(
[
"+-----------+-----------+",
"| more | more spam |",
"| spam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\ | & eggs |",
"+-----------+-----------+",
"| 2 | foo |",
"| | bar |",
"+-----------+-----------+",
]
)
result = tabulate(table, headers, tablefmt="pretty")
assert_equal(expected, result)


def test_pretty_multiline_with_empty_cells():
"Output: pretty with multiline cells and empty cells with headers"
table = [
Expand Down Expand Up @@ -889,6 +941,25 @@ def test_rst_multiline():
assert_equal(expected, result)


def test_rst_multiline_with_links():
"Output: rst with multiline cells with headers"
table = [[2, "foo\nbar"]]
headers = ("more\nspam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\", "more spam\n& eggs")
expected = "\n".join(
[
"=========== ===========",
" more more spam",
" spam \x1b]8;;target\x1b\\eggs\x1b]8;;\x1b\\ & eggs",
"=========== ===========",
" 2 foo",
" bar",
"=========== ===========",
]
)
result = tabulate(table, headers, tablefmt="rst")
assert_equal(expected, result)


def test_rst_multiline_with_empty_cells():
"Output: rst with multiline cells and empty cells with headers"
table = [
Expand Down
46 changes: 46 additions & 0 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,52 @@ def test_alignment_of_colored_cells():
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("expected: %r\n\ngot: %r\n" % (expected, formatted))
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("expected: %r\n\ngot: %r\n" % (expected, formatted))
assert_equal(expected, formatted)


def test_iter_of_iters_with_headers():
"Regression: Generator of generators with a gen. of headers (issue #9)."

Expand Down