2 # -*- coding: utf-8 -*-
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
12 def test_parseheaderstring():
13 """ test for the header parsing function"""
14 source
= r
'''item1: one
16 this item must get ignored because there is no colon sign in it
19 d
= poheader
.parseheaderstring(source
)
21 assert type(d
) == ordereddict
23 assert d
['item1'] == 'one'
24 assert d
['item2'] == 'two:two'
25 assert d
['item3'] == 'three'
28 '''test the update function'''
29 # do we really add nothing if add==False ?
30 d
= poheader
.update({}, test
='hello')
32 # do we add if add==True ?
33 d
= poheader
.update({}, add
=True, Test
='hello')
35 assert d
['Test'] == 'hello'
36 # do we really update ?
37 d
= poheader
.update({'Test':'hello'}, add
=True, Test
='World')
39 assert d
['Test'] == 'World'
40 # does key rewrite work ?
41 d
= poheader
.update({}, add
=True, test_me
='hello')
42 assert d
['Test-Me'] == 'hello'
43 # is the order correct ?
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"
54 def poparse(posource
):
55 """helper that parses po source without requiring files"""
56 dummyfile
= wStringIO
.StringIO(posource
)
57 return po
.pofile(dummyfile
)
59 def poxliffparse(posource
):
60 """helper that parses po source into poxliffFile"""
61 poxli
= poxliff
.PoXliffFile()
65 def check_po_date(datestring
):
66 """Check the validity of a PO date.
68 The datestring must be in the format: 2007-06-08 10:08+0200
71 # We don't include the timezone offset as part of our format,
72 # because time.strptime() does not recognize %z
73 # The use of %z is deprecated in any case.
74 date_format
= "%Y-%m-%d %H:%M"
76 # Get the timezone offset (last 4 digits):
78 assert type(int(tz
)) == int
80 # Strip the timezone from the string, typically something like "+0200".
81 # This is to make the datestring conform to the specified format,
82 # we can't add %z to the format.
83 datestring
= datestring
[0:-5]
85 # Check that the date can be parsed
86 assert type(time
.strptime(datestring
, date_format
)) == time
.struct_time
90 headerdict
= pofile
.makeheaderdict(po_revision_date
=True)
91 check_po_date(headerdict
["POT-Creation-Date"])
92 check_po_date(headerdict
["PO-Revision-Date"])
94 headerdict
= pofile
.makeheaderdict(pot_creation_date
=time
.localtime(),
95 po_revision_date
=time
.localtime())
96 check_po_date(headerdict
["POT-Creation-Date"])
97 check_po_date(headerdict
["PO-Revision-Date"])
102 # The following will only work on Unix because of tzset() and %z
103 if time
.__dict
__.has_key('tzset'):
104 os
.environ
['TZ'] = 'Asia/Kabul'
106 assert time
.timezone
== -16200
108 assert poheader
.tzstring() == time
.strftime("%z")
110 os
.environ
['TZ'] = 'Asia/Tehran'
112 assert time
.timezone
== -12600
114 assert poheader
.tzstring() == time
.strftime("%z")
116 os
.environ
['TZ'] = 'Canada/Newfoundland'
118 assert time
.timezone
== 12600
120 assert poheader
.tzstring() == time
.strftime("%z")
122 os
.environ
['TZ'] = 'US/Eastern'
124 assert time
.timezone
== 18000
126 assert poheader
.tzstring() == time
.strftime("%z")
128 os
.environ
['TZ'] = 'Asia/Seoul'
130 assert time
.timezone
== -32400
132 assert poheader
.tzstring() == time
.strftime("%z")
134 os
.environ
['TZ'] = 'Africa/Johannesburg'
136 assert time
.timezone
== -7200
138 assert poheader
.tzstring() == time
.strftime("%z")
140 os
.environ
['TZ'] = 'Africa/Windhoek'
142 assert time
.timezone
== -3600
144 # For some reason python's %z doesn't know about Windhoek DST
145 #assert poheader.tzstring() == time.strftime("%z")
147 os
.environ
['TZ'] = 'Egypt'
149 assert time
.timezone
== -7200
151 assert poheader
.tzstring() == time
.strftime("%z")
153 os
.environ
['TZ'] = 'UTC'
155 assert time
.timezone
== 0
157 assert poheader
.tzstring() == time
.strftime("%z")
159 def test_header_blank():
163 assert len(pofile
.units
) == 1
164 header
= pofile
.header()
165 assert header
.isheader()
166 assert not header
.isblank()
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;"
181 """test header functionality"""
182 posource
= r
'''# other comment\n
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"
196 pofile
= poparse(posource
)
199 ## TODO: enable this code if PoXliffFile is able to parse a header
201 ## poxliffsource = r'''<?xml version="1.0" encoding="utf-8"?>
202 ##<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
204 ##<file datatype="po" original="test.po" source-language="en-US"><body><trans-unit approved="no" id="1" restype="x-gettext-domain-header" xml:space="preserve"><source>Project-Id-Version: PACKAGE VERSION
205 ##Report-Msgid-Bugs-To:
206 ##POT-Creation-Date: 2006-03-08 17:30+0200
207 ##PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
208 ##Last-Translator: FULL NAME <ph id="1"><EMAIL@ADDRESS></ph>
209 ##Language-Team: LANGUAGE <ph id="2"><LL@li.org></ph>
211 ##Content-Type: text/plain; charset=UTF-8
212 ##Content-Transfer-Encoding: 8bit
213 ##Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;
214 ##</source><target>Project-Id-Version: PACKAGE VERSION
215 ##Report-Msgid-Bugs-To:
216 ##POT-Creation-Date: 2006-03-08 17:30+0200
217 ##PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
218 ##Last-Translator: FULL NAME <ph id="1"><EMAIL@ADDRESS></ph>
219 ##Language-Team: LANGUAGE <ph id="2"><LL@li.org></ph>
221 ##Content-Type: text/plain; charset=UTF-8
222 ##Content-Transfer-Encoding: 8bit
223 ##Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;
224 ##</target><context-group name="po-entry" purpose="information"><context context-type="x-po-trancomment">other comment\n</context></context-group><note from="po-translator">other comment\n</note></trans-unit></body></file></xliff>
226 ## pofile = poparse(poxliffsource)
230 def test_plural_equation():
231 """test that we work with the equation even is the last semicolon is left out, since gettext
232 tools don't seem to mind"""
233 posource
= r
'''msgid ""
235 "Plural-Forms: nplurals=2; plural=(n != 1)%s\n"
237 for colon
in ("", ";"):
238 pofile
= poparse(posource
% colon
)
240 assert len(pofile
.units
) == 1
241 header
= pofile
.units
[0]
242 assert header
.isheader()
243 assert not header
.isblank()
245 headeritems
= pofile
.parseheader()
246 nplural
, plural
= pofile
.getheaderplural()
247 assert nplural
== "2"
248 assert plural
== "(n != 1)"
249 ## TODO: add the same test for PoXliffFile
251 def test_plural_equation_across_lines():
252 """test that we work if the plural equation spans more than one line"""
253 posource
= r
'''msgid ""
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"
258 pofile
= poparse(posource
)
260 assert len(pofile
.units
) == 1
261 header
= pofile
.units
[0]
262 assert header
.isheader()
263 assert not header
.isblank()
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 ## TODO: add the same test for PoXliffFile