Package translate :: Package storage :: Module test_poheader
[hide private]
[frames] | no frames]

Source Code for Module translate.storage.test_poheader

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  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   
12 -def test_parseheaderstring():
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
27 -def test_update():
28 '''test the update function''' 29 # do we really add nothing if add==False ? 30 d = poheader.update({}, test='hello') 31 assert len(d) == 0 32 # do we add if add==True ? 33 d = poheader.update({}, add=True, Test='hello') 34 assert len(d) == 1 35 assert d['Test'] == 'hello' 36 # do we really update ? 37 d = poheader.update({'Test':'hello'}, add=True, Test='World') 38 assert len(d) == 1 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 ? 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
54 -def poparse(posource):
55 """helper that parses po source without requiring files""" 56 dummyfile = wStringIO.StringIO(posource) 57 return po.pofile(dummyfile)
58
59 -def poxliffparse(posource):
60 """helper that parses po source into poxliffFile""" 61 poxli = poxliff.PoXliffFile() 62 poxli.parse(posource) 63 return poxli
64
65 -def check_po_date(datestring):
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 # 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" 75 76 # Get the timezone offset (last 4 digits): 77 tz = datestring[-4:] 78 assert type(int(tz)) == int 79 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] 84 85 # Check that the date can be parsed 86 assert type(time.strptime(datestring, date_format)) == time.struct_time
87
88 -def test_po_dates():
89 pofile = po.pofile() 90 headerdict = pofile.makeheaderdict(po_revision_date=True) 91 check_po_date(headerdict["POT-Creation-Date"]) 92 check_po_date(headerdict["PO-Revision-Date"]) 93 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"])
98
99 -def test_timezones():
100 pofile = po.pofile() 101 102 # The following will only work on Unix because of tzset() and %z 103 if time.__dict__.has_key('tzset'): 104 os.environ['TZ'] = 'America/Argentina/Cordoba' 105 time.tzset() 106 assert time.timezone == 10800 107 # Typically "-0300" 108 assert pofile.tzstring() == time.strftime("%z") 109 110 os.environ['TZ'] = 'Asia/Kabul' 111 time.tzset() 112 assert time.timezone == -16200 113 # Typically "+0430" 114 assert pofile.tzstring() == time.strftime("%z") 115 116 os.environ['TZ'] = 'Asia/Tehran' 117 time.tzset() 118 assert time.timezone == -12600 119 # Typically "+0330" 120 assert pofile.tzstring() == time.strftime("%z") 121 122 os.environ['TZ'] = 'Canada/Newfoundland' 123 time.tzset() 124 assert time.timezone == 12600 125 # Typically "-0230" 126 assert pofile.tzstring() == time.strftime("%z") 127 128 os.environ['TZ'] = 'US/Eastern' 129 time.tzset() 130 assert time.timezone == 18000 131 # Typically "-0400" 132 assert pofile.tzstring() == time.strftime("%z") 133 134 os.environ['TZ'] = 'Asia/Seoul' 135 time.tzset() 136 assert time.timezone == -32400 137 # Typically "+0900" 138 assert pofile.tzstring() == time.strftime("%z") 139 140 os.environ['TZ'] = 'Africa/Johannesburg' 141 time.tzset() 142 assert time.timezone == -7200 143 # Typically "+0200" 144 assert pofile.tzstring() == time.strftime("%z") 145 146 os.environ['TZ'] = 'Africa/Windhoek' 147 time.tzset() 148 assert time.timezone == -3600 149 # Typically "+0100" 150 # For some reason python's %z doesn't know about Windhoek DST 151 #assert pofile.tzstring() == time.strftime("%z") 152 153 os.environ['TZ'] = 'Egypt' 154 time.tzset() 155 assert time.timezone == -7200 156 # Typically "+0300" 157 assert pofile.tzstring() == time.strftime("%z") 158 159 os.environ['TZ'] = 'UTC' 160 time.tzset() 161 assert time.timezone == 0 162 # Typically "+0000" 163 assert pofile.tzstring() == time.strftime("%z")
164
165 -def test_header_blank():
166 167 def compare(pofile): 168 print pofile 169 assert len(pofile.units) == 1 170 header = pofile.header() 171 assert header.isheader() 172 assert not header.isblank() 173 174 headeritems = pofile.parseheader() 175 assert headeritems["Project-Id-Version"] == "PACKAGE VERSION" 176 assert headeritems["Report-Msgid-Bugs-To"] == "" 177 check_po_date(headeritems["POT-Creation-Date"]) 178 assert headeritems["PO-Revision-Date"] == "YEAR-MO-DA HO:MI+ZONE" 179 assert headeritems["Last-Translator"] == "FULL NAME <EMAIL@ADDRESS>" 180 assert headeritems["Language-Team"] == "LANGUAGE <LL@li.org>" 181 assert headeritems["MIME-Version"] == "1.0" 182 assert headeritems["Content-Type"] == "text/plain; charset=UTF-8" 183 assert headeritems["Content-Transfer-Encoding"] == "8bit" 184 assert headeritems["Plural-Forms"] == "nplurals=INTEGER; plural=EXPRESSION;"
185 186 187 """test header functionality""" 188 posource = r'''# other comment\n 189 msgid "" 190 msgstr "" 191 "Project-Id-Version: PACKAGE VERSION\n" 192 "Report-Msgid-Bugs-To: \n" 193 "POT-Creation-Date: 2006-03-08 17:30+0200\n" 194 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 195 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" 196 "Language-Team: LANGUAGE <LL@li.org>\n" 197 "MIME-Version: 1.0\n" 198 "Content-Type: text/plain; charset=UTF-8\n" 199 "Content-Transfer-Encoding: 8bit\n" 200 "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" 201 ''' 202 pofile = poparse(posource) 203 compare(pofile) 204 205 ## TODO: enable this code if PoXliffFile is able to parse a header 206 ## 207 ## poxliffsource = r'''<?xml version="1.0" encoding="utf-8"?> 208 ##<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1"> 209 ## 210 ##<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 211 ##Report-Msgid-Bugs-To: 212 ##POT-Creation-Date: 2006-03-08 17:30+0200 213 ##PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE 214 ##Last-Translator: FULL NAME <ph id="1">&lt;EMAIL@ADDRESS&gt;</ph> 215 ##Language-Team: LANGUAGE <ph id="2">&lt;LL@li.org&gt;</ph> 216 ##MIME-Version: 1.0 217 ##Content-Type: text/plain; charset=UTF-8 218 ##Content-Transfer-Encoding: 8bit 219 ##Plural-Forms: nplurals=INTEGER; plural=EXPRESSION; 220 ##</source><target>Project-Id-Version: PACKAGE VERSION 221 ##Report-Msgid-Bugs-To: 222 ##POT-Creation-Date: 2006-03-08 17:30+0200 223 ##PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE 224 ##Last-Translator: FULL NAME <ph id="1">&lt;EMAIL@ADDRESS&gt;</ph> 225 ##Language-Team: LANGUAGE <ph id="2">&lt;LL@li.org&gt;</ph> 226 ##MIME-Version: 1.0 227 ##Content-Type: text/plain; charset=UTF-8 228 ##Content-Transfer-Encoding: 8bit 229 ##Plural-Forms: nplurals=INTEGER; plural=EXPRESSION; 230 ##</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> 231 ##''' 232 ## pofile = poparse(poxliffsource) 233 ## compare(pofile) 234 235
236 -def test_plural_equation():
237 """test that we work with the equation even is the last semicolon is left out, since gettext 238 tools don't seem to mind""" 239 posource = r'''msgid "" 240 msgstr "" 241 "Plural-Forms: nplurals=2; plural=(n != 1)%s\n" 242 ''' 243 for colon in ("", ";"): 244 pofile = poparse(posource % colon) 245 print pofile 246 assert len(pofile.units) == 1 247 header = pofile.units[0] 248 assert header.isheader() 249 assert not header.isblank() 250 251 headeritems = pofile.parseheader() 252 nplural, plural = pofile.getheaderplural() 253 assert nplural == "2" 254 assert plural == "(n != 1)"
255 ## TODO: add the same test for PoXliffFile 256
257 -def test_plural_equation_across_lines():
258 """test that we work if the plural equation spans more than one line""" 259 posource = r'''msgid "" 260 msgstr "" 261 "Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%" 262 "10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" 263 ''' 264 pofile = poparse(posource) 265 print pofile 266 assert len(pofile.units) == 1 267 header = pofile.units[0] 268 assert header.isheader() 269 assert not header.isblank() 270 271 headeritems = pofile.parseheader() 272 nplural, plural = pofile.getheaderplural() 273 assert nplural == "3" 274 assert plural == "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)"
275 ## TODO: add the same test for PoXliffFile 276