-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdomain_test.go
More file actions
354 lines (297 loc) · 8.77 KB
/
Copy pathdomain_test.go
File metadata and controls
354 lines (297 loc) · 8.77 KB
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
package gotext
import (
"testing"
)
const (
enUSFixture = "fixtures/en_US/default.po"
arFixture = "fixtures/ar/categories.po"
)
// since both Po and Mo just pass-through to Domain for MarshalBinary and UnmarshalBinary, test it here
func TestBinaryEncoding(t *testing.T) {
// Create po objects
po := NewPo()
po2 := NewPo()
// Parse file
po.ParseFile(enUSFixture)
buff, err := po.GetDomain().MarshalBinary()
if err != nil {
t.Fatal(err)
}
err = po2.GetDomain().UnmarshalBinary(buff)
if err != nil {
t.Fatal(err)
}
// Test translations
tr := po2.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Test translations
tr = po2.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
func TestDomain_GetTranslations(t *testing.T) {
po := NewPo()
po.ParseFile(enUSFixture)
domain := po.GetDomain()
all := domain.GetTranslations()
if len(all) != len(domain.translations) {
t.Error("lengths should match")
}
for k, v := range domain.translations {
if all[k] == v {
t.Error("GetTranslations should be returning a copy, but pointers are equal")
}
if all[k].ID != v.ID {
t.Error("IDs should match")
}
if all[k].PluralID != v.PluralID {
t.Error("PluralIDs should match")
}
if all[k].dirty != v.dirty {
t.Error("dirty flag should match")
}
if len(all[k].Trs) != len(v.Trs) {
t.Errorf("Trs length does not match: %d != %d", len(all[k].Trs), len(v.Trs))
}
if len(all[k].Refs) != len(v.Refs) {
t.Errorf("Refs length does not match: %d != %d", len(all[k].Refs), len(v.Refs))
}
}
}
func TestDomain_GetCtxTranslations(t *testing.T) {
po := NewPo()
po.ParseFile(enUSFixture)
domain := po.GetDomain()
all := domain.GetCtxTranslations()
if len(all) != len(domain.contextTranslations) {
t.Error("lengths should match")
}
if domain.contextTranslations["Ctx"] == nil {
t.Error("Context 'Ctx' should exist")
}
for k, v := range domain.contextTranslations {
for kk, vv := range v {
if all[k][kk] == vv {
t.Error("GetCtxTranslations should be returning a copy, but pointers are equal")
}
if all[k][kk].ID != vv.ID {
t.Error("IDs should match")
}
if all[k][kk].PluralID != vv.PluralID {
t.Error("PluralIDs should match")
}
if all[k][kk].dirty != vv.dirty {
t.Error("dirty flag should match")
}
if len(all[k][kk].Trs) != len(vv.Trs) {
t.Errorf("Trs length does not match: %d != %d", len(all[k][kk].Trs), len(vv.Trs))
}
if len(all[k][kk].Refs) != len(vv.Refs) {
t.Errorf("Refs length does not match: %d != %d", len(all[k][kk].Refs), len(vv.Refs))
}
}
}
}
func TestDomain_IsTranslated(t *testing.T) {
englishPo := NewPo()
englishPo.ParseFile(enUSFixture)
english := englishPo.GetDomain()
// singular and plural
if english.IsTranslated("My Text") {
t.Error("'My text' should be reported as translated.")
}
if english.IsTranslated("Another string") {
t.Error("'Another string' should be reported as not translated.")
}
if !english.IsTranslatedN("Empty plural form singular", 1) {
t.Error("'Empty plural form singular' should be reported as translated for n=1.")
}
if english.IsTranslatedN("Empty plural form singular", 0) {
t.Error("'Empty plural form singular' should be reported as not translated for n=0.")
}
arabicPo := NewPo()
arabicPo.ParseFile(arFixture)
arabic := arabicPo.GetDomain()
// multiple plurals
if !arabic.IsTranslated("Load %d more document") {
t.Error("Arabic singular should be reported as translated.")
}
if !arabic.IsTranslatedN("Load %d more document", 0) {
t.Error("Arabic plural should be reported as translated for n=0.")
}
if !arabic.IsTranslatedN("Load %d more document", 1) {
t.Error("Arabic plural should be reported as translated for n=1.")
}
if !arabic.IsTranslatedN("Load %d more document", 100) {
t.Error("Arabic plural should be reported as translated for n=100.")
}
// context
if !english.IsTranslatedC("One with var: %s", "Ctx") {
t.Error("Context singular should be reported as translated.")
}
if !english.IsTranslatedNC("One with var: %s", 0, "Ctx") {
t.Error("Context plural should be reported as translated for n=0")
}
if !english.IsTranslatedNC("One with var: %s", 2, "Ctx") {
t.Error("Context plural should be reported as translated for n=2")
}
}
func TestDomain_CheckExportFormatting(t *testing.T) {
po := NewPo()
po.Set("myid", "test string\nwith \"newline\"")
poBytes, _ := po.MarshalText()
expectedOutput := `msgid ""
msgstr ""
msgid "myid"
msgstr ""
"test string\n"
"with \"newline\""`
if string(poBytes) != expectedOutput {
t.Errorf("Exported PO format does not match. Received:\n\n%v\n\n\nExpected:\n\n%v", string(poBytes), expectedOutput)
}
}
func TestDomain_GetWithVar(t *testing.T) {
po := NewPo()
po.ParseFile(enUSFixture)
domain := po.GetDomain()
// Test singular with variable
v := "My Text"
tr := domain.Get(v)
if tr != "My Text" {
t.Errorf("Expected 'MyText' but got '%s'", tr)
}
tr = po.Get(v)
if tr != "My Text" {
t.Errorf("Expected 'MyText' but got '%s'", tr)
}
}
func TestDomain_Append(t *testing.T) {
d := NewDomain()
d.Set("test", "translated")
b := []byte("prefix: ")
res := d.Append(b, "test")
if string(res) != "prefix: translated" {
t.Errorf("Expected 'prefix: translated', got '%s'", string(res))
}
res = d.Append(nil, "missing")
if string(res) != "missing" {
t.Errorf("Expected 'missing', got '%s'", string(res))
}
}
func TestDomain_AppendN(t *testing.T) {
d := NewDomain()
d.SetN("one", "many", 1, "singular")
d.SetN("one", "many", 2, "plural")
res := d.AppendN(nil, "one", "many", 1)
if string(res) != "singular" {
t.Errorf("Expected 'singular', got '%s'", string(res))
}
res = d.AppendN(nil, "one", "many", 2)
if string(res) != "plural" {
t.Errorf("Expected 'plural', got '%s'", string(res))
}
res = d.AppendN(nil, "missing", "missings", 1)
if string(res) != "missing" {
t.Errorf("Expected 'missing', got '%s'", string(res))
}
res = d.AppendN(nil, "missing", "missings", 2)
if string(res) != "missings" {
t.Errorf("Expected 'missings', got '%s'", string(res))
}
}
func TestDomain_AppendC(t *testing.T) {
d := NewDomain()
d.SetC("test", "ctx", "translated")
res := d.AppendC(nil, "test", "ctx")
if string(res) != "translated" {
t.Errorf("Expected 'translated', got '%s'", string(res))
}
res = d.AppendC(nil, "test", "wrong_ctx")
if string(res) != "test" {
t.Errorf("Expected 'test', got '%s'", string(res))
}
}
func TestDomain_AppendNC(t *testing.T) {
d := NewDomain()
d.SetNC("one", "many", "ctx", 1, "singular")
d.SetNC("one", "many", "ctx", 2, "plural")
res := d.AppendNC(nil, "one", "many", 1, "ctx")
if string(res) != "singular" {
t.Errorf("Expected 'singular', got '%s'", string(res))
}
res = d.AppendNC(nil, "one", "many", 2, "ctx")
if string(res) != "plural" {
t.Errorf("Expected 'plural', got '%s'", string(res))
}
}
func TestDomain_SetNC(t *testing.T) {
d := NewDomain()
d.SetNC("one", "many", "ctx", 1, "singular")
// Update existing
d.SetNC("one", "many", "ctx", 1, "singular_updated")
res := d.GetNC("one", "many", 1, "ctx")
if res != "singular_updated" {
t.Errorf("Expected 'singular_updated', got '%s'", res)
}
// New one in existing context
d.SetNC("two", "plural_two", "ctx", 1, "two_singular")
res = d.GetNC("two", "plural_two", 1, "ctx")
if res != "two_singular" {
t.Errorf("Expected 'two_singular', got '%s'", res)
}
}
func TestDomain_Refs(t *testing.T) {
d := NewDomain()
refs := []string{"file.go:10", "file.go:20"}
d.SetRefs("test", refs)
gotRefs := d.GetRefs("test")
if len(gotRefs) != 2 || gotRefs[0] != refs[0] || gotRefs[1] != refs[1] {
t.Errorf("Expected %v, got %v", refs, gotRefs)
}
if d.GetRefs("missing") != nil {
t.Error("Expected nil for missing refs")
}
// Update refs
newRefs := []string{"file.go:30"}
d.SetRefs("test", newRefs)
gotRefs = d.GetRefs("test")
if len(gotRefs) != 1 || gotRefs[0] != newRefs[0] {
t.Errorf("Expected %v, got %v", newRefs, gotRefs)
}
}
func TestDomain_HeaderMap(t *testing.T) {
d := NewDomain()
d.Headers.Del("Missing") // No-op but increases coverage
d.Headers.Set("Key", "Value")
if d.Headers.Get("Key") != "Value" {
t.Error("Header Get/Set failed")
}
d.Headers.Add("Key", "Value2")
values := d.Headers.Values("Key")
if len(values) != 2 || values[1] != "Value2" {
t.Error("Header Add failed")
}
d.Headers.Del("Key")
if d.Headers.Get("Key") != "" {
t.Error("Header Del failed")
}
var nilHeaders HeaderMap
if nilHeaders.Get("Any") != "" {
t.Error("Nil headers should return empty string")
}
if nilHeaders.Values("Any") != nil {
t.Error("Nil headers should return nil values")
}
}
func TestDomain_SetPluralResolver(t *testing.T) {
d := NewDomain()
d.SetPluralResolver(func(n int) int {
return 5
})
if d.pluralForm(10) != 5 {
t.Error("Custom plural resolver failed")
}
}