diff options
| author | Alexey Stepanov <penguinolog@users.noreply.github.com> | 2023-05-09 09:13:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-09 09:13:40 +0200 |
| commit | ffbfa07533809a523938d5e342ff7482a12dd5d0 (patch) | |
| tree | f82251e47e72b9fe873f0eb20410c105387407b9 /urwid/tests/test_text_layout.py | |
| parent | d26cb42a9fd28cb0743ad04d5ed2a0c7f28b89e3 (diff) | |
| download | urwid-master.tar.gz | |
* 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 <alekseis@nvidia.com>
Diffstat (limited to 'urwid/tests/test_text_layout.py')
| -rw-r--r-- | urwid/tests/test_text_layout.py | 63 |
1 files changed, 52 insertions, 11 deletions
diff --git a/urwid/tests/test_text_layout.py b/urwid/tests/test_text_layout.py index c87d12c..95cba1c 100644 --- a/urwid/tests/test_text_layout.py +++ b/urwid/tests/test_text_layout.py @@ -1,11 +1,24 @@ from __future__ import annotations +import contextlib import unittest +from collections.abc import Generator import urwid from urwid import text_layout +@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 CalcBreaksTest: def cbtest(self, width, exp): result = text_layout.default_layout.calculate_text_segments( @@ -33,8 +46,12 @@ class CalcBreaksCharTest(CalcBreaksTest, unittest.TestCase): class CalcBreaksDBCharTest(CalcBreaksTest, unittest.TestCase): def setUp(self): + self.old_encoding = urwid.util._target_encoding urwid.set_encoding("euc-jp") + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + mode = 'any' text = "abfgh\xA1\xA1j\xA1\xA1xskhtrvs\naltjhgsdf\xA1\xA1jahtshgf" # tests @@ -68,8 +85,12 @@ class CalcBreaksWordTest2(CalcBreaksTest, unittest.TestCase): class CalcBreaksDBWordTest(CalcBreaksTest, unittest.TestCase): def setUp(self): + self.old_encoding = urwid.util._target_encoding urwid.set_encoding("euc-jp") + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + mode = 'space' text = "hel\xA1\xA1 world\nout-\xA1\xA1tre blah" # tests @@ -81,9 +102,13 @@ class CalcBreaksDBWordTest(CalcBreaksTest, unittest.TestCase): class CalcBreaksUTF8Test(CalcBreaksTest, unittest.TestCase): - def setUp(self): + def setUp(self) -> None: + self.old_encoding = urwid.util._target_encoding urwid.set_encoding("utf-8") + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + mode = 'space' text = '\xe6\x9b\xbf\xe6\xb4\xbc\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba' do = [ @@ -95,20 +120,28 @@ class CalcBreaksUTF8Test(CalcBreaksTest, unittest.TestCase): class CalcBreaksCantDisplayTest(unittest.TestCase): def test(self): - urwid.set_encoding("euc-jp") - self.assertRaises(text_layout.CanNotDisplayText, - text_layout.default_layout.calculate_text_segments, - b'\xA1\xA1', 1, 'space' ) - urwid.set_encoding("utf-8") - self.assertRaises(text_layout.CanNotDisplayText, - text_layout.default_layout.calculate_text_segments, - b'\xe9\xa2\x96', 1, 'space' ) + with encoding("euc-jp"): + self.assertRaises( + text_layout.CanNotDisplayText, + text_layout.default_layout.calculate_text_segments, + b'\xA1\xA1', 1, 'space' + ) + with encoding("utf-8"): + self.assertRaises( + text_layout.CanNotDisplayText, + text_layout.default_layout.calculate_text_segments, + b'\xe9\xa2\x96', 1, 'space' + ) class SubsegTest(unittest.TestCase): def setUp(self): + self.old_encoding = urwid.util._target_encoding urwid.set_encoding("euc-jp") + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + def st(self, seg, text, start, end, exp): text = text.encode('iso8859-1') s = urwid.LayoutSegment(seg) @@ -151,9 +184,13 @@ class SubsegTest(unittest.TestCase): class CalcTranslateTest: - def setUp(self): + def setUp(self) -> None: + self.old_encoding = urwid.util._target_encoding urwid.set_encoding("utf-8") + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + def test1_left(self): result = urwid.default_layout.layout( self.text, self.width, 'left', self.mode) @@ -225,9 +262,13 @@ class CalcTranslateWordTest2(CalcTranslateTest, unittest.TestCase): class CalcTranslateWordTest3(CalcTranslateTest, unittest.TestCase): - def setUp(self): + def setUp(self) -> None: + self.old_encoding = urwid.util._target_encoding urwid.set_encoding('utf-8') + def tearDown(self) -> None: + urwid.set_encoding(self.old_encoding) + text = b'\xe6\x9b\xbf\xe6\xb4\xbc\n\xe6\xb8\x8e\xe6\xba\x8f\xe6\xbd\xba' width = 10 mode = 'space' |
