Skip to content

gh-130655: add tests for dgettext #134594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 44 additions & 1 deletion Lib/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


# TODO:
# - Add new tests, for example for "dgettext"
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep the TODO, since it is still valid (the first part).

It can be updated with the issue number if you wish.

# - Tests should have only one assert.

GNU_MO_DATA = b'''\
Expand Down Expand Up @@ -937,6 +936,50 @@ def test_lazy_import(self):
ensure_lazy_imports("gettext", {"re", "warnings", "locale"})


class DGettextTest(GettextBaseTest):

def setUp(self):
GettextBaseTest.setUp(self)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
GettextBaseTest.setUp(self)
super().setUp()

gettext.bindtextdomain('gettext', os.curdir)
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to tearDown this one?


def test_dgettext_found_translation(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Join this one with the other two, these all test the exact same thing (which is well tested above too).

result = gettext.dgettext('gettext', 'mullusk')
self.assertEqual(result, 'bacon')

def test_dgettext_fallback_cases(self):
test_cases = [
('gettext', 'missing message'),
('nonexistent_domain', 'mullusk'),
('', 'mullusk'),
]
for domain, message in test_cases:
with self.subTest(domain=domain, message=message):
result = gettext.dgettext(domain, message)
if domain == '':
expected = gettext.gettext(message)
else:
expected = message
Comment on lines +958 to +961
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just store the expected, it is clearer. And this part is made simpler.

self.assertEqual(result, expected)

def test_dgettext_luxury_yacht_translation(self):
result = gettext.dgettext('gettext', 'Raymond Luxury Yach-t')
self.assertEqual(result, 'Throatwobbler Mangrove')

def test_dgettext_nudge_nudge_translation(self):
result = gettext.dgettext('gettext', 'nudge nudge')
self.assertEqual(result, 'wink wink')

def test_dgettext_multiline_translation(self):
Comment on lines +964 to +972
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need these these anyway, it's long and pretty much identical to the previous two. dgettext is a pretty simple wrapper. Maybe merge the first two test_dgettext_translation for organization

message = '''This module provides internationalization and localization
support for your Python programs by providing an interface to the GNU
gettext message catalog library.'''
expected = '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
trggrkg zrffntr pngnybt yvoenel.'''
result = gettext.dgettext('gettext', message)
self.assertEqual(result, expected)


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

Expand Down
Loading