2 # -*- coding: utf-8 -*-
4 from translate
.convert
import po2php
5 from translate
.convert
import test_convert
6 from translate
.misc
import wStringIO
7 from translate
.storage
import po
10 def po2php(self
, posource
):
11 """helper that converts po source to .php source without requiring files"""
12 inputfile
= wStringIO
.StringIO(posource
)
13 inputpo
= po
.pofile(inputfile
)
14 convertor
= po2php
.po2php()
15 outputphp
= convertor
.convertstore(inputpo
)
18 def merge2php(self
, phpsource
, posource
):
19 """helper that merges po translations to .php source without requiring files"""
20 inputfile
= wStringIO
.StringIO(posource
)
21 inputpo
= po
.pofile(inputfile
)
22 templatefile
= wStringIO
.StringIO(phpsource
)
23 #templatephp = php.phpfile(templatefile)
24 convertor
= po2php
.rephp(templatefile
)
25 outputphp
= convertor
.convertstore(inputpo
)
29 def test_merging_simple(self
):
30 """check the simplest case of merging a translation"""
31 posource
= '''#: $lang['name']\nmsgid "value"\nmsgstr "waarde"\n'''
32 phptemplate
= '''$lang['name'] = 'value';\n'''
33 phpexpected
= '''$lang['name'] = 'waarde';\n'''
34 phpfile
= self
.merge2php(phptemplate
, posource
)
36 assert phpfile
== [phpexpected
]
38 def test_space_preservation(self
):
39 """check that we preserve any spacing in php files when merging"""
40 posource
= '''#: $lang['name']\nmsgid "value"\nmsgstr "waarde"\n'''
41 phptemplate
= '''$lang['name'] = 'value';\n'''
42 phpexpected
= '''$lang['name'] = 'waarde';\n'''
43 phpfile
= self
.merge2php(phptemplate
, posource
)
45 assert phpfile
== [phpexpected
]
47 def test_merging_blank_entries(self
):
48 """check that we can correctly merge entries that are blank in the template"""
49 posource
= '''#: accesskey-accept
51 "_: accesskey-accept\n"
54 phptemplate
= '''$lang['accesskey-accept'] = '';\n'''
55 phpexpected
= '''$lang['accesskey-accept'] = '';\n'''
56 phpfile
= self
.merge2php(phptemplate
, posource
)
58 assert phpfile
== [phpexpected
]
60 def test_merging_fuzzy(self
):
61 """check merging a fuzzy translation"""
62 posource
= '''#: $lang['name']\n#, fuzzy\nmsgid "value"\nmsgstr "waarde"\n'''
63 phptemplate
= '''$lang['name'] = 'value';\n'''
64 phpexpected
= '''$lang['name'] = 'value';\n'''
65 phpfile
= self
.merge2php(phptemplate
, posource
)
67 assert phpfile
== [phpexpected
]
69 def test_locations_with_spaces(self
):
70 """check that a location with spaces in php but spaces removed in PO is used correctly"""
71 posource
= '''#: $lang['name']\nmsgid "value"\nmsgstr "waarde"\n'''
72 phptemplate
= '''$lang[ 'name' ] = 'value';\n'''
73 phpexpected
= '''$lang[ 'name' ] = 'waarde';\n'''
74 phpfile
= self
.merge2php(phptemplate
, posource
)
76 assert phpfile
== [phpexpected
]
78 # def test_merging_propertyless_template(self):
79 # """check that when merging with a template with no property values that we copy the template"""
81 # proptemplate = "# A comment\n"
82 # propexpected = proptemplate
83 # propfile = self.merge2prop(proptemplate, posource)
85 # assert propfile == [propexpected]
87 class TestPO2PhpCommand(test_convert
.TestConvertCommand
, TestPO2Php
):
88 """Tests running actual po2php commands on files"""
89 convertmodule
= po2php
90 defaultoptions
= {"progress": "none"}
93 """tests getting help"""
94 options
= test_convert
.TestConvertCommand
.test_help(self
)
95 options
= self
.help_check(options
, "-t TEMPLATE, --template=TEMPLATE")
96 options
= self
.help_check(options
, "--fuzzy")
97 options
= self
.help_check(options
, "--nofuzzy", last
=True)