diff --git a/tests/test_utils_tqdm_mod.py b/tests/test_utils_tqdm_mod.py index 74bdba12f..869ed7885 100644 --- a/tests/test_utils_tqdm_mod.py +++ b/tests/test_utils_tqdm_mod.py @@ -82,31 +82,47 @@ def test_format_sizeof_alignment(): # test format_sizeof rounding using tqdm def test_float_alignment_in_bar(): - import io - from contextlib import redirect_stdout + # Capture the tqdm output into a string buffer f = io.StringIO() with redirect_stdout(f): bar = tqdm_mod(total=100.12, desc="AlignBar", file=f) - bar.set(1.3) + bar.set(1.3333) bar.set(12.5) - bar.set(99.9) + bar.set(99.98) bar.set(100.0) - bar.set(100.1) + bar.set(100.12) bar.close() + # Extract lines containing the label lines = extract_lines(f.getvalue(), "AlignBar") - - # Extrait uniquement les morceaux contenant "xx.x/100.1" + + # Expected strings that should appear in the output, rounded with format_sizeof + expected_values = [ + " 1.3/100.1", + " 12.5/100.1", + " 99.9/100.1", + "100.0/100.1", + "100.1/100.1", + ] + + # Extract progress values matching the format "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" + values.append(match.group(0)) - # Vérifie que toutes les chaînes ont exactement la même longueur + # Ensure raw 100.12 never appears : format_sizeof must have truncated it + assert all("100.12" not in line for line in lines), "Unrounded value '100.12' found in output!" + + # Ensure all expected formatted values are found in the output + for expected in expected_values: + assert expected in values, f"Missing expected value: {expected}" + + # Ensure all values are visually aligned thanks to format_sizeof : all outouts with same length lengths = [len(v) for v in values] - assert len(set(lengths)) == 1, f"Lengths differ: {lengths}, values: {values}" + assert len(set(lengths)) == 1, f"Misaligned values: {values}" def test_custom_unit():