Files
slic/tests/test_utils_printing.py
T
tligui_y 51575412fa
Run CI Tests / test (push) Successful in 33s
Update tests/test_utils_printing.py
2025-07-29 20:23:15 +02:00

156 lines
4.8 KiB
Python

import pytest
from slic.utils.printing import (
maxlen, maxstrlen, mk_pad, strlen,
_transpose, _prepend, _fmt_table_line, _fmt_label_sep,
printable_dict, printable_dict_of_dicts,
itemize, printable_table
)
@pytest.mark.parametrize("seq,expected", [
(["a", "abc", ""], 3),
([], 0),
([[1, 2], [3], [4, 5, 6]], 3),
([{"a": 2, "b": 3}, {}, {"c": 4}], 2),
])
def test_maxlen_valid(seq, expected):
assert maxlen(seq) == expected
@pytest.mark.parametrize("value,expected", [
(42, 2),
("hello", 5),
(False, 5),
(None, 4),
([1, 2, 3], 9),
({"a": 1}, 8),
((1, 2), 6),
("", 0),
("this is a phrase", 16),
])
def test_strlen(value, expected):
assert strlen(value) == expected
@pytest.mark.parametrize("seq,expected", [
([1, True, 3.1415], 6),
(["aa", "bbb", "c"], 3),
([[1, 2], [], [1, 2, 3]], 9),
([{"a": 1}, {}, {"a": 1, "b": 2}], 16),
([None, False, 12345], 5),
])
def test_maxstrlen(seq, expected):
assert maxstrlen(seq) == expected
@pytest.mark.parametrize("data,expected", [
([[1, 2], [3, 4]], [(1, 3), (2, 4)]),
([["a", "b"], ["c", "d"]], [("a", "c"), ("b", "d")]),
([[{"x": 1}, {"y": 2}], [{"x": 3}, {"y": 4}]], [({"x": 1}, {"x": 3}), ({"y": 2}, {"y": 4})]),
])
def test_transpose_matrix(data, expected):
assert _transpose(data) == expected
@pytest.mark.parametrize("initial,prepend,expected", [
([2, 3], 1, [1, 2, 3]),
(["b", "c"], "a", ["a", "b", "c"]),
([{"b": 2}], {"a": 1}, [{"a": 1}, {"b": 2}]),
([[2], [3]], [1], [[1], [2], [3]]),
])
def test_prepend(initial, prepend, expected):
_prepend(prepend, initial)
assert initial == expected
@pytest.mark.parametrize("entries, widths, expected", [
(["a", "bbb"], [3, 5], " a bbb"),
([1, 2], [2, 2], " 1 2"),
(["long", "val"], [6, 4], " long val"),
([True, False], [5, 6], " True False"),
([123, 4567], [5, 5], " 123 4567"),
(["text with space", "end"], [16, 5], " text with space end"),
([{"a": 1}, {"b": 2}], [10, 10], " {'a': 1} {'b': 2}"),
([[1, 2], [3, 4]], [10, 10], " [1, 2] [3, 4]"),
])
def test_fmt_table_line(entries, widths, expected):
assert _fmt_table_line(entries, widths) == expected
@pytest.mark.parametrize("widths,line,expected", [
([3, 4], "-", "--- ----"),
([2, 3], "=", "== ==="),
([5, 2], "*", "***** **"),
])
def test_fmt_label_sep(widths, line, expected):
assert _fmt_label_sep(widths, line) == expected
@pytest.mark.parametrize("d, header, expected_lines", [
(
{"medium": True, "very_long_key": 3.14, "x": 1},
"HeaderTest",
[
"HeaderTest:",
"-----------",
"medium: True",
"very_long_key: 3.14",
"x: 1",
""
]
)
])
def test_printable_dict_with_header(d, header, expected_lines):
out = printable_dict(d, header=header)
for line in expected_lines:
assert line in out
def test_printable_dict_of_dicts():
d = {
"SectionOne": {
"first": 1,
"long_second": "value"
},
"AnotherSection": {
"x": True,
"another_long_key": None
}
}
expected = (
"AnotherSection:\n"
"---------------\n"
"another_long_key: None\n"
"x: True\n"
"\n"
"SectionOne:\n"
"-----------\n"
"first: 1\n"
"long_second: value\n"
""
)
out = printable_dict_of_dicts(d)
assert out == expected
@pytest.mark.parametrize("data, labels, expected", [
(
[
["X1", True, 0.1234, {"meta": "ok"}],
["AnotherSample", False, 98765.4321, {"meta": [1, 2, 3]}],
["Z", None, 0.0, {"meta": {"nested_key": 42}}]
],
["ID", "✓ Success?", "SuperPrecisionValue", "Result Metadata"],
(
"A: ID\n"
"B: ✓ Success?\n"
"C: SuperPrecisionValue\n"
"D: Result Metadata\n"
"\n"
"# A B C D\n"
"- ------------- ----- ---------- ----------------------------\n"
"0 X1 True 0.1234 {'meta': 'ok'}\n"
"1 AnotherSample False 98765.4321 {'meta': [1, 2, 3]}\n"
"2 Z None 0.0 {'meta': {'nested_key': 42}}"
)
)
])
def test_printable_table(data, labels, expected):
out = printable_table(data, labels=labels, enumerate_lines=True, make_legend=True)
print(repr(out))
print(repr(expected))
assert out == expected