diff --git a/tests/test_utils_tqdm_mod.py b/tests/test_utils_tqdm_mod.py index 0b79e51c6..74bdba12f 100644 --- a/tests/test_utils_tqdm_mod.py +++ b/tests/test_utils_tqdm_mod.py @@ -63,26 +63,51 @@ def test_set_progress_multiple_points(): assert expected_value in lines[i] bar = get_bar_visual(lines[i]) -# test format_sizeof rounding -def test_set_progress_decimal_precision(): +def test_format_sizeof_alignment(): + floats = [0.1, 1.3, 12.5, 99.9, 100.0, 100.1] + expected_outputs = [ + " 0.1", + " 1.3", + " 12.5", + " 99.9", + "100.0", + "100.1", + ] + + for val, expected in zip(floats, expected_outputs): + result = format_sizeof(val) + assert result == expected, f"For {val}, expected '{expected}' but got '{result}'" + assert len(result) == 5, f"Length mismatch: got '{result}' of length {len(result)}" + + +# test format_sizeof rounding using tqdm +def test_float_alignment_in_bar(): + import io + from contextlib import redirect_stdout + f = io.StringIO() with redirect_stdout(f): - bar = tqdm_mod(total=5, desc="FloatBar", file=f, miniters=1, mininterval=0) - bar.set(1.3333) - bar.set(3.6666) - bar.set(4.999) + bar = tqdm_mod(total=100.12, desc="AlignBar", file=f) + bar.set(1.3) + bar.set(12.5) + bar.set(99.9) + bar.set(100.0) + bar.set(100.1) bar.close() - lines = extract_lines(f.getvalue(), "FloatBar") + lines = extract_lines(f.getvalue(), "AlignBar") + + # Extrait uniquement les morceaux contenant "xx.x/100.1" + values = [] + for line in lines: + match = re.search(r"(\d{1,3}\.\d)/100\.1", line) + if match: + values.append(match.group(0)) # e.g. " 1.3/100.1" - expected_values = [ - "1.3/5", - "3.7/5", - "5.0/5", - ] + # Vérifie que toutes les chaînes ont exactement la même longueur + lengths = [len(v) for v in values] + assert len(set(lengths)) == 1, f"Lengths differ: {lengths}, values: {values}" - for expected in expected_values: - assert any(expected in line for line in lines), f"{expected} not found in output:\n" + "\n".join(lines) def test_custom_unit(): f = io.StringIO()