Files
slic/tests/test_utils_cpint.py
T
tligui_y 23eda376fb
Run CI Tests / test (push) Has been cancelled
Add tests/test_utils_cpint.py
2025-07-25 10:46:43 +02:00

81 lines
3.8 KiB
Python

import pytest
import sys
import os
from colorama import Style
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from slic.utils.cprint import *
from slic.utils.cprint import _load_color_variants
# Test _load_color_variants
@pytest.mark.parametrize("base_color", [
"red", "blue", "yellow", "green", "cyan", "magenta", "white", "black"
])
def test_load_color_variants_all_keys_and_types(base_color):
variants = _load_color_variants(base_color)
expected_suffixes = ["--", "-", "", "+", "++"]
for suf in expected_suffixes:
key = base_color + suf
assert key in variants, f"Missing variant key: {key}"
ansi = variants[key]
assert isinstance(ansi, str) and ansi.startswith("\x1b"), f"Invalid ANSI code for {key}: {ansi}"
# Check that all values are unique
assert len(set(variants.values())) == len(variants)
# Tprint and indirectly the intermediate functions
@pytest.mark.parametrize(
"objects,color_spec,sep,expected_flatten,expected_error",
[
# ----- Valid color variants -----
([["Fancy", "list"], {"a": 7}, None], ("red", "+"), " | ", "['Fancy', 'list'] | {'a': 7} | None", None),
([{"k": [1,2]}, 99, ["X", ["Y"]]], ("blue", "++"), " - ","{'k': [1, 2]} - 99 - ['X', ['Y']]", None),
([[], {}, "End"], ("magenta", "--"), " / ", "[] / {} / End", None),
([["", [3,4]], "done", 0], ("green", ""), ";", "['', [3, 4]];done;0", None),
([["alpha", None], ["beta", {}], "stop"], ("yellow", ""), "::", "['alpha', None]::['beta', {}]::stop", None),
([["deep", ["deeper", ["deepest"]]], "X"], ("cyan", "+"), " ... ", "['deep', ['deeper', ['deepest']]] ... X", None),
([{"dict": {"nested": [4,5]}}, [True, False], 6.28], ("white", "++"), " // ", "{'dict': {'nested': [4, 5]}} // [True, False] // 6.28", None),
([["A", ["B"]], "string", "C"], ("red", "--"), "==", "['A', ['B']]==string==C", None),
([["Test", None, []], {"v": 0}], ("green", "++"), " ++ ", "['Test', None, []] ++ {'v': 0}", None),
# ----- No color -----
([["no", "color"], "plain"], None, ";", "['no', 'color'];plain", None),
([["simple"], "", 12], None, " | ", "['simple'] | | 12", None),
([[["very", "deep"]], {"ok": True}], None, " : ", "[['very', 'deep']] : {'ok': True}", None),
# ----- Invalid colors-----
([["fail", "color"], 123], ("green", "!!"), "|", "['fail', 'color']|123", ValueError),
([["error"], {}], ("cyan", "xxx"), " * ", "['error'] * {}", ValueError),
([["nope"], ["bad"]], ("magenta", "invalid"), "//", "['nope']//['bad']", ValueError),
(["wrong", "base"], ("notacolor", ""), "--", "wrong--base", ValueError),
]
)
def test_cprint_all_cases_fancy(capsys, objects, color_spec, sep, expected_flatten, expected_error):
# Check if flatten_strings is correct
assert flatten_strings(objects, sep) == expected_flatten
# Build color key or None
if color_spec is None:
color_key = None
else:
color_key = color_spec[0] + color_spec[1]
# Test error if expected
if expected_error is not None:
with pytest.raises(expected_error):
cprint(*objects, color=color_key, sep=sep)
return
# Otherwise, check output as usual
if color_key is None:
# No color: output is just flatten, no color ANSI
cprint(*objects, color=None, sep=sep)
out = capsys.readouterr().out
assert out == expected_flatten + "\n"
assert "\x1b" not in out
else:
# Valid color: get code, check output
ansi_variants = _load_color_variants(color_spec[0])
ansi_code = ansi_variants[color_key]
expected = f"{ansi_code}{expected_flatten}{Style.RESET_ALL}\n"
cprint(*objects, color=color_key, sep=sep)
out = capsys.readouterr().out
assert expected in out