-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,15 @@ | |
import unittest | ||
import unittest.mock | ||
from functools import partial | ||
import tempfile | ||
import shutil | ||
|
||
from test import support | ||
from test.support import cpython_only, os_helper | ||
from test.support.import_helper import ensure_lazy_imports | ||
|
||
|
||
# TODO: | ||
# - Add new tests, for example for "dgettext" | ||
# - 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"}) | ||
|
||
|
||
class DGettextTest(unittest.TestCase): | ||
"""Test dgettext() function, which allows translations from specific domains.""" | ||
|
||
def setUp(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() | ||
|
||
def setup_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') | ||
with open(mofile, 'wb') as fp: | ||
fp.write(b'\x00\x00\x00\x00') | ||
return mofile | ||
|
||
def test_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) | ||
with unittest.mock.patch('gettext.dgettext') as mock_dgettext: | ||
mock_dgettext.return_value = 'test message translation' | ||
result = gettext.dgettext(self.domain, 'test message') | ||
self.assertEqual(result, 'test message translation') | ||
|
||
def test_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') | ||
|
||
def test_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') | ||
|
||
def test_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() | ||
|
||
|
There was a problem hiding this comment.
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.