60 lines
2.2 KiB
Python
60 lines
2.2 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
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from slic.utils.tqdm_mod import *
|
|
|
|
|
|
def extract_last_line(output):
|
|
return output.strip().splitlines()[-1]
|
|
|
|
# Test
|
|
|
|
def test_complete_progress_bar():
|
|
f = io.StringIO()
|
|
with redirect_stdout(f):
|
|
for _ in tqdm_mod(range(3), desc="TestBar", file=f, dynamic_ncols=False):
|
|
sleep(0.001)
|
|
last_line = extract_last_line(f.getvalue())
|
|
assert last_line == "TestBar: 100%|########################| 3.00/3.00 [00:00<00:00, 3.0 Hz]"
|
|
|
|
def test_set_progress_multiple_points():
|
|
f = io.StringIO()
|
|
with redirect_stdout(f):
|
|
bar = tqdm_mod(total=5, desc="SetBar", file=f, dynamic_ncols=False)
|
|
bar.set(1.0)
|
|
bar.set(2.0)
|
|
bar.set(3.5)
|
|
bar.set(5.0)
|
|
bar.close()
|
|
|
|
lines = f.getvalue().strip().splitlines()
|
|
setbar_lines = [line for line in lines if "SetBar:" in line]
|
|
|
|
assert setbar_lines[0] == "SetBar: 20%|#####6 | 1.00/5.00 [00:00<00:00, 1.0 Hz]"
|
|
assert setbar_lines[1] == "SetBar: 40%|###########2 | 2.00/5.00 [00:00<00:00, 2.0 Hz]"
|
|
assert setbar_lines[2] == "SetBar: 70%|###################7 | 3.50/5.00 [00:00<00:00, 3.5 Hz]"
|
|
assert setbar_lines[3] == "SetBar: 100%|##############################| 5.00/5.00 [00:00<00:00, 5.0 Hz]"
|
|
|
|
def test_custom_unit():
|
|
f = io.StringIO()
|
|
with redirect_stdout(f):
|
|
for _ in tqdm_mod(range(4), desc="StepBar", unit="step", file=f, dynamic_ncols=False):
|
|
sleep(0.001)
|
|
last_line = extract_last_line(f.getvalue())
|
|
assert last_line == "StepBar: 100%|########################| 4.00/4.00 [00:00<00:00, 4.0step/s]"
|
|
|
|
def test_clamp_above_total():
|
|
f = io.StringIO()
|
|
with redirect_stdout(f):
|
|
bar = tqdm_mod(total=10, desc="ClampBar", file=f, dynamic_ncols=False)
|
|
bar.set(12) # Clamp to 10
|
|
bar.close()
|
|
last_line = extract_last_line(f.getvalue())
|
|
assert last_line == "ClampBar: 100%|##############################| 10.0/10.0 [00:00<00:00, 10.0 Hz]" |