| 1 | import re |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | import pytermgui as ptg |
| 6 | from pytermgui.window_manager.layouts import Auto, Static |
| 7 | |
| 8 | |
| 9 | def test_auto(): |
| 10 | layout = ptg.Layout() |
| 11 | layout.add_slot("Header") |
| 12 | layout.add_break() |
| 13 | layout.add_slot("Body") |
| 14 | layout.add_break() |
| 15 | layout.add_slot("Footer") |
| 16 | |
| 17 | assert str(layout.header.width) == "Auto(value=0)" |
| 18 | |
| 19 | layout.apply() |
| 20 | assert len(layout.slots) == 5 |
| 21 | |
| 22 | |
| 23 | def test_static(): |
| 24 | layout = ptg.Layout() |
| 25 | layout.add_slot("one", width=10, height=15) |
| 26 | layout.add_break() |
| 27 | layout.add_slot("two") |
| 28 | |
| 29 | assert str(layout.one.width) == "Static(value=10)" |
| 30 | |
| 31 | layout.apply() |
| 32 | |
| 33 | assert isinstance(layout.one.width, Static) |
| 34 | assert isinstance(layout.one.height, Static) |
| 35 | assert isinstance(layout.two.width, Auto) |
| 36 | assert isinstance(layout.two.height, Auto) |
| 37 | |
| 38 | |
| 39 | def test_relative(): |
| 40 | layout = ptg.Layout() |
| 41 | layout.add_slot("one", width=0.9, height=0.1) |
| 42 | |
| 43 | assert ( |
| 44 | re.match( |
| 45 | r"Relative\(value=[\d]+, scale=0\.9, bound=<function Layout.add_slot.<locals>.<lambda> at 0x[0-9a-fA-F]+>\)", |
| 46 | str(layout.one.width), |
| 47 | ) |
| 48 | is not None |
| 49 | ) |
| 50 | |
| 51 | layout.apply() |
| 52 | assert layout.one.width.value == int(ptg.terminal.width * 0.9) |
| 53 | |
| 54 | with pytest.raises(TypeError): |
| 55 | layout.one.width.value = 10 |
| 56 | |
| 57 | |
| 58 | def test_detach(): |
| 59 | layout = ptg.Layout() |
| 60 | slot = layout.add_slot("Body") |
| 61 | slot.content = ptg.Window() |
| 62 | layout.apply() |
| 63 | |
| 64 | slot.detach_content() |
| 65 | |
| 66 | |
| 67 | def test_wrong_detach(): |
| 68 | layout = ptg.Layout() |
| 69 | slot = layout.add_slot("Body") |
| 70 | |
| 71 | with pytest.raises(AttributeError): |
| 72 | slot.detach_content() |
| 73 | |
| 74 | |
| 75 | def test_wrong_getattr(): |
| 76 | layout = ptg.Layout() |
| 77 | layout.add_slot("Body") |
| 78 | |
| 79 | with pytest.raises(AttributeError): |
| 80 | layout.body1 |
| 81 | |
| 82 | |
| 83 | def test_add_index(): |
| 84 | layout = ptg.Layout() |
| 85 | layout.add_slot("Body") |
| 86 | layout.add_slot("Header", index=0) |
| 87 | |
| 88 | |
| 89 | def test_assign(): |
| 90 | layout = ptg.Layout() |
| 91 | layout.add_slot("Body") |
| 92 | layout.assign(ptg.Container()) |
| 93 | |
| 94 | |
| 95 | def test_wrong_assign(): |
| 96 | layout = ptg.Layout() |
| 97 | |
| 98 | layout.assign(ptg.Container(), index=2) |
| 99 | assert layout.slots == [] |