Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 106 additions & 1 deletion Lib/test/test_gettext.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please revert these unrelated changes here?

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@


# TODO:
# - Add new tests, for example for "dgettext"
# - Add new tests, for example for "dgettext", "dngettext", "dpgettext",
# "dnpgettext" (gh-130655)
# - Tests should have only one assert.

GNU_MO_DATA = b'''\
Expand Down Expand Up @@ -944,6 +945,110 @@ def test_translation_fallback(self):
self.assertIsInstance(t, gettext.NullTranslations)


class DGettextTests(GettextBaseTest):

def setUp(self):
super().setUp()
gettext.bindtextdomain('gettext', os.curdir)
gettext.textdomain('gettext')

def test_dgettext_translation(self):
cases = [
('gettext', 'nudge nudge', 'wink wink'),
('gettext', 'mullusk', 'bacon'),
('gettext', 'Raymond Luxury Yach-t', 'Throatwobbler Mangrove'),
('gettext', 'albatross', 'albatross'),
('gettext', 'missing message', 'missing message'),
('nonexistent_domain', 'mullusk', 'mullusk'),
('', 'mullusk', 'mullusk'),
]
for domain, msgid, expected in cases:
with self.subTest(domain=domain, msgid=msgid):
self.assertEqual(gettext.dgettext(domain, msgid), expected)


class DnGettextTests(GettextBaseTest):

def setUp(self):
super().setUp()
gettext.bindtextdomain('gettext', os.curdir)
gettext.textdomain('gettext')

def test_dngettext_translation(self):
cases = [
('gettext', 'There is %s file', 'There are %s files', 1,
'Hay %s fichero'),
('gettext', 'There is %s file', 'There are %s files', 5,
'Hay %s ficheros'),
('gettext', '%d file deleted', '%d files deleted', 1,
'%d file deleted'),
('gettext', '%d file deleted', '%d files deleted', 5,
'%d files deleted'),
('nonexistent_domain', 'There is %s file', 'There are %s files', 1,
'There is %s file'),
('nonexistent_domain', 'There is %s file', 'There are %s files', 5,
'There are %s files'),
]
for domain, msgid1, msgid2, n, expected in cases:
with self.subTest(domain=domain, n=n):
self.assertEqual(gettext.dngettext(domain, msgid1, msgid2, n),
expected)


class DpGettextTests(GettextBaseTest):

def setUp(self):
super().setUp()
gettext.bindtextdomain('gettext', os.curdir)
gettext.textdomain('gettext')

def test_dpgettext_translation(self):
cases = [
('gettext', 'my context', 'nudge nudge',
'wink wink (in "my context")'),
('gettext', 'my other context', 'nudge nudge',
'wink wink (in "my other context")'),
('gettext', 'my context', 'albatross', 'albatross'),
('gettext', 'wrong context', 'nudge nudge', 'nudge nudge'),
('nonexistent_domain', 'my context', 'nudge nudge', 'nudge nudge'),
]
for domain, context, msgid, expected in cases:
with self.subTest(domain=domain, context=context):
self.assertEqual(gettext.dpgettext(domain, context, msgid),
expected)


class DnpGettextTests(GettextBaseTest):

def setUp(self):
super().setUp()
gettext.bindtextdomain('gettext', os.curdir)
gettext.textdomain('gettext')

def test_dnpgettext_translation(self):
cases = [
('gettext', 'With context', 'There is %s file',
'There are %s files', 1, 'Hay %s fichero (context)'),
('gettext', 'With context', 'There is %s file',
'There are %s files', 5, 'Hay %s ficheros (context)'),
('gettext', 'With context', '%d file deleted',
'%d files deleted', 1, '%d file deleted'),
('gettext', 'With context', '%d file deleted',
'%d files deleted', 5, '%d files deleted'),
('gettext', 'Wrong context', 'There is %s file',
'There are %s files', 1, 'There is %s file'),
('nonexistent_domain', 'With context', 'There is %s file',
'There are %s files', 1, 'There is %s file'),
('nonexistent_domain', 'With context', 'There is %s file',
'There are %s files', 5, 'There are %s files'),
]
for domain, context, msgid1, msgid2, n, expected in cases:
with self.subTest(domain=domain, context=context, n=n):
self.assertEqual(
gettext.dnpgettext(domain, context, msgid1, msgid2, n),
expected)


if __name__ == '__main__':
unittest.main()

Expand Down
12 changes: 8 additions & 4 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,15 +1288,19 @@ def test_reconfigure_errors(self):
txt.reconfigure(encoding='\udcfe')
with self.assertRaises(LookupError):
txt.reconfigure(encoding='locale\0')
# TODO: txt.reconfigure(encoding='utf-8\0')
# TODO: txt.reconfigure(encoding='nonexisting')
with self.assertRaises(LookupError):
txt.reconfigure(encoding='utf-8\0')
with self.assertRaises(LookupError):
txt.reconfigure(encoding='nonexisting')
Comment on lines +1291 to +1294

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep invalid reconfigure cases implementation-specific

When this shared test runs for PyTextIOWrapperTest, these new assertRaises(LookupError) checks fail: _pyio.TextIOWrapper.reconfigure() only stores the new encoding/errors values here and does not look them up until an encoder/decoder is later created, so encoding='utf-8\0', encoding='nonexisting', and the analogous invalid errors cases below return normally. In a normal C build, utf-8\0 and invalid error-handler names are also accepted at reconfigure time because the C path passes nul-terminated strings and does not validate error handlers unless debug/dev-mode checks apply, so this makes test_io fail instead of adding passing coverage.

Useful? React with 👍 / 👎.

with self.assertRaises(TypeError):
txt.reconfigure(errors=42)
if self.is_C:
with self.assertRaises(UnicodeEncodeError):
txt.reconfigure(errors='\udcfe')
# TODO: txt.reconfigure(errors='ignore\0')
# TODO: txt.reconfigure(errors='nonexisting')
with self.assertRaises(LookupError):
txt.reconfigure(errors='ignore\0')
with self.assertRaises(LookupError):
txt.reconfigure(errors='nonexisting')
with self.assertRaises(TypeError):
txt.reconfigure(newline=42)
with self.assertRaises(ValueError):
Expand Down
Loading