summaryrefslogtreecommitdiff
path: root/urwid/tests/test_text_layout.py
diff options
context:
space:
mode:
Diffstat (limited to 'urwid/tests/test_text_layout.py')
-rw-r--r--urwid/tests/test_text_layout.py63
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'