import colorama import pytest import os import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from slic.utils.rangebar import RangeBar def test_full_progress_bar(capsys): with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(100) captured = capsys.readouterr() lines = captured.out.strip("\r\n").split("\r") assert lines[0].startswith("[ 0 kg |") expected = ( f"[ 0 kg |{colorama.Fore.GREEN}{'█' * 10}{colorama.Fore.RESET}| 100 kg ] " f"{colorama.Style.BRIGHT}100 kg{colorama.Style.RESET_ALL}" ) assert lines[-1] == expected def test_half_progress_bar(capsys): with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(50) expected_bar = f"{'█' * 5}{' ' * 5}" captured = capsys.readouterr() lines = captured.out.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 lines[-1] == expected def test_zero_progress_bar(capsys): with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(0) expected_bar = f"{' ' * 10}" # not bloc at the start captured = capsys.readouterr() lines = captured.out.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 lines[-1] == expected def test_overflow_bar(capsys): with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar: bar.show(15) captured = capsys.readouterr() lines = captured.out.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 lines[-1] == expected def test_underflow_bar(capsys): with RangeBar(0, 10, width=10, fmt=".0f", units="m") as bar: bar.show(-5) captured = capsys.readouterr() lines = captured.out.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 lines[-1] == expected def test_repr(): bar = RangeBar(0, 100, width=10, units="kg", fmt=".0f") expected = "[ 0 kg |10 blocks| 100 kg ]" assert repr(bar) == expected @pytest.mark.parametrize("value, expected_bar_part", [ (0, ' ' * 10), (10, '█' + ' ' * 9), (25, '██▌' + ' ' * 7), (27, '██▊' + ' ' * 7), (49, '████▉' + ' ' * 5), (50, '█' * 5 + ' ' * 5), (51, '█' * 5 + '▏' + ' ' * 4), (73, '█' * 7 + '▎' + ' ' * 2), (75, '█' * 7 + '▌' + ' ' * 2), (90, '█' * 9 + ' ' * 1), (100, '█' * 10) ]) def test_each_value_separately(capsys, value, expected_bar_part): with RangeBar(0, 100, width=10, units="kg", fmt=".0f") as bar: bar.show(value) captured = capsys.readouterr() result = captured.out.strip() expected = ( 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}"