From ffbfa07533809a523938d5e342ff7482a12dd5d0 Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Tue, 9 May 2023 09:13:40 +0200 Subject: Fix `TextCanvas` `CanvasError("Attribute extends beyond text...")` (#555) * Fix TextCanvas `CanvasError("Attribute extends beyond text...") * `[[]] * ...` causes list of 1 list with pointers amount equal to multiplier instead of "list of lists" * Add 2 basic font tests which check for Canvas create issue * Add few type annotations during debug process Fix: #554 * Force tests to restore default encoding in tearDown Tests order change should not cause tests failures --------- Co-authored-by: Aleksei Stepanov --- urwid/tests/test_widget.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'urwid/tests/test_widget.py') diff --git a/urwid/tests/test_widget.py b/urwid/tests/test_widget.py index 4ea2944..9da2abb 100644 --- a/urwid/tests/test_widget.py +++ b/urwid/tests/test_widget.py @@ -1,10 +1,23 @@ from __future__ import annotations +import contextlib import unittest +from collections.abc import Generator import urwid +@contextlib.contextmanager +def encoding(encoding_name: str) -> Generator[None, None, None]: + old_encoding = 'ascii' # Default fallback value + try: + old_encoding = urwid.util._target_encoding + urwid.set_encoding(encoding_name) + yield + finally: + urwid.set_encoding(old_encoding) + + class TextTest(unittest.TestCase): def setUp(self): self.t = urwid.Text("I walk the\ncity in the night") @@ -33,10 +46,10 @@ class TextTest(unittest.TestCase): assert got == expected, f"got: {got!r} expected: {expected!r}" def test5_encode_error(self): - urwid.set_encoding("ascii") - expected = [b"? "] - got = urwid.Text('û').render((3,))._text - assert got == expected, f"got: {got!r} expected: {expected!r}" + with encoding("ascii"): + expected = [b"? "] + got = urwid.Text('û').render((3,))._text + assert got == expected, f"got: {got!r} expected: {expected!r}" class EditTest(unittest.TestCase): @@ -87,12 +100,12 @@ class EditTest(unittest.TestCase): self.ktest(self.t3,'down','down',15,"down at bottom") def test_utf8_input(self): - urwid.set_encoding("utf-8") - self.t1.set_edit_text('') - self.t1.keypress((12,), 'û') - self.assertEqual(self.t1.edit_text, 'û'.encode()) - self.t4.keypress((12,), 'û') - self.assertEqual(self.t4.edit_text, 'û') + with encoding("utf-8"): + self.t1.set_edit_text('') + self.t1.keypress((12,), 'û') + self.assertEqual(self.t1.edit_text, 'û'.encode()) + self.t4.keypress((12,), 'û') + self.assertEqual(self.t4.edit_text, 'û') class EditRenderTest(unittest.TestCase): -- cgit v1.2.1