Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
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
base:main
Are you sure you want to change the base?
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,14 +4,15 @@ | ||
importunittest | ||
importunittest.mock | ||
fromfunctoolsimportpartial | ||
importtempfile | ||
importshutil | ||
fromtestimportsupport | ||
fromtest.supportimportcpython_only,os_helper | ||
fromtest.support.import_helperimportensure_lazy_imports | ||
# TODO: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'''\ | ||
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Instead of using | ||
self.addCleanup(shutil.rmtree,self.localedir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Prefer using | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Avoid using docstrings for | ||
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() | ||
Uh oh!
There was an error while loading.Please reload this page.