112 lines
3.1 KiB
Python
112 lines
3.1 KiB
Python
# 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)
|
|
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/5"),
|
|
("40%", "2/5"),
|
|
("70%", "3.5/5.0"),
|
|
("100%", "5/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])
|
|
|
|
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}'" |