This commit is contained in:
@@ -1,23 +1,19 @@
|
|||||||
import colorama
|
import colorama
|
||||||
import pytest
|
import pytest
|
||||||
from io import StringIO
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# pour s'assurer que slic est bien importable
|
||||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
from slic.utils.rangebar import *
|
from slic.utils.rangebar import RangeBar
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def capture_stdout(monkeypatch):
|
|
||||||
buf = StringIO()
|
|
||||||
monkeypatch.setattr("sys.stdout", buf)
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def test_full_progress_bar(capture_stdout):
|
def test_full_progress_bar(capsys):
|
||||||
out = capture_stdout
|
|
||||||
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
||||||
bar.show(100)
|
bar.show(100)
|
||||||
|
|
||||||
lines = out.getvalue().strip("\r\n").split("\r")
|
captured = capsys.readouterr()
|
||||||
|
lines = captured.out.strip("\r\n").split("\r")
|
||||||
assert lines[0].startswith("[ 0 kg |")
|
assert lines[0].startswith("[ 0 kg |")
|
||||||
expected = (
|
expected = (
|
||||||
f"[ 0 kg |{colorama.Fore.GREEN}{'█' * 10}{colorama.Fore.RESET}| 100 kg ] "
|
f"[ 0 kg |{colorama.Fore.GREEN}{'█' * 10}{colorama.Fore.RESET}| 100 kg ] "
|
||||||
@@ -25,14 +21,14 @@ def test_full_progress_bar(capture_stdout):
|
|||||||
)
|
)
|
||||||
assert lines[-1] == expected
|
assert lines[-1] == expected
|
||||||
|
|
||||||
def test_half_progress_bar(capture_stdout):
|
|
||||||
out = capture_stdout
|
def test_half_progress_bar(capsys):
|
||||||
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
||||||
bar.show(50)
|
bar.show(50)
|
||||||
|
|
||||||
# à 50% pile → 5 blocs pleins, 0 partiel, 5 vides
|
|
||||||
expected_bar = f"{'█' * 5}{' ' * 5}"
|
expected_bar = f"{'█' * 5}{' ' * 5}"
|
||||||
lines = out.getvalue().strip("\r\n").split("\r")
|
captured = capsys.readouterr()
|
||||||
|
lines = captured.out.strip("\r\n").split("\r")
|
||||||
assert lines[0].startswith("[ 0 kg |")
|
assert lines[0].startswith("[ 0 kg |")
|
||||||
expected = (
|
expected = (
|
||||||
f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] "
|
f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] "
|
||||||
@@ -40,13 +36,14 @@ def test_half_progress_bar(capture_stdout):
|
|||||||
)
|
)
|
||||||
assert lines[-1] == expected
|
assert lines[-1] == expected
|
||||||
|
|
||||||
def test_zero_progress_bar(capture_stdout):
|
|
||||||
out = capture_stdout
|
def test_zero_progress_bar(capsys):
|
||||||
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
||||||
bar.show(0)
|
bar.show(0)
|
||||||
|
|
||||||
expected_bar = f"{' ' * 10}" # pas de bloc au tout début
|
expected_bar = f"{' ' * 10}" # pas de bloc au tout début
|
||||||
lines = out.getvalue().strip("\r\n").split("\r")
|
captured = capsys.readouterr()
|
||||||
|
lines = captured.out.strip("\r\n").split("\r")
|
||||||
assert lines[0].startswith("[ 0 kg |")
|
assert lines[0].startswith("[ 0 kg |")
|
||||||
expected = (
|
expected = (
|
||||||
f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] "
|
f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] "
|
||||||
@@ -54,12 +51,13 @@ def test_zero_progress_bar(capture_stdout):
|
|||||||
)
|
)
|
||||||
assert lines[-1] == expected
|
assert lines[-1] == expected
|
||||||
|
|
||||||
def test_overflow_bar(capture_stdout):
|
|
||||||
out = capture_stdout
|
def test_overflow_bar(capsys):
|
||||||
with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar:
|
with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar:
|
||||||
bar.show(15)
|
bar.show(15)
|
||||||
|
|
||||||
lines = out.getvalue().strip("\r\n").split("\r")
|
captured = capsys.readouterr()
|
||||||
|
lines = captured.out.strip("\r\n").split("\r")
|
||||||
assert lines[0].startswith("[ 0 m |")
|
assert lines[0].startswith("[ 0 m |")
|
||||||
expected = (
|
expected = (
|
||||||
f"[ 0 m |{colorama.Fore.RED}{'>' * 10}{colorama.Fore.RESET}| 10 m ] "
|
f"[ 0 m |{colorama.Fore.RED}{'>' * 10}{colorama.Fore.RESET}| 10 m ] "
|
||||||
@@ -67,12 +65,13 @@ def test_overflow_bar(capture_stdout):
|
|||||||
)
|
)
|
||||||
assert lines[-1] == expected
|
assert lines[-1] == expected
|
||||||
|
|
||||||
def test_underflow_bar(capture_stdout):
|
|
||||||
out = capture_stdout
|
def test_underflow_bar(capsys):
|
||||||
with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar:
|
with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar:
|
||||||
bar.show(-5)
|
bar.show(-5)
|
||||||
|
|
||||||
lines = out.getvalue().strip("\r\n").split("\r")
|
captured = capsys.readouterr()
|
||||||
|
lines = captured.out.strip("\r\n").split("\r")
|
||||||
assert lines[0].startswith("[ 0 m |")
|
assert lines[0].startswith("[ 0 m |")
|
||||||
expected = (
|
expected = (
|
||||||
f"[ 0 m |{colorama.Fore.RED}{'<' * 10}{colorama.Fore.RESET}| 10 m ] "
|
f"[ 0 m |{colorama.Fore.RED}{'<' * 10}{colorama.Fore.RESET}| 10 m ] "
|
||||||
@@ -80,18 +79,18 @@ def test_underflow_bar(capture_stdout):
|
|||||||
)
|
)
|
||||||
assert lines[-1] == expected
|
assert lines[-1] == expected
|
||||||
|
|
||||||
|
|
||||||
def test_repr():
|
def test_repr():
|
||||||
bar = RangeBar(0, 100, width=10, units="kg", fmt=".0f")
|
bar = RangeBar(0, 100, width=10, units="kg", fmt=".0f")
|
||||||
expected = "[ 0 kg |10 blocks| 100 kg ]"
|
expected = "[ 0 kg |10 blocks| 100 kg ]"
|
||||||
assert repr(bar) == expected
|
assert repr(bar) == expected
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("value, expected_bar_part", [
|
@pytest.mark.parametrize("value, expected_bar_part", [
|
||||||
(0, ' ' * 10),
|
(0, ' ' * 10),
|
||||||
(10, '█' + ' ' * 9),
|
(10, '█' + ' ' * 9),
|
||||||
(25, '██▌' + ' ' * 7),
|
(25, '██▌' + ' ' * 7),
|
||||||
(27, '██▊' + ' ' * 7),
|
(27, '██▊' + ' ' * 7),
|
||||||
(49, '████▉' + ' ' * 5),
|
(49, '████▉' + ' ' * 5),
|
||||||
(50, '█' * 5 + ' ' * 5),
|
(50, '█' * 5 + ' ' * 5),
|
||||||
(51, '█' * 5 + '▏' + ' ' * 4),
|
(51, '█' * 5 + '▏' + ' ' * 4),
|
||||||
@@ -100,13 +99,15 @@ import pytest
|
|||||||
(90, '█' * 9 + ' ' * 1),
|
(90, '█' * 9 + ' ' * 1),
|
||||||
(100, '█' * 10)
|
(100, '█' * 10)
|
||||||
])
|
])
|
||||||
def test_each_value_separately(capture_stdout, value, expected_bar_part):
|
def test_each_value_separately(capsys, value, expected_bar_part):
|
||||||
out = capture_stdout
|
|
||||||
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar:
|
||||||
bar.show(value)
|
bar.show(value)
|
||||||
|
|
||||||
result = out.getvalue().strip()
|
captured = capsys.readouterr()
|
||||||
expected = f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar_part}{colorama.Fore.RESET}| 100 kg ] {colorama.Style.BRIGHT}{value} kg{colorama.Style.RESET_ALL}"
|
result = captured.out.strip()
|
||||||
|
expected = (
|
||||||
assert expected in result, f"\nExpected: {expected}\nGot: {result}"
|
f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar_part}{colorama.Fore.RESET}| 100 kg ] "
|
||||||
|
f"{colorama.Style.BRIGHT}{value} kg{colorama.Style.RESET_ALL}"
|
||||||
|
)
|
||||||
|
|
||||||
|
assert expected in result, f"\nExpected: {expected}\nGot: {result}"
|
||||||
Reference in New Issue
Block a user