1
2
3
4
5 package gettext
6
7 import (
8 "io/ioutil"
9 "strings"
10 "testing"
11 )
12
13 var testZipData = func() []byte {
14 if data, err := ioutil.ReadFile("./examples/locale.zip"); err == nil {
15 return data
16 }
17 return nil
18 }()
19
20 func TestGettext(t *testing.T) {
21 SetDomain("hello")
22
23
24 BindLocale(New("hello", "./examples/locale"))
25 testGettext(t, true)
26 BindLocale(New("hello", "", nil))
27 testGettext(t, false)
28
29
30 BindLocale(New("hello", "./examples/locale.zip", nil))
31 testGettext(t, true)
32 BindLocale(New("hello", "", nil))
33 testGettext(t, false)
34
35
36 BindLocale(New("hello", "locale.zip", testZipData))
37 testGettext(t, true)
38 BindLocale(New("hello", "", nil))
39 testGettext(t, false)
40 }
41
42 func TestGetdata(t *testing.T) {
43 SetDomain("hello")
44
45
46 BindLocale(New("hello", "./examples/locale", nil))
47 testGetdata(t, true)
48 BindLocale(New("hello", "", nil))
49 testGetdata(t, false)
50
51
52 BindLocale(New("hello", "./examples/locale.zip", nil))
53 testGetdata(t, true)
54 BindLocale(New("hello", "", nil))
55 testGetdata(t, false)
56
57
58 BindLocale(New("hello", "locale.zip", testZipData))
59 testGetdata(t, true)
60 BindLocale(New("hello", "", nil))
61 testGetdata(t, false)
62 }
63
64 func testGettext(t *testing.T, hasTransle bool) {
65 for i, v := range testTexts {
66 if lang := SetLanguage(v.lang); lang != v.lang {
67 t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
68 }
69 if hasTransle {
70 if dst := PGettext(v.ctx, v.src); dst != v.dst {
71 t.Fatalf("%d: expect = %q, got = %q", i, v.dst, dst)
72 }
73 } else {
74 if dst := PGettext(v.ctx, v.src); dst != v.src {
75 t.Fatalf("%d: expect = %s, got = %v", i, v.src, dst)
76 }
77 }
78 }
79 }
80
81 func testGetdata(t *testing.T, hasTransle bool) {
82 for i, v := range testResources {
83 if lang := SetLanguage(v.lang); lang != v.lang {
84 t.Fatalf("%d: expect = %s, got = %v", i, v.lang, lang)
85 }
86 if hasTransle {
87 v.data = strings.Replace(v.data, "\r", "", -1)
88 data := strings.Replace(string(Getdata(v.path)), "\r", "", -1)
89 if data != v.data {
90 t.Fatalf("%d: expect = %q, got = %q", i, v.data, data)
91 }
92 } else {
93 if data := string(Getdata(v.path)); data != "" {
94 t.Fatalf("%d: expect = %s, got = %v", i, "", data)
95 }
96 }
97 }
98 }
99
100 func BenchmarkGettext(b *testing.B) {
101 SetLanguage("zh_CN")
102 BindLocale(New("hello", "./examples/locale", nil))
103 SetDomain("hello")
104
105 b.ResetTimer()
106 for i := 0; i < b.N; i++ {
107 PGettext(testTexts[0].ctx, testTexts[0].src)
108 }
109 }
110 func BenchmarkGettext_Zip(b *testing.B) {
111 SetLanguage("zh_CN")
112 BindLocale(New("hello", "./examples/locale.zip", nil))
113 SetDomain("hello")
114
115 b.ResetTimer()
116 for i := 0; i < b.N; i++ {
117 PGettext(testTexts[0].ctx, testTexts[0].src)
118 }
119 }
120
121 func BenchmarkGetdata(b *testing.B) {
122 SetLanguage("zh_CN")
123 BindLocale(New("hello", "./examples/locale", nil))
124 SetDomain("hello")
125
126 b.ResetTimer()
127 for i := 0; i < b.N; i++ {
128 Getdata(testResources[0].path)
129 }
130 }
131 func BenchmarkGetdata_Zip(b *testing.B) {
132 SetLanguage("zh_CN")
133 BindLocale(New("hello", "./examples/locale.zip", nil))
134 SetDomain("hello")
135
136 b.ResetTimer()
137 for i := 0; i < b.N; i++ {
138 Getdata(testResources[0].path)
139 }
140 }
141
142 var testTexts = []struct {
143 lang string
144 ctx string
145 src string
146 dst string
147 }{
148
149 {"default", "main.init", "Gettext in init.", "Gettext in init."},
150 {"default", "main.main", "Hello, world!", "Hello, world!"},
151 {"default", "main.func", "Gettext in func.", "Gettext in func."},
152 {"default", "github.com/chai2010/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "pkg hi: Hello, world!"},
153
154
155 {"zh_CN", "main.init", "Gettext in init.", "Init函数中的Gettext.(ctx:main.init)"},
156 {"zh_CN", "main.main", "Hello, world!", "你好, 世界!(ctx:main.main)"},
157 {"zh_CN", "main.func", "Gettext in func.", "闭包函数中的Gettext.(ctx:main.func)"},
158 {"zh_CN", "code.google.com/p/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "来自\"Hi\"包的问候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)"},
159
160
161 {"zh_TW", "main.init", "Gettext in init.", "Init函數中的Gettext.(ctx:main.init)"},
162 {"zh_TW", "main.main", "Hello, world!", "你好, 世界!(ctx:main.main)"},
163 {"zh_TW", "main.func", "Gettext in func.", "閉包函數中的Gettext.(ctx:main.func)"},
164 {"zh_TW", "code.google.com/p/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "來自\"Hi\"包的問候: 你好, 世界!(ctx:code.google.com/p/gettext-go/examples/hi.SayHi)"},
165 }
166
167 var testResources = []struct {
168 lang string
169 path string
170 data string
171 }{
172
173 {
174 "default",
175 "poems.txt",
176 `Drinking Alone Under the Moon
177 Li Bai
178
179 flowers among one jar liquor
180 alone carouse without mutual intimate
181
182 raise cup greet bright moon
183 facing shadow become three persons
184
185 moon since not free to-drink
186 shadow follow accompany my body
187
188 briefly accompany moon with shadow
189 go happy should avail-oneself-of spring
190
191 my song moon walk-to-and-fro irresolute
192 my dance shadow fragments disorderly
193
194 sober time together mix glad
195 drunk after each divide scatter
196
197 eternal connect without consciouness-of-self roam
198 mutual appointment remote cloud Milky-Way
199 `,
200 },
201
202
203 {
204 "zh_CN",
205 "poems.txt",
206 `yuèxiàdúzhuó
207 月下独酌
208 lǐbái
209 李白
210
211 huājiānyīhújiǔ,dúzhuówúxiānɡqīn。
212 花间一壶酒,独酌无相亲。
213 jǔbēiyāomínɡyuè,duìyǐnɡchénɡsānrén。
214 举杯邀明月,对影成三人。
215 yuèjìbùjiěyǐn,yǐnɡtúsuíwǒshēn。
216 月既不解饮,影徒随我身。
217 zànbànyuèjiānɡyǐnɡ,xínɡlèxūjíchūn。
218 暂伴月将影,行乐须及春。
219 wǒɡēyuèpáihuái,wǒwǔyǐnɡlínɡluàn。
220 我歌月徘徊,我舞影零乱。
221 xǐnɡshítónɡjiāohuān,zuìhòuɡèfēnsàn。
222 醒时同交欢,醉后各分散。
223 yǒnɡjiéwúqínɡyóu,xiānɡqīmiǎoyúnhàn。
224 永结无情游,相期邈云汉。
225 `,
226 },
227
228
229 {
230 "zh_TW",
231 "poems.txt",
232 `yuèxiàdúzhuó
233 月下獨酌
234 lǐbái
235 李白
236
237 huājiānyīhújiǔ,dúzhuówúxiānɡqīn。
238 花間一壺酒,獨酌無相親。
239 jǔbēiyāomínɡyuè,duìyǐnɡchénɡsānrén。
240 舉杯邀明月,對影成三人。
241 yuèjìbùjiěyǐn,yǐnɡtúsuíwǒshēn。
242 月既不解飲,影徒隨我身。
243 zànbànyuèjiānɡyǐnɡ,xínɡlèxūjíchūn。
244 暫伴月將影,行樂須及春。
245 wǒɡēyuèpáihuái,wǒwǔyǐnɡlínɡluàn。
246 我歌月徘徊,我舞影零亂。
247 xǐnɡshítónɡjiāohuān,zuìhòuɡèfēnsàn。
248 醒時同交歡,醉後各分散。
249 yǒnɡjiéwúqínɡyóu,xiānɡqīmiǎoyúnhàn。
250 永結無情遊,相期邈雲漢。
251 `,
252 },
253 }
254
View as plain text