-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_po.py
469 lines (409 loc) · 13.8 KB
/
test_po.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import unittest
import itertools
from openformats.exceptions import ParseError
from openformats.formats.po import PoHandler
from openformats.strings import OpenString
from openformats.tests.formats.common import CommonFormatTestMixin
from openformats.tests.utils.strings import (strip_leading_spaces,
generate_random_string)
class PoTestCase(CommonFormatTestMixin, unittest.TestCase):
HANDLER_CLASS = PoHandler
TESTFILE_BASE = "openformats/tests/formats/po/files"
def setUp(self):
super(PoTestCase, self).setUp()
self.handler = PoHandler()
self.order_generator = itertools.count()
def _create_openstring(self, pluralized, extra_context=None):
context_dict = extra_context if extra_context is not None else {}
context_dict.update({
'order': self.order_generator.next(),
'pluralized': pluralized
})
key = generate_random_string()
if pluralized:
key = ':'.join([key, generate_random_string()])
openstring = OpenString(
key,
{
0: generate_random_string(),
1: generate_random_string()
} if pluralized else generate_random_string(),
**context_dict
)
openstring.string_hash
return openstring
def test_openstring_has_all_fields(self):
source = strip_leading_spaces(u"""
msgid ""
msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: en\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
# translator-comments1
# translator-comments2
#. extracted-comments1
#. extracted-comments2
#: validators.py:9
#: validators.py:11
#, python-format
#, another-flag
msgid "metest"
msgstr "msgstr1"
""")
template, stringset = self.handler.parse(source)
expected_comment = (
'extracted-comments1\nextracted-comments2\n'
u' translator-comments1\n translator-comments2'
)
self.assertEquals(len(stringset), 1)
self.assertEquals(
stringset[0].developer_comment,
expected_comment
)
self.assertEquals(
stringset[0].occurrences,
u'validators.py:9, validators.py:11'
)
self.assertEquals(
stringset[0].flags,
u'python-format, another-flag'
)
def test_removes_untranslated_non_pluralized(self):
string1 = self._create_openstring(False)
string2 = self._create_openstring(False)
string3 = self._create_openstring(False)
source = strip_leading_spaces(u"""
#
msgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
msgid "{s2_key}"
msgstr "{s2_str}"
msgid "{s3_key}"
msgstr "{s3_str}"
""".format(**{
's1_key': string1.key,
's1_str': string1.string,
's2_key': string2.key,
's2_str': string2.string,
's3_key': string3.key,
's3_str': string3.string
}))
template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [string1, string3])
self.assertEquals(
compiled,
strip_leading_spaces(
u"""# \nmsgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
msgid "{s3_key}"
msgstr "{s3_str}"
""".format(**{
's1_key': string1.key,
's1_str': string1.string,
's3_key': string3.key,
's3_str': string3.string
}))
)
def test_removes_untranslated_pluralized(self):
string1 = self._create_openstring(True)
keys1 = string1.key.split(':')
string2 = self._create_openstring(True)
keys2 = string2.key.split(':')
string3 = self._create_openstring(True)
keys3 = string3.key.split(':')
source = strip_leading_spaces(u"""
#
msgid ""
msgstr ""
msgid "{s1_key}"
msgid_plural "{s1_key_plural}"
msgstr[0] "{s1_str_singular}"
msgstr[1] "{s1_str_plural}"
msgid "{s2_key}"
msgid_plural "{s2_key_plural}"
msgstr[0] "{s2_str_singular}"
msgstr[1] "{s2_str_plural}"
msgid "{s3_key}"
msgid_plural "{s3_key_plural}"
msgstr[0] "{s3_str_singular}"
msgstr[1] "{s3_str_plural}"
""".format(**{
's1_key': keys1[0],
's1_key_plural': keys1[1],
's1_str_singular': string1.string[0],
's1_str_plural': string1.string[1],
's2_key': keys2[0],
's2_key_plural': keys2[1],
's2_str_singular': string2.string[0],
's2_str_plural': string2.string[1],
's3_key': keys3[0],
's3_key_plural': keys3[1],
's3_str_singular': string3.string[0],
's3_str_plural': string3.string[1]
}))
template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [string1, string3])
self.assertEquals(
compiled,
strip_leading_spaces(
u"""# \nmsgid ""
msgstr ""
msgid "{s1_key}"
msgid_plural "{s1_key_plural}"
msgstr[0] "{s1_str_singular}"
msgstr[1] "{s1_str_plural}"
msgid "{s3_key}"
msgid_plural "{s3_key_plural}"
msgstr[0] "{s3_str_singular}"
msgstr[1] "{s3_str_plural}"
""".format(**{
's1_key': keys1[0],
's1_key_plural': keys1[1],
's1_str_singular': string1.string[0],
's1_str_plural': string1.string[1],
's3_key': keys3[0],
's3_key_plural': keys3[1],
's3_str_singular': string3.string[0],
's3_str_plural': string3.string[1]
}))
)
def test_not_source_removes_untranslated_on_upload(self):
string1 = self._create_openstring(False)
source = strip_leading_spaces(u"""
#
msgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
msgid "a_random_key"
msgstr " "
""".format(**{
's1_key': string1.key,
's1_str': string1.string
}))
template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [string1])
self.assertEquals(
compiled,
strip_leading_spaces(
u"""# \nmsgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
""".format(**{
's1_key': string1.key,
's1_str': string1.string
}))
)
def test_duplicated_text_is_not_confused(self):
string1 = self._create_openstring(False)
source = strip_leading_spaces(u"""
msgctxt ""
msgid "{s1_key}"
msgstr "{s1_key}"
msgctxt "t2"
msgid "{s1_key}"
msgstr "{s1_key}"
""".format(**{
"s1_key": string1.key,
}))
template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [string1])
self.assertEquals(
compiled,
strip_leading_spaces(u"""# \nmsgid ""
msgstr ""
msgctxt ""
msgid "{s1_key}"
msgstr "{s1_str}"
""".format(**{
"s1_key": string1.key,
"s1_str": string1.string
}))
)
def test_fuzzy_flag_removes_entry_but_keeps_strings(self):
string1 = self._create_openstring(False)
string2 = self._create_openstring(False, extra_context={'fuzzy': True})
source = strip_leading_spaces(u"""
#
msgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
#, fuzzy
msgid "{s2_key}"
msgstr "{s2_str}"
""".format(**{
's1_key': string1.key,
's1_str': string1.string,
's2_key': string2.key,
's2_str': string2.string
}))
template, stringset = self.handler.parse(source)
compiled = self.handler.compile(template, [string1])
self.assertEquals(
compiled,
strip_leading_spaces(
u"""# \nmsgid ""
msgstr ""
msgid "{s1_key}"
msgstr "{s1_str}"
""".format(**{
's1_key': string1.key,
's1_str': string1.string
}))
)
def test_only_keys_error(self):
self._test_parse_error(
u"""
msgid "msgid1"
msgstr ""
msgid "msgid2"
msgstr "msgstr2"
""",
u"A non-empty msgstr was found on the entry with "
u"msgid `msgid2`. Remove and try again.",
parse_kwargs={'is_source': True}
)
self._test_parse_error(
u"""
msgid "msgid1"
msgid_plural "msgid_plural1"
msgstr[0] ""
msgstr[1] ""
msgid "msgid2"
msgstr "msgstr2"
""",
u"A non-empty msgstr was found on the entry with "
u"msgid `msgid2`. Remove and try again.",
parse_kwargs={'is_source': True}
)
def test_only_values_error(self):
self._test_parse_error(
u"""
msgid "msgid1"
msgstr "msgstr1"
msgid "msgid2"
msgstr ""
""",
u"The entry with msgid `msgid2` includes an empty msgstr. "
u"Provide a value and try again.",
parse_kwargs={'is_source': True}
)
def test_msgstr_in_plural_entry(self):
self._test_parse_error(
u"""
msgid "p1"
msgid_plural "p2"
msgstr "msgstr"
""",
u"An unexpected msgstr was found on the pluralized entry with "
u"msgid `p1` and msgid_plural `p2`."
)
def test_pluralized_msgstr_in_non_pluralized_entry(self):
self._test_parse_error(
u"""
msgid "p1"
msgstr[0] "StringPlural1"
msgstr[1] "StringsPlural2"
""",
u"Found unexpected msgstr[*] on the non pluralized entry with "
u"msgid `p1`."
)
def test_empty_msgid(self):
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgid ""
msgstr "Not a plural"
""",
u"Found empty msgid."
)
def test_duplicate_keys(self):
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgid "p1"
msgstr "1"
msgid "p1"
msgstr "2"
""",
u"A duplicate msgid was detected (p1). Use a unique "
u"msgid or add a msgctxt to differentiate."
)
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgctxt "t1"
msgid "p1"
msgstr "1"
msgctxt "t1"
msgid "p1"
msgstr "2"
""",
u"A duplicate msgid was detected (p1). Use a unique "
u"msgid or a unique msgctxt to differentiate "
u"(the existing msgctxt `t1` is a duplicate one)."
)
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgid "p1"
msgid_plural "p2"
msgstr[0] "1"
msgstr[1] "2"
msgid "p1"
msgid_plural "p2"
msgstr[0] "3"
msgstr[1] "4"
""",
u"A duplicate (msgid, msgid_plural) combination was "
u"detected (p1, p2). Use a unique msgid, msgid_plural "
u"combination or add a msgctxt to differentiate."
)
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgctxt "t1"
msgid "p1"
msgid_plural "p2"
msgstr[0] "1"
msgstr[1] "2"
msgctxt "t1"
msgid "p1"
msgid_plural "p2"
msgstr[0] "3"
msgstr[1] "4"
""",
u"A duplicate (msgid, msgid_plural) combination was "
u"detected (p1, p2). Use a unique msgid, msgid_plural "
u"combination or a unique msgctxt to differentiate "
u"(the existing msgctxt `t1` is a duplicate one)."
)
def test_incomplete_plurals(self):
self._test_parse_error(
u"""
msgid ""
msgstr ""
msgid "p1"
msgid_plural "p2"
msgstr[0] "Not a plural"
msgstr[1] ""
""",
u"Incomplete plural forms found on the entry with msgid `p1` "
u"and msgid_plural `p2`."
)
def test_invalid_po_raised_as_parseerror(self):
source = "blyargh"
with self.assertRaises(ParseError):
self.handler.parse(source)