fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_po2php.py
blobfb54bb5cad20902be00be5c4b4e57c8742e0be9d
1 #!/usr/bin/env python
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
9 class TestPO2Php:
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)
16 return outputphp
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)
26 print outputphp
27 return outputphp
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)
35 print phpfile
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)
44 print phpfile
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
50 msgid ""
51 "_: accesskey-accept\n"
53 msgstr ""'''
54 phptemplate = '''$lang['accesskey-accept'] = '';\n'''
55 phpexpected = '''$lang['accesskey-accept'] = '';\n'''
56 phpfile = self.merge2php(phptemplate, posource)
57 print phpfile
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)
66 print phpfile
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)
75 print phpfile
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"""
80 # posource = ""
81 # proptemplate = "# A comment\n"
82 # propexpected = proptemplate
83 # propfile = self.merge2prop(proptemplate, posource)
84 # print propfile
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"}
92 def test_help(self):
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)