Skip to content

Commit ee77877

Browse files
100% coverage of find
1 parent b93716f commit ee77877

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

Lib/test/test_gettext.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,16 @@ def test_expand_lang(self):
717717
return_value=locale):
718718
self.assertEqual(gettext._expand_lang(locale), expanded)
719719

720+
# helper for FindTestCase
721+
def _clearvars(env):
722+
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
723+
env.unset(key)
724+
720725
class FindTestCase(unittest.TestCase):
721726
def test_find_with_env_vars(self):
722727
# test that find correctly finds the environment variable LANGUAGE
723728
# when languages are not supplied
724729
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
725-
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
726-
env.unset(key)
727730
env.set('LANGUAGE', 'ga_IE')
728731

729732
locale_dir = os.path.join(tempdir, "locale")
@@ -739,8 +742,7 @@ def test_find_with_env_vars(self):
739742
def test_find_with_lanuages(self):
740743
# test that passed languages are used
741744
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
742-
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
743-
env.set(key, 'pt_BR')
745+
env.set('LANGUAGE', 'pt_BR')
744746

745747
locale_dir = os.path.join(tempdir, "locale")
746748
mofile_dir = os.path.join(locale_dir, "ga_IE", "LC_MESSAGES")
@@ -752,26 +754,47 @@ def test_find_with_lanuages(self):
752754
result = gettext.find("mofile", localedir=locale_dir, languages=['ga_IE'])
753755
self.assertEqual(result, mo_file)
754756

755-
def test_find_with_all(self):
757+
def test_find_with_no_lang(self):
758+
# no language can be found
759+
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
760+
_clearvars(env)
761+
result = gettext.find('foo')
762+
self.assertEqual(result, None)
763+
764+
def test_find_with_c(self):
765+
# 'C' is already in languages
766+
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
767+
env.set('LANGUAGE', 'C')
768+
result = gettext.find('foo')
769+
self.assertEqual(result, None)
770+
771+
def test_find_all(self):
756772
# test that all are returned when all is set
757773
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
758-
for key in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
759-
env.unset(key)
760-
env.set('LANGUAGE', 'ga_IE')
774+
_clearvars(env)
761775

762776
locale_dir = os.path.join(tempdir, "locale")
763777
paths = []
764-
for lang in ["ga_IE", "ga"]:
778+
for lang in ["ga_IE", "es_ES"]:
765779
mofile_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
766780
os.makedirs(mofile_dir)
767781
mo_file = os.path.join(mofile_dir, "mofile.mo")
768782
with open(mo_file, "wb") as f:
769783
f.write(b"\xDE\x12\x04\x95")
770784
paths.append(mo_file)
771785

772-
result = gettext.find('mofile', localedir=locale_dir, all=True)
786+
result = gettext.find('mofile', localedir=locale_dir,
787+
languages=["ga_IE", "es_ES"], all=True)
773788
self.assertEqual(sorted(result), sorted(paths))
774789

790+
def test_find_dedupelication(self):
791+
# test that find removes duplicate languages
792+
with os_helper.EnvironmentVarGuard() as env, os_helper.temp_cwd() as tempdir:
793+
_clearvars(env)
794+
795+
result = gettext.find("foo", languages=['ga_IE', 'ga_IE'])
796+
self.assertEqual(result, None)
797+
775798

776799
class MiscTestCase(unittest.TestCase):
777800
def test__all__(self):

0 commit comments

Comments
 (0)