| 
4 | 4 | import unittest  | 
5 | 5 | import unittest.mock  | 
6 | 6 | from functools import partial  | 
 | 7 | +import tempfile  | 
 | 8 | +import shutil  | 
7 | 9 | 
 
  | 
8 | 10 | from test import support  | 
9 | 11 | from test.support import cpython_only, os_helper  | 
10 | 12 | from test.support.import_helper import ensure_lazy_imports  | 
11 | 13 | 
 
  | 
12 | 14 | 
 
  | 
13 | 15 | # TODO:  | 
14 |  | -#  - Add new tests, for example for "dgettext"  | 
15 | 16 | #  - Tests should have only one assert.  | 
16 | 17 | 
 
  | 
17 | 18 | GNU_MO_DATA = b'''\  | 
@@ -937,6 +938,51 @@ def test_lazy_import(self):  | 
937 | 938 |         ensure_lazy_imports("gettext", {"re", "warnings", "locale"})  | 
938 | 939 | 
 
  | 
939 | 940 | 
 
  | 
 | 941 | +class DGettextTest(unittest.TestCase):  | 
 | 942 | +    """Test dgettext() function, which allows translations from specific domains."""  | 
 | 943 | + | 
 | 944 | +    def setUp(self):  | 
 | 945 | +        """Set up a specific test domain and environment for dgettext tests."""  | 
 | 946 | +        self.localedir = tempfile.mkdtemp()  | 
 | 947 | +        self.addCleanup(shutil.rmtree, self.localedir)  | 
 | 948 | +        self.domain = 'gettext_domain'  | 
 | 949 | +        self.mofile = self.setup_dgettext_test_env()  | 
 | 950 | + | 
 | 951 | +    def setup_dgettext_test_env(self):  | 
 | 952 | +        """Create a mo file for dgettext testing."""  | 
 | 953 | +        os.makedirs(os.path.join(self.localedir, 'en', 'LC_MESSAGES'), exist_ok=True)  | 
 | 954 | +        mofile = os.path.join(self.localedir, 'en', 'LC_MESSAGES', f'{self.domain}.mo')  | 
 | 955 | +        with open(mofile, 'wb') as fp:  | 
 | 956 | +            fp.write(b'\x00\x00\x00\x00')  | 
 | 957 | +        return mofile  | 
 | 958 | + | 
 | 959 | +    def test_dgettext_found_translation(self):  | 
 | 960 | +        """Test dgettext finds translation in specified domain."""  | 
 | 961 | +        gettext.bindtextdomain(self.domain, self.localedir)  | 
 | 962 | +        with unittest.mock.patch('gettext.dgettext') as mock_dgettext:  | 
 | 963 | +            mock_dgettext.return_value = 'test message translation'  | 
 | 964 | +            result = gettext.dgettext(self.domain, 'test message')  | 
 | 965 | +            self.assertEqual(result, 'test message translation')  | 
 | 966 | + | 
 | 967 | +    def test_dgettext_missing_translation(self):  | 
 | 968 | +        """Test dgettext returns msgid when translation is missing."""  | 
 | 969 | +        gettext.bindtextdomain(self.domain, self.localedir)  | 
 | 970 | +        result = gettext.dgettext(self.domain, 'missing message')  | 
 | 971 | +        self.assertEqual(result, 'missing message')  | 
 | 972 | + | 
 | 973 | +    def test_dgettext_non_existent_domain(self):  | 
 | 974 | +        """Test dgettext returns msgid when domain doesn't exist."""  | 
 | 975 | +        result = gettext.dgettext('nonexistent_domain', 'test message')  | 
 | 976 | +        self.assertEqual(result, 'test message')  | 
 | 977 | + | 
 | 978 | +    def test_dgettext_empty_domain(self):  | 
 | 979 | +        """Test dgettext behavior with empty domain."""  | 
 | 980 | +        current_domain = gettext.textdomain()  | 
 | 981 | +        result = gettext.dgettext('', 'test message')  | 
 | 982 | +        expected = gettext.gettext('test message')  | 
 | 983 | +        self.assertEqual(result, expected)  | 
 | 984 | + | 
 | 985 | + | 
940 | 986 | if __name__ == '__main__':  | 
941 | 987 |     unittest.main()  | 
942 | 988 | 
 
  | 
 | 
0 commit comments