1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
|
from __future__ import annotations
import typing
import unittest
import urwid
from tests.util import SelectableText
if typing.TYPE_CHECKING:
from collections.abc import Collection
from typing_extensions import Literal
class ColumnsTest(unittest.TestCase):
def test_basic_sizing(self) -> None:
box_only = urwid.SolidFill("#")
flow_only = urwid.ProgressBar(None, None)
fixed_only = urwid.BigText("0", urwid.Thin3x3Font())
flow_fixed = urwid.Text("text")
with self.subTest("No sizing calculation possible"), self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
widget = urwid.Columns(((urwid.PACK, fixed_only), box_only), box_columns=(1,))
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), widget.sizing())
for description, kwargs in (
("BOX-only widget", {"widget_list": (box_only,)}),
('BOX-only widget with "get height from max"', {"widget_list": (box_only,), "box_columns": (0,)}),
("No FLOW - BOX only", {"widget_list": (box_only, urwid.SolidFill(" ")), "box_columns": (0, 1)}),
("GIVEN BOX only -> BOX only", {"widget_list": ((5, box_only),), "box_columns": (0,)}),
):
with self.subTest(description):
widget = urwid.Columns(**kwargs)
self.assertEqual(frozenset((urwid.BOX,)), widget.sizing())
cols, rows = 2, 2
self.assertEqual((cols, rows), widget.pack((cols, rows)))
canvas = widget.render((cols, rows))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
with self.subTest("FLOW-only"):
widget = urwid.Columns((flow_only,))
self.assertEqual(frozenset((urwid.FLOW,)), widget.sizing())
cols, rows = 2, 1
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
with self.subTest('BOX/FLOW by "box_columns": can be rendered as box only as fallback'):
widget = urwid.Columns((flow_only, box_only), box_columns=(1,))
self.assertEqual(frozenset((urwid.FLOW,)), widget.sizing())
cols, rows = 2, 1
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
self.assertEqual((cols, rows), widget.pack((cols, rows)))
canvas = widget.render((cols, rows))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
with self.subTest("FLOW/FIXED"):
widget = urwid.Columns((flow_fixed,))
self.assertEqual(frozenset((urwid.FLOW, urwid.FIXED)), widget.sizing())
cols, rows = 4, 1
self.assertEqual((cols, rows), widget.pack(()))
canvas = widget.render(())
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
cols, rows = 2, 2
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
with self.subTest("FIXED only"):
widget = urwid.Columns(((urwid.PACK, fixed_only),))
self.assertEqual(frozenset((urwid.FLOW, urwid.FIXED)), widget.sizing())
cols, rows = 3, 3
self.assertEqual((cols, rows), widget.pack(()))
canvas = widget.render(())
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
with self.subTest("FIXED only use FLOW methods - drop & replace by solid"):
widget = urwid.Columns(((urwid.PACK, fixed_only),))
self.assertEqual(frozenset((urwid.FLOW, urwid.FIXED)), widget.sizing())
cols, rows = 2, 1
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
self.assertEqual(" ", str(canvas))
with self.subTest("FIXED only use FLOW methods - add empty space"):
widget = urwid.Columns(((urwid.PACK, fixed_only),))
self.assertEqual(frozenset((urwid.FLOW, urwid.FIXED)), widget.sizing())
cols, rows = 5, 3
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
self.assertEqual(
(
"┌─┐ ",
"│ │ ",
"└─┘ ",
),
canvas.decoded_text,
)
with self.subTest("GIVEN BOX + FLOW/FIXED, but other widgets do not support box"):
widget = urwid.Columns((flow_fixed, (3, box_only)), box_columns=(1,))
self.assertEqual(frozenset((urwid.FLOW, urwid.FIXED)), widget.sizing())
cols, rows = 7, 1
self.assertEqual((cols, rows), widget.pack(()))
canvas = widget.render(())
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
cols, rows = 4, 4
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
self.assertEqual([b"t###", b"e###", b"x###", b"t###"], canvas.text)
cols, rows = 2, 2
self.assertEqual((cols, rows), widget.pack((cols, rows)))
canvas = widget.render((cols, rows))
self.assertEqual(cols, canvas.cols())
self.assertEqual(rows, canvas.rows())
self.assertEqual([b"te", b"xt"], canvas.text)
with self.subTest("Wrong sizing -> fallback to historic hardcoded"):
with self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
widget = urwid.Columns(((urwid.PACK, box_only),))
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), widget.sizing())
self.assertEqual(
f"Sizing combination of widget {box_only} (position=0) not supported: PACK box=False",
str(ctx.warnings[0].message),
)
with self.subTest("Wrong sizing -> fallback to historic hardcoded 2"):
with self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
widget = urwid.Columns(((urwid.WEIGHT, 1, fixed_only),))
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), widget.sizing())
self.assertEqual(
f"Sizing combination of widget {fixed_only} (position=0) not supported: WEIGHT box=False",
str(ctx.warnings[0].message),
)
with (
self.subTest('BOX not added to "box_columns" but widget handled as FLOW'),
self.assertWarns(urwid.widget.ColumnsWarning) as ctx,
):
self.maxDiff = None
contents = (
(urwid.WEIGHT, 1, urwid.SolidFill()),
(urwid.FIXED, 10, urwid.Button("OK", align=urwid.CENTER)),
(urwid.WEIGHT, 1, urwid.SolidFill()),
(urwid.FIXED, 10, urwid.Button("Cancel", align=urwid.CENTER)),
(urwid.WEIGHT, 1, urwid.SolidFill()),
)
widget = urwid.Columns(contents)
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), widget.sizing())
self.assertEqual(
"Columns widget contents flags not allow to determine supported render kind:\n"
"BOX WEIGHT, FIXED|FLOW GIVEN\n"
"Using fallback hardcoded BOX|FLOW sizing kind.",
str(ctx.warnings[0].message),
)
cols, rows = 30, 1
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(rows, canvas.rows())
self.assertEqual(cols, canvas.cols())
self.assertEqual([b" < OK > < Cancel > "], canvas.text)
self.assertEqual(
"Widgets in columns [0, 2, 4] "
f"({[elem[-1] for elem in contents[:6:2]]}) "
f'are BOX widgets not marked "box_columns" '
f"while FLOW render is requested (size=(30,))",
str(ctx.warnings[2].message),
)
self.assertEqual("OK", widget.focus.label)
self.assertIsNone(widget.keypress((cols,), "right"))
self.assertEqual(
"Widgets in columns [0, 2, 4] "
f"({[elem[-1] for elem in contents[:6:2]]}) "
f'are BOX widgets not marked "box_columns" '
f"while FLOW render is requested (size=(30,))",
str(ctx.warnings[3].message),
)
self.assertEqual("Cancel", widget.focus.label)
def test_pack_render_fixed(self) -> None:
"""Cover weighted FIXED/FLOW widgets pack.
One of the popular use-cases: rows of buttons/radiobuttons/checkboxes in pop-up windows.
"""
widget = urwid.Columns(
(urwid.Button(label, align=urwid.CENTER) for label in ("OK", "Cancel", "Help")),
dividechars=1,
)
self.assertEqual((32, 1), widget.pack(()))
self.assertEqual([b"< OK > < Cancel > < Help >"], widget.render(()).text)
def test_render_fixed_consistency(self) -> None:
"""Widgets supporting FIXED should be rendered with PACK side the same way as not FIXED."""
widget = urwid.Columns(((urwid.PACK, urwid.Text("Prefix:")), (urwid.PACK, urwid.Button("Btn"))), dividechars=1)
cols, _ = widget.pack(())
self.assertEqual(((7, 7), (1, 1), ((), ())), widget.get_column_sizes(()))
self.assertEqual(((7, 7), (1, 1), ((7,), (7,))), widget.get_column_sizes((cols,)))
self.assertEqual(("Prefix: < Btn >",), widget.render(()).decoded_text)
self.assertEqual(("Prefix: < Btn >",), widget.render((cols,)).decoded_text)
def test_render_pack_item_not_fit(self):
items = urwid.Text("123"), urwid.Text("456")
widget = urwid.Columns((urwid.PACK, item) for item in items)
# Make width < widget fixed pack
width = items[0].pack(())[0] - 1
height = items[0].rows((width,))
self.assertEqual((width, height), widget.pack((width,)))
canvas = widget.render((width,))
# Rendered should be not empty
self.assertEqual(items[0].render((width,)).decoded_text, canvas.decoded_text)
self.assertEqual(width, canvas.cols())
def test_pack_render_broken_sizing(self) -> None:
use_sizing = frozenset((urwid.BOX,))
class BrokenSizing(urwid.Text):
def sizing(self) -> frozenset[urwid.Sizing]:
return use_sizing
with self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
element = BrokenSizing("Test")
widget = urwid.Columns(((urwid.PACK, element),))
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), widget.sizing())
self.assertEqual(
f"Sizing combination of widget {element} (position=0) not supported: PACK box=False",
str(ctx.warnings[0].message),
)
with self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
canvas = widget.render((10,))
self.assertEqual([b"Test "], canvas.text)
self.assertEqual(
f"Unusual widget {element} sizing for {urwid.PACK} (box=False). "
f"Assuming wrong sizing and using FLOW for width calculation",
str(ctx.warnings[0].message),
)
def test_pack_render_skip_widget(self):
widget = urwid.Columns(((0, urwid.Text("Ignore\nme")), (urwid.Text("Count"))))
cols, rows = 5, 1
self.assertEqual((cols, rows), widget.pack(()))
self.assertEqual((cols, rows), widget.pack((cols,)))
self.assertEqual("Count", str(widget.render(())))
self.assertEqual("Count", str(widget.render((cols,))))
def test_pack_flow_with_fixed_item(self):
widget = urwid.Columns(
(
(urwid.PACK, urwid.BigText("3", urwid.Thin3x3Font())),
(urwid.Text("Multi\nline\ntext\n???")),
),
dividechars=1,
)
with self.subTest("Skip"):
self.assertEqual(3, widget.rows((3,)))
self.assertEqual((3, 3), widget.pack((3,)))
self.assertEqual(
(
"┌─┐",
" ─┤",
"└─┘",
),
widget.render((3,)).decoded_text,
)
with self.subTest("Fit all"):
self.assertEqual(4, widget.rows((9,)))
self.assertEqual((9, 4), widget.pack((9,)))
self.assertEqual(
(
"┌─┐ Multi",
" ─┤ line ",
"└─┘ text ",
" ??? ",
),
widget.render((9,)).decoded_text,
)
with self.subTest("Use SolidCanvas instead"):
self.assertEqual(1, widget.rows((2,)))
self.assertEqual((2, 1), widget.pack((2,)))
self.assertEqual((" ",), widget.render((2,)).decoded_text)
def test_pack_render_empty_widget(self):
widget = urwid.Columns(())
self.assertEqual(frozenset((urwid.FLOW, urwid.BOX)), widget.sizing())
cols, rows = 10, 1
self.assertEqual((cols, rows), widget.pack((cols,)))
canvas = widget.render((cols,))
self.assertEqual(rows, canvas.rows())
self.assertEqual((" ",), canvas.decoded_text)
def test_not_a_widget(self):
class NotAWidget:
__slots__ = ("name", "symbol")
def __init__(self, name: str, symbol: bytes) -> None:
self.name = name
self.symbol = symbol
def __repr__(self) -> str:
return f"{self.__class__.__name__}(name={self.name!r}, symbol={self.symbol!r})"
def selectable(self) -> bool:
return False
def rows(self, max_col_row: tuple[int], focus: bool = False) -> int:
return 1
def render(self, max_col_row: tuple[int, int] | tuple[int], focus: bool = False) -> urwid.Canvas:
maxcol = max_col_row[0]
line = self.symbol * maxcol
if len(max_col_row) == 1:
return urwid.TextCanvas((line,), maxcol=maxcol)
return urwid.TextCanvas((line,) * max_col_row[1], maxcol=maxcol)
with self.subTest("Box"), self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
items = (NotAWidget("First", b"*"), NotAWidget("Second", b"^"))
widget = urwid.Columns(items)
self.assertEqual(("**^^", "**^^"), widget.render((4, 2)).decoded_text)
self.assertEqual(f"{items[0]!r} is not a Widget", str(ctx.warnings[0].message))
self.assertEqual(f"{items[1]!r} is not a Widget", str(ctx.warnings[1].message))
with self.subTest("Flow"), self.assertWarns(urwid.widget.ColumnsWarning) as ctx:
items = (NotAWidget("First", b"*"), NotAWidget("Second", b"^"))
widget = urwid.Columns(items)
self.assertEqual(("***^^^",), widget.render((6,)).decoded_text)
self.assertEqual(f"{items[0]!r} is not a Widget", str(ctx.warnings[0].message))
self.assertEqual(f"{items[1]!r} is not a Widget", str(ctx.warnings[1].message))
def test_zero_width_column(self):
elem_1 = urwid.BoxAdapter(urwid.SolidFill("#"), 2)
elem_2 = urwid.BoxAdapter(urwid.SolidFill("*"), 4)
widget = urwid.Columns((elem_1, (0, elem_2)))
self.assertEqual((3, 2), widget.pack((3,)))
canvas = widget.render((3,))
self.assertEqual(2, canvas.rows())
self.assertEqual(3, canvas.cols())
self.assertEqual([b"###", b"###"], canvas.text)
def test_focus_column(self):
button_1 = urwid.Button("Selectable")
button_2 = urwid.Button("Other selectable")
widget_list = [
(urwid.PACK, urwid.Text("Non selectable")),
(urwid.PACK, button_1),
(urwid.PACK, button_2),
(urwid.PACK, urwid.Text("The end")),
]
with self.subTest("Not provided -> select first selectable"):
widget = urwid.Columns(widget_list)
self.assertEqual(1, widget.focus_position)
widget.keypress((), "left")
self.assertEqual(1, widget.focus_position)
with self.subTest("Exact index"):
widget = urwid.Columns(widget_list, focus_column=2)
self.assertEqual(2, widget.focus_position)
widget.keypress((), "left")
self.assertEqual(1, widget.focus_position)
with self.subTest("Exact widget"):
widget = urwid.Columns(widget_list, focus_column=button_2)
self.assertEqual(2, widget.focus_position)
self.assertEqual(button_2, widget.focus)
widget.keypress((), "left")
self.assertEqual(1, widget.focus_position)
def test_pack_not_enough_info(self):
"""Test special case for not official fixed pack and render support."""
widget = urwid.Columns(
(
(urwid.WEIGHT, 16, urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE)),
(10, urwid.Button("First")),
urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE),
(10, urwid.Button("Second")),
(urwid.WEIGHT, 16, urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE)),
),
box_columns=(0, 2, 4),
)
cols, rows = 53, 1
self.assertEqual(frozenset((urwid.FLOW,)), widget.sizing())
self.assertEqual((cols, rows), widget.pack(()))
self.assertEqual(
("░░░░░░░░░░░░░░░░< First >░< Second >░░░░░░░░░░░░░░░░",),
widget.render(()).decoded_text,
)
def test_no_height(self):
widget = urwid.Columns(
(
(urwid.WEIGHT, 16, urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE)),
urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE),
(urwid.WEIGHT, 16, urwid.SolidFill(urwid.SolidFill.Symbols.LITE_SHADE)),
),
box_columns=(0, 1, 2),
)
self.assertEqual(frozenset((urwid.BOX,)), widget.sizing())
with self.assertRaises(urwid.widget.ColumnsError):
widget.pack(())
def assert_column_widths(
self,
expected: Collection[int],
widget: urwid.Columns,
size: tuple[int, int] | tuple[int] | tuple[()],
description: str,
) -> None:
column_widths, _, _ = widget.get_column_sizes(size)
self.assertEqual(expected, column_widths, f"{description} expected {expected}, got {column_widths}")
def test_widths(self):
x = urwid.Text("") # sample "column"
self.assert_column_widths((20,), urwid.Columns([x]), (20,), "simple 1")
self.assert_column_widths((10, 10), urwid.Columns([x, x]), (20,), "simple 2")
self.assert_column_widths((10, 9), urwid.Columns([x, x], 1), (20,), "simple 2+1")
self.assert_column_widths((6, 6, 6), urwid.Columns([x, x, x], 1), (20,), "simple 3+1")
self.assert_column_widths((5, 6, 5), urwid.Columns([x, x, x], 2), (20,), "simple 3+2")
self.assert_column_widths((6, 6, 5), urwid.Columns([x, x, x], 2), (21,), "simple 3+2")
simple_4 = urwid.Columns([x, x, x, x], 1)
for expected, cols, description in (
((6, 5, 6, 5), 25, "simple 4+1"),
((1, 1, 1, 1), 7, "squish 4+1"),
((1, 2, 1), 6, "squish 4+1"),
((2, 1), 4, "squish 4+1"),
):
with self.subTest(description):
self.assert_column_widths(expected, simple_4, (cols,), description)
fixed = urwid.Columns([("fixed", 4, x), ("fixed", 6, x), ("fixed", 2, x)], 1)
for expected, size, description in (
((4, 6, 2), (), "FIXED"),
((4, 6, 2), (25,), "fixed 3"),
((4, 6), (13,), "fixed 3 cut"),
((4,), (10,), "fixed 3 cut2"),
):
with self.subTest(description):
self.assert_column_widths(expected, fixed, size, description)
mixed = urwid.Columns([("weight", 2, x), ("fixed", 5, x), x, ("weight", 3, x)], 1)
for expected, cols, description in (
((2, 5, 1, 3), 14, "mixed 4"),
((1, 5, 1, 2), 12, "mixed 4 a"),
((2, 5, 1), 10, "mixed 4 b"),
((4, 5, 2, 6), 20, "mixed 4 c"),
):
with self.subTest(description):
self.assert_column_widths(expected, mixed, (cols,), description)
def test_widths_focus_end(self):
x = urwid.Text("") # sample "column"
self.assert_column_widths(
(10, 10),
urwid.Columns([x, x], focus_column=1),
(20,),
"end simple 2",
)
self.assert_column_widths(
(10, 9),
urwid.Columns([x, x], 1, 1),
(20,),
"end simple 2+1",
)
self.assert_column_widths(
(6, 6, 6),
urwid.Columns([x, x, x], 1, 2),
(20,),
"end simple 3+1",
)
self.assert_column_widths(
(5, 6, 5),
urwid.Columns([x, x, x], 2, 2),
(20,),
"end simple 3+2",
)
self.assert_column_widths(
(6, 6, 5),
urwid.Columns([x, x, x], 2, 2),
(21,),
"end simple 3+2",
)
simple_4 = urwid.Columns([x, x, x, x], 1, 3)
for expected, cols, description in (
((6, 5, 6, 5), 25, "end simple 4+1"),
((1, 1, 1, 1), 7, "end squish 4+1"),
((0, 1, 2, 1), 6, "end squish 4+1"),
((0, 0, 2, 1), 4, "end squish 4+1"),
):
with self.subTest(description):
self.assert_column_widths(expected, simple_4, (cols,), description)
fixed = urwid.Columns([("fixed", 4, x), ("fixed", 6, x), ("fixed", 2, x)], 1, 2)
for expected, size, description in (
((4, 6, 2), (), "FIXED"),
((4, 6, 2), (25,), "end fixed 3"),
((0, 6, 2), (13,), "end fixed 3 cut"),
((0, 0, 2), (8,), "end fixed 3 cut2"),
):
with self.subTest(description):
self.assert_column_widths(expected, fixed, size, description)
mixed = urwid.Columns([("weight", 2, x), ("fixed", 5, x), x, ("weight", 3, x)], 1, 3)
for expected, cols, description in (
((2, 5, 1, 3), 14, "end mixed 4"),
((1, 5, 1, 2), 12, "end mixed 4 a"),
((0, 5, 1, 2), 10, "end mixed 4 b"),
((4, 5, 2, 6), 20, "end mixed 4 c"),
):
with self.subTest(description):
self.assert_column_widths(expected, mixed, (cols,), description)
def check_move_cursor(
self,
expected: bool,
widget: urwid.Columns,
size: tuple[int, int] | tuple[int] | tuple[()],
col: int | Literal["left", "right"],
row: int,
focus_position: int,
pref_col: int | Literal["left", "right"] | None,
description: str,
) -> None:
moved = widget.move_cursor_to_coords(size, col, row)
self.assertEqual(expected, moved, f"{description} expected {expected!r}, got {moved!r}")
self.assertEqual(
focus_position,
widget.focus_position,
f"{description} expected focus_position {focus_position!r}, got {widget.focus_position!r}",
)
w_pref_col = widget.get_pref_col(size)
self.assertEqual(
pref_col,
w_pref_col,
f"{description} expected pref_col {pref_col!r}, got {w_pref_col!r}",
)
def test_move_cursor(self):
e, s, x = urwid.Edit("", ""), SelectableText(""), urwid.Text("")
self.check_move_cursor(
False,
urwid.Columns([x, x, x], 1),
(20,),
9,
0,
0,
None,
"nothing selectable",
)
mid = urwid.Columns([x, s, x], 1)
self.check_move_cursor(
True,
mid,
(20,),
9,
0,
1,
9,
"dead on",
)
self.check_move_cursor(
True,
mid,
(20,),
6,
0,
1,
6,
"l edge",
)
self.check_move_cursor(
True,
mid,
(20,),
13,
0,
1,
13,
"r edge",
)
self.check_move_cursor(
True,
mid,
(20,),
2,
0,
1,
2,
"l off",
)
self.check_move_cursor(
True,
mid,
(20,),
17,
0,
1,
17,
"r off",
)
self.check_move_cursor(
True,
urwid.Columns([x, x, s], 1),
(20,),
2,
0,
2,
2,
"l off 2",
)
self.check_move_cursor(
True,
urwid.Columns([s, x, x], 1),
(20,),
17,
0,
0,
17,
"r off 2",
)
self.check_move_cursor(
True,
urwid.Columns([s, s, x], 1),
(20,),
6,
0,
0,
6,
"l between",
)
self.check_move_cursor(
True,
urwid.Columns([x, s, s], 1),
(20,),
13,
0,
1,
13,
"r between",
)
l_2 = urwid.Columns([s, s, x], 2)
self.check_move_cursor(
True,
l_2,
(22,),
6,
0,
0,
6,
"l between 2l",
)
self.check_move_cursor(
True,
l_2,
(22,),
7,
0,
1,
7,
"l between 2r",
)
r_2 = urwid.Columns([x, s, s], 2)
self.check_move_cursor(
True,
r_2,
(22,),
14,
0,
1,
14,
"r between 2l",
)
self.check_move_cursor(
True,
r_2,
(22,),
15,
0,
2,
15,
"r between 2r",
)
# unfortunate pref_col shifting
edge = urwid.Columns([x, e, x], 1)
self.check_move_cursor(
True,
edge,
(20,),
6,
0,
1,
7,
"l e edge",
)
self.check_move_cursor(
True,
edge,
(20,),
13,
0,
1,
12,
"r e edge",
)
# 'left'/'right' special cases
full = urwid.Columns([e, e, e])
self.check_move_cursor(
True,
full,
(12,),
"right",
0,
2,
"right",
"l e edge",
)
self.check_move_cursor(
True,
full,
(12,),
"left",
0,
0,
"left",
"r e edge",
)
def test_init_with_a_generator(self):
urwid.Columns(urwid.Text(c) for c in "ABC")
def test_old_attributes(self):
c = urwid.Columns([urwid.Text("a"), urwid.SolidFill("x")], box_columns=[1])
with self.assertWarns(DeprecationWarning):
self.assertEqual(c.box_columns, [1])
with self.assertWarns(DeprecationWarning):
c.box_columns = []
self.assertEqual(c.box_columns, [])
def test_box_column(self):
c = urwid.Columns([urwid.Filler(urwid.Edit()), urwid.Text("")], box_columns=[0])
c.keypress((10,), "x")
c.get_cursor_coords((10,))
c.move_cursor_to_coords((10,), 0, 0)
c.mouse_event((10,), "foo", 1, 0, 0, True)
c.get_pref_col((10,))
def test_length(self):
columns = urwid.Columns(urwid.Text(c) for c in "ABC")
self.assertEqual(3, len(columns))
self.assertEqual(3, len(columns.contents))
def test_common(self):
t1 = urwid.Text("one")
t2 = urwid.Text("two")
t3 = urwid.Text("three")
sf = urwid.SolidFill("x")
c = urwid.Columns([])
with self.subTest("Focus"):
self.assertEqual(c.focus, None)
self.assertRaises(IndexError, lambda: getattr(c, "focus_position"))
self.assertRaises(IndexError, lambda: setattr(c, "focus_position", None))
self.assertRaises(IndexError, lambda: setattr(c, "focus_position", 0))
with self.subTest("Contents change"):
c.contents = [
(t1, ("pack", None, False)),
(t2, ("weight", 1, False)),
(sf, ("weight", 2, True)),
(t3, ("given", 10, False)),
]
c.focus_position = 1
del c.contents[0]
self.assertEqual(c.focus_position, 0)
c.contents[0:0] = [(t3, ("given", 10, False)), (t2, ("weight", 1, False))]
c.contents.insert(3, (t1, ("pack", None, False)))
self.assertEqual(c.focus_position, 2)
with self.subTest("Contents change validation"):
c.contents.clear()
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append(t1))
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, None)))
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, "given")))
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, ("given", None))))
# Incorrect kind
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, ("what", 1, False))))
# Incorrect box field
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, ("given", 1, None))))
# Incorrect size type
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, ("given", (), False))))
# Incorrect size
self.assertRaises(urwid.ColumnsError, lambda: c.contents.append((t1, ("given", -1, False))))
# Float and int weight accepted
c.contents.append((t1, ("weight", 1, False)))
c.contents.append((t2, ("weight", 0.5, False)))
self.assertEqual(("one two",), c.render(()).decoded_text)
def test_focus_position(self):
t1 = urwid.Text("one")
t2 = urwid.Text("two")
c = urwid.Columns([t1, t2])
self.assertEqual(c.focus, t1)
self.assertEqual(c.focus_position, 0)
c.focus_position = 1
self.assertEqual(c.focus, t2)
self.assertEqual(c.focus_position, 1)
c.focus_position = 0
self.assertRaises(IndexError, lambda: setattr(c, "focus_position", -1))
self.assertRaises(IndexError, lambda: setattr(c, "focus_position", 2))
def test_deprecated(self):
t1 = urwid.Text("one")
t2 = urwid.Text("two")
sf = urwid.SolidFill("x")
# old methods:
c = urwid.Columns([t1, ("weight", 3, t2), sf], box_columns=[2])
with self.subTest("Focus"):
c.set_focus(0)
self.assertRaises(IndexError, lambda: c.set_focus(-1))
self.assertRaises(IndexError, lambda: c.set_focus(3))
c.set_focus(t2)
self.assertEqual(c.focus_position, 1)
self.assertRaises(ValueError, lambda: c.set_focus("nonexistant"))
with self.subTest("Contents"):
self.assertEqual(c.widget_list, [t1, t2, sf])
self.assertEqual(c.column_types, [("weight", 1), ("weight", 3), ("weight", 1)])
self.assertEqual(c.box_columns, [2])
with self.subTest("Contents change"):
c.widget_list = [t2, t1, sf]
self.assertEqual(c.widget_list, [t2, t1, sf])
self.assertEqual(c.box_columns, [2])
self.assertEqual(
c.contents,
[(t2, ("weight", 1, False)), (t1, ("weight", 3, False)), (sf, ("weight", 1, True))],
)
self.assertEqual(c.focus_position, 1) # focus unchanged
c.column_types = [("flow", None), ("weight", 2), ("fixed", 5)] # use the old name
self.assertEqual(c.column_types, [("flow", None), ("weight", 2), ("fixed", 5)])
self.assertEqual(
c.contents,
[(t2, ("pack", None, False)), (t1, ("weight", 2, False)), (sf, ("given", 5, True))],
)
self.assertEqual(c.focus_position, 1) # focus unchanged
with self.subTest("Contents change 2"):
c.widget_list = [t1]
self.assertEqual(len(c.contents), 1)
self.assertEqual(c.focus_position, 0)
c.widget_list.extend([t2, t1])
self.assertEqual(len(c.contents), 3)
self.assertEqual(c.column_types, [("flow", None), ("weight", 1), ("weight", 1)])
c.column_types[:] = [("weight", 2)]
self.assertEqual(len(c.contents), 1)
def test_regression_columns_different_height(self):
size = (20, 5)
box_w = urwid.SolidFill("#")
f_f_widget = urwid.Text("Fixed/Flow")
box_flow = urwid.LineBox(urwid.Filler(f_f_widget, valign=urwid.TOP))
self.assertIn(urwid.BOX, box_w.sizing())
self.assertEqual(frozenset((urwid.BOX, urwid.FLOW)), box_flow.sizing())
with self.subTest("BoxFlow weight"):
widget = urwid.Columns(((1, box_w), box_flow))
self.assertEqual(
(
"#┌─────────────────┐",
"#│Fixed/Flow │",
"#│ │",
"#│ │",
"#└─────────────────┘",
),
widget.render(size, False).decoded_text,
)
with self.subTest("BoxFlow GIVEN"):
widget = urwid.Columns((box_w, (12, box_flow)))
self.assertEqual(
(
"########┌──────────┐",
"########│Fixed/Flow│",
"########│ │",
"########│ │",
"########└──────────┘",
),
widget.render(size, False).decoded_text,
)
|