diff --git a/tests/test_utils_printing.py b/tests/test_utils_printing.py index 2fa56876..f48792fd 100644 --- a/tests/test_utils_printing.py +++ b/tests/test_utils_printing.py @@ -1,22 +1,11 @@ import pytest from slic.utils.printing import ( - printable_dict, - printable_dict_of_dicts, - printable_table, - itemize, - maxstrlen, - maxlen, - mk_pad, - strlen, - format_header, - _prepend, - _transpose, - _fmt_table_data, - _fmt_table_line, - _fmt_label_sep, + maxlen, maxstrlen, mk_pad, strlen, + _transpose, _prepend, _fmt_table_line, _fmt_label_sep, + printable_dict, printable_dict_of_dicts, + itemize, printable_table ) -# maxlen / maxstrlen / strlen @pytest.mark.parametrize("seq,expected", [ (["a", "abc", ""], 3), ([], 0), @@ -26,201 +15,147 @@ from slic.utils.printing import ( def test_maxlen_valid(seq, expected): assert maxlen(seq) == expected -@pytest.mark.parametrize("seq,expected", [ - ([1, True, 3.1415], 6), # "3.1415" - (["aa", "bbb", "c"], 3), # "bbb" - ([[1, 2], [], [1, 2, 3]], 9), # "[1, 2, 3]" - ([{"a": 1}, {}, {"a": 1, "b": 2}], 16), # "{'a': 1, 'b': 2}" - ([None, False, 12345], 5), # "12345" -]) -def test_maxstrlen(seq, expected): - assert maxstrlen(seq) == expected - @pytest.mark.parametrize("value,expected", [ - (42, 2), # int → "42" - ("hello", 5), # str → "hello" - (False, 5), # bool → "False" - (None, 4), # None → "None" - ([1, 2, 3], len(str([1, 2, 3]))), # list → "[1, 2, 3]" - ({'a': 1}, len(str({'a': 1}))), # dict → "{'a': 1}" - ((1, 2), len(str((1, 2)))), # tuple → "(1, 2)" - ("", 0), # empty string - ("ok ok", 5), + (42, 2), + ("hello", 5), + (False, 5), + (None, 4), + ([1, 2, 3], 9), + ({"a": 1}, 10), + ((1, 2), 8), + ("", 0), + ("this is a phrase", 16), ]) def test_strlen(value, expected): assert strlen(value) == expected -# mk_pad -@pytest.mark.parametrize("x,length,expected", [ - ("a", 4, " "), - ("abc", 4, " "), - ("abcd", 4, ""), +@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_mk_pad(x, length, expected): - assert mk_pad(x, length) == expected +def test_maxstrlen(seq, expected): + assert maxstrlen(seq) == expected -# format_header -@pytest.mark.parametrize("msg,line,expected", [ - ("Title", "-", "Title:\n------"), - ("Hellooo", "=", "Hellooo:\n========"), -]) -def test_format_header(msg, line, expected): - assert format_header(msg, line) == expected - -# itemize -@pytest.mark.parametrize("items,header,bullet,expected_lines", [ - (["apple", "banana"], None, "-", ["- apple", "- banana"]), - (["x", "y"], "Vars", "*", ["Vars:\n-----", "* x", "* y"]), - (["one"], None, "+", ["+ one"]), -]) -def test_itemize(items, header, bullet, expected_lines): - result = itemize(items, header=header, bullet=bullet or "-") - for expected in expected_lines: - assert expected in result - -# printable_dict -@pytest.mark.parametrize("d,header,sorter,expected_lines", [ - ( - {"x": 1, "medium": True, "very_long_key": 3.14}, - "HeaderTest", - sorted, - [ - "HeaderTest:\n-----------", - "medium: True", - "very_long_key:3.14", - "x: 1" - ] - ), - ( - {"short": None, "loooooongkey": [1, 2]}, - None, - None, - [ - "short: None", - "loooooongkey:[1, 2]" - ] - ), -]) -def test_printable_dict_variants_with_alignment(d, header, sorter, expected_lines): - out = printable_dict(d, header=header, sorter=sorter) - for expected in expected_lines: - assert expected in out, f"Missing or misaligned line: {expected}" - if header: - assert format_header(header) in out - - -# printable_dict_of_dicts -@pytest.mark.parametrize("d,expected_blocks", [ - ( - { - "General": { - "a": 1, - "long_key": True, - "very_very_long_key": 3.14 - }, - "MetaInfo": { - "short": None, - "a_much_longer_key": {"x": 1} - } - }, - [ - format_header("General"), - "a: 1", - "long_key: True", - "very_very_long_key: 3.14", - format_header("MetaInfo"), - "short: None", - "a_much_longer_key: {'x': 1}" - ] - ), -]) -def test_printable_dict_of_dicts_complete(d, expected_blocks): - out = printable_dict_of_dicts(d) - for line in expected_blocks: - assert line in out, f"Expected line not found or misaligned: {line}" - assert out.endswith("\n") or out.endswith("\n\n") # blank line at the end - - -#_transpose @pytest.mark.parametrize("data,expected", [ - ([[1, 2], [3, 4]], [(1, 3), (2, 4)]), # ints - ([["a", "b"], ["c", "d"]], [("a", "c"), ("b", "d")]), # strings - ([[{"x": 1}, {"y": 2}], [{"x": 3}, {"y": 4}]], [({"x": 1}, {"x": 3}), ({"y": 2}, {"y": 4})]), # dict + ([[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 -# _prepend @pytest.mark.parametrize("initial,prepend,expected", [ - ([2, 3], 1, [1, 2, 3]), # int - (["b", "c"], "a", ["a", "b", "c"]), # str - ([{"b": 2}], {"a": 1}, [{"a": 1}, {"b": 2}]), # dict - ([[2], [3]], [1], [[1], [2], [3]]), # list of lists + ([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 -# _fmt_table_line @pytest.mark.parametrize("entries, widths, expected", [ - (["a", "bbb"], [3, 5], " a bbb"), # simple string - ([1, 2], [2, 2], " 1 2"), # int - (["long", "val"], [6, 4], " long val"), # more padding - ([True, False], [5, 6], " True False"), # bool - ([123, 4567], [5, 5], " 123 4567"), # numbers - (["text with space", "end"], [16, 5], " text with space end"), # long text - ([{"a": 1}, {"b": 2}], [12, 12], " {'a': 1} {'b': 2}"), # dicts - ([[1, 2], [3, 4]], [10, 10], " [1, 2] [3, 4]"), # lists - ([[{"x": 1}], [{"y": 2}]], [16, 16], " [{'x': 1}] [{'y': 2}]"), # list of dict - ([[1, [2, 3]], [4, [5, 6]]], [18, 18], " [1, [2, 3]] [4, [5, 6]]"), # nested lists + (["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 -# _fmt_label_sep @pytest.mark.parametrize("widths,line,expected", [ - ([3, 4], "-", "--- ----"), # standard - ([2, 3], "=", "== ==="), # custom char - ([10, 12], "-", "---------- ------------"), # colonnes larges - ([len(str([1, 2, 3])), len(str({'a': 1}))], "-", - "--------- ----------"), # largeur basée sur list/dict str - ([len(str([{'x': 1}]))] * 2, "=", - "============== =============="), # list of dict - ([len(str([1, [2, 3]])), len(str([4, [5, 6]]))], "#", - "############## ##############"), # nested lists + ([3, 4], "-", "--- ----"), + ([2, 3], "=", "== ==="), + ([5, 2], "*", "***** **"), ]) def test_fmt_label_sep(widths, line, expected): assert _fmt_label_sep(widths, line) == expected - -# printable_table -def test_printable_table_exact_output(): - data = [ - ["X1", True, 0.1234, {"meta": "ok"}], - ["AnotherSample", False, 98765.4321, {"meta": [1, 2, 3]}], - ["Z", None, 0.0, {"meta": {"nested_key": 42}}] - ] - labels = ["ID", "✓ Success?", "SuperPrecisionValue", "Result Metadata"] - - expected = ( - "A: ID\n" - "B: ✓ Success?\n" - "C: SuperPrecisionValue\n" - "D: Result Metadata\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}}\n" -) - - out = printable_table( - data, - labels=labels, - enumerate_lines=True, - make_legend=True +@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 = ( + "SectionOne:\n" + "-----------\n" + "first: 1\n" + "long_second: value\n" + "\n" + "AnotherSection:\n" + "---------------\n" + "x: True\n" + "another_long_key: None\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 +" + "B: ✓ Success? +" + "C: SuperPrecisionValue +" + "D: Result Metadata +" + "# A B C D +" + "------------------------------------------------------------------ +" + "0 X1 True 0.1234 {'meta': 'ok'} +" + "1 AnotherSample False 98765.4321 {'meta': [1, 2, 3]} +" + "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) assert out == expected