forked from leonelquinteros/gotext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mo_test.go
246 lines (202 loc) · 5.48 KB
/
mo_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
/*
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
package gotext
import (
"testing"
)
func TestMo_Get(t *testing.T) {
// Create mo object
mo := NewMo()
f, err := enUSFixture.Open("fixtures/en_US/default.mo")
if err != nil {
t.Fatal(err)
}
// Parse file
mo.ParseFile(f)
// Test translations
tr := mo.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Test translations
tr = mo.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}
func TestMo(t *testing.T) {
// Create mo object
mo := NewMo()
f, err := enUSFixture.Open("fixtures/en_US/default.mo")
if err != nil {
t.Fatal(err)
}
// Parse file
mo.ParseFile(f)
// Test translations
tr := mo.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
v := "Variable"
tr = mo.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 id
tr = mo.Get("multilineid")
if tr != "id with multiline content" {
t.Errorf("Expected 'id with multiline content' but got '%s'", tr)
}
// Test multi-line plural id
tr = mo.Get("multilinepluralid")
if tr != "plural id with multiline content" {
t.Errorf("Expected 'plural id with multiline content' but got '%s'", tr)
}
// Test multi-line
tr = mo.Get("Multi-line")
if tr != "Multi line" {
t.Errorf("Expected 'Multi line' but got '%s'", tr)
}
// Test plural
tr = mo.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 not existent translations
tr = mo.Get("This is a test")
if tr != "This is a test" {
t.Errorf("Expected 'This is a test' but got '%s'", tr)
}
tr = mo.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 context translations
v = "Test"
tr = mo.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 = mo.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 default plural vs singular return responses
tr = mo.GetN("Original", "Original plural", 4)
if tr != "Original plural" {
t.Errorf("Expected 'Original plural' but got '%s'", tr)
}
tr = mo.GetN("Original", "Original plural", 1)
if tr != "Original" {
t.Errorf("Expected 'Original' but got '%s'", tr)
}
// Test empty Translation strings
tr = mo.Get("Empty Translation")
if tr != "Empty Translation" {
t.Errorf("Expected 'Empty Translation' but got '%s'", tr)
}
tr = mo.Get("Empty plural form singular")
if tr != "Singular translated" {
t.Errorf("Expected 'Singular translated' but got '%s'", tr)
}
tr = mo.GetN("Empty plural form singular", "Empty plural form", 1)
if tr != "Singular translated" {
t.Errorf("Expected 'Singular translated' but got '%s'", tr)
}
tr = mo.GetN("Empty plural form singular", "Empty plural form", 2)
if tr != "Empty plural form" {
t.Errorf("Expected 'Empty plural form' but got '%s'", tr)
}
// Test last Translation
tr = mo.Get("More")
if tr != "More translation" {
t.Errorf("Expected 'More translation' but got '%s'", tr)
}
}
func TestMoRace(t *testing.T) {
// Create mo object
mo := NewMo()
// Create sync channels
pc := make(chan bool)
rc := make(chan bool)
// Parse po content in a goroutine
go func(mo *Mo, done chan bool) {
f, err := enUSFixture.Open("fixtures/en_US/default.mo")
if err != nil {
t.Fatal(err)
}
// Parse file
mo.ParseFile(f)
done <- true
}(mo, pc)
// Read some Translation on a goroutine
go func(mo *Mo, done chan bool) {
mo.Get("My text")
done <- true
}(mo, rc)
// Read something at top level
mo.Get("My text")
// Wait for goroutines to finish
<-pc
<-rc
}
func TestNewMoTranslatorRace(t *testing.T) {
// Create Po object
mo := NewMo()
// Create sync channels
pc := make(chan bool)
rc := make(chan bool)
// Parse po content in a goroutine
go func(mo Translator, done chan bool) {
f, err := enUSFixture.Open("fixtures/en_US/default.mo")
if err != nil {
t.Fatal(err)
}
// Parse file
mo.ParseFile(f)
done <- true
}(mo, pc)
// Read some Translation on a goroutine
go func(mo Translator, done chan bool) {
mo.Get("My text")
done <- true
}(mo, rc)
// Read something at top level
mo.Get("My text")
// Wait for goroutines to finish
<-pc
<-rc
}
func TestMoBinaryEncoding(t *testing.T) {
// Create mo objects
mo := NewMo()
mo2 := NewMo()
f, err := enUSFixture.Open("fixtures/en_US/default.mo")
if err != nil {
t.Fatal(err)
}
// Parse file
mo.ParseFile(f)
buff, err := mo.MarshalBinary()
if err != nil {
t.Fatal(err)
}
err = mo2.UnmarshalBinary(buff)
if err != nil {
t.Fatal(err)
}
// Test translations
tr := mo2.Get("My text")
if tr != translatedText {
t.Errorf("Expected '%s' but got '%s'", translatedText, tr)
}
// Test translations
tr = mo2.Get("language")
if tr != "en_US" {
t.Errorf("Expected 'en_US' but got '%s'", tr)
}
}