|
| 1 | +""" |
| 2 | +Tests for RefreshPrinter |
| 3 | +""" |
| 4 | +# Standard |
| 5 | +from unittest import mock |
| 6 | +import io |
| 7 | + |
| 8 | +# Third Party |
| 9 | +import pytest |
| 10 | + |
| 11 | +# Local |
| 12 | +from scriptit import RefreshPrinter |
| 13 | + |
| 14 | + |
| 15 | +def test_refresh_printer_multi_line(): |
| 16 | + """Test that adding multiple lines refreshes correctly""" |
| 17 | + stream = io.StringIO() |
| 18 | + printer = RefreshPrinter(write_stream=stream) |
| 19 | + |
| 20 | + # Perform initial add and refresh |
| 21 | + printer.add("Line one") |
| 22 | + printer.add("Line two") |
| 23 | + printer.refresh() |
| 24 | + assert printer.refreshes == 1 |
| 25 | + printed_lines = stream.getvalue().split("\n") |
| 26 | + assert len(printed_lines) == 3 # Two plus final empty line |
| 27 | + assert not any(line.startswith(RefreshPrinter.UP_LINE) for line in printed_lines) |
| 28 | + |
| 29 | + # Perform a second refresh and make sure clearing happens |
| 30 | + printer.add("Line three") |
| 31 | + printer.add("Line four") |
| 32 | + printer.refresh() |
| 33 | + assert printer.refreshes == 2 |
| 34 | + printed_lines = stream.getvalue().split("\n") |
| 35 | + assert len(printed_lines) == 6 # First 2, clear, 2 new, final \n |
| 36 | + assert printed_lines[2].count(RefreshPrinter.UP_LINE) == 3 |
| 37 | + |
| 38 | + |
| 39 | +def test_refresh_printer_newline_in_add(): |
| 40 | + """Make sure that a newline in add is handled as separate lines""" |
| 41 | + stream = io.StringIO() |
| 42 | + printer = RefreshPrinter(write_stream=stream) |
| 43 | + |
| 44 | + # Perform initial add and refresh |
| 45 | + printer.add("Line one\nLine two") |
| 46 | + printer.refresh() |
| 47 | + assert printer.refreshes == 1 |
| 48 | + printed_lines = stream.getvalue().split("\n") |
| 49 | + assert len(printed_lines) == 3 # Two plus final empty line |
| 50 | + assert not any(line.startswith(RefreshPrinter.UP_LINE) for line in printed_lines) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize("wrap", [True, False]) |
| 54 | +def test_refresh_printer_wrap_lines(wrap): |
| 55 | + """Make sure long lines get wrapped if requested""" |
| 56 | + term_size_mock = mock.MagicMock() |
| 57 | + term_width = 5 |
| 58 | + term_size_mock.columns = term_width |
| 59 | + with mock.patch("shutil.get_terminal_size", return_value=term_size_mock): |
| 60 | + stream = io.StringIO() |
| 61 | + printer = RefreshPrinter(write_stream=stream) |
| 62 | + |
| 63 | + # Perform initial add and refresh |
| 64 | + long_line = "*" * int(2.5 * term_width) |
| 65 | + printer.add(long_line, wrap=wrap) |
| 66 | + printer.refresh() |
| 67 | + printed_lines = stream.getvalue().split("\n") |
| 68 | + assert len(printed_lines) == 4 if wrap else 2 |
| 69 | + |
| 70 | + |
| 71 | +def test_clear_last_report_chars(): |
| 72 | + """Make sure that shorter lines in an update don't retain characters from |
| 73 | + previous reports |
| 74 | + """ |
| 75 | + stream = io.StringIO() |
| 76 | + printer = RefreshPrinter(write_stream=stream) |
| 77 | + |
| 78 | + # Perform initial add and refresh |
| 79 | + printer.add("Line one") |
| 80 | + printer.refresh() |
| 81 | + |
| 82 | + # Perform a second with a shorter line and make sure that the line does not |
| 83 | + # have any of the previous report in it |
| 84 | + printer.add("two") |
| 85 | + printer.refresh() |
| 86 | + printed_lines = stream.getvalue().split("\n") |
| 87 | + assert len(printed_lines) == 4 # First 1, clear, 1 new, final \n |
| 88 | + assert printed_lines[1].count(RefreshPrinter.UP_LINE) == 2 |
| 89 | + assert printed_lines[2].strip() == "two" |
0 commit comments