| 1 | import pytermgui as ptg |
| 2 | |
| 3 | |
| 4 | def test_auto_label(): |
| 5 | obj = ptg.Widget.from_data("hello") |
| 6 | |
| 7 | assert isinstance(obj, ptg.Label) |
| 8 | assert obj.value == "hello" |
| 9 | |
| 10 | |
| 11 | def test_auto_splitter(): |
| 12 | items = {ptg.Label("hello"), ptg.Button("there")} |
| 13 | |
| 14 | obj = ptg.Widget.from_data(tuple(items)) |
| 15 | |
| 16 | assert isinstance(obj, ptg.Splitter) |
| 17 | assert set(obj._widgets) == items |
| 18 | |
| 19 | |
| 20 | def test_auto_button(): |
| 21 | def _callback(btn: ptg.Button) -> None: |
| 22 | return None |
| 23 | |
| 24 | obj = ptg.Widget.from_data(["Label", _callback]) |
| 25 | |
| 26 | assert isinstance(obj, ptg.Button) |
| 27 | assert obj.onclick is _callback |
| 28 | assert obj.label == "Label" |
| 29 | |
| 30 | |
| 31 | def test_auto_checkbox(): |
| 32 | obj = ptg.Widget.from_data([True]) |
| 33 | |
| 34 | assert isinstance(obj, ptg.Checkbox) |
| 35 | assert obj.checked == True |
| 36 | |
| 37 | |
| 38 | def test_auto_toggle(): |
| 39 | obj = ptg.Widget.from_data([("One", "Two")]) |
| 40 | |
| 41 | assert isinstance(obj, ptg.Toggle) |
| 42 | assert obj.label == "One" |
| 43 | |
| 44 | |
| 45 | def _test_prompt(obj: ptg.Splitter, left: ptg.Label, right: ptg.Label): |
| 46 | assert isinstance(obj, ptg.Splitter) |
| 47 | assert isinstance(obj[0], ptg.Label) and obj[0].value == left.value |
| 48 | assert isinstance(obj[1], ptg.Label) and obj[1].value == right.value |
| 49 | |
| 50 | |
| 51 | def test_auto_prompt(): |
| 52 | left = ptg.Label("Key") |
| 53 | right = ptg.Label("Value") |
| 54 | obj = ptg.Widget.from_data({left: right}) |
| 55 | |
| 56 | _test_prompt(obj, left, right) |
| 57 | |
| 58 | |
| 59 | def test_auto_prompt_multirow(): |
| 60 | items = {"One": "Two", "Three": "Four", "Five": "Six"} |
| 61 | rows = ptg.Widget.from_data(items) |
| 62 | |
| 63 | for row, (left, right) in zip(rows, items.items()): |
| 64 | _test_prompt(row, ptg.Label(left), ptg.Label(right)) |
| 65 | |
| 66 | |
| 67 | def test_auto_bad_object(): |
| 68 | assert ptg.Widget.from_data(1) is None |