Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API support for passing min_width parameter to simple_separated_format #326

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
inline assert_equals and remove noisy print statements in testing
so that pytest can show useful diffs
  • Loading branch information
Stefan Sullivan committed May 10, 2024
commit 970383b9885494f8afb72066cca7552bd9da205d
5 changes: 0 additions & 5 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 8 additions & 33 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

"""


import os
import sys


import subprocess
import tempfile


from common import assert_equal


SAMPLE_SIMPLE_FORMAT = "\n".join(
[
"----- ------ -------------",
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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()
50 changes: 25 additions & 25 deletions test/test_input.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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():
Expand All @@ -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():
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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
Loading