-
Notifications
You must be signed in to change notification settings - Fork 5
/
po_test.exs
520 lines (455 loc) · 16.3 KB
/
po_test.exs
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
defmodule Expo.Parser.PoTest do
@moduledoc false
# Tests taken & adapted from
# https://github.com/elixir-gettext/gettext/blob/600e4630fb7db514d464f92e2069a138cf9c68a1/test/gettext/po/parser_test.exs
use ExUnit.Case, async: true
alias Expo.Parser.Po
alias Expo.Translation
alias Expo.Translations
doctest Po
@example :expo |> Application.app_dir("priv/test/example.po") |> File.read!()
test "works" do
assert {:ok,
%Translations{
headers: [
"Project-Id-Version: Project\n",
"POT-Creation-Date: \n",
"PO-Revision-Date: \n",
"Last-Translator: \n",
"Language-Team: Language Team\n",
"Language: en\n",
"MIME-Version: 1.0\n",
"Content-Type: text/plain; charset=UTF-8\n",
"Content-Transfer-Encoding: 8bit\n",
"Plural-Forms: nplurals=2; plural=(n != 1);\n",
"X-Generator: Poedit 3.0.1\n",
"X-Poedit-SourceCharset: UTF-8\n"
],
translations: [
%Translation.Singular{
msgctxt: nil,
msgid: ["foo"],
msgstr: ["bar"],
comments: ["This is a translation", "Ah, another comment!"],
extracted_comments: ["An extracted comment"],
flags: [["flag1", "flag2"]],
previous_msgids: ["previous-untranslated-string"],
references: [[{"lib/foo.ex", 32}]],
obsolete: false
},
%Translation.Plural{
msgctxt: nil,
msgid: ["{count} New Notification"],
msgid_plural: ["{count} New Notifications"],
msgstr: %{0 => ["{count} Nuova notifica"], 1 => ["{count} Nuove notifiche"]},
comments: [],
extracted_comments: [],
flags: [],
previous_msgids: [],
references: [],
obsolete: false
},
%Translation.Singular{
comments: [],
msgctxt: nil,
extracted_comments: [],
flags: [],
msgid: ["hello"],
msgstr: ["ciao"],
previous_msgids: [],
references: [],
obsolete: true
},
%Translation.Plural{
comments: [],
msgctxt: nil,
extracted_comments: [],
flags: [],
msgid: ["{count} Test"],
msgid_plural: ["{count} Tests"],
msgstr: %{0 => ["{count} Test"], 1 => ["{count} Tests"]},
previous_msgids: [],
references: [],
obsolete: true
}
]
}} = Po.parse(@example)
end
test "parse/1 with single strings" do
assert {:ok,
%Translations{
translations: [%Translation.Singular{msgid: ["hello"], msgstr: ["ciao"]}]
}} =
Po.parse("""
msgid "hello"
msgstr "ciao"
""")
end
test "parse/1 with multiple concatenated strings" do
assert {:ok,
%Translations{
translations: [
%Translation.Singular{msgid: ["hello", " world"], msgstr: ["ciao", " mondo"]}
]
}} =
Po.parse("""
msgid "hello" " world"
msgstr "ciao" " mondo"
""")
end
test "parse/1 with multiple translations" do
assert {:ok,
%Translations{
translations: [
%Translation.Singular{msgid: ["hello"], msgstr: ["ciao"]},
%Translation.Singular{msgid: ["word"], msgstr: ["parola"]}
]
}} =
Po.parse("""
msgid "hello"
msgstr "ciao"
msgid "word"
msgstr "parola"
""")
end
test "parse/1 with unicode characters in the strings" do
assert {:ok,
%Translations{translations: [%Translation.Singular{msgid: ["føø"], msgstr: ["bårπ"]}]}} =
Po.parse("""
msgid "føø"
msgstr "bårπ"
""")
end
test "parse/1 with a pluralized string" do
assert {:ok,
%Translations{
translations: [
%Translation.Plural{
msgid: ["foo"],
msgstr: %{0 => ["bar"], 1 => ["bars"], 2 => ["barres"]}
}
]
}} =
Po.parse("""
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgstr[1] "bars"
msgstr[2] "barres"
""")
end
test "comments are associated with translations" do
assert {:ok,
%Translations{
translations: [
%Translation.Singular{
msgid: ["foo"],
msgstr: ["bar"],
comments: ["This is a translation", "Ah, another comment!"],
extracted_comments: ["An extracted comment"],
references: [[{"lib/foo.ex", 32}]]
}
]
}} =
Po.parse("""
# This is a translation
#: lib/foo.ex:32
# Ah, another comment!
#. An extracted comment
msgid "foo"
msgstr "bar"
""")
end
test "comments always belong to the next translation" do
assert {:ok,
%Translations{
translations: [
%Translation.Singular{msgid: ["a"], msgstr: ["b"]},
%Translation.Singular{msgid: ["c"], msgstr: ["d"], comments: ["Comment"]}
]
}} =
Po.parse("""
msgid "a"
msgstr "b"
# Comment
msgid "c"
msgstr "d"
""")
end
test "syntax error when there is no 'msgid'" do
assert {:error, {:parse_error, "expected msgid followed by strings", _context, 1}} =
Po.parse("msgstr \"foo\"")
assert {:error, {:parse_error, "expected msgid followed by strings", _context, 1}} =
Po.parse("msgstr \"foo\"")
assert {:error, {:parse_error, "expected msgid followed by strings", _context, 1}} =
Po.parse("\"foo\"")
end
test "if there's a msgid_plural, then plural forms must follow" do
assert {:error, {:parse_error, "expected plural form (like [0])", _context, 3}} =
Po.parse("""
msgid "foo"
msgid_plural "foos"
msgstr "bar"
""")
end
test "'msgid_plural' must come after 'msgid'" do
# TODO: Needs better message
assert {:error, {:parse_error, "expected whitespace", _context, 1}} =
Po.parse("msgid_plural ")
end
test "comments can't be placed between 'msgid' and 'msgstr'" do
assert {:error, {:parse_error, "expected msgid_plural followed by strings", _context, 2}} =
Po.parse("""
msgid "foo"
# Comment
msgstr "bar"
""")
assert {:error, {:parse_error, "expected string \"msgstr\"", _context, 3}} =
Po.parse("""
msgid "foo"
msgid_plural "foo"
# Comment
msgstr[0] "bar"
""")
end
# TODO: Should work
# test "files with just comments are ok (the comments are discarded)" do
# assert {:ok, _translations} =
# Po.parse("""
# # A comment
# # Another comment
# """)
# end
test "reference are extracted into the :reference field of a translation" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
#: foo.ex:1
#: f:2
#: filename with spaces.ex:12
# Not a reference comment
# : Not a reference comment either
#: another/ref/comment.ex:83
msgid "foo"
msgstr "bar"
""")
assert translation.references == [
[{"foo.ex", 1}],
[{"f", 2}],
[{"filename with spaces.ex", 12}],
[{"another/ref/comment.ex", 83}]
]
# All the reference comments are removed.
assert translation.comments == [
"Not a reference comment",
": Not a reference comment either"
]
end
test "extracted comments are extracted into the :extracted_comments field of a translation" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
#. Extracted comment
# Not an extracted comment
#.Another extracted comment
msgid "foo"
msgstr "bar"
""")
assert translation.extracted_comments == [
"Extracted comment",
"Another extracted comment"
]
# All the reference comments are removed.
assert translation.comments == [
"Not an extracted comment"
]
end
test "flags are extracted in to the :flags field of a translation" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
#, flag,a-flag b-flag, c-flag
# comment
#, flag, ,d-flag ,, e-flag
msgid "foo"
msgstr "bar"
""")
assert Enum.sort(translation.flags) == [
["flag", "a-flag b-flag", "c-flag"],
["flag", "d-flag ", "e-flag"]
]
assert translation.comments == ["comment"]
end
test "the line of a translation is the line of its msgid" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
msgid "foo"
msgstr "bar"
""")
assert translation.meta.msgid_source_line == 3
end
test "the line of a plural translation is the line of its msgid" do
assert {:ok, %Translations{translations: [%Translation.Plural{} = translation]}} =
Po.parse("""
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
""")
assert translation.meta.msgid_source_line == 3
end
test "headers are parsed when present" do
assert {:ok, %Translations{translations: [], headers: headers}} =
Po.parse(~S"""
msgid ""
msgstr "Language: en_US\n"
"Last-Translator: Jane Doe <jane@doe.com>\n"
""")
assert ["Language: en_US\n", "Last-Translator: Jane Doe <jane@doe.com>\n"] = headers
end
test "duplicated translations cause a parse error" do
assert {:error, {:duplicate_translation, "found duplicate on line 1 for msgid: 'foo'", 4}} =
Po.parse("""
msgid "foo"
msgstr "bar"
msgid "foo"
msgstr "baz"
msgid "foo"
msgstr "bong"
""")
# Works if the msgid is split differently as well
assert {:error, {:duplicate_translation, "found duplicate on line 1 for msgid: 'foo'", 4}} =
Po.parse("""
msgid "foo" ""
msgstr "bar"
msgid "" "foo"
msgstr "baz"
""")
end
test "duplicated plural translations cause a parse error" do
assert {:error,
{:duplicate_translation,
"found duplicate on line 1 for msgid: 'foo' and msgid_plural: 'foos'",
5}} =
Po.parse("""
msgid "foo"
msgid_plural "foos"
msgstr[0] "bar"
msgid "foo"
msgid_plural "foos"
msgstr[0] "baz"
""")
end
# TODO: Fix
# test "an empty list of tokens is parsed as an empty list of translations" do
# assert {:ok, %Translations{translations: [], headers: []}} = Po.parse("")
# end
test "multiple references on the same line are parsed correctly" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
#: foo.ex:1 bar.ex:2 with spaces.ex:3
#: baz.ex:3 with:colon.ex:12
msgid "foo"
msgstr "bar"
""")
assert translation.references == [
[{"foo.ex", 1}, {"bar.ex", 2}, {"with spaces.ex", 3}],
[{"baz.ex", 3}, {"with:colon.ex", 12}]
]
end
test "top-of-the-file comments are extracted correctly" do
assert {:ok, %Translations{translations: [], top_comments: top_comments}} =
Po.parse("""
# Top of the file
## Top of the file with two hashes
msgid ""
msgstr "Language: en_US\\n"
""")
assert ["Top of the file", "# Top of the file with two hashes"] = top_comments
end
test "msgctxt is parsed correctly for translations" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
msgctxt "my_" "context"
msgid "my_msgid"
msgstr "my_msgstr"
""")
assert translation.msgctxt == ["my_", "context"]
assert translation.msgid == ["my_msgid"]
assert translation.msgstr == ["my_msgstr"]
end
test "msgctxt is parsed correctly for plural translations" do
assert {:ok, %Translations{translations: [%Translation.Plural{} = translation]}} =
Po.parse("""
msgctxt "my_" "context"
msgid "my_msgid"
msgid_plural "my_msgid_plural"
msgstr[0] "my_msgstr"
""")
assert translation.msgctxt == ["my_", "context"]
assert translation.msgid == ["my_msgid"]
assert translation.msgid_plural == ["my_msgid_plural"]
assert translation.msgstr[0] == ["my_msgstr"]
end
test "msgctxt is nil when no msgctxt is present in a translation" do
assert {:ok, %Translations{translations: [%Translation.Singular{} = translation]}} =
Po.parse("""
msgid "my_msgid"
msgstr "my_msgstr"
""")
assert translation.msgctxt == nil
end
test "msgctxt causes a syntax error when misplaced" do
# Badly placed msgctxt still causes a syntax error
assert {:error, {:parse_error, "expected msgid_plural followed by strings", _context, 2}} =
Po.parse("""
msgid "my_msgid"
msgctxt "my_context"
msgstr "my_msgstr"
""")
end
test "msgctxt should not cause duplication translations" do
assert {:ok,
%Translations{
translations: [
%Translation.Singular{} = translation1,
%Translation.Singular{} = translation2
]
}} =
Po.parse("""
msgctxt "my_" "context"
msgid "my_msgid"
msgstr "my_msgstr"
msgid "my_msgid"
msgstr "my_msgstr"
""")
assert translation1.msgctxt == ["my_", "context"]
assert translation1.msgid == ["my_msgid"]
assert translation1.msgstr == ["my_msgstr"]
assert translation2.msgctxt == nil
assert translation2.msgid == ["my_msgid"]
assert translation2.msgstr == ["my_msgstr"]
end
test "msgctxt should not cause duplication for plural translations" do
assert {:ok,
%Translations{
translations: [
%Translation.Plural{} = translation1,
%Translation.Plural{} = translation2
]
}} =
Po.parse("""
msgctxt "my_" "context"
msgid "my_msgid"
msgid_plural "my_msgid_plural"
msgstr[0] "my_msgstr"
msgid "my_msgid"
msgid_plural "my_msgid_plural"
msgstr[0] "my_msgstr"
""")
assert translation1.msgctxt == ["my_", "context"]
assert translation1.msgid == ["my_msgid"]
assert translation1.msgid_plural == ["my_msgid_plural"]
assert translation1.msgstr[0] == ["my_msgstr"]
assert translation2.msgctxt == nil
assert translation2.msgid == ["my_msgid"]
assert translation2.msgid_plural == ["my_msgid_plural"]
assert translation2.msgstr[0] == ["my_msgstr"]
end
end