diff --git a/tests/test_utils_rangebar.py b/tests/test_utils_rangebar.py index 66374a43e..944c34819 100644 --- a/tests/test_utils_rangebar.py +++ b/tests/test_utils_rangebar.py @@ -6,7 +6,6 @@ import os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from slic.utils.rangebar import * - @pytest.fixture def capture_stdout(monkeypatch): buf = StringIO() @@ -18,64 +17,67 @@ def test_full_progress_bar(capture_stdout): with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(100) - line = out.getvalue().strip("\r\n") + lines = out.getvalue().strip("\r\n").split("\r") + assert lines[0].startswith("[ 0 kg |") # initial __enter__ line expected = ( f"[ 0 kg |{colorama.Fore.GREEN}{'█' * 10}{colorama.Fore.RESET}| 100 kg ] " f"{colorama.Style.BRIGHT}100 kg{colorama.Style.RESET_ALL}" ) - assert line == expected + assert lines[-1] == expected def test_half_progress_bar(capture_stdout): out = capture_stdout with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(50) - # 5 full + 1 partial + 4 empty expected_bar = f"{'█' * 5}▌{' ' * 4}" - line = out.getvalue().strip("\r\n") + lines = out.getvalue().strip("\r\n").split("\r") + assert lines[0].startswith("[ 0 kg |") expected = ( f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] " f"{colorama.Style.BRIGHT}50 kg{colorama.Style.RESET_ALL}" ) - assert line == expected + assert lines[-1] == expected def test_zero_progress_bar(capture_stdout): out = capture_stdout with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(0) - # no full block, one empty partial, 9 spaces expected_bar = f"{'▏'}{' ' * 9}" - line = out.getvalue().strip("\r\n") + lines = out.getvalue().strip("\r\n").split("\r") + assert lines[0].startswith("[ 0 kg |") expected = ( f"[ 0 kg |{colorama.Fore.GREEN}{expected_bar}{colorama.Fore.RESET}| 100 kg ] " f"{colorama.Style.BRIGHT}0 kg{colorama.Style.RESET_ALL}" ) - assert line == expected + assert lines[-1] == expected def test_overflow_bar(capture_stdout): out = capture_stdout with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar: bar.show(15) - line = out.getvalue().strip("\r\n") + lines = out.getvalue().strip("\r\n").split("\r") + assert lines[0].startswith("[ 0 m |") expected = ( f"[ 0 m |{colorama.Fore.RED}{'>' * 10}{colorama.Fore.RESET}| 10 m ] " f"{colorama.Style.BRIGHT}15 m{colorama.Style.RESET_ALL}" ) - assert line == expected + assert lines[-1] == expected def test_underflow_bar(capture_stdout): out = capture_stdout with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar: bar.show(-5) - line = out.getvalue().strip("\r\n") + lines = out.getvalue().strip("\r\n").split("\r") + assert lines[0].startswith("[ 0 m |") expected = ( f"[ 0 m |{colorama.Fore.RED}{'<' * 10}{colorama.Fore.RESET}| 10 m ] " f"{colorama.Style.BRIGHT}-5 m{colorama.Style.RESET_ALL}" ) - assert line == expected + assert lines[-1] == expected def test_repr(): bar = RangeBar(0, 100, width=10, units="kg", fmt=".0f") @@ -95,6 +97,7 @@ def test_multiple_show_evolution(capture_stdout): result = out.getvalue() lines = result.strip().split("\r") + expected_lines = [ f"[ 0 kg |{colorama.Fore.GREEN}{'▏'}{' ' * 9}{colorama.Fore.RESET}| 100 kg ] {colorama.Style.BRIGHT}0 kg{colorama.Style.RESET_ALL}", f"[ 0 kg |{colorama.Fore.GREEN}█{'▏'}{' ' * 8}{colorama.Fore.RESET}| 100 kg ] {colorama.Style.BRIGHT}10 kg{colorama.Style.RESET_ALL}", @@ -105,5 +108,6 @@ def test_multiple_show_evolution(capture_stdout): f"[ 0 kg |{colorama.Fore.GREEN}██████████{colorama.Fore.RESET}| 100 kg ] {colorama.Style.BRIGHT}100 kg{colorama.Style.RESET_ALL}", ] + # Ignore the first __enter__ print for expected in expected_lines: assert expected in lines