|
1 | 1 | import musicalgestures |
2 | 2 | from musicalgestures._utils import * |
| 3 | +import musicalgestures._utils as _utils_module |
3 | 4 | import numpy as np |
4 | 5 | import os |
5 | 6 | import itertools |
@@ -169,6 +170,71 @@ def test_repr(self): |
169 | 170 | assert print(pb) == None |
170 | 171 |
|
171 | 172 |
|
| 173 | +class Test_show_progress: |
| 174 | + def setup_method(self): |
| 175 | + # Always restore the default state after each test |
| 176 | + _utils_module._SHOW_PROGRESS = True |
| 177 | + |
| 178 | + def teardown_method(self): |
| 179 | + _utils_module._SHOW_PROGRESS = True |
| 180 | + |
| 181 | + def test_show_progress_default_is_true(self): |
| 182 | + assert _utils_module._SHOW_PROGRESS is True |
| 183 | + |
| 184 | + def test_show_progress_disable(self): |
| 185 | + show_progress(False) |
| 186 | + assert _utils_module._SHOW_PROGRESS is False |
| 187 | + |
| 188 | + def test_show_progress_enable(self): |
| 189 | + _utils_module._SHOW_PROGRESS = False |
| 190 | + show_progress(True) |
| 191 | + assert _utils_module._SHOW_PROGRESS is True |
| 192 | + |
| 193 | + def test_show_progress_exposed_on_module(self): |
| 194 | + # Ensure show_progress is accessible via the top-level package |
| 195 | + assert hasattr(musicalgestures, 'show_progress') |
| 196 | + assert callable(musicalgestures.show_progress) |
| 197 | + |
| 198 | + def test_progress_bar_suppressed_does_not_print(self, capsys): |
| 199 | + show_progress(False) |
| 200 | + pb = MgProgressbar(total=100) |
| 201 | + for step in range(103): |
| 202 | + pb.progress(step) |
| 203 | + captured = capsys.readouterr() |
| 204 | + assert captured.out == "" |
| 205 | + |
| 206 | + def test_progress_bar_suppressed_marks_finished(self): |
| 207 | + show_progress(False) |
| 208 | + pb = MgProgressbar(total=100) |
| 209 | + assert not pb.finished |
| 210 | + pb.progress(200) |
| 211 | + assert pb.finished |
| 212 | + |
| 213 | + def test_progress_bar_suppressed_finished_returns_early(self): |
| 214 | + show_progress(False) |
| 215 | + pb = MgProgressbar(total=100) |
| 216 | + pb.finished = True |
| 217 | + # Should return immediately without touching _SHOW_PROGRESS check |
| 218 | + assert pb.progress(50) == None |
| 219 | + |
| 220 | + def test_progress_bar_enabled_after_disable(self, capsys): |
| 221 | + show_progress(False) |
| 222 | + pb = MgProgressbar(total=100) |
| 223 | + pb.could_not_get_terminal_window = True |
| 224 | + for step in range(50): |
| 225 | + pb.progress(step) |
| 226 | + show_progress(True) |
| 227 | + pb2 = MgProgressbar(total=100) |
| 228 | + pb2.could_not_get_terminal_window = True |
| 229 | + import time |
| 230 | + for step in range(103): |
| 231 | + time.sleep(0.01) |
| 232 | + pb2.progress(step) |
| 233 | + captured = capsys.readouterr() |
| 234 | + # pb2 ran with progress bars enabled so output should be non-empty |
| 235 | + assert len(captured.out) > 0 |
| 236 | + |
| 237 | + |
172 | 238 | class Test_roundup: |
173 | 239 | def test_positive(self): |
174 | 240 | assert roundup(10, 4) == 12 |
|
0 commit comments