This commit is contained in:
@@ -82,31 +82,47 @@ def test_format_sizeof_alignment():
|
|||||||
|
|
||||||
# test format_sizeof rounding using tqdm
|
# test format_sizeof rounding using tqdm
|
||||||
def test_float_alignment_in_bar():
|
def test_float_alignment_in_bar():
|
||||||
import io
|
|
||||||
from contextlib import redirect_stdout
|
|
||||||
|
|
||||||
|
# Capture the tqdm output into a string buffer
|
||||||
f = io.StringIO()
|
f = io.StringIO()
|
||||||
with redirect_stdout(f):
|
with redirect_stdout(f):
|
||||||
bar = tqdm_mod(total=100.12, desc="AlignBar", file=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(12.5)
|
||||||
bar.set(99.9)
|
bar.set(99.98)
|
||||||
bar.set(100.0)
|
bar.set(100.0)
|
||||||
bar.set(100.1)
|
bar.set(100.12)
|
||||||
bar.close()
|
bar.close()
|
||||||
|
|
||||||
|
# Extract lines containing the label
|
||||||
lines = extract_lines(f.getvalue(), "AlignBar")
|
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 = []
|
values = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
match = re.search(r"(\d{1,3}\.\d)/100\.1", line)
|
match = re.search(r"(\d{1,3}\.\d)/100\.1", line)
|
||||||
if match:
|
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]
|
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():
|
def test_custom_unit():
|
||||||
|
|||||||
Reference in New Issue
Block a user