1
2
3
4 import os, time
5 from translate.storage import po
6 from translate.storage import poxliff
7 from translate.storage import poheader
8 from translate.misc.dictutils import ordereddict
9 from translate.misc import wStringIO
10
11
13 """ test for the header parsing function"""
14 source = r'''item1: one
15 item2: two:two
16 this item must get ignored because there is no colon sign in it
17 item3: three
18 '''
19 d = poheader.parseheaderstring(source)
20 print type(d)
21 assert type(d) == ordereddict
22 assert len(d) == 3
23 assert d['item1'] == 'one'
24 assert d['item2'] == 'two:two'
25 assert d['item3'] == 'three'
26
28 '''test the update function'''
29
30 d = poheader.update({}, test='hello')
31 assert len(d) == 0
32
33 d = poheader.update({}, add=True, Test='hello')
34 assert len(d) == 1
35 assert d['Test'] == 'hello'
36
37 d = poheader.update({'Test':'hello'}, add=True, Test='World')
38 assert len(d) == 1
39 assert d['Test'] == 'World'
40
41 d = poheader.update({}, add=True, test_me='hello')
42 assert d['Test-Me'] == 'hello'
43
44 d = ordereddict()
45 d['Project-Id-Version'] = 'abc'
46 d['POT-Creation-Date'] = 'now'
47 d = poheader.update(d, add=True, Test='hello', Report_Msgid_Bugs_To='bugs@list.org')
48 assert d.keys()[0] == "Project-Id-Version"
49 assert d.keys()[1] == "Report-Msgid-Bugs-To"
50 assert d.keys()[2] == "POT-Creation-Date"
51 assert d.keys()[3] == "Test"
52
53
55 """helper that parses po source without requiring files"""
56 dummyfile = wStringIO.StringIO(posource)
57 return po.pofile(dummyfile)
58
60 """helper that parses po source into poxliffFile"""
61 poxli = poxliff.PoXliffFile()
62 poxli.parse(posource)
63 return poxli
64
66 """Check the validity of a PO date.
67
68 The datestring must be in the format: 2007-06-08 10:08+0200
69 """
70
71
72
73
74 date_format = "%Y-%m-%d %H:%M"
75
76
77 tz = datestring[-4:]
78 assert type(int(tz)) == int
79
80
81
82
83 datestring = datestring[0:-5]
84
85
86 assert type(time.strptime(datestring, date_format)) == time.struct_time
87
98
100 pofile = po.pofile()
101
102
103 if time.__dict__.has_key('tzset'):
104 os.environ['TZ'] = 'Asia/Kabul'
105 time.tzset()
106 assert time.timezone == -16200
107
108 assert poheader.tzstring() == time.strftime("%z")
109
110 os.environ['TZ'] = 'Asia/Tehran'
111 time.tzset()
112 assert time.timezone == -12600
113
114 assert poheader.tzstring() == time.strftime("%z")
115
116 os.environ['TZ'] = 'Canada/Newfoundland'
117 time.tzset()
118 assert time.timezone == 12600
119
120 assert poheader.tzstring() == time.strftime("%z")
121
122 os.environ['TZ'] = 'US/Eastern'
123 time.tzset()
124 assert time.timezone == 18000
125
126 assert poheader.tzstring() == time.strftime("%z")
127
128 os.environ['TZ'] = 'Asia/Seoul'
129 time.tzset()
130 assert time.timezone == -32400
131
132 assert poheader.tzstring() == time.strftime("%z")
133
134 os.environ['TZ'] = 'Africa/Johannesburg'
135 time.tzset()
136 assert time.timezone == -7200
137
138 assert poheader.tzstring() == time.strftime("%z")
139
140 os.environ['TZ'] = 'Africa/Windhoek'
141 time.tzset()
142 assert time.timezone == -3600
143
144
145
146
147 os.environ['TZ'] = 'Egypt'
148 time.tzset()
149 assert time.timezone == -7200
150
151 assert poheader.tzstring() == time.strftime("%z")
152
153 os.environ['TZ'] = 'UTC'
154 time.tzset()
155 assert time.timezone == 0
156
157 assert poheader.tzstring() == time.strftime("%z")
158
160
161 def compare(pofile):
162 print pofile
163 assert len(pofile.units) == 1
164 header = pofile.header()
165 assert header.isheader()
166 assert not header.isblank()
167
168 headeritems = pofile.parseheader()
169 assert headeritems["Project-Id-Version"] == "PACKAGE VERSION"
170 assert headeritems["Report-Msgid-Bugs-To"] == ""
171 check_po_date(headeritems["POT-Creation-Date"])
172 assert headeritems["PO-Revision-Date"] == "YEAR-MO-DA HO:MI+ZONE"
173 assert headeritems["Last-Translator"] == "FULL NAME <EMAIL@ADDRESS>"
174 assert headeritems["Language-Team"] == "LANGUAGE <LL@li.org>"
175 assert headeritems["MIME-Version"] == "1.0"
176 assert headeritems["Content-Type"] == "text/plain; charset=UTF-8"
177 assert headeritems["Content-Transfer-Encoding"] == "8bit"
178 assert headeritems["Plural-Forms"] == "nplurals=INTEGER; plural=EXPRESSION;"
179
180
181 """test header functionality"""
182 posource = r'''# other comment\n
183 msgid ""
184 msgstr ""
185 "Project-Id-Version: PACKAGE VERSION\n"
186 "Report-Msgid-Bugs-To: \n"
187 "POT-Creation-Date: 2006-03-08 17:30+0200\n"
188 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
189 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
190 "Language-Team: LANGUAGE <LL@li.org>\n"
191 "MIME-Version: 1.0\n"
192 "Content-Type: text/plain; charset=UTF-8\n"
193 "Content-Transfer-Encoding: 8bit\n"
194 "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
195 '''
196 pofile = poparse(posource)
197 compare(pofile)
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
249
250
252 """test that we work if the plural equation spans more than one line"""
253 posource = r'''msgid ""
254 msgstr ""
255 "Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%"
256 "10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
257 '''
258 pofile = poparse(posource)
259 print pofile
260 assert len(pofile.units) == 1
261 header = pofile.units[0]
262 assert header.isheader()
263 assert not header.isblank()
264
265 headeritems = pofile.parseheader()
266 nplural, plural = pofile.getheaderplural()
267 assert nplural == "3"
268 assert plural == "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"
269
270