def _check_signature(function, expected_sig): if not signature: skip("") actual_sig = signature(function) print("expected: %s\nactual: %s\n" % (expected_sig, str(actual_sig))) for (e, ev), (a, av) in zip(expected_sig, actual_sig.parameters.items()): assert e == a and ev == av.default
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) for (e, ev), (a, av) in zip(expected_sig, actual_sig.parameters.items()): assert e == a and ev == av.default
def test_numpy_2d_firstrow(): "Input: a 2D NumPy array with the first row as headers." try: import numpy na = numpy.arange(1, 10, dtype=numpy.int32).reshape((3, 3)) ** 3 expected = "\n".join( [" 1 8 27", "--- --- ----", " 64 125 216", "343 512 729"] ) result = tabulate(na, headers="firstrow") assert_equal(expected, result) except ImportError: skip("test_numpy_2d_firstrow is skipped")
def test_wrap_text_wide_chars(): "Internal: Wrap wide characters based on column width" try: import wcwidth # noqa except ImportError: skip("test_wrap_text_wide_chars is skipped") rows = [["청자청자청자청자청자", "약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다"]] widths = [5, 20] expected = [["청자\n청자\n청자\n청자\n청자", "약간 감싸면 더 잘\n보일 수있는 다소 긴\n설명입니다"]] result = T._wrap_text_to_colwidths(rows, widths) assert_equal(result, expected)
def test_pandas_firstrow(): "Input: a Pandas DataFrame with the first row as headers." try: import pandas df = pandas.DataFrame([["one", 1], ["two", None]], columns=["string", "number"], index=["a", "b"]) expected = "\n".join( ["a one 1.0", "--- ----- -----", "b two nan"]) result = tabulate(df, headers="firstrow") assert_equal(expected, result) except ImportError: skip("test_pandas_firstrow is skipped")
def test_wrap_wide_char_multiword(): """TextWrapper: wrapping support for wide characters with multiple words""" try: import wcwidth # noqa except ImportError: skip("test_wrap_wide_char is skipped") data = "약간 감싸면 더 잘 보일 수있는 다소 긴 설명입니다" expected = ["약간 감싸면 더", "잘 보일 수있는", "다소 긴", "설명입니다"] wrapper = CTW(width=15) result = wrapper.wrap(data) assert_equal(expected, result)
def test_wrap_wide_char_longword(): """TextWrapper: wrapping wide char word that needs to be broken up""" try: import wcwidth # noqa except ImportError: skip("test_wrap_wide_char_longword is skipped") data = "약간감싸면더잘보일수있" expected = ["약간", "감싸", "면더", "잘보", "일수", "있"] # Explicit odd number to ensure the 2 width is taken into account wrapper = CTW(width=5) result = wrapper.wrap(data) assert_equal(expected, result)
def test_pandas(): "Input: a Pandas DataFrame." try: import pandas df = pandas.DataFrame([["one", 1], ["two", None]], index=["a", "b"]) expected = "\n".join([ " string number", "-- -------- --------", "a one 1", "b two nan", ]) result = tabulate(df, headers=["string", "number"]) assert_equal(expected, result) except ImportError: skip("test_pandas is skipped")
def test_multiline_with_wide_characters(): "Regression: multiline tables with varying number of wide characters (github issue #28)" try: import wcwidth # noqa table = [["가나\n가ab", "가나", "가나"]] result = tabulate(table, tablefmt="fancy_grid") expected = "\n".join([ "╒══════╤══════╤══════╕", "│ 가나 │ 가나 │ 가나 │", "│ 가ab │ │ │", "╘══════╧══════╧══════╛", ]) assert_equal(result, expected) except ImportError: skip( "test_multiline_with_wide_characters is skipped (requires wcwidth lib)" )
def test_pandas_without_index(): "Output: a pandas Dataframe without an index" try: import pandas df = pandas.DataFrame([["one", 1], ["two", None]], columns=["string", "number"], index=["a", "b"]) expected = "\n".join([ "string number", "-------- --------", "one 1", "two nan", ]) result = tabulate(df, headers="keys", showindex=False) assert_equal(expected, result) except ImportError: skip("test_pandas_without_index is skipped")
def test_numpy_2d_keys(): "Input: a 2D NumPy array with column indices as headers." try: import numpy na = (numpy.arange(1, 10, dtype=numpy.float32).reshape( (3, 3))**3) * 0.5 expected = "\n".join([ " 0 1 2", "----- ----- -----", " 0.5 4 13.5", " 32 62.5 108", "171.5 256 364.5", ]) result = tabulate(na, headers="keys") assert_equal(expected, result) except ImportError: skip("test_numpy_2d_keys is skipped")
def test_numpy_2d(): "Input: a 2D NumPy array with headers." try: import numpy na = (numpy.arange(1, 10, dtype=numpy.float32).reshape( (3, 3))**3) * 0.5 expected = "\n".join([ " a b c", "----- ----- -----", " 0.5 4 13.5", " 32 62.5 108", "171.5 256 364.5", ]) result = tabulate(na, ["a", "b", "c"]) assert_equal(expected, result) except ImportError: skip("test_numpy_2d is skipped")
def test_grid_wide_characters(): "Output: grid with wide characters in headers" try: import wcwidth # noqa except ImportError: skip("test_grid_wide_characters is skipped") headers = list(_test_table_headers) headers[1] = "配列" expected = "\n".join([ "+-----------+----------+", "| strings | 配列 |", "+===========+==========+", "| spam | 41.9999 |", "+-----------+----------+", "| eggs | 451 |", "+-----------+----------+", ]) result = tabulate(_test_table, headers, tablefmt="grid") assert_equal(expected, result)
def test_wrap_text_to_colwidths_colors_wide_char(): """Internal: autowrapped text can retain a ANSI colors with wide chars""" try: import wcwidth # noqa except ImportError: skip("test_wrap_text_to_colwidths_colors_wide_char is skipped") data = [[("\033[31m약간 감싸면 더 잘 보일 수있는 다소 긴" " 설명입니다 설명입니다 설명입니다 설명입니다 설명\033[0m")]] result = T._wrap_text_to_colwidths(data, [30]) expected = [[ "\n".join([ "\033[31m약간 감싸면 더 잘 보일 수있는\033[0m", "\033[31m다소 긴 설명입니다 설명입니다\033[0m", "\033[31m설명입니다 설명입니다 설명\033[0m", ]) ]] assert_equal(expected, result)
def test_numpy_array_as_headers(): "Regression: NumPy array used as headers (issue #62)" try: import numpy as np headers = np.array(["foo", "bar"]) result = tabulate([], headers, tablefmt="plain") expected = "foo bar" assert_equal(result, expected) except ImportError: raise skip("")
def test_mix_normal_and_wide_characters(): "Regression: wide characters in a grid format (issue #51)" try: import wcwidth # noqa ru_text = "\u043f\u0440\u0438\u0432\u0435\u0442" cn_text = "\u4f60\u597d" result = tabulate([[ru_text], [cn_text]], tablefmt="grid") expected = "\n".join( [ "+--------+", "| \u043f\u0440\u0438\u0432\u0435\u0442 |", "+--------+", "| \u4f60\u597d |", "+--------+", ] ) assert_equal(result, expected) except ImportError: skip("test_mix_normal_and_wide_characters is skipped (requires wcwidth lib)")
def test_pandas_rst_with_index(): "Output: a pandas Dataframe with an index in ReStructuredText format" try: import pandas df = pandas.DataFrame([["one", 1], ["two", None]], columns=["string", "number"], index=["a", "b"]) expected = "\n".join([ "==== ======== ========", ".. string number", "==== ======== ========", "a one 1", "b two nan", "==== ======== ========", ]) result = tabulate(df, tablefmt="rst", headers="keys") assert_equal(expected, result) except ImportError: skip("test_pandas_rst_with_index is skipped")
def test_sqlite3(): "Input: an sqlite3 cursor" try: import sqlite3 conn = sqlite3.connect(":memory:") cursor = conn.cursor() cursor.execute("CREATE TABLE people (name, age, height)") for values in [("Alice", 23, 169.5), ("Bob", 27, 175.0)]: cursor.execute("INSERT INTO people VALUES (?, ?, ?)", values) cursor.execute("SELECT name, age, height FROM people ORDER BY name") result = tabulate(cursor, headers=["whom", "how old", "how tall"]) expected = """\ whom how old how tall ------ --------- ---------- Alice 23 169.5 Bob 27 175""" assert_equal(expected, result) except ImportError: skip("test_sqlite3 is skipped")
def test_wrap_mixed_string(): """TextWrapper: wrapping string with mix of wide and non-wide chars""" try: import wcwidth # noqa except ImportError: skip("test_wrap_wide_char is skipped") data = ("This content of this string (この文字列のこの内容) contains " "multiple character types (複数の文字タイプが含まれています)") expected = [ "This content of this", "string (この文字列の", "この内容) contains", "multiple character", "types (複数の文字タイ", "プが含まれています)", ] wrapper = CTW(width=21) result = wrapper.wrap(data) assert_equal(expected, result)
def test_numpy_record_array_headers(): "Input: a 2D NumPy record array with user-supplied headers." try: import numpy na = numpy.asarray( [("Alice", 23, 169.5), ("Bob", 27, 175.0)], dtype={ "names": ["name", "age", "height"], "formats": ["a32", "uint8", "float32"], }, ) expected = "\n".join([ "person years cm", "-------- ------- -----", "Alice 23 169.5", "Bob 27 175", ]) result = tabulate(na, headers=["person", "years", "cm"]) assert_equal(expected, result) except ImportError: skip("test_numpy_2d_keys is skipped")
def test_alignment_of_decimal_numbers_with_commas(): "Regression: alignment for decimal numbers with comma separators" skip("test is temporarily disable until the feature is reimplemented")