From 289b1ae2e4fd1e86e9b9c68c92339add631014e9 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 09:02:18 -0700 Subject: [PATCH 01/11] add test dependencies to pyproject.toml --- pyproject.toml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 5a8c1fd..e15df51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,12 @@ Homepage = "https://github.com/astanin/python-tabulate" [project.optional-dependencies] widechars = ["wcwidth"] +test = [ + "pytest", + "numpy", + "pandas", + "wcwidth", +] [project.scripts] tabulate = "tabulate:_main" @@ -37,3 +43,7 @@ packages = ["tabulate"] [tool.setuptools_scm] write_to = "tabulate/version.py" + +[tool.pytest.ini_options] +minversion = "6.0" +testpaths = ["tests"] From 1fb5aa00a70ec38e9e9279eacd4a65dadb9b9828 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 11:05:09 -0700 Subject: [PATCH 02/11] Turn TableFormat into a class with optional values --- tabulate/__init__.py | 162 +++++++++++++++++++++++-------------------- 1 file changed, 88 insertions(+), 74 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 11bb865..a943395 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -56,50 +56,66 @@ def _is_file(f): DataRow = namedtuple("DataRow", ["begin", "sep", "end"]) -# A table structure is supposed to be: -# -# --- lineabove --------- -# headerrow -# --- linebelowheader --- -# datarow -# --- linebetweenrows --- -# ... (more datarows) ... -# --- linebetweenrows --- -# last datarow -# --- linebelow --------- -# -# TableFormat's line* elements can be -# -# - either None, if the element is not used, -# - or a Line tuple, -# - or a function: [col_widths], [col_alignments] -> string. -# -# TableFormat's *row elements can be -# -# - either None, if the element is not used, -# - or a DataRow tuple, -# - or a function: [cell_values], [col_widths], [col_alignments] -> string. -# -# padding (an integer) is the amount of white space around data values. -# -# with_header_hide: -# -# - either None, to display all table elements unconditionally, -# - or a list of elements not to be displayed if the table has column headers. -# -TableFormat = namedtuple( - "TableFormat", - [ - "lineabove", - "linebelowheader", - "linebetweenrows", - "linebelow", - "headerrow", - "datarow", - "padding", - "with_header_hide", - ], -) +class TableFormat: + """ + TableFormat is a data container + + A table structure is supposed to be: + + --- lineabove --------- + headerrow + --- linebelowheader --- + datarow + --- linebetweenrows --- + ... (more datarows) ... + --- linebetweenrows --- + last datarow + --- linebelow --------- + + TableFormat's line* elements can be + + - either None, if the element is not used, + - or a Line tuple, + - or a function: [col_widths], [col_alignments] -> string. + + TableFormat's *row elements can be + + - either None, if the element is not used, + - or a DataRow tuple, + - or a function: [cell_values], [col_widths], [col_alignments] -> string. + + padding (an integer) is the amount of white space around data values. + + with_header_hide: + + - either None, to display all table elements unconditionally, + - or a list of elements not to be displayed if the table has column headers. + + """ + def __init__(self, + lineabove, linebelowheader, linebetweenrows, linebelow, headerrow, datarow, padding, with_header_hide, + min_padding=MIN_PADDING, + disable_numparse=None, + numalign="decimal", + stralign="left", + multiline=True, + ): + # tuple fields + self.lineabove = lineabove + self.linebelowheader = linebelowheader + self.linebetweenrows = linebetweenrows + self.linebelow = linebelow + self.headerrow = headerrow + self.datarow = datarow + self.padding = padding + self.with_header_hide = with_header_hide + + # optional fields + self.min_padding = min_padding + self.disable_numparse = disable_numparse + self.numalign = numalign + self.stralign = stralign + self.multiline = multiline def _is_separating_line(row): @@ -479,6 +495,7 @@ def escape_empty(val): datarow=DataRow("|", "|", "|"), padding=1, with_header_hide=["lineabove"], + multiline=False, ), "pipe": TableFormat( lineabove=_pipe_line_with_colons, @@ -529,6 +546,10 @@ def escape_empty(val): datarow=DataRow("|", "|", "|"), padding=1, with_header_hide=None, + min_padding=0, + disable_numparse=True, + numalign="center", + stralign="center", ), "psql": TableFormat( lineabove=Line("+", "-", "+", "+"), @@ -564,6 +585,7 @@ def escape_empty(val): datarow=partial(_mediawiki_row_with_attrs, "|"), padding=0, with_header_hide=None, + multiline=False, ), "moinmoin": TableFormat( lineabove=None, @@ -574,6 +596,7 @@ def escape_empty(val): datarow=partial(_moin_row_with_attrs, "||"), padding=1, with_header_hide=None, + multiline=False, ), "youtrack": TableFormat( lineabove=None, @@ -584,6 +607,7 @@ def escape_empty(val): datarow=DataRow("| ", " | ", " |"), padding=1, with_header_hide=None, + multiline=False, ), "html": TableFormat( lineabove=_html_begin_table_without_header, @@ -594,6 +618,7 @@ def escape_empty(val): datarow=partial(_html_row_with_attrs, "td", False), padding=0, with_header_hide=["lineabove"], + multiline=False, ), "unsafehtml": TableFormat( lineabove=_html_begin_table_without_header, @@ -604,6 +629,7 @@ def escape_empty(val): datarow=partial(_html_row_with_attrs, "td", True), padding=0, with_header_hide=["lineabove"], + multiline=False, ), "latex": TableFormat( lineabove=_latex_line_begin_tabular, @@ -614,6 +640,7 @@ def escape_empty(val): datarow=_latex_row, padding=1, with_header_hide=None, + multiline=False ), "latex_raw": TableFormat( lineabove=_latex_line_begin_tabular, @@ -624,6 +651,7 @@ def escape_empty(val): datarow=partial(_latex_row, escrules={}), padding=1, with_header_hide=None, + multiline=False, ), "latex_booktabs": TableFormat( lineabove=partial(_latex_line_begin_tabular, booktabs=True), @@ -634,6 +662,7 @@ def escape_empty(val): datarow=_latex_row, padding=1, with_header_hide=None, + multiline=False, ), "latex_longtable": TableFormat( lineabove=partial(_latex_line_begin_tabular, longtable=True), @@ -644,6 +673,7 @@ def escape_empty(val): datarow=_latex_row, padding=1, with_header_hide=None, + multiline=False, ), "tsv": TableFormat( lineabove=None, @@ -654,6 +684,7 @@ def escape_empty(val): datarow=DataRow("", "\t", ""), padding=0, with_header_hide=None, + multiline=False, ), "textile": TableFormat( lineabove=None, @@ -664,6 +695,7 @@ def escape_empty(val): datarow=_textile_row_with_attrs, padding=1, with_header_hide=None, + multiline=False, ), "asciidoc": TableFormat( lineabove=partial(_asciidoc_row, False), @@ -674,6 +706,7 @@ def escape_empty(val): datarow=partial(_asciidoc_row, False), padding=1, with_header_hide=["lineabove"], + multiline=False, ), } @@ -2082,6 +2115,9 @@ def tabulate( """ + if not isinstance(tablefmt, TableFormat): + tablefmt = _table_formats.get(tablefmt, _table_formats["simple"]) + if tabular_data is None: tabular_data = [] @@ -2124,19 +2160,9 @@ def tabulate( if tablefmt == "rst": list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers) - # PrettyTable formatting does not use any extra padding. - # Numbers are not parsed and are treated the same as strings for alignment. - # Check if pretty is the format being used and override the defaults so it - # does not impact other formats. - min_padding = MIN_PADDING - if tablefmt == "pretty": - min_padding = 0 - disable_numparse = True - numalign = "center" if numalign == _DEFAULT_ALIGN else numalign - stralign = "center" if stralign == _DEFAULT_ALIGN else stralign - else: - numalign = "decimal" if numalign == _DEFAULT_ALIGN else numalign - stralign = "left" if stralign == _DEFAULT_ALIGN else stralign + disable_numparse = tablefmt.disable_numparse if tablefmt.disable_numparse is not None else disable_numparse + numalign = tablefmt.numalign if numalign == _DEFAULT_ALIGN else numalign + stralign = tablefmt.stralign if stralign == _DEFAULT_ALIGN else stralign # optimization: look for ANSI control codes once, # enable smart width functions only if a control code is found @@ -2156,16 +2182,7 @@ def tabulate( has_invisible = _ansi_codes.search(plain_text) is not None enable_widechars = wcwidth is not None and WIDE_CHARS_MODE - if ( - not isinstance(tablefmt, TableFormat) - and tablefmt in multiline_formats - and _is_multiline(plain_text) - ): - tablefmt = multiline_formats.get(tablefmt, tablefmt) - is_multiline = True - else: - is_multiline = False - width_fn = _choose_width_fn(has_invisible, enable_widechars, is_multiline) + width_fn = _choose_width_fn(has_invisible, enable_widechars, tablefmt.multiline) # format rows and columns, convert numeric values to strings cols = list(izip_longest(*list_of_lists)) @@ -2217,10 +2234,10 @@ def tabulate( elif align != "global": aligns[idx] = align minwidths = ( - [width_fn(h) + min_padding for h in headers] if headers else [0] * len(cols) + [width_fn(h) + tablefmt.min_padding for h in headers] if headers else [0] * len(cols) ) cols = [ - _align_column(c, a, minw, has_invisible, enable_widechars, is_multiline) + _align_column(c, a, minw, has_invisible, enable_widechars, tablefmt.multiline) for c, a, minw in zip(cols, aligns, minwidths) ] @@ -2251,7 +2268,7 @@ def tabulate( for minw, c in zip(minwidths, t_cols) ] headers = [ - _align_header(h, a, minw, width_fn(h), is_multiline, width_fn) + _align_header(h, a, minw, width_fn(h), tablefmt.multiline, width_fn) for h, a, minw in zip(headers, aligns_headers, minwidths) ] rows = list(zip(*cols)) @@ -2259,15 +2276,12 @@ def tabulate( minwidths = [max(width_fn(cl) for cl in c) for c in cols] rows = list(zip(*cols)) - if not isinstance(tablefmt, TableFormat): - tablefmt = _table_formats.get(tablefmt, _table_formats["simple"]) - ra_default = rowalign if isinstance(rowalign, str) else None rowaligns = _expand_iterable(rowalign, len(rows), ra_default) _reinsert_separating_lines(rows, separating_lines) return _format_table( - tablefmt, headers, aligns_headers, rows, minwidths, aligns, is_multiline, rowaligns=rowaligns + tablefmt, headers, aligns_headers, rows, minwidths, aligns, tablefmt.multiline, rowaligns=rowaligns ) From 970383b9885494f8afb72066cca7552bd9da205d Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 11:10:16 -0700 Subject: [PATCH 03/11] inline assert_equals and remove noisy print statements in testing so that pytest can show useful diffs --- test/common.py | 5 - test/test_cli.py | 41 +---- test/test_input.py | 50 +++--- test/test_internal.py | 48 ++--- test/test_output.py | 368 +++++++++++++++++++-------------------- test/test_regression.py | 74 ++++---- test/test_textwrapper.py | 18 +- 7 files changed, 287 insertions(+), 317 deletions(-) diff --git a/test/common.py b/test/common.py index 4cd3709..029652d 100644 --- a/test/common.py +++ b/test/common.py @@ -2,11 +2,6 @@ from pytest import skip, raises # noqa import warnings -def assert_equal(expected, result): - print("Expected:\n%s\n" % expected) - print("Got:\n%s\n" % result) - assert expected == result - def assert_in(result, expected_set): nums = range(1, len(expected_set) + 1) diff --git a/test/test_cli.py b/test/test_cli.py index ce85f19..a313b88 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -2,18 +2,11 @@ """ - import os import sys - - import subprocess import tempfile - -from common import assert_equal - - SAMPLE_SIMPLE_FORMAT = "\n".join( [ "----- ------ -------------", @@ -116,9 +109,7 @@ def test_script_from_stdin_to_stdout(): cmd = [sys.executable, "tabulate/__init__.py"] out = run_and_capture_stdout(cmd, input=sample_input()) expected = SAMPLE_SIMPLE_FORMAT - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_from_file_to_stdout(): @@ -129,9 +120,7 @@ def test_script_from_file_to_stdout(): cmd = [sys.executable, "tabulate/__init__.py", tmpfile.name] out = run_and_capture_stdout(cmd) expected = SAMPLE_SIMPLE_FORMAT - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_from_file_to_file(): @@ -150,16 +139,12 @@ def test_script_from_file_to_file(): out = run_and_capture_stdout(cmd) # check that nothing is printed to stdout expected = "" - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() # check that the output was written to file output_file.seek(0) out = output_file.file.read() expected = SAMPLE_SIMPLE_FORMAT - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_header_option(): @@ -169,10 +154,7 @@ def test_script_header_option(): raw_table = sample_input(with_headers=True) out = run_and_capture_stdout(cmd, input=raw_table) expected = SAMPLE_SIMPLE_FORMAT_WITH_HEADERS - print(out) - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_sep_option(): @@ -182,9 +164,7 @@ def test_script_sep_option(): raw_table = sample_input(sep=",") out = run_and_capture_stdout(cmd, input=raw_table) expected = SAMPLE_SIMPLE_FORMAT - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_floatfmt_option(): @@ -201,9 +181,7 @@ def test_script_floatfmt_option(): raw_table = sample_input() out = run_and_capture_stdout(cmd, input=raw_table) expected = SAMPLE_GRID_FORMAT_WITH_DOT1E_FLOATS - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() def test_script_format_option(): @@ -213,7 +191,4 @@ def test_script_format_option(): raw_table = sample_input(with_headers=True) out = run_and_capture_stdout(cmd, input=raw_table) expected = SAMPLE_GRID_FORMAT_WITH_HEADERS - print(out) - print("got: ", repr(out)) - print("expected:", repr(expected)) - assert_equal(out.splitlines(), expected.splitlines()) + assert out.splitlines() == expected.splitlines() diff --git a/test/test_input.py b/test/test_input.py index a178bd9..304939c 100644 --- a/test/test_input.py +++ b/test/test_input.py @@ -1,7 +1,7 @@ """Test support of the various forms of tabular data.""" from tabulate import tabulate -from common import assert_equal, assert_in, raises, skip +from common import assert_in, raises, skip try: from collections import UserDict @@ -17,7 +17,7 @@ def test_iterable_of_iterables(): ["- - - - -", "0 1 2 3 4", "5 4 3 2 1", "- - - - -"] ) result = tabulate(ii) - assert_equal(expected, result) + assert expected == result def test_iterable_of_iterables_headers(): @@ -32,7 +32,7 @@ def test_iterable_of_iterables_headers(): ] ) result = tabulate(ii, "abcde") - assert_equal(expected, result) + assert expected == result def test_iterable_of_iterables_firstrow(): @@ -47,7 +47,7 @@ def test_iterable_of_iterables_firstrow(): ] ) result = tabulate(ii, "firstrow") - assert_equal(expected, result) + assert expected == result def test_list_of_lists(): @@ -62,7 +62,7 @@ def test_list_of_lists(): ] ) result = tabulate(ll, headers=["string", "number"]) - assert_equal(expected, result) + assert expected == result def test_list_of_lists_firstrow(): @@ -77,7 +77,7 @@ def test_list_of_lists_firstrow(): ] ) result = tabulate(ll, headers="firstrow") - assert_equal(expected, result) + assert expected == result def test_list_of_lists_keys(): @@ -87,7 +87,7 @@ def test_list_of_lists_keys(): ["0 1 2", "--- --- ---", "a one 1", "b two"] ) result = tabulate(ll, headers="keys") - assert_equal(expected, result) + assert expected == result def test_dict_like(): @@ -123,7 +123,7 @@ def test_numpy_2d(): ] ) result = tabulate(na, ["a", "b", "c"]) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d is skipped") @@ -138,7 +138,7 @@ def test_numpy_2d_firstrow(): [" 1 8 27", "--- --- ----", " 64 125 216", "343 512 729"] ) result = tabulate(na, headers="firstrow") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d_firstrow is skipped") @@ -159,7 +159,7 @@ def test_numpy_2d_keys(): ] ) result = tabulate(na, headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d_keys is skipped") @@ -185,7 +185,7 @@ def test_numpy_record_array(): ] ) result = tabulate(na) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d_keys is skipped") @@ -211,7 +211,7 @@ def test_numpy_record_array_keys(): ] ) result = tabulate(na, headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d_keys is skipped") @@ -237,7 +237,7 @@ def test_numpy_record_array_headers(): ] ) result = tabulate(na, headers=["person", "years", "cm"]) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_numpy_2d_keys is skipped") @@ -257,7 +257,7 @@ def test_pandas(): ] ) result = tabulate(df, headers=["string", "number"]) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas is skipped") @@ -274,7 +274,7 @@ def test_pandas_firstrow(): ["a one 1.0", "--- ----- -----", "b two nan"] ) result = tabulate(df, headers="firstrow") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_firstrow is skipped") @@ -296,7 +296,7 @@ def test_pandas_keys(): ] ) result = tabulate(df, headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_keys is skipped") @@ -318,7 +318,7 @@ def test_sqlite3(): ------ --------- ---------- Alice 23 169.5 Bob 27 175""" - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_sqlite3 is skipped") @@ -342,7 +342,7 @@ def test_sqlite3_keys(): ------ --------- ---------- Alice 23 169.5 Bob 27 175""" - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_sqlite3_keys is skipped") @@ -355,7 +355,7 @@ def test_list_of_namedtuples(): lt = [NT(1, 2), NT(3, 4)] expected = "\n".join(["- -", "1 2", "3 4", "- -"]) result = tabulate(lt) - assert_equal(expected, result) + assert expected == result def test_list_of_namedtuples_keys(): @@ -368,7 +368,7 @@ def test_list_of_namedtuples_keys(): [" foo bar", "----- -----", " 1 2", " 3 4"] ) result = tabulate(lt, headers="keys") - assert_equal(expected, result) + assert expected == result def test_list_of_dicts(): @@ -428,7 +428,7 @@ def test_list_of_dicts_with_missing_keys(): ] ) result = tabulate(lod, headers="keys") - assert_equal(expected, result) + assert expected == result def test_list_of_dicts_firstrow(): @@ -475,7 +475,7 @@ def test_list_of_ordereddicts(): lod = [od, od] expected = "\n".join([" b a", "--- ---", " 1 2", " 1 2"]) result = tabulate(lod, headers="keys") - assert_equal(expected, result) + assert expected == result def test_py37orlater_list_of_dataclasses_keys(): @@ -494,7 +494,7 @@ def test_py37orlater_list_of_dataclasses_keys(): "Bob 27 175", ] ) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_py37orlater_list_of_dataclasses_keys is skipped") @@ -515,7 +515,7 @@ def test_py37orlater_list_of_dataclasses_headers(): "Bob 27 175", ] ) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_py37orlater_list_of_dataclasses_headers is skipped") @@ -527,4 +527,4 @@ def test_list_bytes(): ["bytes", "---------------------------", r"b'\xe4\xbd\xa0\xe5\xa5\xbd'", "你好"] ) result = tabulate(lb, headers=["bytes"]) - assert_equal(expected, result) + assert expected == result diff --git a/test/test_internal.py b/test/test_internal.py index 64e1d12..54e22a9 100644 --- a/test/test_internal.py +++ b/test/test_internal.py @@ -2,15 +2,15 @@ import tabulate as T -from common import assert_equal, skip, rows_to_pipe_table_str, cols_to_pipe_str +from common import skip, rows_to_pipe_table_str, cols_to_pipe_str def test_multiline_width(): "Internal: _multiline_width()" multiline_string = "\n".join(["foo", "barbaz", "spam"]) - assert_equal(T._multiline_width(multiline_string), 6) + assert T._multiline_width(multiline_string) == 6 oneline_string = "12345" - assert_equal(T._multiline_width(oneline_string), len(oneline_string)) + assert T._multiline_width(oneline_string) == len(oneline_string) def test_align_column_decimal(): @@ -25,7 +25,7 @@ def test_align_column_decimal(): " 1e+234 ", " 1.0e234", ] - assert_equal(expected, result) + assert expected == result def test_align_column_decimal_with_thousand_separators(): @@ -40,7 +40,7 @@ def test_align_column_decimal_with_thousand_separators(): " 1e+234 ", " 1.0e234", ] - assert_equal(expected, output) + assert expected == output def test_align_column_decimal_with_incorrect_thousand_separators(): @@ -55,7 +55,7 @@ def test_align_column_decimal_with_incorrect_thousand_separators(): " 1e+234 ", " 1.0e234", ] - assert_equal(expected, output) + assert expected == output def test_align_column_none(): @@ -63,7 +63,7 @@ def test_align_column_none(): column = ["123.4", "56.7890"] output = T._align_column(column, None) expected = ["123.4", "56.7890"] - assert_equal(expected, output) + assert expected == output def test_align_column_multiline(): @@ -71,7 +71,7 @@ def test_align_column_multiline(): column = ["1", "123", "12345\n6"] output = T._align_column(column, "center", is_multiline=True) expected = [" 1 ", " 123 ", "12345" + "\n" + " 6 "] - assert_equal(expected, output) + assert expected == output def test_align_cell_veritically_one_line_only(): @@ -94,7 +94,7 @@ def test_align_cell_veritically_top_single_text_multiple_pad(): expected = ["one line", " ", " "] - assert_equal(expected, result) + assert expected == result def test_align_cell_veritically_center_single_text_multiple_pad(): @@ -103,7 +103,7 @@ def test_align_cell_veritically_center_single_text_multiple_pad(): expected = [" ", "one line", " "] - assert_equal(expected, result) + assert expected == result def test_align_cell_veritically_bottom_single_text_multiple_pad(): @@ -112,7 +112,7 @@ def test_align_cell_veritically_bottom_single_text_multiple_pad(): expected = [" ", " ", "one line"] - assert_equal(expected, result) + assert expected == result def test_align_cell_veritically_top_multi_text_multiple_pad(): @@ -122,7 +122,7 @@ def test_align_cell_veritically_top_multi_text_multiple_pad(): expected = ["just", "one ", "cell", " ", " ", " "] - assert_equal(expected, result) + assert expected == result def test_align_cell_veritically_center_multi_text_multiple_pad(): @@ -134,7 +134,7 @@ def test_align_cell_veritically_center_multi_text_multiple_pad(): # at top when required to do make a judgement expected = [" ", "just", "one ", "cell", " ", " "] - assert_equal(expected, result) + assert expected == result def test_align_cell_veritically_bottom_multi_text_multiple_pad(): @@ -144,7 +144,7 @@ def test_align_cell_veritically_bottom_multi_text_multiple_pad(): expected = [" ", " ", " ", "just", "one ", "cell"] - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_colwidths(): @@ -170,7 +170,7 @@ def test_wrap_text_to_colwidths(): ] result = T._wrap_text_to_colwidths(rows, widths) - assert_equal(expected, result) + assert expected == result def test_wrap_text_wide_chars(): @@ -185,7 +185,7 @@ def test_wrap_text_wide_chars(): expected = [["청자\n청자\n청자\n청자\n청자", "약간 감싸면 더 잘\n보일 수있는 다소 긴\n설명입니다"]] result = T._wrap_text_to_colwidths(rows, widths) - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_numbers(): @@ -202,7 +202,7 @@ def test_wrap_text_to_numbers(): ] result = T._wrap_text_to_colwidths(rows, widths, numparses=[True, True, False]) - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_colwidths_single_ansi_colors_full_cell(): @@ -229,7 +229,7 @@ def test_wrap_text_to_colwidths_single_ansi_colors_full_cell(): ) ] ] - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_colwidths_colors_wide_char(): @@ -253,7 +253,7 @@ def test_wrap_text_to_colwidths_colors_wide_char(): ) ] ] - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_colwidths_multi_ansi_colors_full_cell(): @@ -281,7 +281,7 @@ def test_wrap_text_to_colwidths_multi_ansi_colors_full_cell(): ) ] ] - assert_equal(expected, result) + assert expected == result def test_wrap_text_to_colwidths_multi_ansi_colors_in_subset(): @@ -308,7 +308,7 @@ def test_wrap_text_to_colwidths_multi_ansi_colors_in_subset(): ) ] ] - assert_equal(expected, result) + assert expected == result def test__remove_separating_lines(): @@ -324,8 +324,8 @@ def test__remove_separating_lines(): result, sep_lines = T._remove_separating_lines(with_rows) expected = rows_to_pipe_table_str([[0, "a"], [1, "b"], [2, "c"], [3, "c"]]) - assert_equal(expected, rows_to_pipe_table_str(result)) - assert_equal("2|4|6", cols_to_pipe_str(sep_lines)) + assert expected == rows_to_pipe_table_str(result) + assert "2|4|6" == cols_to_pipe_str(sep_lines) def test__reinsert_separating_lines(): @@ -342,4 +342,4 @@ def test__reinsert_separating_lines(): T._reinsert_separating_lines(sans_rows, sep_lines) expected = rows_to_pipe_table_str(with_rows) - assert_equal(expected, rows_to_pipe_table_str(sans_rows)) + assert expected == rows_to_pipe_table_str(sans_rows) diff --git a/test/test_output.py b/test/test_output.py index d572498..3e141d4 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1,7 +1,7 @@ """Test output of the various forms of tabular data.""" import tabulate as tabulate_module -from common import assert_equal, raises, skip, check_warnings +from common import raises, skip, check_warnings from tabulate import tabulate, simple_separated_format, SEPARATING_LINE # _test_table shows @@ -19,14 +19,14 @@ def test_plain(): ["strings numbers", "spam 41.9999", "eggs 451"] ) result = tabulate(_test_table, _test_table_headers, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_headerless(): "Output: plain without headers" expected = "\n".join(["spam 41.9999", "eggs 451"]) result = tabulate(_test_table, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_multiline_headerless(): @@ -36,7 +36,7 @@ def test_plain_multiline_headerless(): ["foo bar hello", " baz", " bau", " multiline", " world"] ) result = tabulate(table, stralign="center", tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_multiline(): @@ -52,7 +52,7 @@ def test_plain_multiline(): ] ) result = tabulate(table, headers, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_multiline_with_links(): @@ -68,7 +68,7 @@ def test_plain_multiline_with_links(): ] ) result = tabulate(table, headers, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_multiline_with_empty_cells(): @@ -87,7 +87,7 @@ def test_plain_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_multiline_with_empty_cells_headerless(): @@ -97,7 +97,7 @@ def test_plain_multiline_with_empty_cells_headerless(): ["0", "1", "2 very long data fold", " this"] ) result = tabulate(table, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_plain_maxcolwidth_autowraps(): @@ -107,7 +107,7 @@ def test_plain_maxcolwidth_autowraps(): result = tabulate( table, headers="firstrow", tablefmt="plain", maxcolwidths=[10, 10] ) - assert_equal(expected, result) + assert expected == result def test_plain_maxcolwidth_autowraps_with_sep(): @@ -124,7 +124,7 @@ def test_plain_maxcolwidth_autowraps_with_sep(): result = tabulate( table, headers="firstrow", tablefmt="plain", maxcolwidths=[10, 10] ) - assert_equal(expected, result) + assert expected == result def test_plain_maxcolwidth_autowraps_wide_chars(): @@ -149,7 +149,7 @@ def test_plain_maxcolwidth_autowraps_wide_chars(): result = tabulate( table, headers="firstrow", tablefmt="plain", maxcolwidths=[10, 30] ) - assert_equal(expected, result) + assert expected == result def test_maxcolwidth_single_value(): @@ -168,7 +168,7 @@ def test_maxcolwidth_single_value(): ] ) result = tabulate(table, headers="firstrow", tablefmt="plain", maxcolwidths=6) - assert_equal(expected, result) + assert expected == result def test_maxcolwidth_pad_tailing_widths(): @@ -188,7 +188,7 @@ def test_maxcolwidth_pad_tailing_widths(): result = tabulate( table, headers="firstrow", tablefmt="plain", maxcolwidths=[None, 6] ) - assert_equal(expected, result) + assert expected == result def test_maxcolwidth_honor_disable_parsenum(): @@ -211,7 +211,7 @@ def test_maxcolwidth_honor_disable_parsenum(): ) # Grid makes showing the alignment difference a little easier result = tabulate(table, tablefmt="grid", maxcolwidths=6, disable_numparse=[2]) - assert_equal(expected, result) + assert expected == result def test_plain_maxheadercolwidths_autowraps(): @@ -225,7 +225,7 @@ def test_plain_maxheadercolwidths_autowraps(): maxcolwidths=[10, 10], maxheadercolwidths=[None, 2], ) - assert_equal(expected, result) + assert expected == result def test_simple(): @@ -239,7 +239,7 @@ def test_simple(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_with_sep_line(): @@ -254,7 +254,7 @@ def test_simple_with_sep_line(): ] ) result = tabulate(_test_table_with_sep_line, _test_table_headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_readme_example_with_sep(): @@ -270,7 +270,7 @@ def test_readme_example_with_sep(): ] ) result = tabulate(table, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_2(): @@ -286,7 +286,7 @@ def test_simple_multiline_2(): ) table = [["key", "value"], ["foo", "bar"], ["spam", "multiline\nworld"]] result = tabulate(table, headers="firstrow", stralign="center", tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_2_with_sep_line(): @@ -308,7 +308,7 @@ def test_simple_multiline_2_with_sep_line(): ["spam", "multiline\nworld"], ] result = tabulate(table, headers="firstrow", stralign="center", tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_headerless(): @@ -317,7 +317,7 @@ def test_simple_headerless(): ["---- --------", "spam 41.9999", "eggs 451", "---- --------"] ) result = tabulate(_test_table, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_headerless_with_sep_line(): @@ -332,7 +332,7 @@ def test_simple_headerless_with_sep_line(): ] ) result = tabulate(_test_table_with_sep_line, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_headerless(): @@ -350,7 +350,7 @@ def test_simple_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline(): @@ -367,7 +367,7 @@ def test_simple_multiline(): ] ) result = tabulate(table, headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_with_links(): @@ -384,7 +384,7 @@ def test_simple_multiline_with_links(): ] ) result = tabulate(table, headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_with_empty_cells(): @@ -404,7 +404,7 @@ def test_simple_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_simple_multiline_with_empty_cells_headerless(): @@ -421,7 +421,7 @@ def test_simple_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_github(): @@ -435,7 +435,7 @@ def test_github(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="github") - assert_equal(expected, result) + assert expected == result def test_grid(): @@ -452,7 +452,7 @@ def test_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_wide_characters(): @@ -475,7 +475,7 @@ def test_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_headerless(): @@ -490,7 +490,7 @@ def test_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_multiline_headerless(): @@ -509,7 +509,7 @@ def test_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_multiline(): @@ -528,7 +528,7 @@ def test_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_multiline_with_empty_cells(): @@ -551,7 +551,7 @@ def test_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_grid_multiline_with_empty_cells_headerless(): @@ -570,7 +570,7 @@ def test_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid(): @@ -587,7 +587,7 @@ def test_simple_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_wide_characters(): @@ -610,7 +610,7 @@ def test_simple_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_headerless(): @@ -625,7 +625,7 @@ def test_simple_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_multiline_headerless(): @@ -644,7 +644,7 @@ def test_simple_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_multiline(): @@ -663,7 +663,7 @@ def test_simple_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_multiline_with_empty_cells(): @@ -686,7 +686,7 @@ def test_simple_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_simple_grid_multiline_with_empty_cells_headerless(): @@ -705,7 +705,7 @@ def test_simple_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="simple_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid(): @@ -722,7 +722,7 @@ def test_rounded_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_wide_characters(): @@ -745,7 +745,7 @@ def test_rounded_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_headerless(): @@ -760,7 +760,7 @@ def test_rounded_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_multiline_headerless(): @@ -779,7 +779,7 @@ def test_rounded_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_multiline(): @@ -798,7 +798,7 @@ def test_rounded_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_multiline_with_empty_cells(): @@ -821,7 +821,7 @@ def test_rounded_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_rounded_grid_multiline_with_empty_cells_headerless(): @@ -840,7 +840,7 @@ def test_rounded_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="rounded_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid(): @@ -857,7 +857,7 @@ def test_heavy_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_wide_characters(): @@ -880,7 +880,7 @@ def test_heavy_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_headerless(): @@ -895,7 +895,7 @@ def test_heavy_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_multiline_headerless(): @@ -914,7 +914,7 @@ def test_heavy_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_multiline(): @@ -933,7 +933,7 @@ def test_heavy_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_multiline_with_empty_cells(): @@ -956,7 +956,7 @@ def test_heavy_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_heavy_grid_multiline_with_empty_cells_headerless(): @@ -975,7 +975,7 @@ def test_heavy_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="heavy_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid(): @@ -992,7 +992,7 @@ def test_mixed_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_wide_characters(): @@ -1015,7 +1015,7 @@ def test_mixed_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_headerless(): @@ -1030,7 +1030,7 @@ def test_mixed_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_multiline_headerless(): @@ -1049,7 +1049,7 @@ def test_mixed_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_multiline(): @@ -1068,7 +1068,7 @@ def test_mixed_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_multiline_with_empty_cells(): @@ -1091,7 +1091,7 @@ def test_mixed_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_mixed_grid_multiline_with_empty_cells_headerless(): @@ -1110,7 +1110,7 @@ def test_mixed_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="mixed_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid(): @@ -1127,7 +1127,7 @@ def test_double_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_wide_characters(): @@ -1150,7 +1150,7 @@ def test_double_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_headerless(): @@ -1165,7 +1165,7 @@ def test_double_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_multiline_headerless(): @@ -1184,7 +1184,7 @@ def test_double_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_multiline(): @@ -1203,7 +1203,7 @@ def test_double_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_multiline_with_empty_cells(): @@ -1226,7 +1226,7 @@ def test_double_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_double_grid_multiline_with_empty_cells_headerless(): @@ -1245,7 +1245,7 @@ def test_double_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="double_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid(): @@ -1262,7 +1262,7 @@ def test_fancy_grid(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_wide_characters(): @@ -1285,7 +1285,7 @@ def test_fancy_grid_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_headerless(): @@ -1300,7 +1300,7 @@ def test_fancy_grid_headerless(): ] ) result = tabulate(_test_table, tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_multiline_headerless(): @@ -1319,7 +1319,7 @@ def test_fancy_grid_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_multiline(): @@ -1338,7 +1338,7 @@ def test_fancy_grid_multiline(): ] ) result = tabulate(table, headers, tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_multiline_with_empty_cells(): @@ -1361,7 +1361,7 @@ def test_fancy_grid_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_multiline_with_empty_cells_headerless(): @@ -1380,7 +1380,7 @@ def test_fancy_grid_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="fancy_grid") - assert_equal(expected, result) + assert expected == result def test_fancy_grid_multiline_row_align(): @@ -1411,7 +1411,7 @@ def test_fancy_grid_multiline_row_align(): ] ) result = tabulate(table, tablefmt="fancy_grid", rowalign=[None, "center", "bottom"]) - assert_equal(expected, result) + assert expected == result def test_outline(): @@ -1427,7 +1427,7 @@ def test_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="outline") - assert_equal(expected, result) + assert expected == result def test_outline_wide_characters(): @@ -1449,7 +1449,7 @@ def test_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="outline") - assert_equal(expected, result) + assert expected == result def test_outline_headerless(): @@ -1463,7 +1463,7 @@ def test_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="outline") - assert_equal(expected, result) + assert expected == result def test_simple_outline(): @@ -1479,7 +1479,7 @@ def test_simple_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="simple_outline") - assert_equal(expected, result) + assert expected == result def test_simple_outline_wide_characters(): @@ -1501,7 +1501,7 @@ def test_simple_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="simple_outline") - assert_equal(expected, result) + assert expected == result def test_simple_outline_headerless(): @@ -1515,7 +1515,7 @@ def test_simple_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="simple_outline") - assert_equal(expected, result) + assert expected == result def test_rounded_outline(): @@ -1531,7 +1531,7 @@ def test_rounded_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="rounded_outline") - assert_equal(expected, result) + assert expected == result def test_rounded_outline_wide_characters(): @@ -1553,7 +1553,7 @@ def test_rounded_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="rounded_outline") - assert_equal(expected, result) + assert expected == result def test_rounded_outline_headerless(): @@ -1567,7 +1567,7 @@ def test_rounded_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="rounded_outline") - assert_equal(expected, result) + assert expected == result def test_heavy_outline(): @@ -1583,7 +1583,7 @@ def test_heavy_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="heavy_outline") - assert_equal(expected, result) + assert expected == result def test_heavy_outline_wide_characters(): @@ -1605,7 +1605,7 @@ def test_heavy_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="heavy_outline") - assert_equal(expected, result) + assert expected == result def test_heavy_outline_headerless(): @@ -1619,7 +1619,7 @@ def test_heavy_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="heavy_outline") - assert_equal(expected, result) + assert expected == result def test_mixed_outline(): @@ -1635,7 +1635,7 @@ def test_mixed_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="mixed_outline") - assert_equal(expected, result) + assert expected == result def test_mixed_outline_wide_characters(): @@ -1657,7 +1657,7 @@ def test_mixed_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="mixed_outline") - assert_equal(expected, result) + assert expected == result def test_mixed_outline_headerless(): @@ -1671,7 +1671,7 @@ def test_mixed_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="mixed_outline") - assert_equal(expected, result) + assert expected == result def test_double_outline(): @@ -1687,7 +1687,7 @@ def test_double_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="double_outline") - assert_equal(expected, result) + assert expected == result def test_double_outline_wide_characters(): @@ -1709,7 +1709,7 @@ def test_double_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="double_outline") - assert_equal(expected, result) + assert expected == result def test_double_outline_headerless(): @@ -1723,7 +1723,7 @@ def test_double_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="double_outline") - assert_equal(expected, result) + assert expected == result def test_fancy_outline(): @@ -1739,7 +1739,7 @@ def test_fancy_outline(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="fancy_outline") - assert_equal(expected, result) + assert expected == result def test_fancy_outline_wide_characters(): @@ -1761,7 +1761,7 @@ def test_fancy_outline_wide_characters(): ] ) result = tabulate(_test_table, headers, tablefmt="fancy_outline") - assert_equal(expected, result) + assert expected == result def test_fancy_outline_headerless(): @@ -1775,7 +1775,7 @@ def test_fancy_outline_headerless(): ] ) result = tabulate(_test_table, tablefmt="fancy_outline") - assert_equal(expected, result) + assert expected == result def test_pipe(): @@ -1789,7 +1789,7 @@ def test_pipe(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="pipe") - assert_equal(expected, result) + assert expected == result def test_pipe_headerless(): @@ -1798,7 +1798,7 @@ def test_pipe_headerless(): ["|:-----|---------:|", "| spam | 41.9999 |", "| eggs | 451 |"] ) result = tabulate(_test_table, tablefmt="pipe") - assert_equal(expected, result) + assert expected == result def test_presto(): @@ -1812,14 +1812,14 @@ def test_presto(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_presto_headerless(): "Output: presto without headers" expected = "\n".join([" spam | 41.9999", " eggs | 451"]) result = tabulate(_test_table, tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_presto_multiline_headerless(): @@ -1835,7 +1835,7 @@ def test_presto_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_presto_multiline(): @@ -1852,7 +1852,7 @@ def test_presto_multiline(): ] ) result = tabulate(table, headers, tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_presto_multiline_with_empty_cells(): @@ -1872,7 +1872,7 @@ def test_presto_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_presto_multiline_with_empty_cells_headerless(): @@ -1887,7 +1887,7 @@ def test_presto_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="presto") - assert_equal(expected, result) + assert expected == result def test_orgtbl(): @@ -1901,14 +1901,14 @@ def test_orgtbl(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="orgtbl") - assert_equal(expected, result) + assert expected == result def test_orgtbl_headerless(): "Output: orgtbl without headers" expected = "\n".join(["| spam | 41.9999 |", "| eggs | 451 |"]) result = tabulate(_test_table, tablefmt="orgtbl") - assert_equal(expected, result) + assert expected == result def test_asciidoc(): @@ -1924,7 +1924,7 @@ def test_asciidoc(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="asciidoc") - assert_equal(expected, result) + assert expected == result def test_asciidoc_headerless(): @@ -1939,7 +1939,7 @@ def test_asciidoc_headerless(): ] ) result = tabulate(_test_table, tablefmt="asciidoc") - assert_equal(expected, result) + assert expected == result def test_psql(): @@ -1955,7 +1955,7 @@ def test_psql(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_psql_headerless(): @@ -1969,7 +1969,7 @@ def test_psql_headerless(): ] ) result = tabulate(_test_table, tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_psql_multiline_headerless(): @@ -1987,7 +1987,7 @@ def test_psql_multiline_headerless(): ] ) result = tabulate(table, stralign="center", tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_psql_multiline(): @@ -2006,7 +2006,7 @@ def test_psql_multiline(): ] ) result = tabulate(table, headers, tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_psql_multiline_with_empty_cells(): @@ -2028,7 +2028,7 @@ def test_psql_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_psql_multiline_with_empty_cells_headerless(): @@ -2045,7 +2045,7 @@ def test_psql_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="psql") - assert_equal(expected, result) + assert expected == result def test_pretty(): @@ -2061,7 +2061,7 @@ def test_pretty(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_headerless(): @@ -2075,7 +2075,7 @@ def test_pretty_headerless(): ] ) result = tabulate(_test_table, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_multiline_headerless(): @@ -2093,7 +2093,7 @@ def test_pretty_multiline_headerless(): ] ) result = tabulate(table, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_multiline(): @@ -2112,7 +2112,7 @@ def test_pretty_multiline(): ] ) result = tabulate(table, headers, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_multiline_with_links(): @@ -2131,7 +2131,7 @@ def test_pretty_multiline_with_links(): ] ) result = tabulate(table, headers, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_multiline_with_empty_cells(): @@ -2153,7 +2153,7 @@ def test_pretty_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_pretty_multiline_with_empty_cells_headerless(): @@ -2170,7 +2170,7 @@ def test_pretty_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="pretty") - assert_equal(expected, result) + assert expected == result def test_jira(): @@ -2184,7 +2184,7 @@ def test_jira(): ) result = tabulate(_test_table, _test_table_headers, tablefmt="jira") - assert_equal(expected, result) + assert expected == result def test_jira_headerless(): @@ -2192,7 +2192,7 @@ def test_jira_headerless(): expected = "\n".join(["| spam | 41.9999 |", "| eggs | 451 |"]) result = tabulate(_test_table, tablefmt="jira") - assert_equal(expected, result) + assert expected == result def test_rst(): @@ -2208,7 +2208,7 @@ def test_rst(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_with_empty_values_in_first_column(): @@ -2226,7 +2226,7 @@ def test_rst_with_empty_values_in_first_column(): ] ) result = tabulate(test_data, test_headers, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_headerless(): @@ -2235,7 +2235,7 @@ def test_rst_headerless(): ["==== ========", "spam 41.9999", "eggs 451", "==== ========"] ) result = tabulate(_test_table, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_multiline(): @@ -2254,7 +2254,7 @@ def test_rst_multiline(): ] ) result = tabulate(table, headers, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_multiline_with_links(): @@ -2273,7 +2273,7 @@ def test_rst_multiline_with_links(): ] ) result = tabulate(table, headers, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_multiline_with_empty_cells(): @@ -2295,7 +2295,7 @@ def test_rst_multiline_with_empty_cells(): ] ) result = tabulate(table, headers="firstrow", tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_rst_multiline_with_empty_cells_headerless(): @@ -2312,7 +2312,7 @@ def test_rst_multiline_with_empty_cells_headerless(): ] ) result = tabulate(table, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_mediawiki(): @@ -2331,7 +2331,7 @@ def test_mediawiki(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="mediawiki") - assert_equal(expected, result) + assert expected == result def test_mediawiki_headerless(): @@ -2348,7 +2348,7 @@ def test_mediawiki_headerless(): ] ) result = tabulate(_test_table, tablefmt="mediawiki") - assert_equal(expected, result) + assert expected == result def test_moinmoin(): @@ -2361,7 +2361,7 @@ def test_moinmoin(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="moinmoin") - assert_equal(expected, result) + assert expected == result def test_youtrack(): @@ -2374,7 +2374,7 @@ def test_youtrack(): ] ) result = tabulate(_test_table, _test_table_headers, tablefmt="youtrack") - assert_equal(expected, result) + assert expected == result def test_moinmoin_headerless(): @@ -2386,7 +2386,7 @@ def test_moinmoin_headerless(): ] ) result = tabulate(_test_table, tablefmt="moinmoin") - assert_equal(expected, result) + assert expected == result _test_table_html_headers = ["", "<&numbers&>"] @@ -2414,7 +2414,7 @@ def test_html(): ] ) result = tabulate(_test_table_html, _test_table_html_headers, tablefmt="html") - assert_equal(expected, result) + assert expected == result assert hasattr(result, "_repr_html_") assert result._repr_html_() == result.str @@ -2437,7 +2437,7 @@ def test_unsafehtml(): result = tabulate( _test_table_unsafehtml, _test_table_unsafehtml_headers, tablefmt="unsafehtml" ) - assert_equal(expected, result) + assert expected == result assert hasattr(result, "_repr_html_") assert result._repr_html_() == result.str @@ -2455,7 +2455,7 @@ def test_html_headerless(): ] ) result = tabulate(_test_table_html, tablefmt="html") - assert_equal(expected, result) + assert expected == result assert hasattr(result, "_repr_html_") assert result._repr_html_() == result.str @@ -2473,7 +2473,7 @@ def test_unsafehtml_headerless(): ] ) result = tabulate(_test_table_unsafehtml, tablefmt="unsafehtml") - assert_equal(expected, result) + assert expected == result assert hasattr(result, "_repr_html_") assert result._repr_html_() == result.str @@ -2495,7 +2495,7 @@ def test_latex(): r"\end{tabular}", ] ) - assert_equal(expected, result) + assert expected == result def test_latex_raw(): @@ -2519,7 +2519,7 @@ def test_latex_raw(): r"\end{tabular}", ] ) - assert_equal(expected, result) + assert expected == result def test_latex_headerless(): @@ -2535,7 +2535,7 @@ def test_latex_headerless(): r"\end{tabular}", ] ) - assert_equal(expected, result) + assert expected == result def test_latex_booktabs(): @@ -2553,7 +2553,7 @@ def test_latex_booktabs(): r"\end{tabular}", ] ) - assert_equal(expected, result) + assert expected == result def test_latex_booktabs_headerless(): @@ -2569,7 +2569,7 @@ def test_latex_booktabs_headerless(): r"\end{tabular}", ] ) - assert_equal(expected, result) + assert expected == result def test_textile(): @@ -2579,7 +2579,7 @@ def test_textile(): |<. spam |>. 41.9999 | |<. eggs |>. 451 |""" - assert_equal(expected, result) + assert expected == result def test_textile_with_header(): @@ -2590,7 +2590,7 @@ def test_textile_with_header(): |<. spam |>. 41.9999 | |<. eggs |>. 451 |""" - assert_equal(expected, result) + assert expected == result def test_textile_with_center_align(): @@ -2600,56 +2600,56 @@ def test_textile_with_center_align(): |=. spam |>. 41.9999 | |=. eggs |>. 451 |""" - assert_equal(expected, result) + assert expected == result def test_no_data(): "Output: table with no data" expected = "\n".join(["strings numbers", "--------- ---------"]) result = tabulate(None, _test_table_headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_empty_data(): "Output: table with empty data" expected = "\n".join(["strings numbers", "--------- ---------"]) result = tabulate([], _test_table_headers, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_no_data_without_headers(): "Output: table with no data and no headers" expected = "" result = tabulate(None, tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_empty_data_without_headers(): "Output: table with empty data and no headers" expected = "" result = tabulate([], tablefmt="simple") - assert_equal(expected, result) + assert expected == result def test_intfmt(): "Output: integer format" result = tabulate([[10000], [10]], intfmt=",", tablefmt="plain") expected = "10,000\n 10" - assert_equal(expected, result) + assert expected == result def test_empty_data_with_headers(): "Output: table with empty data and headers as firstrow" expected = "" result = tabulate([], headers="firstrow") - assert_equal(expected, result) + assert expected == result def test_floatfmt(): "Output: floating point format" result = tabulate([["1.23456789"], [1.0]], floatfmt=".3f", tablefmt="plain") expected = "1.235\n1.000" - assert_equal(expected, result) + assert expected == result def test_floatfmt_multi(): @@ -2658,7 +2658,7 @@ def test_floatfmt_multi(): [[0.12345, 0.12345, 0.12345]], floatfmt=(".1f", ".3f"), tablefmt="plain" ) expected = "0.1 0.123 0.12345" - assert_equal(expected, result) + assert expected == result def test_colalign_multi(): @@ -2667,7 +2667,7 @@ def test_colalign_multi(): [["one", "two"], ["three", "four"]], colalign=("right",), tablefmt="plain" ) expected = " one two\nthree four" - assert_equal(expected, result) + assert expected == result def test_colalign_multi_with_sep_line(): @@ -2678,7 +2678,7 @@ def test_colalign_multi_with_sep_line(): tablefmt="plain", ) expected = " one two\n\nthree four" - assert_equal(expected, result) + assert expected == result def test_column_global_and_specific_alignment(): """ Test `colglobalalign` and `"global"` parameter for `colalign`. """ @@ -2691,7 +2691,7 @@ def test_column_global_and_specific_alignment(): " 1 2 3 4", "111 222 333 444", "--- --- --- ---"]) - assert_equal(expected, result) + assert expected == result def test_headers_global_and_specific_alignment(): """ Test `headersglobalalign` and `headersalign`. """ @@ -2707,7 +2707,7 @@ def test_headers_global_and_specific_alignment(): "--- --- --- --- --- ---", "1 2 3 4 5 6", "111 222 333 444 555 666"]) - assert_equal(expected, result) + assert expected == result def test_colalign_or_headersalign_too_long(): """ Test `colalign` and `headersalign` too long. """ @@ -2721,7 +2721,7 @@ def test_colalign_or_headersalign_too_long(): "--- ---", " 1 2", "111 222"]) - assert_equal(expected, result) + assert expected == result def test_warning_when_colalign_or_headersalign_is_string(): """ Test user warnings when `colalign` or `headersalign` is a string. """ @@ -2757,7 +2757,7 @@ def test_float_conversions(): "+-------+-------------+--------------+------------+------------+-------------+", ] ) - assert_equal(expected, result) + assert expected == result def test_missingval(): @@ -2766,7 +2766,7 @@ def test_missingval(): [["Alice", 10], ["Bob", None]], missingval="n/a", tablefmt="plain" ) expected = "Alice 10\nBob n/a" - assert_equal(expected, result) + assert expected == result def test_missingval_multi(): @@ -2777,14 +2777,14 @@ def test_missingval_multi(): tablefmt="plain", ) expected = "Alice Bob Charlie\nn/a ?" - assert_equal(expected, result) + assert expected == result def test_column_alignment(): "Output: custom alignment for text and numbers" expected = "\n".join(["----- ---", "Alice 1", " Bob 333", "----- ---"]) result = tabulate([["Alice", 1], ["Bob", 333]], stralign="right", numalign="center") - assert_equal(expected, result) + assert expected == result def test_unaligned_separated(): @@ -2798,7 +2798,7 @@ def test_unaligned_separated(): stralign=None, numalign=None, ) - assert_equal(expected, result) + assert expected == result def test_pandas_with_index(): @@ -2818,7 +2818,7 @@ def test_pandas_with_index(): ] ) result = tabulate(df, headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_with_index is skipped") @@ -2842,7 +2842,7 @@ def test_pandas_without_index(): ] ) result = tabulate(df, headers="keys", showindex=False) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_without_index is skipped") @@ -2866,7 +2866,7 @@ def test_pandas_rst_with_index(): ] ) result = tabulate(df, tablefmt="rst", headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_rst_with_index is skipped") @@ -2891,7 +2891,7 @@ def test_pandas_rst_with_named_index(): ] ) result = tabulate(df, tablefmt="rst", headers="keys") - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_pandas_rst_with_index is skipped") @@ -2901,7 +2901,7 @@ def test_dict_like_with_index(): dd = {"b": range(101, 104)} expected = "\n".join([" b", "-- ---", " 0 101", " 1 102", " 2 103"]) result = tabulate(dd, "keys", showindex=True) - assert_equal(expected, result) + assert expected == result def test_list_of_lists_with_index(): @@ -2913,7 +2913,7 @@ def test_list_of_lists_with_index(): [" a b", "-- --- ---", " 0 0 101", " 1 1 102", " 2 2 103"] ) result = tabulate(dd, headers=["a", "b"], showindex=True) - assert_equal(expected, result) + assert expected == result def test_list_of_lists_with_index_with_sep_line(): @@ -2932,7 +2932,7 @@ def test_list_of_lists_with_index_with_sep_line(): ] ) result = tabulate(dd, headers=["a", "b"], showindex=True) - assert_equal(expected, result) + assert expected == result def test_list_of_lists_with_supplied_index(): @@ -2942,7 +2942,7 @@ def test_list_of_lists_with_supplied_index(): [" a b", "-- --- ---", " 1 0 101", " 2 1 102", " 3 2 103"] ) result = tabulate(dd, headers=["a", "b"], showindex=[1, 2, 3]) - assert_equal(expected, result) + assert expected == result # TODO: make it a separate test case # the index must be as long as the number of rows with raises(ValueError): @@ -2956,7 +2956,7 @@ def test_list_of_lists_with_index_firstrow(): [" a b", "-- --- ---", " 0 0 101", " 1 1 102", " 2 2 103"] ) result = tabulate(dd, headers="firstrow", showindex=True) - assert_equal(expected, result) + assert expected == result # TODO: make it a separate test case # the index must be as long as the number of rows with raises(ValueError): @@ -2974,9 +2974,9 @@ def test_disable_numparse_default(): ] ) result = tabulate(_test_table, _test_table_headers) - assert_equal(expected, result) + assert expected == result result = tabulate(_test_table, _test_table_headers, disable_numparse=False) - assert_equal(expected, result) + assert expected == result def test_disable_numparse_true(): @@ -2990,7 +2990,7 @@ def test_disable_numparse_true(): ] ) result = tabulate(_test_table, _test_table_headers, disable_numparse=True) - assert_equal(expected, result) + assert expected == result def test_disable_numparse_list(): @@ -3001,13 +3001,13 @@ def test_disable_numparse_list(): ["h1 h2 h3", "---- ---- -------", "foo bar 42992e1"] ) result = tabulate(test_table, table_headers, disable_numparse=[2]) - assert_equal(expected, result) + assert expected == result expected = "\n".join( ["h1 h2 h3", "---- ---- ------", "foo bar 429920"] ) result = tabulate(test_table, table_headers, disable_numparse=[0, 1]) - assert_equal(expected, result) + assert expected == result def test_preserve_whitespace(): @@ -3019,11 +3019,11 @@ def test_preserve_whitespace(): ["h1 h2 h3", "----- ------- ----", " foo bar foo"] ) result = tabulate(test_table, table_headers) - assert_equal(expected, result) + assert expected == result tabulate_module.PRESERVE_WHITESPACE = False table_headers = ["h1", "h2", "h3"] test_table = [[" foo", " bar ", "foo"]] expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"]) result = tabulate(test_table, table_headers) - assert_equal(expected, result) + assert expected == result diff --git a/test/test_regression.py b/test/test_regression.py index 8f60ce7..191dff9 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -1,7 +1,7 @@ """Regression tests.""" from tabulate import tabulate, TableFormat, Line, DataRow -from common import assert_equal, skip +from common import skip def test_ansi_color_in_table_cells(): @@ -17,7 +17,7 @@ def test_ansi_color_in_table_cells(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_alignment_of_colored_cells(): @@ -40,7 +40,7 @@ def test_alignment_of_colored_cells(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_alignment_of_link_cells(): @@ -63,7 +63,7 @@ def test_alignment_of_link_cells(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_alignment_of_link_text_cells(): @@ -86,7 +86,7 @@ def test_alignment_of_link_text_cells(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_iter_of_iters_with_headers(): @@ -113,7 +113,7 @@ def mk_headers(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_datetime_values(): @@ -132,7 +132,7 @@ def test_datetime_values(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_simple_separated_format(): @@ -143,7 +143,7 @@ def test_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) + assert expected == formatted def test_simple_separated_format_with_headers(): @@ -154,7 +154,7 @@ def test_simple_separated_format_with_headers(): formatted = tabulate( [[1, 2]], headers=["a", "b"], tablefmt=simple_separated_format("|") ) - assert_equal(expected, formatted) + assert expected == formatted def test_column_type_of_bytestring_columns(): @@ -163,14 +163,14 @@ def test_column_type_of_bytestring_columns(): result = _column_type([b"foo", b"bar"]) expected = bytes - assert_equal(expected, result) + assert expected == result 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(expected, result) + assert expected == result lod = [{p: i for p in range(5)} for i in range(5)] result = tabulate(lod, "keys") @@ -185,7 +185,7 @@ def test_numeric_column_headers(): " 4 4 4 4 4", ] ) - assert_equal(expected, result) + assert expected == result def test_88_256_ANSI_color_codes(): @@ -201,7 +201,7 @@ def test_88_256_ANSI_color_codes(): ] ) print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") - assert_equal(expected, formatted) + assert expected == formatted def test_column_with_mixed_value_types(): @@ -209,7 +209,7 @@ def test_column_with_mixed_value_types(): expected = "\n".join(["-----", "", "a", "я", "0", "False", "-----"]) data = [[None], ["a"], ["\u044f"], [0], [False]] table = tabulate(data) - assert_equal(table, expected) + assert table == expected def test_latex_escape_special_chars(): @@ -226,14 +226,14 @@ def test_latex_escape_special_chars(): ] ) result = tabulate([["&%^_$#{}<>~"]], ["foo^bar"], tablefmt="latex") - assert_equal(expected, result) + assert expected == result 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(expected, result) + assert expected == result def test_ansi_color_for_decimal_numbers(): @@ -243,7 +243,7 @@ def test_ansi_color_for_decimal_numbers(): ["------- ---", "Magenta \x1b[95m1.1\x1b[0m", "------- ---"] ) result = tabulate(table) - assert_equal(expected, result) + assert expected == result def test_alignment_of_decimal_numbers_with_ansi_color(): @@ -253,7 +253,7 @@ def test_alignment_of_decimal_numbers_with_ansi_color(): table = [[v1], [v2]] expected = "\n".join(["\x1b[95m12.34\x1b[0m", " \x1b[95m1.23456\x1b[0m"]) result = tabulate(table, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_alignment_of_decimal_numbers_with_commas(): @@ -274,7 +274,7 @@ def test_long_integers(): table = [[18446744073709551614]] result = tabulate(table, tablefmt="plain") expected = "18446744073709551614" - assert_equal(expected, result) + assert expected == result def test_colorclass_colors(): @@ -285,7 +285,7 @@ def test_colorclass_colors(): s = colorclass.Color("{magenta}3.14{/magenta}") result = tabulate([[s]], tablefmt="plain") expected = "\x1b[35m3.14\x1b[39m" - assert_equal(expected, result) + assert expected == result except ImportError: class textclass(str): @@ -294,7 +294,7 @@ class textclass(str): s = textclass("\x1b[35m3.14\x1b[39m") result = tabulate([[s]], tablefmt="plain") expected = "\x1b[35m3.14\x1b[39m" - assert_equal(expected, result) + assert expected == result def test_mix_normal_and_wide_characters(): @@ -314,7 +314,7 @@ def test_mix_normal_and_wide_characters(): "+--------+", ] ) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_mix_normal_and_wide_characters is skipped (requires wcwidth lib)") @@ -334,7 +334,7 @@ def test_multiline_with_wide_characters(): "╘══════╧══════╧══════╛", ] ) - assert_equal(expected, result) + assert expected == result except ImportError: skip("test_multiline_with_wide_characters is skipped (requires wcwidth lib)") @@ -344,7 +344,7 @@ def test_align_long_integers(): table = [[int(1)], [int(234)]] result = tabulate(table, tablefmt="plain") expected = "\n".join([" 1", "234"]) - assert_equal(expected, result) + assert expected == result def test_numpy_array_as_headers(): @@ -355,7 +355,7 @@ def test_numpy_array_as_headers(): headers = np.array(["foo", "bar"]) result = tabulate([], headers, tablefmt="plain") expected = "foo bar" - assert_equal(expected, result) + assert expected == result except ImportError: raise skip("") @@ -365,7 +365,7 @@ def test_boolean_columns(): xortable = [[False, True], [True, False]] expected = "\n".join(["False True", "True False"]) result = tabulate(xortable, tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_ansi_color_bold_and_fgcolor(): @@ -383,14 +383,14 @@ def test_ansi_color_bold_and_fgcolor(): "+---+---+---+", ] ) - assert_equal(expected, result) + assert expected == result def test_empty_table_with_keys_as_header(): "Regression: headers='keys' on an empty table (issue #81)" result = tabulate([], headers="keys") expected = "" - assert_equal(expected, result) + assert expected == result def test_escape_empty_cell_in_first_column_in_rst(): @@ -409,7 +409,7 @@ def test_escape_empty_cell_in_first_column_in_rst(): ] ) result = tabulate(table, headers, tablefmt="rst") - assert_equal(expected, result) + assert expected == result def test_ragged_rows(): @@ -417,7 +417,7 @@ def test_ragged_rows(): 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(expected, result) + assert expected == result def test_empty_pipe_table_with_columns(): @@ -426,7 +426,7 @@ def test_empty_pipe_table_with_columns(): headers = ["Col1", "Col2"] expected = "\n".join(["| Col1 | Col2 |", "|--------|--------|"]) result = tabulate(table, headers, tablefmt="pipe") - assert_equal(expected, result) + assert expected == result def test_custom_tablefmt(): @@ -444,7 +444,7 @@ def test_custom_tablefmt(): rows = [["foo", "bar"], ["baz", "qux"]] expected = "\n".join(["A B", "--- ---", "foo bar", "baz qux"]) result = tabulate(rows, headers=["A", "B"], tablefmt=tablefmt) - assert_equal(expected, result) + assert expected == result def test_string_with_comma_between_digits_without_floatfmt_grouping_option(): @@ -452,7 +452,7 @@ def test_string_with_comma_between_digits_without_floatfmt_grouping_option(): table = [["126,000"]] expected = "126,000" result = tabulate(table, tablefmt="plain") - assert_equal(expected, result) # no exception + assert expected == result def test_iterable_row_index(): @@ -469,7 +469,7 @@ def count(start, step=1): expected = "1 a\n2 b\n3 c" result = tabulate(table, showindex=count(1), tablefmt="plain") - assert_equal(expected, result) + assert expected == result def test_preserve_line_breaks_with_maxcolwidths(): @@ -485,13 +485,13 @@ def test_preserve_line_breaks_with_maxcolwidths(): ] ) result = tabulate(table, tablefmt="grid", maxcolwidths=10) - assert_equal(expected, result) + assert expected == result def test_exception_on_empty_data_with_maxcolwidths(): "Regression: exception on empty data when using maxcolwidths (github issue #180)" result = tabulate([], maxcolwidths=5) - assert_equal(result, "") + assert result == "" def test_numpy_int64_as_integer(): @@ -509,6 +509,6 @@ def test_numpy_int64_as_integer(): "| 1 | 3.14 |", ] ) - assert_equal(expected, result) + assert expected == result except ImportError: raise skip("") diff --git a/test/test_textwrapper.py b/test/test_textwrapper.py index f3070b1..cba04ea 100644 --- a/test/test_textwrapper.py +++ b/test/test_textwrapper.py @@ -6,7 +6,7 @@ from tabulate import _CustomTextWrap as CTW, tabulate from textwrap import TextWrapper as OTW -from common import skip, assert_equal +from common import skip def test_wrap_multiword_non_wide(): @@ -58,7 +58,7 @@ def test_wrap_wide_char_multiword(): wrapper = CTW(width=15) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrap_wide_char_longword(): @@ -75,7 +75,7 @@ def test_wrap_wide_char_longword(): # Explicit odd number to ensure the 2 width is taken into account wrapper = CTW(width=5) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrap_mixed_string(): @@ -100,13 +100,13 @@ def test_wrap_mixed_string(): ] wrapper = CTW(width=21) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrapper_len_ignores_color_chars(): data = "\033[31m\033[104mtenletters\033[0m" result = CTW._len(data) - assert_equal(10, result) + assert 10 == result def test_wrap_full_line_color(): @@ -123,7 +123,7 @@ def test_wrap_full_line_color(): ] wrapper = CTW(width=20) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrap_color_in_single_line(): @@ -139,7 +139,7 @@ def test_wrap_color_in_single_line(): ] wrapper = CTW(width=25) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrap_color_line_splillover(): @@ -155,7 +155,7 @@ def test_wrap_color_line_splillover(): ] wrapper = CTW(width=25) result = wrapper.wrap(data) - assert_equal(expected, result) + assert expected == result def test_wrap_datetime(): @@ -183,4 +183,4 @@ def test_wrap_datetime(): "+---------+--------+", ] expected = "\n".join(expected) - assert_equal(expected, result) + assert expected == result From eb8132a36f8c1c1ab1d44765b84571ef5403e873 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 11:11:03 -0700 Subject: [PATCH 04/11] use subprocess.run in unit tests --- test/test_cli.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/test/test_cli.py b/test/test_cli.py index a313b88..d75b158 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -77,15 +77,7 @@ def sample_input(sep=" ", with_headers=False): def run_and_capture_stdout(cmd, input=None): - x = subprocess.Popen( - cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE - ) - input_buf = input.encode() if input else None - out, err = x.communicate(input=input_buf) - out = out.decode("utf-8") - if x.returncode != 0: - raise OSError(err) - return out + return subprocess.run(cmd, input=input, check=True, capture_output=True, encoding='utf-8').stdout class TemporaryTextFile: From f83e97b671a67adca9bab4eadd372272dd2e92aa Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 11:17:22 -0700 Subject: [PATCH 05/11] fix rst regression regression --- tabulate/__init__.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index a943395..32a5575 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -99,6 +99,7 @@ def __init__(self, numalign="decimal", stralign="left", multiline=True, + escape_first_column=None, ): # tuple fields self.lineabove = lineabove @@ -116,6 +117,7 @@ def __init__(self, self.numalign = numalign self.stralign = stralign self.multiline = multiline + self.escape_first_column = escape_first_column def _is_separating_line(row): @@ -306,10 +308,10 @@ def escape_char(c): return _build_simple_row(escaped_values, rowfmt) -def _rst_escape_first_column(rows, headers): +def _rst_escape_first_column(rows, headers, escape_value='..'): def escape_empty(val): if isinstance(val, (str, bytes)) and not val.strip(): - return ".." + return escape_value else: return val @@ -570,6 +572,7 @@ def escape_empty(val): datarow=DataRow("", " ", ""), padding=0, with_header_hide=None, + escape_first_column='..', ), "mediawiki": TableFormat( lineabove=Line( @@ -2157,8 +2160,8 @@ def tabulate( # empty values in the first column of RST tables should be escaped (issue #82) # "" should be escaped as "\\ " or ".." - if tablefmt == "rst": - list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers) + if tablefmt.escape_first_column is not None: + list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers, tablefmt.escape_first_column) disable_numparse = tablefmt.disable_numparse if tablefmt.disable_numparse is not None else disable_numparse numalign = tablefmt.numalign if numalign == _DEFAULT_ALIGN else numalign From ccce663376315195f853e0c8abe685990c582e3f Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 12:29:02 -0700 Subject: [PATCH 06/11] fix multi-line empty string padding --- tabulate/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 32a5575..4d786a3 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -1171,7 +1171,7 @@ def _align_column( if is_multiline: if not enable_widechars and not has_invisible: padded_strings = [ - "\n".join([padfn(maxwidth, s) for s in ms.splitlines()]) + "\n".join([padfn(maxwidth, s) for s in (ms.splitlines() or [ms])]) for ms in strings ] else: @@ -1184,7 +1184,7 @@ def _align_column( # wcswidth and _visible_width don't count invisible characters; # padfn doesn't need to apply another correction padded_strings = [ - "\n".join([padfn(w, s) for s, w in zip((ms.splitlines() or ms), mw)]) + "\n".join([padfn(w, s) for s, w in zip((ms.splitlines() or [ms]), mw)]) for ms, mw in zip(strings, visible_widths) ] else: # single-line cell values From 186faad9f9905b1adaffacbd1fa4a0012913491a Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 12:42:27 -0700 Subject: [PATCH 07/11] inline assert_in and cleanup print --- test/common.py | 8 -------- test/test_api.py | 4 +--- test/test_input.py | 23 +++++++++-------------- test/test_regression.py | 8 -------- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/test/common.py b/test/common.py index 029652d..992c39c 100644 --- a/test/common.py +++ b/test/common.py @@ -3,14 +3,6 @@ import warnings -def assert_in(result, expected_set): - nums = range(1, len(expected_set) + 1) - for i, expected in zip(nums, expected_set): - print("Expected %d:\n%s\n" % (i, expected)) - print("Got:\n%s\n" % result) - assert result in expected_set - - def cols_to_pipe_str(cols): return "|".join([str(col) for col in cols]) diff --git a/test/test_api.py b/test/test_api.py index e658e82..6050837 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -14,9 +14,8 @@ def test_tabulate_formats(): - "API: tabulate_formats is a list of strings" "" + "API: tabulate_formats is a list of strings" supported = tabulate_formats - print("tabulate_formats = %r" % supported) assert type(supported) is list for fmt in supported: assert type(fmt) is str # noqa @@ -26,7 +25,6 @@ def _check_signature(function, expected_sig): if not signature: skip("") actual_sig = signature(function) - print(f"expected: {expected_sig}\nactual: {str(actual_sig)}\n") assert len(actual_sig.parameters) == len(expected_sig) diff --git a/test/test_input.py b/test/test_input.py index 304939c..e5f3b2a 100644 --- a/test/test_input.py +++ b/test/test_input.py @@ -1,13 +1,9 @@ """Test support of the various forms of tabular data.""" from tabulate import tabulate -from common import assert_in, raises, skip +from common import raises, skip -try: - from collections import UserDict -except ImportError: - # Python2 - from UserDict import UserDict +from collections import UserDict def test_iterable_of_iterables(): @@ -103,8 +99,7 @@ def test_dict_like(): [" b a", "--- ---", "101 0", "102 1", "103 2", "104"] ) result = tabulate(dd, "keys") - print("Keys' order: %s" % dd.keys()) - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_numpy_2d(): @@ -377,7 +372,7 @@ def test_list_of_dicts(): expected1 = "\n".join(["- -", "1 2", "3 4", "- -"]) expected2 = "\n".join(["- -", "2 1", "4 3", "- -"]) result = tabulate(lod) - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_userdicts(): @@ -386,7 +381,7 @@ def test_list_of_userdicts(): expected1 = "\n".join(["- -", "1 2", "3 4", "- -"]) expected2 = "\n".join(["- -", "2 1", "4 3", "- -"]) result = tabulate(lod) - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_dicts_keys(): @@ -399,7 +394,7 @@ def test_list_of_dicts_keys(): [" bar foo", "----- -----", " 2 1", " 4 3"] ) result = tabulate(lod, headers="keys") - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_userdicts_keys(): @@ -412,7 +407,7 @@ def test_list_of_userdicts_keys(): [" bar foo", "----- -----", " 2 1", " 4 3"] ) result = tabulate(lod, headers="keys") - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_dicts_with_missing_keys(): @@ -442,7 +437,7 @@ def test_list_of_dicts_firstrow(): [" BAR FOO baz", "----- ----- -----", " 4 3 5"] ) result = tabulate(lod, headers="firstrow") - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_dicts_with_dict_of_headers(): @@ -456,7 +451,7 @@ def test_list_of_dicts_with_dict_of_headers(): ["LETTERS DIGITS", "--------- --------", "ABCDE 12345"] ) result = tabulate(table, headers=headers) - assert_in(result, [expected1, expected2]) + assert result in [expected1, expected2] def test_list_of_dicts_with_list_of_headers(): diff --git a/test/test_regression.py b/test/test_regression.py index 191dff9..3354bd7 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -16,7 +16,6 @@ def test_ansi_color_in_table_cells(): "| test | \x1b[31mtest\x1b[0m | \x1b[32mtest\x1b[0m |", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -39,7 +38,6 @@ def test_alignment_of_colored_cells(): "+--------+--------+--------+", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -62,7 +60,6 @@ def test_alignment_of_link_cells(): "+--------+--------+--------+", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -85,7 +82,6 @@ def test_alignment_of_link_text_cells(): "+--------+----------+--------+", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -112,7 +108,6 @@ def mk_headers(): " 0 1 2", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -131,7 +126,6 @@ def test_datetime_values(): "------------------- ---------- --------", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted @@ -142,7 +136,6 @@ def test_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 expected == formatted @@ -200,7 +193,6 @@ def test_88_256_ANSI_color_codes(): "| \x1b[48;5;196mred\x1b[49m | \x1b[38;5;196mred\x1b[39m |", ] ) - print(f"expected: {expected!r}\n\ngot: {formatted!r}\n") assert expected == formatted From 476e40008c403c6d6ce337383139182cff5604fe Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Fri, 10 May 2024 13:19:16 -0700 Subject: [PATCH 08/11] add support and tests for min_padding --- tabulate/__init__.py | 3 ++- test/test_api.py | 16 ++++++++-------- test/test_output.py | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 4d786a3..1f5df18 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -813,7 +813,7 @@ def escape_empty(val): ) -def simple_separated_format(separator): +def simple_separated_format(separator, **kwargs): """Construct a simple TableFormat with columns separated by a separator. >>> tsv = simple_separated_format("\\t") ; \ @@ -830,6 +830,7 @@ def simple_separated_format(separator): datarow=DataRow("", separator, ""), padding=0, with_header_hide=None, + **kwargs ) diff --git a/test/test_api.py b/test/test_api.py index 6050837..45ef644 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -25,15 +25,12 @@ def _check_signature(function, expected_sig): if not signature: skip("") actual_sig = signature(function) - - assert len(actual_sig.parameters) == len(expected_sig) - - for (e, ev), (a, av) in zip(expected_sig, actual_sig.parameters.items()): - assert e == a and ev == av.default + actual_sig = [(k, v.default) for k, v in actual_sig.parameters.items()] + assert actual_sig == expected_sig def test_tabulate_signature(): - "API: tabulate() type signature is unchanged" "" + "API: tabulate() type signature is unchanged" assert type(tabulate) is type(lambda: None) # noqa expected_sig = [ ("tabular_data", _empty), @@ -58,7 +55,10 @@ def test_tabulate_signature(): def test_simple_separated_format_signature(): - "API: simple_separated_format() type signature is unchanged" "" + "API: simple_separated_format() type signature is unchanged" assert type(simple_separated_format) is type(lambda: None) # noqa - expected_sig = [("separator", _empty)] + expected_sig = [ + ("separator", _empty), + ("kwargs", _empty), + ] _check_signature(simple_separated_format, expected_sig) diff --git a/test/test_output.py b/test/test_output.py index 3e141d4..bdf904c 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1,4 +1,5 @@ """Test output of the various forms of tabular data.""" +import textwrap import tabulate as tabulate_module from common import raises, skip, check_warnings @@ -242,6 +243,25 @@ def test_simple(): assert expected == result +def test_simple_with_zero_padding(): + """ Output custom simple table built with a new column separator """ + expected = textwrap.dedent("""\ + strings numbers + eggs 451""") + result = tabulate([_test_table[-1]], _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0)) + assert result == expected + + +def test_simple_with_zero_padding_and_alignment(): + """ Output custom simple table built with a new column separator """ + expected = textwrap.dedent("""\ + strings numbers + spam 41.9999 + eggs 451""") + result = tabulate(_test_table, _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0)) + assert result == expected + + def test_simple_with_sep_line(): "Output: simple with headers and separating line" expected = "\n".join( From c928342e559ece8a4b548c1f30da1aed5e09e8c8 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Mon, 13 May 2024 15:11:15 -0700 Subject: [PATCH 09/11] add "dev" optional dependencies for testing everything locally --- pyproject.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index e15df51..e6f80b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,11 @@ test = [ "pandas", "wcwidth", ] +dev = [ + "tox", + "black", + "pre-commit", +] [project.scripts] tabulate = "tabulate:_main" From 303a042196e211597b6fab1a9b54c84f8fe46dad Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Mon, 13 May 2024 15:13:44 -0700 Subject: [PATCH 10/11] flake8 and black formatting changes --- tabulate/__init__.py | 90 +++++++++++++++++++++---------- test/common.py | 2 +- test/test_cli.py | 5 +- test/test_output.py | 126 +++++++++++++++++++++++++------------------ 4 files changed, 143 insertions(+), 80 deletions(-) diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 1f5df18..aa66823 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -92,15 +92,24 @@ class TableFormat: - or a list of elements not to be displayed if the table has column headers. """ - def __init__(self, - lineabove, linebelowheader, linebetweenrows, linebelow, headerrow, datarow, padding, with_header_hide, - min_padding=MIN_PADDING, - disable_numparse=None, - numalign="decimal", - stralign="left", - multiline=True, - escape_first_column=None, - ): + + def __init__( + self, + lineabove, + linebelowheader, + linebetweenrows, + linebelow, + headerrow, + datarow, + padding, + with_header_hide, + min_padding=MIN_PADDING, + disable_numparse=None, + numalign="decimal", + stralign="left", + multiline=True, + escape_first_column=None, + ): # tuple fields self.lineabove = lineabove self.linebelowheader = linebelowheader @@ -308,7 +317,7 @@ def escape_char(c): return _build_simple_row(escaped_values, rowfmt) -def _rst_escape_first_column(rows, headers, escape_value='..'): +def _rst_escape_first_column(rows, headers, escape_value=".."): def escape_empty(val): if isinstance(val, (str, bytes)) and not val.strip(): return escape_value @@ -572,7 +581,7 @@ def escape_empty(val): datarow=DataRow("", " ", ""), padding=0, with_header_hide=None, - escape_first_column='..', + escape_first_column="..", ), "mediawiki": TableFormat( lineabove=Line( @@ -643,7 +652,7 @@ def escape_empty(val): datarow=_latex_row, padding=1, with_header_hide=None, - multiline=False + multiline=False, ), "latex_raw": TableFormat( lineabove=_latex_line_begin_tabular, @@ -830,7 +839,7 @@ def simple_separated_format(separator, **kwargs): datarow=DataRow("", separator, ""), padding=0, with_header_hide=None, - **kwargs + **kwargs, ) @@ -1308,7 +1317,7 @@ def _align_header( def _remove_separating_lines(rows): - if type(rows) == list: + if isinstance(rows, list): separating_lines = [] sans_rows = [] for index, row in enumerate(rows): @@ -1356,7 +1365,8 @@ def _bool(val): def _normalize_tabular_data(tabular_data, headers, showindex="default"): - """Transform a supported data type to a list of lists, and a list of headers, with headers padding. + """Transform a supported data type to a list of lists, and a list of headers, with headers + padding. Supported tabular data types: @@ -2162,9 +2172,15 @@ def tabulate( # empty values in the first column of RST tables should be escaped (issue #82) # "" should be escaped as "\\ " or ".." if tablefmt.escape_first_column is not None: - list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers, tablefmt.escape_first_column) + list_of_lists, headers = _rst_escape_first_column( + list_of_lists, headers, tablefmt.escape_first_column + ) - disable_numparse = tablefmt.disable_numparse if tablefmt.disable_numparse is not None else disable_numparse + disable_numparse = ( + tablefmt.disable_numparse + if tablefmt.disable_numparse is not None + else disable_numparse + ) numalign = tablefmt.numalign if numalign == _DEFAULT_ALIGN else numalign stralign = tablefmt.stralign if stralign == _DEFAULT_ALIGN else stralign @@ -2223,22 +2239,28 @@ def tabulate( # align columns # first set global alignment - if colglobalalign is not None: # if global alignment provided + if colglobalalign is not None: # if global alignment provided aligns = [colglobalalign] * len(cols) - else: # default + else: # default aligns = [numalign if ct in [int, float] else stralign for ct in coltypes] # then specific alignements if colalign is not None: assert isinstance(colalign, Iterable) if isinstance(colalign, str): - warnings.warn(f"As a string, `colalign` is interpreted as {[c for c in colalign]}. Did you mean `colglobalalign = \"{colalign}\"` or `colalign = (\"{colalign}\",)`?", stacklevel=2) + warnings.warn( + f"As a string, `colalign` is interpreted as {[c for c in colalign]}. " + f'Did you mean `colglobalalign = "{colalign}"` or `colalign = ("{colalign}",)`?', + stacklevel=2, + ) for idx, align in enumerate(colalign): if not idx < len(aligns): break elif align != "global": aligns[idx] = align minwidths = ( - [width_fn(h) + tablefmt.min_padding for h in headers] if headers else [0] * len(cols) + [width_fn(h) + tablefmt.min_padding for h in headers] + if headers + else [0] * len(cols) ) cols = [ _align_column(c, a, minw, has_invisible, enable_widechars, tablefmt.multiline) @@ -2250,20 +2272,25 @@ def tabulate( # align headers and add headers t_cols = cols or [[""]] * len(headers) # first set global alignment - if headersglobalalign is not None: # if global alignment provided + if headersglobalalign is not None: # if global alignment provided aligns_headers = [headersglobalalign] * len(t_cols) - else: # default + else: # default aligns_headers = aligns or [stralign] * len(headers) # then specific header alignements if headersalign is not None: assert isinstance(headersalign, Iterable) if isinstance(headersalign, str): - warnings.warn(f"As a string, `headersalign` is interpreted as {[c for c in headersalign]}. Did you mean `headersglobalalign = \"{headersalign}\"` or `headersalign = (\"{headersalign}\",)`?", stacklevel=2) + warnings.warn( + f"As a string, `headersalign` is interpreted as {[c for c in headersalign]}. " + f'Did you mean `headersglobalalign = "{headersalign}"` ' + f'or `headersalign = ("{headersalign}",)`?', + stacklevel=2, + ) for idx, align in enumerate(headersalign): hidx = headers_pad + idx if not hidx < len(aligns_headers): break - elif align == "same" and hidx < len(aligns): # same as column align + elif align == "same" and hidx < len(aligns): # same as column align aligns_headers[hidx] = aligns[hidx] elif align != "global": aligns_headers[hidx] = align @@ -2285,7 +2312,14 @@ def tabulate( _reinsert_separating_lines(rows, separating_lines) return _format_table( - tablefmt, headers, aligns_headers, rows, minwidths, aligns, tablefmt.multiline, rowaligns=rowaligns + tablefmt, + headers, + aligns_headers, + rows, + minwidths, + aligns, + tablefmt.multiline, + rowaligns=rowaligns, ) @@ -2416,7 +2450,9 @@ def str(self): return self -def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_multiline, rowaligns): +def _format_table( + fmt, headers, headersaligns, rows, colwidths, colaligns, is_multiline, rowaligns +): """Produce a plain-text representation of the table.""" lines = [] hidden = fmt.with_header_hide if (headers and fmt.with_header_hide) else [] diff --git a/test/common.py b/test/common.py index 992c39c..f056179 100644 --- a/test/common.py +++ b/test/common.py @@ -15,6 +15,7 @@ def rows_to_pipe_table_str(rows): return "\n".join(lines) + def check_warnings(func_args_kwargs, *, num=None, category=None, contain=None): func, args, kwargs = func_args_kwargs with warnings.catch_warnings(record=True) as W: @@ -28,4 +29,3 @@ def check_warnings(func_args_kwargs, *, num=None, category=None, contain=None): assert all([issubclass(w.category, category) for w in W]) if contain is not None: assert all([contain in str(w.message) for w in W]) - diff --git a/test/test_cli.py b/test/test_cli.py index d75b158..d234d9d 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -77,7 +77,10 @@ def sample_input(sep=" ", with_headers=False): def run_and_capture_stdout(cmd, input=None): - return subprocess.run(cmd, input=input, check=True, capture_output=True, encoding='utf-8').stdout + proc = subprocess.run( + cmd, input=input, check=True, capture_output=True, encoding="utf-8" + ) + return proc.stdout class TemporaryTextFile: diff --git a/test/test_output.py b/test/test_output.py index bdf904c..d420081 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -244,21 +244,33 @@ def test_simple(): def test_simple_with_zero_padding(): - """ Output custom simple table built with a new column separator """ - expected = textwrap.dedent("""\ + """Output custom simple table built with a new column separator""" + expected = textwrap.dedent( + """\ strings numbers - eggs 451""") - result = tabulate([_test_table[-1]], _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0)) + eggs 451""" + ) + result = tabulate( + [_test_table[-1]], + _test_table_headers, + tablefmt=simple_separated_format(" ", min_padding=0), + ) assert result == expected def test_simple_with_zero_padding_and_alignment(): - """ Output custom simple table built with a new column separator """ - expected = textwrap.dedent("""\ + """Output custom simple table built with a new column separator""" + expected = textwrap.dedent( + """\ strings numbers spam 41.9999 - eggs 451""") - result = tabulate(_test_table, _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0)) + eggs 451""" + ) + result = tabulate( + _test_table, + _test_table_headers, + tablefmt=simple_separated_format(" ", min_padding=0), + ) assert result == expected @@ -2700,60 +2712,72 @@ def test_colalign_multi_with_sep_line(): expected = " one two\n\nthree four" assert expected == result + def test_column_global_and_specific_alignment(): - """ Test `colglobalalign` and `"global"` parameter for `colalign`. """ - table = [[1,2,3,4],[111,222,333,444]] - colglobalalign = 'center' - colalign = ('global','left', 'right') + """Test `colglobalalign` and `"global"` parameter for `colalign`.""" + table = [[1, 2, 3, 4], [111, 222, 333, 444]] + colglobalalign = "center" + colalign = ("global", "left", "right") result = tabulate(table, colglobalalign=colglobalalign, colalign=colalign) - expected = '\n'.join([ - "--- --- --- ---", - " 1 2 3 4", - "111 222 333 444", - "--- --- --- ---"]) + expected = "\n".join( + [ + "--- --- --- ---", + " 1 2 3 4", + "111 222 333 444", + "--- --- --- ---", + ] + ) assert expected == result + def test_headers_global_and_specific_alignment(): - """ Test `headersglobalalign` and `headersalign`. """ - table = [[1,2,3,4,5,6],[111,222,333,444,555,666]] - colglobalalign = 'center' - colalign = ('left',) - headers = ['h', 'e', 'a', 'd', 'e', 'r'] - headersglobalalign = 'right' - headersalign = ('same', 'same', 'left', 'global', 'center') - result = tabulate(table, headers=headers, colglobalalign=colglobalalign, colalign=colalign, headersglobalalign=headersglobalalign, headersalign=headersalign) - expected = '\n'.join([ - "h e a d e r", - "--- --- --- --- --- ---", - "1 2 3 4 5 6", - "111 222 333 444 555 666"]) + """Test `headersglobalalign` and `headersalign`.""" + table = [[1, 2, 3, 4, 5, 6], [111, 222, 333, 444, 555, 666]] + colglobalalign = "center" + colalign = ("left",) + headers = ["h", "e", "a", "d", "e", "r"] + headersglobalalign = "right" + headersalign = ("same", "same", "left", "global", "center") + result = tabulate( + table, + headers=headers, + colglobalalign=colglobalalign, + colalign=colalign, + headersglobalalign=headersglobalalign, + headersalign=headersalign, + ) + expected = "\n".join( + [ + "h e a d e r", + "--- --- --- --- --- ---", + "1 2 3 4 5 6", + "111 222 333 444 555 666", + ] + ) assert expected == result + def test_colalign_or_headersalign_too_long(): - """ Test `colalign` and `headersalign` too long. """ - table = [[1,2],[111,222]] - colalign = ('global', 'left', 'center') - headers = ['h'] - headersalign = ('center', 'right', 'same') - result = tabulate(table, headers=headers, colalign=colalign, headersalign=headersalign) - expected = '\n'.join([ - " h", - "--- ---", - " 1 2", - "111 222"]) + """Test `colalign` and `headersalign` too long.""" + table = [[1, 2], [111, 222]] + colalign = ("global", "left", "center") + headers = ["h"] + headersalign = ("center", "right", "same") + result = tabulate( + table, headers=headers, colalign=colalign, headersalign=headersalign + ) + expected = "\n".join([" h", "--- ---", " 1 2", "111 222"]) assert expected == result + def test_warning_when_colalign_or_headersalign_is_string(): - """ Test user warnings when `colalign` or `headersalign` is a string. """ - table = [[1,"bar"]] - opt = { - 'colalign': "center", - 'headers': ['foo', '2'], - 'headersalign': "center"} - check_warnings((tabulate, [table], opt), - num = 2, - category = UserWarning, - contain = "As a string") + """Test user warnings when `colalign` or `headersalign` is a string.""" + table = [[1, "bar"]] + opt = {"colalign": "center", "headers": ["foo", "2"], "headersalign": "center"} + check_warnings( + (tabulate, [table], opt), num=2, category=UserWarning, contain="As a string" + ) + def test_float_conversions(): "Output: float format parsed" From 28c18e435532394407e4079ebbd7461c82a1f101 Mon Sep 17 00:00:00 2001 From: Stefan Sullivan Date: Wed, 15 May 2024 12:28:22 -0700 Subject: [PATCH 11/11] test on ubuntu pandas fails to install on 32-bit python on 64-bit windows, sometimes Why do we test on windows anyways? --- appveyor.yml | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 4eb2dd8..9ca1fbe 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -image: Visual Studio 2022 +image: Ubuntu environment: matrix: @@ -8,22 +8,18 @@ environment: # The list here is complete (excluding Python 2.6, which # isn't covered by this document) at the time of writing. - - PYTHON: "C:\\Python37" - - PYTHON: "C:\\Python38" - - PYTHON: "C:\\Python39" - - PYTHON: "C:\\Python37-x64" - - PYTHON: "C:\\Python38-x64" - - PYTHON: "C:\\Python39-x64" - - PYTHON: "C:\\Python310-x64" - - PYTHON: "C:\\Python311-x64" + - PYTHON: 3.7 + - PYTHON: 3.8 + - PYTHON: 3.9 + - PYTHON: 3.10 + - PYTHON: 3.11 + - PYTHON: 3.12 install: # Newer setuptools is needed for proper support of pyproject.toml - - "%PYTHON%\\python.exe -m pip install setuptools --upgrade" - # We need wheel installed to build wheels - - "%PYTHON%\\python.exe -m pip install wheel --upgrade" - - "%PYTHON%\\python.exe -m pip install build setuptools_scm" - - "%PYTHON%\\python.exe -m pip install pytest numpy pandas" + - source ~/venv${PYTHON}/bin/activate + - which python + - python -m pip install build pytest numpy pandas build: off @@ -35,20 +31,22 @@ test_script: # Note that you must use the environment variable %PYTHON% to refer to # the interpreter you're using - Appveyor does not do anything special # to put the Python version you want to use on PATH. - #- "build.cmd %PYTHON%\\python.exe setup.py test" - - "%PYTHON%\\python.exe -m pytest -v --doctest-modules --ignore benchmark.py" + - source ~/venv${PYTHON}/bin/activate + - which python + - python -m pytest -v --doctest-modules --ignore benchmark.py + after_test: # This step builds your wheels. # Again, you only need build.cmd if you're building C extensions for # 64-bit Python 3.3/3.4. And you need to use %PYTHON% to get the correct # interpreter - #- "build.cmd %PYTHON%\\python.exe setup.py bdist_wheel" - - "%PYTHON%\\python.exe -m build -nswx ." + - source ~/venv${PYTHON}/bin/activate + - which python + - python -m build . artifacts: - # bdist_wheel puts your built wheel in the dist directory - - path: dist\* + - path: dist/* #on_success: # You can use this step to upload your artifacts to a public website.