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'] = 'America/Argentina/Cordoba' 
105          time.tzset() 
106          assert time.timezone == 10800 
107           
108          assert pofile.tzstring() == time.strftime("%z") 
109   
110          os.environ['TZ'] = 'Asia/Kabul' 
111          time.tzset() 
112          assert time.timezone == -16200 
113           
114          assert pofile.tzstring() == time.strftime("%z") 
115   
116          os.environ['TZ'] = 'Asia/Tehran' 
117          time.tzset() 
118          assert time.timezone == -12600 
119           
120          assert pofile.tzstring() == time.strftime("%z") 
121   
122          os.environ['TZ'] = 'Canada/Newfoundland' 
123          time.tzset() 
124          assert time.timezone == 12600 
125           
126          assert pofile.tzstring() == time.strftime("%z") 
127   
128          os.environ['TZ'] = 'US/Eastern' 
129          time.tzset() 
130          assert time.timezone == 18000 
131           
132          assert pofile.tzstring() == time.strftime("%z") 
133   
134          os.environ['TZ'] = 'Asia/Seoul' 
135          time.tzset() 
136          assert time.timezone == -32400 
137           
138          assert pofile.tzstring() == time.strftime("%z") 
139   
140          os.environ['TZ'] = 'Africa/Johannesburg' 
141          time.tzset() 
142          assert time.timezone == -7200 
143           
144          assert pofile.tzstring() == time.strftime("%z") 
145   
146          os.environ['TZ'] = 'Africa/Windhoek' 
147          time.tzset() 
148          assert time.timezone == -3600 
149           
150           
151           
152   
153          os.environ['TZ'] = 'Egypt' 
154          time.tzset() 
155          assert time.timezone == -7200 
156           
157          assert pofile.tzstring() == time.strftime("%z") 
158   
159          os.environ['TZ'] = 'UTC' 
160          time.tzset() 
161          assert time.timezone == 0 
162           
163          assert pofile.tzstring() == time.strftime("%z") 
 164   
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   
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   
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   
256   
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   
276