summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tools
diff options
context:
space:
mode:
authorjack1142 <6032823+jack1142@users.noreply.github.com>2020-11-09 22:50:45 (GMT)
committerGitHub <noreply@github.com>2020-11-09 22:50:45 (GMT)
commitbfc6b63102d37ccb58a71711e2342143cd9f4d86 (patch)
tree7363462e163ebfa86f099802493389b42f217871 /Lib/test/test_tools
parent1f73c320e2921605c4963e202f6bdac1ef18f2ce (diff)
downloadcpython-bfc6b63102d37ccb58a71711e2342143cd9f4d86.zip
cpython-bfc6b63102d37ccb58a71711e2342143cd9f4d86.tar.gz
cpython-bfc6b63102d37ccb58a71711e2342143cd9f4d86.tar.bz2
bpo-36310: Allow pygettext.py to detect calls to gettext in f-strings. (GH-19875)
Adds support to Tools/i18n/pygettext.py for gettext calls in f-strings. This process is done by parsing the f-strings, processing each value, and flagging the ones which contain a gettext call. Co-authored-by: Batuhan Taskaya <batuhanosmantaskaya@gmail.com>
Diffstat (limited to 'Lib/test/test_tools')
-rw-r--r--Lib/test/test_tools/test_i18n.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py
index 8da6579..12f778d 100644
--- a/Lib/test/test_tools/test_i18n.py
+++ b/Lib/test/test_tools/test_i18n.py
@@ -220,6 +220,76 @@ class Test_pygettext(unittest.TestCase):
'''))
self.assertIn('doc', msgids)
+ def test_calls_in_fstrings(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_('foo bar')}"
+ '''))
+ self.assertIn('foo bar', msgids)
+
+ def test_calls_in_fstrings_raw(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ rf"{_('foo bar')}"
+ '''))
+ self.assertIn('foo bar', msgids)
+
+ def test_calls_in_fstrings_nested(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"""{f'{_("foo bar")}'}"""
+ '''))
+ self.assertIn('foo bar', msgids)
+
+ def test_calls_in_fstrings_attribute(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{obj._('foo bar')}"
+ '''))
+ self.assertIn('foo bar', msgids)
+
+ def test_calls_in_fstrings_with_call_on_call(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{type(str)('foo bar')}"
+ '''))
+ self.assertNotIn('foo bar', msgids)
+
+ def test_calls_in_fstrings_with_format(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_('foo {bar}').format(bar='baz')}"
+ '''))
+ self.assertIn('foo {bar}', msgids)
+
+ def test_calls_in_fstrings_with_wrong_input_1(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_(f'foo {bar}')}"
+ '''))
+ self.assertFalse([msgid for msgid in msgids if 'foo {bar}' in msgid])
+
+ def test_calls_in_fstrings_with_wrong_input_2(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_(1)}"
+ '''))
+ self.assertNotIn(1, msgids)
+
+ def test_calls_in_fstring_with_multiple_args(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_('foo', 'bar')}"
+ '''))
+ self.assertNotIn('foo', msgids)
+ self.assertNotIn('bar', msgids)
+
+ def test_calls_in_fstring_with_keyword_args(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_('foo', bar='baz')}"
+ '''))
+ self.assertNotIn('foo', msgids)
+ self.assertNotIn('bar', msgids)
+ self.assertNotIn('baz', msgids)
+
+ def test_calls_in_fstring_with_partially_wrong_expression(self):
+ msgids = self.extract_docstrings_from_str(dedent('''\
+ f"{_(f'foo') + _('bar')}"
+ '''))
+ self.assertNotIn('foo', msgids)
+ self.assertIn('bar', msgids)
+
def test_files_list(self):
"""Make sure the directories are inspected for source files
bpo-31920