# test_tqdm_mod.py import io import sys from time import sleep import pytest import tqdm from contextlib import redirect_stdout import os import re sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from slic.utils.tqdm_mod import * def extract_lines(output, label): return [line for line in output.strip().splitlines() if label in line] def get_bar_visual(line): try: return line.split("|")[1] except IndexError: return "" # Tests def test_complete_progress_bar(): f = io.StringIO() with redirect_stdout(f): for _ in tqdm_mod(range(3), desc="TestBar", file=f): sleep(0.001) lines = extract_lines(f.getvalue(), "TestBar") last = lines[-1] bar = get_bar_visual(last) assert last.startswith("TestBar: 100%") assert "3/3" in last assert "Hz" in last # Check that the bar us full assert len(bar.replace("█", "").replace("#", "").strip()) == 0, f"Bar not full: '{bar}'" def test_set_progress_multiple_points(): f = io.StringIO() with redirect_stdout(f): bar = tqdm_mod(total=5, desc="SetBar", file=f, miniters=1, mininterval=0) bar.set(1.0) bar.set(2.0) bar.set(3.5) bar.set(5.0) bar.close() lines = extract_lines(f.getvalue(), "SetBar") for i, (expected_progress, expected_value) in enumerate([ ("0%", "0/5"), ("20%", "1.0/5"), ("40%", "2.0/5"), ("70%", "3.5/5"), ("100%", "5.0/5"), ]): assert re.search(r"^SetBar:\s+" + re.escape(expected_progress), lines[i]) assert expected_value in lines[i] bar = get_bar_visual(lines[i]) # test format_sizeof rounding def test_set_progress_decimal_precision(): 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.close() lines = extract_lines(f.getvalue(), "FloatBar") expected_values = [ "1.3/5", "3.7/5", "5.0/5", ] 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() with redirect_stdout(f): for _ in tqdm_mod(range(4), desc="StepBar", unit="step", file=f): sleep(0.001) lines = extract_lines(f.getvalue(), "StepBar") last = lines[-1] bar = get_bar_visual(last) assert last.startswith("StepBar: 100%") assert "4/4" in last assert "step/s" in last assert len(bar.replace("█", "").replace("#", "").strip()) == 0, f"Bar not full: '{bar}'" def test_clamp_above_total(): f = io.StringIO() with redirect_stdout(f): bar = tqdm_mod(total=10, desc="ClampBar", file=f) bar.set(12) bar.close() lines = extract_lines(f.getvalue(), "ClampBar") last = lines[-1] bar = get_bar_visual(last) assert last.startswith("ClampBar: 100%") assert "10/10" in last assert "Hz" in last # Check that the bar us full assert len(bar.replace("█", "").replace("#", "").strip()) == 0, f"Bar not full: '{bar}'" def test_clamp_below_zero(): f = io.StringIO() with redirect_stdout(f): bar = tqdm_mod(total=10, desc="ClampBar", file=f) bar.set(-1) bar.close() lines = extract_lines(f.getvalue(), "ClampBar") last = lines[-1] bar = get_bar_visual(last) assert re.search(r"^ClampBar:\s+" + re.escape("0%"), last) assert "0/10" in last assert "Hz" in last # Check that the bar us full assert len(bar.replace(" ", "").strip()) == 0, f"Bar not empty: '{bar}'"