|
| 1 | +from pudb.ui_tools import Caption, CaptionParts |
| 2 | +import pytest |
| 3 | +import urwid |
| 4 | + |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def text_markups(): |
| 8 | + from collections import namedtuple |
| 9 | + Markups = namedtuple("Markups", |
| 10 | + ["pudb_version", "hotkey", "full_source_filename", |
| 11 | + "alert", "default_separator", "custom_separator"]) |
| 12 | + |
| 13 | + pudb_version = (None, "PuDB VERSION") |
| 14 | + hotkey = (None, "?:help") |
| 15 | + full_source_filename = (None, "/home/foo - bar/baz.py") |
| 16 | + alert = ("warning", "[POST-MORTEM MODE]") |
| 17 | + default_separator = (None, " - ") |
| 18 | + custom_separator = (None, " | ") |
| 19 | + return Markups(pudb_version, hotkey, full_source_filename, |
| 20 | + alert, default_separator, custom_separator) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def captions(text_markups): |
| 25 | + empty = CaptionParts(*[(None, "")]*4) |
| 26 | + always_display = [ |
| 27 | + text_markups.pudb_version, text_markups.hotkey, |
| 28 | + text_markups.full_source_filename] |
| 29 | + return {"empty": Caption(empty), |
| 30 | + "without_alert": Caption(CaptionParts(*always_display, (None, ""))), |
| 31 | + "with_alert": Caption(CaptionParts(*always_display, text_markups.alert)), |
| 32 | + "custom_separator": Caption(CaptionParts(*always_display, (None, "")), |
| 33 | + separator=text_markups.custom_separator), |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +def test_init(captions): |
| 38 | + for key in ["empty", "without_alert", "with_alert"]: |
| 39 | + assert captions[key].separator == (None, " - ") |
| 40 | + assert captions["custom_separator"].separator == (None, " | ") |
| 41 | + |
| 42 | + |
| 43 | +def test_str(captions): |
| 44 | + assert str(captions["empty"]) == "" |
| 45 | + assert str(captions["without_alert"] |
| 46 | + ) == "PuDB VERSION - ?:help - /home/foo - bar/baz.py" |
| 47 | + assert str(captions["with_alert"] |
| 48 | + ) == "PuDB VERSION - ?:help - /home/foo - bar/baz.py - [POST-MORTEM MODE]" # noqa |
| 49 | + assert str(captions["custom_separator"] |
| 50 | + ) == "PuDB VERSION | ?:help | /home/foo - bar/baz.py" |
| 51 | + |
| 52 | + |
| 53 | +def test_markup(captions): |
| 54 | + assert captions["empty"].markup \ |
| 55 | + == [(None, ""), (None, " - "), |
| 56 | + (None, ""), (None, " - "), |
| 57 | + (None, "")] |
| 58 | + |
| 59 | + assert captions["without_alert"].markup \ |
| 60 | + == [(None, "PuDB VERSION"), (None, " - "), |
| 61 | + (None, "?:help"), (None, " - "), |
| 62 | + (None, "/home/foo - bar/baz.py")] |
| 63 | + |
| 64 | + assert captions["with_alert"].markup \ |
| 65 | + == [(None, "PuDB VERSION"), (None, " - "), |
| 66 | + (None, "?:help"), (None, " - "), |
| 67 | + (None, "/home/foo - bar/baz.py"), (None, " - "), |
| 68 | + ("warning", "[POST-MORTEM MODE]")] |
| 69 | + |
| 70 | + assert captions["custom_separator"].markup \ |
| 71 | + == [(None, "PuDB VERSION"), (None, " | "), |
| 72 | + (None, "?:help"), (None, " | "), |
| 73 | + (None, "/home/foo - bar/baz.py")] |
| 74 | + |
| 75 | + |
| 76 | +def test_render(captions): |
| 77 | + for k in captions.keys(): |
| 78 | + sizes = {"wider_than_caption": (max(1, len(str(captions[k])) + 1), ), |
| 79 | + "equals_caption": (max(1, len(str(captions[k]))), ), |
| 80 | + "narrower_than_caption": (max(1, len(str(captions[k])) - 10), ), |
| 81 | + } |
| 82 | + for s in sizes: |
| 83 | + got = captions[k].render(sizes[s]) |
| 84 | + markup = captions[k]._get_fit_width_markup(sizes[s]) |
| 85 | + expected = urwid.Text(markup).render(sizes[s]) |
| 86 | + assert list(expected.content()) == list(got.content()) |
| 87 | + |
| 88 | + |
| 89 | +def test_set_text(captions): |
| 90 | + assert captions["empty"].caption_parts == CaptionParts(*[(None, "")]*4) |
| 91 | + for key in ["without_alert", "custom_separator"]: |
| 92 | + assert captions[key].caption_parts \ |
| 93 | + == CaptionParts( |
| 94 | + (None, "PuDB VERSION"), |
| 95 | + (None, "?:help"), |
| 96 | + (None, "/home/foo - bar/baz.py"), |
| 97 | + (None, "")) |
| 98 | + assert captions["with_alert"].caption_parts \ |
| 99 | + == CaptionParts( |
| 100 | + (None, "PuDB VERSION"), |
| 101 | + (None, "?:help"), |
| 102 | + (None, "/home/foo - bar/baz.py"), |
| 103 | + ("warning", "[POST-MORTEM MODE]")) |
| 104 | + |
| 105 | + |
| 106 | +def test_rows(captions): |
| 107 | + for caption in captions.values(): |
| 108 | + assert caption.rows(size=(99999, 99999)) == 1 |
| 109 | + assert caption.rows(size=(80, 24)) == 1 |
| 110 | + assert caption.rows(size=(1, 1)) == 1 |
| 111 | + |
| 112 | + |
| 113 | +def test_get_fit_width_markup(captions): |
| 114 | + # No need to check empty caption because |
| 115 | + # len(str(caption)) == 0 always smaller than min terminal column == 1 |
| 116 | + |
| 117 | + # Set up |
| 118 | + caption = captions["with_alert"] |
| 119 | + caption_length = len(str(caption)) |
| 120 | + full_source_filename = caption.caption_parts.full_source_filename[1] |
| 121 | + cut_only_filename = ( |
| 122 | + max(1, caption_length - len(full_source_filename) + 5), ) |
| 123 | + cut_more_than_filename = (max(1, caption_length |
| 124 | + - len(full_source_filename) - len("PuDB VE")), ) |
| 125 | + sizes = {"cut_only_filename": cut_only_filename, |
| 126 | + "cut_more_than_filename": cut_more_than_filename, |
| 127 | + "one_col": (1, ), |
| 128 | + } |
| 129 | + # Test |
| 130 | + assert caption._get_fit_width_markup(sizes["cut_only_filename"]) \ |
| 131 | + == [(None, "PuDB VERSION"), (None, " - "), |
| 132 | + (None, "?:help"), (None, " - "), |
| 133 | + (None, "az.py"), (None, " - "), ("warning", "[POST-MORTEM MODE]")] |
| 134 | + assert caption._get_fit_width_markup(sizes["cut_more_than_filename"]) \ |
| 135 | + == [(None, "RSION"), (None, " - "), |
| 136 | + (None, "?:help"), (None, " - "), |
| 137 | + (None, ""), (None, " - "), ("warning", "[POST-MORTEM MODE]")] |
| 138 | + assert caption._get_fit_width_markup(sizes["one_col"]) \ |
| 139 | + == [(None, "")]*6 + [("warning", "]")] |
| 140 | + |
| 141 | + |
| 142 | +def test_get_shortened_source_filename(captions): |
| 143 | + # No need to check empty caption because |
| 144 | + # len(str(caption)) == 0 always smaller than min terminal column == 1 |
| 145 | + for k in ["with_alert", "without_alert", "custom_separator"]: |
| 146 | + caption_length = len(str(captions[k])) |
| 147 | + sizes = {"cut_at_path_sep": (max(1, caption_length - 1), ), |
| 148 | + "lose_some_dir": (max(1, caption_length - 2), ), |
| 149 | + "lose_all_dir": (max(1, |
| 150 | + caption_length - len("/home/foo - bar/")), ), |
| 151 | + "lose_some_filename_chars": (max(1, |
| 152 | + caption_length - len("/home/foo - bar/ba")), ), |
| 153 | + "lose_all": (max(1, |
| 154 | + caption_length - len("/home/foo - bar/baz.py")), ), |
| 155 | + } |
| 156 | + assert captions[k]._get_shortened_source_filename(sizes["cut_at_path_sep"]) \ |
| 157 | + == "home/foo - bar/baz.py" |
| 158 | + assert captions[k]._get_shortened_source_filename(sizes["lose_some_dir"]) \ |
| 159 | + == "foo - bar/baz.py" |
| 160 | + assert captions[k]._get_shortened_source_filename(sizes["lose_all_dir"]) \ |
| 161 | + == "baz.py" |
| 162 | + assert captions[k]._get_shortened_source_filename( |
| 163 | + sizes["lose_some_filename_chars"]) == "z.py" |
| 164 | + assert captions[k]._get_shortened_source_filename(sizes["lose_all"]) == "" |
0 commit comments