Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
alex-semenyuk wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromalex-semenyuk:add_dgettext_tests
Open
Changes from1 commit
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
NextNext commit
gh-134593: add tests for dgettext
  • Loading branch information
@alex-semenyuk
alex-semenyuk committedMay 23, 2025
commit16165b5ab171f83087d771043fb58abf34eacb1a
48 changes: 47 additions & 1 deletionLib/test/test_gettext.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,14 +4,15 @@
importunittest
importunittest.mock
fromfunctoolsimportpartial
importtempfile
importshutil

fromtestimportsupport
fromtest.supportimportcpython_only,os_helper
fromtest.support.import_helperimportensure_lazy_imports


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

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 DownExpand Up@@ -937,6 +938,51 @@ def test_lazy_import(self):
ensure_lazy_imports("gettext", {"re","warnings","locale"})


classDGettextTest(unittest.TestCase):
"""Test dgettext() function, which allows translations from specific domains."""

defsetUp(self):
"""Set up a specific test domain and environment for dgettext tests."""
self.localedir=tempfile.mkdtemp()
Copy link
Member

Choose a reason for hiding this comment

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

Instead of usingmkdtemp which would create something inside/tmp, usetest.support.temp_dir (I think, check the name) which creates something inside the build folder (it's easier to cleanup)

self.addCleanup(shutil.rmtree,self.localedir)
Copy link
Member

Choose a reason for hiding this comment

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

Prefer usingos_helper.rmtree overshutil.rmtree

self.domain='gettext_domain'
self.mofile=self.setup_dgettext_test_env()

defsetup_dgettext_test_env(self):
"""Create a mo file for dgettext testing."""
os.makedirs(os.path.join(self.localedir,'en','LC_MESSAGES'),exist_ok=True)
mofile=os.path.join(self.localedir,'en','LC_MESSAGES',f'{self.domain}.mo')
withopen(mofile,'wb')asfp:
fp.write(b'\x00\x00\x00\x00')
returnmofile

deftest_dgettext_found_translation(self):

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).

"""Test dgettext finds translation in specified domain."""
Copy link
Member

Choose a reason for hiding this comment

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

Avoid using docstrings fortest_* methods as it will be shown upon failure.

gettext.bindtextdomain(self.domain,self.localedir)
withunittest.mock.patch('gettext.dgettext')asmock_dgettext:
mock_dgettext.return_value='test message translation'
result=gettext.dgettext(self.domain,'test message')
self.assertEqual(result,'test message translation')

deftest_dgettext_missing_translation(self):
"""Test dgettext returns msgid when translation is missing."""
gettext.bindtextdomain(self.domain,self.localedir)
result=gettext.dgettext(self.domain,'missing message')
self.assertEqual(result,'missing message')

deftest_dgettext_non_existent_domain(self):
"""Test dgettext returns msgid when domain doesn't exist."""
result=gettext.dgettext('nonexistent_domain','test message')
self.assertEqual(result,'test message')

deftest_dgettext_empty_domain(self):
"""Test dgettext behavior with empty domain."""
current_domain=gettext.textdomain()
result=gettext.dgettext('','test message')
expected=gettext.gettext('test message')
self.assertEqual(result,expected)


if__name__=='__main__':
unittest.main()

Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp