Mercurial > babel > mirror
annotate babel/messages/tests/extract.py @ 322:b03a6a87e4cb trunk
fix invalid message extraction methods causing:
UnboundLocalError: local variable 'func' referenced before assignment
thanks cramm
author | pjenvey |
---|---|
date | Mon, 18 Feb 2008 18:09:30 +0000 |
parents | 5ca5fbd47766 |
children | b39145076d8a |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
12
e6ba3e878b10
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
1
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
8 # are also available at http://babel.edgewall.org/wiki/License. | |
9 # | |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
12 # history and logs, available at http://babel.edgewall.org/log/. | |
13 | |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
14 import codecs |
1 | 15 import doctest |
36
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
16 from StringIO import StringIO |
222 | 17 import sys |
1 | 18 import unittest |
19 | |
54
7dbcbc3f07e0
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
36
diff
changeset
|
20 from babel.messages import extract |
1 | 21 |
36
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
22 |
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
23 class ExtractPythonTestCase(unittest.TestCase): |
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
24 |
222 | 25 def test_nested_calls(self): |
26 buf = StringIO("""\ | |
27 msg1 = _(i18n_arg.replace(r'\"', '"')) | |
28 msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2) | |
29 msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2) | |
30 msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2) | |
31 msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2)) | |
32 msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2)) | |
33 msg7 = _(hello.there) | |
34 msg8 = gettext('Rabbit') | |
35 msg9 = dgettext('wiki', model.addPage()) | |
36 msg10 = dngettext(getDomain(), 'Page', 'Pages', 3) | |
37 """) | |
38 messages = list(extract.extract_python(buf, | |
39 extract.DEFAULT_KEYWORDS.keys(), | |
40 [], {})) | |
41 self.assertEqual([ | |
42 (1, '_', None, []), | |
43 (2, 'ungettext', (None, None, None), []), | |
44 (3, 'ungettext', (u'Babel', None, None), []), | |
45 (4, 'ungettext', (None, u'Babels', None), []), | |
46 (5, 'ungettext', (u'bunny', u'bunnies', None), []), | |
47 (6, 'ungettext', (None, u'bunnies', None), []), | |
48 (7, '_', None, []), | |
49 (8, 'gettext', u'Rabbit', []), | |
50 (9, 'dgettext', (u'wiki', None), []), | |
51 (10, 'dngettext', (None, u'Page', u'Pages', None), [])], | |
52 messages) | |
53 | |
54 def test_nested_comments(self): | |
55 buf = StringIO("""\ | |
56 msg = ngettext('pylon', # TRANSLATORS: shouldn't be | |
57 'pylons', # TRANSLATORS: seeing this | |
58 count) | |
59 """) | |
60 messages = list(extract.extract_python(buf, ('ngettext',), | |
61 ['TRANSLATORS:'], {})) | |
62 self.assertEqual([(1, 'ngettext', (u'pylon', u'pylons', None), [])], | |
63 messages) | |
64 | |
65 def test_declarations(self): | |
66 buf = StringIO("""\ | |
67 class gettext(object): | |
68 pass | |
69 def render_body(context,x,y=_('Page arg 1'),z=_('Page arg 2'),**pageargs): | |
70 pass | |
71 def ngettext(y='arg 1',z='arg 2',**pageargs): | |
72 pass | |
223 | 73 class Meta: |
74 verbose_name = _('log entry') | |
222 | 75 """) |
76 messages = list(extract.extract_python(buf, | |
77 extract.DEFAULT_KEYWORDS.keys(), | |
78 [], {})) | |
79 self.assertEqual([(3, '_', u'Page arg 1', []), | |
223 | 80 (3, '_', u'Page arg 2', []), |
81 (8, '_', u'log entry', [])], | |
222 | 82 messages) |
83 | |
84 def test_multiline(self): | |
85 buf = StringIO("""\ | |
86 msg1 = ngettext('pylon', | |
87 'pylons', count) | |
88 msg2 = ngettext('elvis', | |
89 'elvises', | |
90 count) | |
91 """) | |
92 messages = list(extract.extract_python(buf, ('ngettext',), [], {})) | |
93 self.assertEqual([(1, 'ngettext', (u'pylon', u'pylons', None), []), | |
94 (3, 'ngettext', (u'elvis', u'elvises', None), [])], | |
95 messages) | |
96 | |
97 def test_triple_quoted_strings(self): | |
98 buf = StringIO("""\ | |
99 msg1 = _('''pylons''') | |
100 msg2 = ngettext(r'''elvis''', \"\"\"elvises\"\"\", count) | |
101 msg2 = ngettext(\"\"\"elvis\"\"\", 'elvises', count) | |
102 """) | |
103 messages = list(extract.extract_python(buf, | |
104 extract.DEFAULT_KEYWORDS.keys(), | |
105 [], {})) | |
106 self.assertEqual([(1, '_', (u'pylons'), []), | |
107 (2, 'ngettext', (u'elvis', u'elvises', None), []), | |
108 (3, 'ngettext', (u'elvis', u'elvises', None), [])], | |
109 messages) | |
110 | |
111 def test_multiline_strings(self): | |
112 buf = StringIO("""\ | |
113 _('''This module provides internationalization and localization | |
114 support for your Python programs by providing an interface to the GNU | |
115 gettext message catalog library.''') | |
116 """) | |
117 messages = list(extract.extract_python(buf, | |
118 extract.DEFAULT_KEYWORDS.keys(), | |
119 [], {})) | |
120 self.assertEqual( | |
121 [(1, '_', | |
122 u'This module provides internationalization and localization\n' | |
123 'support for your Python programs by providing an interface to ' | |
124 'the GNU\ngettext message catalog library.', [])], | |
125 messages) | |
126 | |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
127 def test_concatenated_strings(self): |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
128 buf = StringIO("""\ |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
129 foobar = _('foo' 'bar') |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
130 """) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
131 messages = list(extract.extract_python(buf, |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
132 extract.DEFAULT_KEYWORDS.keys(), |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
133 [], {})) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
134 self.assertEqual(u'foobar', messages[0][2]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
135 |
36
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
136 def test_unicode_string_arg(self): |
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
137 buf = StringIO("msg = _(u'Foo Bar')") |
83
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
138 messages = list(extract.extract_python(buf, ('_',), [], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
139 self.assertEqual(u'Foo Bar', messages[0][2]) |
36
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
140 |
83
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
141 def test_comment_tag(self): |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
142 buf = StringIO(""" |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
143 # NOTE: A translation comment |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
144 msg = _(u'Foo Bar') |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
145 """) |
91
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
146 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
147 self.assertEqual(u'Foo Bar', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
148 self.assertEqual([u'A translation comment'], messages[0][3]) |
83
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
149 |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
150 def test_comment_tag_multiline(self): |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
151 buf = StringIO(""" |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
152 # NOTE: A translation comment |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
153 # with a second line |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
154 msg = _(u'Foo Bar') |
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
155 """) |
91
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
156 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
157 self.assertEqual(u'Foo Bar', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
158 self.assertEqual([u'A translation comment', u'with a second line'], |
83
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
159 messages[0][3]) |
222 | 160 |
85
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
161 def test_translator_comments_with_previous_non_translator_comments(self): |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
162 buf = StringIO(""" |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
163 # This shouldn't be in the output |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
164 # because it didn't start with a comment tag |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
165 # NOTE: A translation comment |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
166 # with a second line |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
167 msg = _(u'Foo Bar') |
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
168 """) |
91
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
169 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
170 self.assertEqual(u'Foo Bar', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
171 self.assertEqual([u'A translation comment', u'with a second line'], |
85
04a2f16bdd04
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
83
diff
changeset
|
172 messages[0][3]) |
83
75d0fa7b4af2
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
80
diff
changeset
|
173 |
91
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
174 def test_comment_tags_not_on_start_of_comment(self): |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
175 buf = StringIO(""" |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
176 # This shouldn't be in the output |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
177 # because it didn't start with a comment tag |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
178 # do NOTE: this will no be a translation comment |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
179 # NOTE: This one will be |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
180 msg = _(u'Foo Bar') |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
181 """) |
5cff450b9ed5
Changed translator comments extraction behaviour in python source code. Match is now true only if the TAG is on the start of the comment. The TAG will also be stripped from the comment. Added a unittest which tests this.
palgarvio
parents:
85
diff
changeset
|
182 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
183 self.assertEqual(u'Foo Bar', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
184 self.assertEqual([u'This one will be'], messages[0][3]) |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
185 |
92
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
186 def test_multiple_comment_tags(self): |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
187 buf = StringIO(""" |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
188 # NOTE1: A translation comment for tag1 |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
189 # with a second line |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
190 msg = _(u'Foo Bar1') |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
191 |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
192 # NOTE2: A translation comment for tag2 |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
193 msg = _(u'Foo Bar2') |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
194 """) |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
195 messages = list(extract.extract_python(buf, ('_',), |
ccb9da614597
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
91
diff
changeset
|
196 ['NOTE1:', 'NOTE2:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
197 self.assertEqual(u'Foo Bar1', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
198 self.assertEqual([u'A translation comment for tag1', |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
199 u'with a second line'], messages[0][3]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
200 self.assertEqual(u'Foo Bar2', messages[1][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
201 self.assertEqual([u'A translation comment for tag2'], messages[1][3]) |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
202 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
203 def test_two_succeeding_comments(self): |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
204 buf = StringIO(""" |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
205 # NOTE: one |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
206 # NOTE: two |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
207 msg = _(u'Foo Bar') |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
208 """) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
209 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
210 self.assertEqual(u'Foo Bar', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
211 self.assertEqual([u'one', u'NOTE: two'], messages[0][3]) |
222 | 212 |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
213 def test_invalid_translator_comments(self): |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
214 buf = StringIO(""" |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
215 # NOTE: this shouldn't apply to any messages |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
216 hello = 'there' |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
217 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
218 msg = _(u'Foo Bar') |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
219 """) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
220 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
221 self.assertEqual(u'Foo Bar', messages[0][2]) |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
222 self.assertEqual([], messages[0][3]) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
223 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
224 def test_invalid_translator_comments2(self): |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
225 buf = StringIO(""" |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
226 # NOTE: Hi! |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
227 hithere = _('Hi there!') |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
228 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
229 # NOTE: you should not be seeing this in the .po |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
230 rows = [[v for v in range(0,10)] for row in range(0,10)] |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
231 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
232 # this (NOTE:) should not show up either |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
233 hello = _('Hello') |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
234 """) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
235 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
236 self.assertEqual(u'Hi there!', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
237 self.assertEqual([u'Hi!'], messages[0][3]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
238 self.assertEqual(u'Hello', messages[1][2]) |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
239 self.assertEqual([], messages[1][3]) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
240 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
241 def test_invalid_translator_comments3(self): |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
242 buf = StringIO(""" |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
243 # NOTE: Hi, |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
244 |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
245 # there! |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
246 hithere = _('Hi there!') |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
247 """) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
248 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
249 self.assertEqual(u'Hi there!', messages[0][2]) |
93
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
250 self.assertEqual([], messages[0][3]) |
f008662b5d6e
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
92
diff
changeset
|
251 |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
252 def test_different_signatures(self): |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
253 buf = StringIO(""" |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
254 foo = _('foo', 'bar') |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
255 n = ngettext('hello', 'there', n=3) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
256 n = ngettext(n=3, 'hello', 'there') |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
257 n = ngettext(n=3, *messages) |
258
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
258 n = ngettext() |
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
259 n = ngettext('foo') |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
260 """) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
261 messages = list(extract.extract_python(buf, ('_', 'ngettext'), [], {})) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
262 self.assertEqual((u'foo', u'bar'), messages[0][2]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
263 self.assertEqual((u'hello', u'there', None), messages[1][2]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
264 self.assertEqual((None, u'hello', u'there'), messages[2][2]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
265 self.assertEqual((None, None), messages[3][2]) |
258
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
266 self.assertEqual(None, messages[4][2]) |
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
267 self.assertEqual(('foo'), messages[5][2]) |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
268 |
164
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
269 def test_utf8_message(self): |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
270 buf = StringIO(""" |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
271 # NOTE: hello |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
272 msg = _('Bonjour à tous') |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
273 """) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
274 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
275 {'encoding': 'utf-8'})) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
276 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
277 self.assertEqual([u'hello'], messages[0][3]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
278 |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
279 def test_utf8_message_with_magic_comment(self): |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
280 buf = StringIO("""# -*- coding: utf-8 -*- |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
281 # NOTE: hello |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
282 msg = _('Bonjour à tous') |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
283 """) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
284 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
285 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
286 self.assertEqual([u'hello'], messages[0][3]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
287 |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
288 def test_utf8_message_with_utf8_bom(self): |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
289 buf = StringIO(codecs.BOM_UTF8 + """ |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
290 # NOTE: hello |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
291 msg = _('Bonjour à tous') |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
292 """) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
293 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
294 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
295 self.assertEqual([u'hello'], messages[0][3]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
296 |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
297 def test_utf8_raw_strings_match_unicode_strings(self): |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
298 buf = StringIO(codecs.BOM_UTF8 + """ |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
299 msg = _('Bonjour à tous') |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
300 msgu = _(u'Bonjour à tous') |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
301 """) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
302 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
303 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
304 self.assertEqual(messages[0][2], messages[1][2]) |
e1199c0fb3bf
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
93
diff
changeset
|
305 |
222 | 306 class ExtractTestCase(unittest.TestCase): |
307 | |
308 def test_invalid_filter(self): | |
309 buf = StringIO("""\ | |
310 msg1 = _(i18n_arg.replace(r'\"', '"')) | |
311 msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2) | |
312 msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2) | |
313 msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2) | |
314 msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2)) | |
315 msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2)) | |
316 msg7 = _(hello.there) | |
317 msg8 = gettext('Rabbit') | |
318 msg9 = dgettext('wiki', model.addPage()) | |
319 msg10 = dngettext(domain, 'Page', 'Pages', 3) | |
320 """) | |
321 messages = \ | |
322 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], | |
323 {})) | |
324 self.assertEqual([(5, (u'bunny', u'bunnies'), []), | |
325 (8, u'Rabbit', []), | |
326 (10, (u'Page', u'Pages'), [])], messages) | |
327 | |
322 | 328 def test_invalid_extract_method(self): |
329 buf = StringIO('') | |
330 self.assertRaises(ValueError, list, extract.extract('spam', buf)) | |
331 | |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
332 def test_different_signatures(self): |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
333 buf = StringIO(""" |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
334 foo = _('foo', 'bar') |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
335 n = ngettext('hello', 'there', n=3) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
336 n = ngettext(n=3, 'hello', 'there') |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
337 n = ngettext(n=3, *messages) |
258
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
338 n = ngettext() |
5ca5fbd47766
skip messages that have less arguments than the keyword spec calls for
pjenvey
parents:
257
diff
changeset
|
339 n = ngettext('foo') |
257
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
340 """) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
341 messages = \ |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
342 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
343 {})) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
344 self.assertEqual(len(messages), 2) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
345 self.assertEqual(u'foo', messages[0][1]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
346 self.assertEqual((u'hello', u'there'), messages[1][1]) |
79c72d3a35f6
add extractor tests for some odder method signatures and concatenated strings
pjenvey
parents:
223
diff
changeset
|
347 |
222 | 348 def test_empty_string_msgid(self): |
349 buf = StringIO("""\ | |
350 msg = _('') | |
351 """) | |
352 stderr = sys.stderr | |
353 sys.stderr = StringIO() | |
354 try: | |
355 messages = \ | |
356 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, | |
357 [], {})) | |
358 self.assertEqual([], messages) | |
359 assert 'warning: Empty msgid.' in sys.stderr.getvalue() | |
360 finally: | |
361 sys.stderr = stderr | |
362 | |
1 | 363 def suite(): |
364 suite = unittest.TestSuite() | |
365 suite.addTest(doctest.DocTestSuite(extract)) | |
36
cfd15b7921f9
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
12
diff
changeset
|
366 suite.addTest(unittest.makeSuite(ExtractPythonTestCase)) |
222 | 367 suite.addTest(unittest.makeSuite(ExtractTestCase)) |
1 | 368 return suite |
369 | |
370 if __name__ == '__main__': | |
371 unittest.main(defaultTest='suite') |