Package translate :: Package convert :: Module test_php2po
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.test_php2po

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.convert import php2po 
  5  from translate.convert import test_convert 
  6  from translate.misc import wStringIO 
  7  from translate.storage import po 
  8  from translate.storage import php 
  9   
10 -class TestPhp2PO:
11 - def php2po(self, phpsource, phptemplate=None):
12 """helper that converts .phperties source to po source without requiring files""" 13 inputfile = wStringIO.StringIO(phpsource) 14 inputphp = php.phpfile(inputfile) 15 convertor = php2po.php2po() 16 if phptemplate: 17 templatefile = wStringIO.StringIO(phptemplate) 18 templatephp = php.phpfile(templatefile) 19 outputpo = convertor.mergestore(templatephp, inputphp) 20 else: 21 outputpo = convertor.convertstore(inputphp) 22 return outputpo
23
24 - def convertphp(self, phpsource):
25 """call the convertphp, return the outputfile""" 26 inputfile = wStringIO.StringIO(phpsource) 27 outputfile = wStringIO.StringIO() 28 templatefile = None 29 assert php2po.convertphp(inputfile, outputfile, templatefile) 30 return outputfile.getvalue()
31
32 - def singleelement(self, pofile):
33 """checks that the pofile contains a single non-header element, and returns it""" 34 assert len(pofile.units) == 2 35 assert pofile.units[0].isheader() 36 print pofile 37 return pofile.units[1]
38
39 - def countelements(self, pofile):
40 """counts the number of non-header entries""" 41 assert pofile.units[0].isheader() 42 print pofile 43 return len(pofile.units) - 1
44
45 - def test_simpleentry(self):
46 """checks that a simple php entry converts properly to a po entry""" 47 phpsource = """$_LANG['simple'] = 'entry';""" 48 pofile = self.php2po(phpsource) 49 pounit = self.singleelement(pofile) 50 assert pounit.source == "entry" 51 assert pounit.target == ""
52
53 - def test_convertphp(self):
54 """checks that the convertphp function is working""" 55 phpsource = """$_LANG['simple'] = 'entry';""" 56 posource = self.convertphp(phpsource) 57 pofile = po.pofile(wStringIO.StringIO(posource)) 58 pounit = self.singleelement(pofile) 59 assert pounit.source == "entry" 60 assert pounit.target == ""
61
62 - def test_unicode(self):
63 """checks that unicode entries convert properly""" 64 unistring = u'Norsk bokm\u00E5l' 65 phpsource = """$lang['nb'] = '%s';""" % unistring 66 pofile = self.php2po(phpsource) 67 pounit = self.singleelement(pofile) 68 print repr(pofile.units[0].target) 69 print repr(pounit.source) 70 assert pounit.source == u'Norsk bokm\u00E5l'
71
72 - def test_multiline(self):
73 """checks that multiline enties can be parsed""" 74 phpsource = r"""$lang['5093'] = 'Unable to connect to your IMAP server. You may have exceeded the maximum number 75 of connections to this server. If so, use the Advanced IMAP Server Settings dialog to 76 reduce the number of cached connections.';""" 77 pofile = self.php2po(phpsource) 78 print repr(pofile.units[1].target) 79 assert self.countelements(pofile) == 1
80
81 - def test_comments_before(self):
82 """test to ensure that we take comments from .php and place them in .po""" 83 phpsource = '''/* Comment */ 84 $lang['prefPanel-smime'] = 'Security';''' 85 pofile = self.php2po(phpsource) 86 pounit = self.singleelement(pofile) 87 assert pounit.getnotes("developer") == "Comment"
88 # TODO write test for inline comments and check for // comments that precede an entry 89
90 - def test_emptyentry(self):
91 """checks that empty definitions survives into po file""" 92 phpsource = '''/* comment */\n$lang['credit'] = '';''' 93 pofile = self.php2po(phpsource) 94 pounit = self.singleelement(pofile) 95 assert pounit.getlocations() == ["$lang['credit']"] 96 assert pounit.getcontext() == "$lang['credit']" 97 assert "#. comment" in str(pofile) 98 assert pounit.source == ""
99
101 """checks that if we translate an empty definition it makes it into the PO""" 102 phptemplate = '''$lang['credit'] = '';''' 103 phpsource = '''$lang['credit'] = 'Translators Names';''' 104 pofile = self.php2po(phpsource, phptemplate) 105 pounit = self.singleelement(pofile) 106 assert pounit.getlocations() == ["$lang['credit']"] 107 assert pounit.source == "" 108 assert pounit.target == "Translators Names"
109
110 - def test_newlines_in_value(self):
111 """check that we can carry newlines that appear in the entry value into the PO""" 112 phpsource = r'''$lang['name'] = 'value1\nvalue2';''' 113 pofile = self.php2po(phpsource) 114 unit = self.singleelement(pofile) 115 assert unit.source == "value1\nvalue2"
116
117 - def test_spaces_in_name(self):
118 """checks that if we have spaces in the name we create a good PO with no spaces""" 119 phptemplate = '''$lang[ 'credit' ] = 'Something';''' 120 phpsource = '''$lang[ 'credit' ] = ''n Ding';''' 121 pofile = self.php2po(phpsource, phptemplate) 122 pounit = self.singleelement(pofile) 123 assert pounit.getlocations() == ["$lang['credit']"]
124
125 -class TestPhp2POCommand(test_convert.TestConvertCommand, TestPhp2PO):
126 """Tests running actual php2po commands on files""" 127 convertmodule = php2po 128 defaultoptions = {"progress": "none"} 129
130 - def test_help(self):
131 """tests getting help""" 132 options = test_convert.TestConvertCommand.test_help(self) 133 options = self.help_check(options, "-P, --pot") 134 options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE") 135 options = self.help_check(options, "--duplicates=DUPLICATESTYLE", last=True)
136