Skip to content

Commit

Permalink
YAML: Fix serialization of empty keys
Browse files Browse the repository at this point in the history
  • Loading branch information
nijel committed Jul 31, 2018
1 parent d1411aa commit c6b8896
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
22 changes: 15 additions & 7 deletions translate/convert/test_po2yaml.py
Expand Up @@ -130,28 +130,36 @@ def test_nested(self):
msgid "booo"
msgstr ""
#: foo->
msgid "bar2"
msgstr ""
#: eggs
msgid "spam"
msgstr ""
"""
template_string = '''
foo:
bar: bar
'': bar2
baz:
boo: booo
eggs: spam
'''
target_store = self._convert_to_store(input_string, template_string)
assert len(target_store.units) == 3
assert len(target_store.units) == 4
assert target_store.units[0].getlocations() == ['foo->bar']
assert target_store.units[0].source == "bar"
assert target_store.units[0].target == "bar"
assert target_store.units[1].getlocations() == ['foo->baz->boo']
assert target_store.units[1].source == "booo"
assert target_store.units[1].target == "booo"
assert target_store.units[2].getlocations() == ['eggs']
assert target_store.units[2].source == "spam"
assert target_store.units[2].target == "spam"
assert target_store.units[1].getlocations() == ['foo->']
assert target_store.units[1].source == "bar2"
assert target_store.units[1].target == "bar2"
assert target_store.units[2].getlocations() == ['foo->baz->boo']
assert target_store.units[2].source == "booo"
assert target_store.units[2].target == "booo"
assert target_store.units[3].getlocations() == ['eggs']
assert target_store.units[3].source == "spam"
assert target_store.units[3].target == "spam"

def test_convert_completion_below_threshold(self):
"""Check no conversion if input completion is below threshold."""
Expand Down
14 changes: 9 additions & 5 deletions translate/convert/test_yaml2po.py
Expand Up @@ -71,23 +71,27 @@ def test_nested(self):
input_string = '''
foo:
bar: bar
'': bar2
baz:
boo: booo
eggs: spam
'''
target_store = self._convert_to_store(input_string)
assert self._count_elements(target_store) == 3
assert self._count_elements(target_store) == 4
assert target_store.units[1].getlocations() == ['foo->bar']
assert target_store.units[1].source == "bar"
assert target_store.units[1].target == ""
assert target_store.units[2].getlocations() == ['foo->baz->boo']
assert target_store.units[2].source == "booo"
assert target_store.units[2].getlocations() == ['foo->']
assert target_store.units[2].source == "bar2"
assert target_store.units[2].target == ""
assert target_store.units[3].getlocations() == ['eggs']
assert target_store.units[3].source == "spam"
assert target_store.units[3].getlocations() == ['foo->baz->boo']
assert target_store.units[3].source == "booo"
assert target_store.units[3].target == ""
assert target_store.units[4].getlocations() == ['eggs']
assert target_store.units[4].source == "spam"
assert target_store.units[4].target == ""

def test_no_duplicates(self):
"""Check converting drops duplicates."""
Expand Down
21 changes: 21 additions & 0 deletions translate/storage/test_yaml.py
Expand Up @@ -350,6 +350,27 @@ def test_key_nesting(self):
value: teststring2
'''

def test_empty_key(self):
store = self.StoreClass()
store.parse('''
'': Jedna
foo:
'': Dve
''')
assert len(store.units) == 2
assert store.units[0].getid() == ''
assert store.units[0].source == 'Jedna'
assert store.units[1].getid() == 'foo->'
assert store.units[1].source == 'Dve'
out = BytesIO()
store.serialize(out)
assert out.getvalue() == b'''? ''
: Jedna
foo:
? ''
: Dve
'''


class TestRubyYAMLResourceStore(test_monolingual.TestMonolingualStore):
StoreClass = yaml.RubyYAMLFile
Expand Down
2 changes: 1 addition & 1 deletion translate/storage/yaml.py
Expand Up @@ -149,7 +149,7 @@ def get_root_node(self, node):
def serialize(self, out):
def nested_set(target, path, value):
if len(path) > 1:
if len(path) == 2 and path[1][0] == '[' and path[1][-1] == ']' and path[1][1:-1].isdigit():
if len(path) == 2 and path[1] and path[1][0] == '[' and path[1][-1] == ']' and path[1][1:-1].isdigit():
if path[0] not in target:
target[path[0]] = []
target[path[0]].append(value)
Expand Down

0 comments on commit c6b8896

Please sign in to comment.