Skip to content

Commit dcae5cd

Browse files
authored
gh-104400: Add more tests to pygettext (GH-108173)
1 parent 556dc9b commit dcae5cd

File tree

8 files changed

+363
-21
lines changed

8 files changed

+363
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <LL@li.org>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: docstrings.py:7
19+
#, docstring
20+
msgid ""
21+
msgstr ""
22+
23+
#: docstrings.py:18
24+
#, docstring
25+
msgid ""
26+
"multiline\n"
27+
" docstring\n"
28+
" "
29+
msgstr ""
30+
31+
#: docstrings.py:25
32+
#, docstring
33+
msgid "docstring1"
34+
msgstr ""
35+
36+
#: docstrings.py:30
37+
#, docstring
38+
msgid "Hello, {}!"
39+
msgstr ""
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Test docstring extraction
2+
from gettext import gettext as _
3+
4+
5+
# Empty docstring
6+
def test(x):
7+
""""""
8+
9+
10+
# Leading empty line
11+
def test2(x):
12+
13+
"""docstring""" # XXX This should be extracted but isn't.
14+
15+
16+
# XXX Multiline docstrings should be cleaned with `inspect.cleandoc`.
17+
def test3(x):
18+
"""multiline
19+
docstring
20+
"""
21+
22+
23+
# Multiple docstrings - only the first should be extracted
24+
def test4(x):
25+
"""docstring1"""
26+
"""docstring2"""
27+
28+
29+
def test5(x):
30+
"""Hello, {}!""".format("world!") # XXX This should not be extracted.
31+
32+
33+
# Nested docstrings
34+
def test6(x):
35+
def inner(y):
36+
"""nested docstring""" # XXX This should be extracted but isn't.
37+
38+
39+
class Outer:
40+
class Inner:
41+
"nested class docstring" # XXX This should be extracted but isn't.
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <LL@li.org>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: fileloc.py:5 fileloc.py:6
19+
msgid "foo"
20+
msgstr ""
21+
22+
#: fileloc.py:9
23+
msgid "bar"
24+
msgstr ""
25+
26+
#: fileloc.py:14 fileloc.py:18
27+
#, docstring
28+
msgid "docstring"
29+
msgstr ""
30+
31+
#: fileloc.py:22 fileloc.py:26
32+
#, docstring
33+
msgid "baz"
34+
msgstr ""
35+
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Test file locations
2+
from gettext import gettext as _
3+
4+
# Duplicate strings
5+
_('foo')
6+
_('foo')
7+
8+
# Duplicate strings on the same line should only add one location to the output
9+
_('bar'), _('bar')
10+
11+
12+
# Duplicate docstrings
13+
class A:
14+
"""docstring"""
15+
16+
17+
def f():
18+
"""docstring"""
19+
20+
21+
# Duplicate message and docstring
22+
_('baz')
23+
24+
25+
def g():
26+
"""baz"""
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) YEAR ORGANIZATION
3+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: PACKAGE VERSION\n"
8+
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
9+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
10+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
11+
"Language-Team: LANGUAGE <LL@li.org>\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"Generated-By: pygettext.py 1.5\n"
16+
17+
18+
#: messages.py:5
19+
msgid ""
20+
msgstr ""
21+
22+
#: messages.py:8 messages.py:9
23+
msgid "parentheses"
24+
msgstr ""
25+
26+
#: messages.py:12
27+
msgid "Hello, world!"
28+
msgstr ""
29+
30+
#: messages.py:15
31+
msgid ""
32+
"Hello,\n"
33+
" multiline!\n"
34+
msgstr ""
35+
36+
#: messages.py:29
37+
msgid "Hello, {}!"
38+
msgstr ""
39+
40+
#: messages.py:33
41+
msgid "1"
42+
msgstr ""
43+
44+
#: messages.py:33
45+
msgid "2"
46+
msgstr ""
47+
48+
#: messages.py:34 messages.py:35
49+
msgid "A"
50+
msgstr ""
51+
52+
#: messages.py:34 messages.py:35
53+
msgid "B"
54+
msgstr ""
55+
56+
#: messages.py:36
57+
msgid "set"
58+
msgstr ""
59+
60+
#: messages.py:42
61+
msgid "nested string"
62+
msgstr ""
63+
64+
#: messages.py:47
65+
msgid "baz"
66+
msgstr ""
67+
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Test message extraction
2+
from gettext import gettext as _
3+
4+
# Empty string
5+
_("")
6+
7+
# Extra parentheses
8+
(_("parentheses"))
9+
((_("parentheses")))
10+
11+
# Multiline strings
12+
_("Hello, "
13+
"world!")
14+
15+
_("""Hello,
16+
multiline!
17+
""")
18+
19+
# Invalid arguments
20+
_()
21+
_(None)
22+
_(1)
23+
_(False)
24+
_(x="kwargs are not allowed")
25+
_("foo", "bar")
26+
_("something", x="something else")
27+
28+
# .format()
29+
_("Hello, {}!").format("world") # valid
30+
_("Hello, {}!".format("world")) # invalid
31+
32+
# Nested structures
33+
_("1"), _("2")
34+
arr = [_("A"), _("B")]
35+
obj = {'a': _("A"), 'b': _("B")}
36+
{{{_('set')}}}
37+
38+
39+
# Nested functions and classes
40+
def test():
41+
_("nested string") # XXX This should be extracted but isn't.
42+
[_("nested string")]
43+
44+
45+
class Foo:
46+
def bar(self):
47+
return _("baz")
48+
49+
50+
def bar(x=_('default value')): # XXX This should be extracted but isn't.
51+
pass
52+
53+
54+
def baz(x=[_('default value')]): # XXX This should be extracted but isn't.
55+
pass
56+
57+
58+
# Shadowing _()
59+
def _(x):
60+
pass
61+
62+
63+
def _(x="don't extract me"):
64+
pass

0 commit comments

Comments
 (0)