forked from leonelquinteros/gotext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_test.go
144 lines (124 loc) · 3.78 KB
/
domain_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
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_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", 0) {
t.Error("'Empty plural form singular' should be reported as translated for n=0.")
}
if english.IsTranslatedN("Empty plural form singular", 1) {
t.Error("'Empty plural form singular' should be reported as not translated for n=1.")
}
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", 5) {
t.Error("Arabic plural should be reported as translated for n=5.")
}
if arabic.IsTranslatedN("Load %d more document", 6) {
t.Error("Arabic plural should be reported as not translated for n=6.")
}
// 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)
}
}