forked from leonelquinteros/gotext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
po_test.go
424 lines (353 loc) · 8.63 KB
/
po_test.go
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
package gotext
import (
"os"
"path"
"testing"
)
func TestPo(t *testing.T) {
// Set PO content
str := `
msgid ""
msgstr ""
# Initial comment
# Headers below
"Language: en\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
# Some comment
msgid "My text"
msgstr "Translated text"
# More comments
msgid "Another string"
msgstr ""
#Multi-line string
msgid "Multi-line"
msgstr "Multi "
"line"
msgid "One with var: %s"
msgid_plural "Several with vars: %s"
msgstr[0] "This one is the singular: %s"
msgstr[1] "This one is the plural: %s"
msgstr[2] "And this is the second plural form: %s"
msgid "This one has invalid syntax translations"
msgid_plural "Plural index"
msgstr[abc] "Wrong index"
msgstr[1 "Forgot to close brackets"
msgstr[0] "Badly formatted string'
msgid "Invalid formatted id[] with no translations
msgctxt "Ctx"
msgid "One with var: %s"
msgid_plural "Several with vars: %s"
msgstr[0] "This one is the singular in a Ctx context: %s"
msgstr[1] "This one is the plural in a Ctx context: %s"
msgid "Some random"
msgstr "Some random translation"
msgctxt "Ctx"
msgid "Some random in a context"
msgstr "Some random translation in a context"
msgid "More"
msgstr "More translation"
`
// Write PO content to file
filename := path.Clean(os.TempDir() + string(os.PathSeparator) + "default.po")
f, err := os.Create(filename)
if err != nil {
t.Fatalf("Can't create test file: %s", err.Error())
}
defer f.Close()
_, err = f.WriteString(str)
if err != nil {
t.Fatalf("Can't write to test file: %s", err.Error())
}
// Create po object
po := new(Po)
// Try to parse a directory
po.ParseFile(path.Clean(os.TempDir()))
// Parse file
po.ParseFile(filename)
// Test translations
tr := po.Get("My text")
if tr != "Translated text" {
t.Errorf("Expected 'Translated text' but got '%s'", tr)
}
v := "Variable"
tr = po.Get("One with var: %s", v)
if tr != "This one is the singular: Variable" {
t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
}
// Test multi-line
tr = po.Get("Multi-line")
if tr != "Multi line" {
t.Errorf("Expected 'Multi line' but got '%s'", tr)
}
// Test plural
tr = po.GetN("One with var: %s", "Several with vars: %s", 2, v)
if tr != "This one is the plural: Variable" {
t.Errorf("Expected 'This one is the plural: Variable' but got '%s'", tr)
}
// Test inexistent translations
tr = po.Get("This is a test")
if tr != "This is a test" {
t.Errorf("Expected 'This is a test' but got '%s'", tr)
}
tr = po.GetN("This is a test", "This are tests", 100)
if tr != "This are tests" {
t.Errorf("Expected 'This are tests' but got '%s'", tr)
}
// Test syntax error parsed translations
tr = po.Get("This one has invalid syntax translations")
if tr != "" {
t.Errorf("Expected '' but got '%s'", tr)
}
tr = po.GetN("This one has invalid syntax translations", "This are tests", 4)
if tr != "Plural index" {
t.Errorf("Expected 'Plural index' but got '%s'", tr)
}
// Test context translations
v = "Test"
tr = po.GetC("One with var: %s", "Ctx", v)
if tr != "This one is the singular in a Ctx context: Test" {
t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
}
// Test plural
tr = po.GetNC("One with var: %s", "Several with vars: %s", 17, "Ctx", v)
if tr != "This one is the plural in a Ctx context: Test" {
t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
}
// Test last translation
tr = po.Get("More")
if tr != "More translation" {
t.Errorf("Expected 'More translation' but got '%s'", tr)
}
}
func TestPoHeaders(t *testing.T) {
// Set PO content
str := `
msgid ""
msgstr ""
# Initial comment
# Headers below
"Language: en\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
# Some comment
msgid "Example"
msgstr "Translated example"
`
// Create po object
po := new(Po)
// Parse
po.Parse(str)
// Check headers expected
if po.Language != "en" {
t.Errorf("Expected 'Language: en' but got '%s'", po.Language)
}
// Check headers expected
if po.PluralForms != "nplurals=2; plural=(n != 1);" {
t.Errorf("Expected 'Plural-Forms: nplurals=2; plural=(n != 1);' but got '%s'", po.PluralForms)
}
}
func TestPluralFormsSingle(t *testing.T) {
// Single form
str := `
"Plural-Forms: nplurals=1; plural=0;"
# Some comment
msgid "Singular"
msgid_plural "Plural"
msgstr[0] "Singular form"
msgstr[1] "Plural form 1"
msgstr[2] "Plural form 2"
msgstr[3] "Plural form 3"
`
// Create po object
po := new(Po)
// Parse
po.Parse(str)
// Check plural form
n := po.pluralForm(0)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(0), got %d", n)
}
n = po.pluralForm(1)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(1), got %d", n)
}
n = po.pluralForm(2)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(2), got %d", n)
}
n = po.pluralForm(3)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(3), got %d", n)
}
n = po.pluralForm(50)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(50), got %d", n)
}
}
func TestPluralForms2(t *testing.T) {
// 2 forms
str := `
"Plural-Forms: nplurals=2; plural=n != 1;"
# Some comment
msgid "Singular"
msgid_plural "Plural"
msgstr[0] "Singular form"
msgstr[1] "Plural form 1"
msgstr[2] "Plural form 2"
msgstr[3] "Plural form 3"
`
// Create po object
po := new(Po)
// Parse
po.Parse(str)
// Check plural form
n := po.pluralForm(0)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(0), got %d", n)
}
n = po.pluralForm(1)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(1), got %d", n)
}
n = po.pluralForm(2)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(2), got %d", n)
}
n = po.pluralForm(3)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(3), got %d", n)
}
}
func TestPluralForms3(t *testing.T) {
// 3 forms
str := `
"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2;"
# Some comment
msgid "Singular"
msgid_plural "Plural"
msgstr[0] "Singular form"
msgstr[1] "Plural form 1"
msgstr[2] "Plural form 2"
msgstr[3] "Plural form 3"
`
// Create po object
po := new(Po)
// Parse
po.Parse(str)
// Check plural form
n := po.pluralForm(0)
if n != 2 {
t.Errorf("Expected 2 for pluralForm(0), got %d", n)
}
n = po.pluralForm(1)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(1), got %d", n)
}
n = po.pluralForm(2)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(2), got %d", n)
}
n = po.pluralForm(3)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(3), got %d", n)
}
n = po.pluralForm(100)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(100), got %d", n)
}
n = po.pluralForm(49)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(3), got %d", n)
}
}
func TestPluralFormsSpecial(t *testing.T) {
// 3 forms special
str := `
"Plural-Forms: nplurals=3;"
"plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;"
# Some comment
msgid "Singular"
msgid_plural "Plural"
msgstr[0] "Singular form"
msgstr[1] "Plural form 1"
msgstr[2] "Plural form 2"
msgstr[3] "Plural form 3"
`
// Create po object
po := new(Po)
// Parse
po.Parse(str)
// Check plural form
n := po.pluralForm(1)
if n != 0 {
t.Errorf("Expected 0 for pluralForm(1), got %d", n)
}
n = po.pluralForm(2)
if n != 1 {
t.Errorf("Expected 1 for pluralForm(2), got %d", n)
}
n = po.pluralForm(4)
if n != 1 {
t.Errorf("Expected 4 for pluralForm(4), got %d", n)
}
n = po.pluralForm(0)
if n != 2 {
t.Errorf("Expected 2 for pluralForm(2), got %d", n)
}
n = po.pluralForm(1000)
if n != 2 {
t.Errorf("Expected 2 for pluralForm(1000), got %d", n)
}
}
func TestTranslationObject(t *testing.T) {
tr := newTranslation()
str := tr.get()
if str != "" {
t.Errorf("Expected '' but got '%s'", str)
}
// Set id
tr.id = "Text"
// Get again
str = tr.get()
if str != "Text" {
t.Errorf("Expected 'Text' but got '%s'", str)
}
}
func TestPoRace(t *testing.T) {
// Set PO content
str := `# Some comment
msgid "My text"
msgstr "Translated text"
# More comments
msgid "Another string"
msgstr ""
msgid "One with var: %s"
msgid_plural "Several with vars: %s"
msgstr[0] "This one is the singular: %s"
msgstr[1] "This one is the plural: %s"
msgstr[2] "And this is the second plural form: %s"
`
// Create Po object
po := new(Po)
// Create sync channels
pc := make(chan bool)
rc := make(chan bool)
// Parse po content in a goroutine
go func(po *Po, done chan bool) {
po.Parse(str)
done <- true
}(po, pc)
// Read some translation on a goroutine
go func(po *Po, done chan bool) {
po.Get("My text")
done <- true
}(po, rc)
// Read something at top level
po.Get("My text")
// Wait for goroutines to finish
<-pc
<-rc
}