Debian Codegraph @

tests/messages/test_extract.py

raw
1 #
2 # Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
3 # All rights reserved.
4 #
5 # This software is licensed as described in the file LICENSE, which
6 # you should have received as part of this distribution. The terms
7 # are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
8 #
9 # This software consists of voluntary contributions made by many
10 # individuals. For the exact contribution history, see the revision
11 # history and logs, available at https://github.com/python-babel/babel/commits/master/.
12
13 import sys
14 from io import BytesIO, StringIO
15
16 import pytest
17
18 from babel.messages import extract
19
20
21 def test_invalid_filter():
22 buf = BytesIO(b"""\
23 msg1 = _(i18n_arg.replace(r'\"', '"'))
24 msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2)
25 msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2)
26 msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2)
27 msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2))
28 msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2))
29 msg7 = _(hello.there)
30 msg8 = gettext('Rabbit')
31 msg9 = dgettext('wiki', model.addPage())
32 msg10 = dngettext(domain, 'Page', 'Pages', 3)
33 """)
34 messages = \
35 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [],
36 {}))
37 assert messages == [
38 (5, ('bunny', 'bunnies'), [], None),
39 (8, 'Rabbit', [], None),
40 (10, ('Page', 'Pages'), [], None),
41 ]
42
43
44 def test_invalid_extract_method():
45 buf = BytesIO(b'')
46 with pytest.raises(ValueError):
47 list(extract.extract('spam', buf))
48
49
50 def test_different_signatures():
51 buf = BytesIO(b"""
52 foo = _('foo', 'bar')
53 n = ngettext('hello', 'there', n=3)
54 n = ngettext(n=3, 'hello', 'there')
55 n = ngettext(n=3, *messages)
56 n = ngettext()
57 n = ngettext('foo')
58 """)
59 messages = \
60 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [],
61 {}))
62 assert len(messages) == 2
63 assert messages[0][1] == 'foo'
64 assert messages[1][1] == ('hello', 'there')
65
66
67 def test_empty_string_msgid():
68 buf = BytesIO(b"""\
69 msg = _('')
70 """)
71 stderr = sys.stderr
72 sys.stderr = StringIO()
73 try:
74 messages = \
75 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS,
76 [], {}))
77 assert messages == []
78 assert 'warning: Empty msgid.' in sys.stderr.getvalue()
79 finally:
80 sys.stderr = stderr
81
82
83 def test_warn_if_empty_string_msgid_found_in_context_aware_extraction_method():
84 buf = BytesIO(b"\nmsg = pgettext('ctxt', '')\n")
85 stderr = sys.stderr
86 sys.stderr = StringIO()
87 try:
88 messages = extract.extract('python', buf)
89 assert list(messages) == []
90 assert 'warning: Empty msgid.' in sys.stderr.getvalue()
91 finally:
92 sys.stderr = stderr
93
94
95 def test_extract_allows_callable():
96 def arbitrary_extractor(fileobj, keywords, comment_tags, options):
97 return [(1, None, (), ())]
98 for x in extract.extract(arbitrary_extractor, BytesIO(b"")):
99 assert x[0] == 1
100
101
102 def test_future():
103 buf = BytesIO(br"""
104 # -*- coding: utf-8 -*-
105 from __future__ import unicode_literals
106 nbsp = _('\xa0')
107 """)
108 messages = list(extract.extract('python', buf,
109 extract.DEFAULT_KEYWORDS, [], {}))
110 assert messages[0][1] == '\xa0'
111
112
113 def test_f_strings():
114 buf = BytesIO(br"""
115 t1 = _('foobar')
116 t2 = _(f'spameggs' f'feast') # should be extracted; constant parts only
117 t2 = _(f'spameggs' 'kerroshampurilainen') # should be extracted (mixing f with no f)
118 t3 = _(f'''whoa! a ''' # should be extracted (continues on following lines)
119 f'flying shark'
120 '... hello'
121 )
122 t4 = _(f'spameggs {t1}') # should not be extracted
123 """)
124 messages = list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], {}))
125 assert len(messages) == 4
126 assert messages[0][1] == 'foobar'
127 assert messages[1][1] == 'spameggsfeast'
128 assert messages[2][1] == 'spameggskerroshampurilainen'
129 assert messages[3][1] == 'whoa! a flying shark... hello'
130
131
132 def test_f_strings_non_utf8():
133 buf = BytesIO(b"""
134 # -- coding: latin-1 --
135 t2 = _(f'\xe5\xe4\xf6' f'\xc5\xc4\xd6')
136 """)
137 messages = list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], {}))
138 assert len(messages) == 1
139 assert messages[0][1] == 'åäöÅÄÖ'
140
141
142 def test_issue_1195():
143 buf = BytesIO(b"""
144 foof = {
145 'test_string': StringWithMeta(
146 # NOTE: Text describing a test string
147 string=_(
148 'Text string that is on a new line'
149 ),
150 ),
151 }
152 """)
153 messages = list(extract.extract('python', buf, {'_': None}, ["NOTE"], {}))
154 message = messages[0]
155 assert message[0] in (5, 6) # Depends on whether #1126 is in
156 assert message[1] == 'Text string that is on a new line'
157 assert message[2] == ['NOTE: Text describing a test string']
158
159
160 def test_issue_1195_2():
161 buf = BytesIO(b"""
162 # NOTE: This should still be considered, even if
163 # the text is far away
164 foof = _(
165
166
167
168
169
170
171
172
173
174 'Hey! Down here!')
175 """)
176 messages = list(extract.extract('python', buf, {'_': None}, ["NOTE"], {}))
177 message = messages[0]
178 assert message[1] == 'Hey! Down here!'
179 assert message[2] == [
180 'NOTE: This should still be considered, even if',
181 'the text is far away',
182 ]