Mercurial > babel > old > mirror
annotate babel/messages/tests/extract.py @ 225:81c21bbd812f
fix skipping of class definitions without parens
fixes #55
author | pjenvey |
---|---|
date | Wed, 18 Jul 2007 20:49:12 +0000 |
parents | 9d0a19b4518b |
children | 043201653d56 |
rev | line source |
---|---|
3 | 1 # -*- coding: utf-8 -*- |
2 # | |
14
29ef15a6fd75
* Removed pkg_resources/setuptools requirement from various places.
cmlenz
parents:
3
diff
changeset
|
3 # Copyright (C) 2007 Edgewall Software |
3 | 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 | |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
14 import codecs |
3 | 15 import doctest |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
16 from StringIO import StringIO |
224 | 17 import sys |
3 | 18 import unittest |
19 | |
56
27d55a07c897
Rename the `babel.catalog` package to `babel.messages` for consistency with the other package names.
cmlenz
parents:
38
diff
changeset
|
20 from babel.messages import extract |
3 | 21 |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
22 |
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
23 class ExtractPythonTestCase(unittest.TestCase): |
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
24 |
224 | 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 | |
225 | 73 class Meta: |
74 verbose_name = _('log entry') | |
224 | 75 """) |
76 messages = list(extract.extract_python(buf, | |
77 extract.DEFAULT_KEYWORDS.keys(), | |
78 [], {})) | |
79 self.assertEqual([(3, '_', u'Page arg 1', []), | |
225 | 80 (3, '_', u'Page arg 2', []), |
81 (8, '_', u'log entry', [])], | |
224 | 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 | |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
127 def test_unicode_string_arg(self): |
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
128 buf = StringIO("msg = _(u'Foo Bar')") |
85
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
129 messages = list(extract.extract_python(buf, ('_',), [], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
130 self.assertEqual(u'Foo Bar', messages[0][2]) |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
131 |
85
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
132 def test_comment_tag(self): |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
133 buf = StringIO(""" |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
134 # NOTE: A translation comment |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
135 msg = _(u'Foo Bar') |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
136 """) |
93
30ed605cff51
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:
87
diff
changeset
|
137 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
138 self.assertEqual(u'Foo Bar', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
139 self.assertEqual([u'A translation comment'], messages[0][3]) |
85
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
140 |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
141 def test_comment_tag_multiline(self): |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
142 buf = StringIO(""" |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
143 # NOTE: A translation comment |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
144 # with a second line |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
145 msg = _(u'Foo Bar') |
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
146 """) |
93
30ed605cff51
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:
87
diff
changeset
|
147 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
148 self.assertEqual(u'Foo Bar', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
149 self.assertEqual([u'A translation comment', u'with a second line'], |
85
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
150 messages[0][3]) |
224 | 151 |
87
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
152 def test_translator_comments_with_previous_non_translator_comments(self): |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
153 buf = StringIO(""" |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
154 # This shouldn't be in the output |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
155 # because it didn't start with a comment tag |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
156 # NOTE: A translation comment |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
157 # with a second line |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
158 msg = _(u'Foo Bar') |
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
159 """) |
93
30ed605cff51
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:
87
diff
changeset
|
160 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
161 self.assertEqual(u'Foo Bar', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
162 self.assertEqual([u'A translation comment', u'with a second line'], |
87
f140be344563
Fixed de-pluralization bug introduced in [85] regarding the extraction of translator comments.
palgarvio
parents:
85
diff
changeset
|
163 messages[0][3]) |
85
e10f82c1d4c4
Add unit tests for extracting translator comments from python sources.
cmlenz
parents:
82
diff
changeset
|
164 |
93
30ed605cff51
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:
87
diff
changeset
|
165 def test_comment_tags_not_on_start_of_comment(self): |
30ed605cff51
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:
87
diff
changeset
|
166 buf = StringIO(""" |
30ed605cff51
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:
87
diff
changeset
|
167 # This shouldn't be in the output |
30ed605cff51
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:
87
diff
changeset
|
168 # because it didn't start with a comment tag |
30ed605cff51
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:
87
diff
changeset
|
169 # do NOTE: this will no be a translation comment |
30ed605cff51
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:
87
diff
changeset
|
170 # NOTE: This one will be |
30ed605cff51
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:
87
diff
changeset
|
171 msg = _(u'Foo Bar') |
30ed605cff51
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:
87
diff
changeset
|
172 """) |
30ed605cff51
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:
87
diff
changeset
|
173 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
174 self.assertEqual(u'Foo Bar', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
175 self.assertEqual([u'This one will be'], messages[0][3]) |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
176 |
94
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
177 def test_multiple_comment_tags(self): |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
178 buf = StringIO(""" |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
179 # NOTE1: A translation comment for tag1 |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
180 # with a second line |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
181 msg = _(u'Foo Bar1') |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
182 |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
183 # NOTE2: A translation comment for tag2 |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
184 msg = _(u'Foo Bar2') |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
185 """) |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
186 messages = list(extract.extract_python(buf, ('_',), |
655b03680d84
Fixed bug introduced in [92], bad use of `lstrip()`. Added a unittest to test multiple translator comment tags.
palgarvio
parents:
93
diff
changeset
|
187 ['NOTE1:', 'NOTE2:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
188 self.assertEqual(u'Foo Bar1', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
189 self.assertEqual([u'A translation comment for tag1', |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
190 u'with a second line'], messages[0][3]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
191 self.assertEqual(u'Foo Bar2', messages[1][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
192 self.assertEqual([u'A translation comment for tag2'], messages[1][3]) |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
193 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
194 def test_two_succeeding_comments(self): |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
195 buf = StringIO(""" |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
196 # NOTE: one |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
197 # NOTE: two |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
198 msg = _(u'Foo Bar') |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
199 """) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
200 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
201 self.assertEqual(u'Foo Bar', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
202 self.assertEqual([u'one', u'NOTE: two'], messages[0][3]) |
224 | 203 |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
204 def test_invalid_translator_comments(self): |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
205 buf = StringIO(""" |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
206 # NOTE: this shouldn't apply to any messages |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
207 hello = 'there' |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
208 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
209 msg = _(u'Foo Bar') |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
210 """) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
211 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
212 self.assertEqual(u'Foo Bar', messages[0][2]) |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
213 self.assertEqual([], messages[0][3]) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
214 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
215 def test_invalid_translator_comments2(self): |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
216 buf = StringIO(""" |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
217 # NOTE: Hi! |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
218 hithere = _('Hi there!') |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
219 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
220 # NOTE: you should not be seeing this in the .po |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
221 rows = [[v for v in range(0,10)] for row in range(0,10)] |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
222 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
223 # this (NOTE:) should not show up either |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
224 hello = _('Hello') |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
225 """) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
226 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
227 self.assertEqual(u'Hi there!', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
228 self.assertEqual([u'Hi!'], messages[0][3]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
229 self.assertEqual(u'Hello', messages[1][2]) |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
230 self.assertEqual([], messages[1][3]) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
231 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
232 def test_invalid_translator_comments3(self): |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
233 buf = StringIO(""" |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
234 # NOTE: Hi, |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
235 |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
236 # there! |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
237 hithere = _('Hi there!') |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
238 """) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
239 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
240 self.assertEqual(u'Hi there!', messages[0][2]) |
95
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
241 self.assertEqual([], messages[0][3]) |
979dc0d7bc5d
Commiting patch provided by pjenvey: Translator comments don't apply unless they immediately preceed the message.
palgarvio
parents:
94
diff
changeset
|
242 |
166
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
243 def test_utf8_message(self): |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
244 buf = StringIO(""" |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
245 # NOTE: hello |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
246 msg = _('Bonjour à tous') |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
247 """) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
248 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
249 {'encoding': 'utf-8'})) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
250 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
251 self.assertEqual([u'hello'], messages[0][3]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
252 |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
253 def test_utf8_message_with_magic_comment(self): |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
254 buf = StringIO("""# -*- coding: utf-8 -*- |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
255 # NOTE: hello |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
256 msg = _('Bonjour à tous') |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
257 """) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
258 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
259 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
260 self.assertEqual([u'hello'], messages[0][3]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
261 |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
262 def test_utf8_message_with_utf8_bom(self): |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
263 buf = StringIO(codecs.BOM_UTF8 + """ |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
264 # NOTE: hello |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
265 msg = _('Bonjour à tous') |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
266 """) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
267 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
268 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
269 self.assertEqual([u'hello'], messages[0][3]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
270 |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
271 def test_utf8_raw_strings_match_unicode_strings(self): |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
272 buf = StringIO(codecs.BOM_UTF8 + """ |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
273 msg = _('Bonjour à tous') |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
274 msgu = _(u'Bonjour à tous') |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
275 """) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
276 messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
277 self.assertEqual(u'Bonjour à tous', messages[0][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
278 self.assertEqual(messages[0][2], messages[1][2]) |
0eccbe635dba
made the python extractor detect source file encodings from the magic encoding
pjenvey
parents:
95
diff
changeset
|
279 |
224 | 280 class ExtractTestCase(unittest.TestCase): |
281 | |
282 def test_invalid_filter(self): | |
283 buf = StringIO("""\ | |
284 msg1 = _(i18n_arg.replace(r'\"', '"')) | |
285 msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2) | |
286 msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2) | |
287 msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2) | |
288 msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2)) | |
289 msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2)) | |
290 msg7 = _(hello.there) | |
291 msg8 = gettext('Rabbit') | |
292 msg9 = dgettext('wiki', model.addPage()) | |
293 msg10 = dngettext(domain, 'Page', 'Pages', 3) | |
294 """) | |
295 messages = \ | |
296 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], | |
297 {})) | |
298 self.assertEqual([(5, (u'bunny', u'bunnies'), []), | |
299 (8, u'Rabbit', []), | |
300 (10, (u'Page', u'Pages'), [])], messages) | |
301 | |
302 def test_empty_string_msgid(self): | |
303 buf = StringIO("""\ | |
304 msg = _('') | |
305 """) | |
306 stderr = sys.stderr | |
307 sys.stderr = StringIO() | |
308 try: | |
309 messages = \ | |
310 list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, | |
311 [], {})) | |
312 self.assertEqual([], messages) | |
313 assert 'warning: Empty msgid.' in sys.stderr.getvalue() | |
314 finally: | |
315 sys.stderr = stderr | |
316 | |
3 | 317 def suite(): |
318 suite = unittest.TestSuite() | |
319 suite.addTest(doctest.DocTestSuite(extract)) | |
38
06b876ed5501
Fix for #8: fix extraction of strings from Python source using prefixes ('u' or 'r') or triple quotes.
cmlenz
parents:
14
diff
changeset
|
320 suite.addTest(unittest.makeSuite(ExtractPythonTestCase)) |
224 | 321 suite.addTest(unittest.makeSuite(ExtractTestCase)) |
3 | 322 return suite |
323 | |
324 if __name__ == '__main__': | |
325 unittest.main(defaultTest='suite') |