def test_write_incomplete_plural(): """Test behaviour with incompletely translated plurals in .po.""" catalog = Catalog() catalog.language = Language('bs') # Bosnian catalog.add(('foo', 'foos'), ('', 'many', 'one', ''), context='foo') print(po2xml(catalog)) assert po2xml(catalog) == {'foo': { 'few': '', 'many': 'many', 'one': 'one', 'other': ''}}
def test_write_ignore_untranslated_plural(): """An untranslated plural is not included in the XML. """ catalog = Catalog() catalog.language = Language('en') catalog.add(('foo', 'foos'), context='foo') assert po2xml(catalog) == {} # Even with ``with_untranslated``, we still do not include # empty plural (they would just block access to the untranslated # master version, which we cannot copy into the target). assert po2xml(catalog) == {}
def test_untranslated(): """Test that by default, untranslated strings are not included in the imported XML. """ catalog = Catalog() catalog.add('green', context='color1') catalog.add('red', 'rot', context='color2') assert po2xml(catalog) == {'color2': 'rot'} # If with_untranslated is passed, then all strings are included. # Note that arrays behave differently (they always include all # strings), and this is tested in test_string_arrays.py). assert po2xml(catalog, with_untranslated=True) ==\ {'color1': 'green', 'color2': 'rot'}
def assert_convert(cls, po, xml=None, namespaces={}): """Helper that passes the string in ``po`` through our po to xml converter, and checks the resulting xml string value against ``xml``. If ``xml`` is not given, we check against ``po`` instead, i.e. expect the string to remain unchanged. """ key = 'test' catalog = Catalog() catalog.add(po, po, context=key) dom = po2xml(catalog) elem = dom.xpath('/resources/string[@name="%s"]' % key)[0] elem_as_text = etree.tostring(elem, encoding=unicode) value = re.match("^[^>]+>(.*)<[^<]+$", elem_as_text).groups(1)[0] match = xml if xml is not None else po print "'%s' == '%s'" % (value, match) print repr(value), '==', repr(match) assert value == match # If ``namespaces`` are set, the test expects those to be defined # in the root of the document for prefix, uri in namespaces.items(): assert dom.nsmap[prefix] == uri # In this case, the reverse (converting back to po) always needs # to give us the original again, so this allows for a nice extra # check. TestFromXML.assert_convert(match, po, namespaces)
def test_write(): """Test writing a basic catalog. """ catalog = Catalog() catalog.add('green', context='colors:0') catalog.add('red', context='colors:1') assert po2xml(catalog) == {'colors': ['green', 'red']}
def test_write_incorrect_plural(): """Test what happens when the .po catalog contains the wrong plural information. """ catalog = Catalog() catalog.language = Language('lt') # Lithuanian # Lithuanian has four plurals, we define 2. catalog._num_plurals, catalog._plural_expr = 2, '(n != 1)' catalog.add(('foo', 'foos'), ( 'a', 'b', ), context='foo') wfunc = TestWarnFunc() xml = po2xml(catalog, warnfunc=wfunc) # A warning was written assert_equal(len(wfunc.logs), 1) assert '2 plurals, we expect 4' in wfunc.logs[0] # The missing plural is empty assert_equal( xml, {'foo': { 'one': 'a', 'other': None, 'few': 'b', 'many': None }})
def assert_convert(cls, po, xml=None, namespaces={}): """Helper that passes the string in ``po`` through our po to xml converter, and checks the resulting xml string value against ``xml``. If ``xml`` is not given, we check against ``po`` instead, i.e. expect the string to remain unchanged. """ key = 'test' catalog = Catalog() catalog.add(po, po, context=key) warnfunc = TestWarnFunc() dom = write_xml(po2xml(catalog, warnfunc=warnfunc), warnfunc=warnfunc) elem = dom.xpath('/resources/string[@name="%s"]' % key)[0] elem_as_text = etree.tostring(elem, encoding=unicode) value = re.match("^[^>]+>(.*)<[^<]+$", elem_as_text).groups(1)[0] match = xml if xml is not None else po print "'%s' == '%s'" % (value, match) print repr(value), '==', repr(match) assert value == match # If ``namespaces`` are set, the test expects those to be defined # in the root of the document for prefix, uri in namespaces.items(): assert dom.nsmap[prefix] == uri # In this case, the reverse (converting back to po) always needs to # give us the original again, so this allows for a nice extra check. if not match: # Skip this if we are doing a special, custom match. TestFromXML.assert_convert(match, po, namespaces) return warnfunc
def test_write_missing_translations(): """[Regression] Specifically test that arrays are not written to the XML in an incomplete fashion if parts of the array are not translated. """ catalog = Catalog() catalog.add('green', context='colors:0') # does not have a translation catalog.add('red', 'rot', context='colors:1') assert po2xml(catalog) == {'colors': ['green', 'rot']}
def test_write(): """Test writing a basic catalog. """ catalog = Catalog() catalog.add('green', context='colors:0') catalog.add('red', context='colors:1') assert etree.tostring(po2xml(catalog, with_untranslated=True)) == \ '<resources><string-array name="colors"><item>green</item><item>red</item></string-array></resources>'
def test_write(): """Test a basic po2xml() call. (what the import command does). """ catalog = Catalog() catalog.language = Language('bs') # Bosnian catalog.add(('foo', 'foos'), ('few', 'many', 'one', 'other'), context='foo') assert po2xml(catalog) == {'foo': { 'few': 'few', 'many': 'many', 'one': 'one', 'other': 'other'}}
def test_write(): """Test writing a basic catalog. """ catalog = Catalog() catalog.add("green", context="colors:0") catalog.add("red", context="colors:1") assert ( etree.tostring(po2xml(catalog, with_untranslated=True)) == '<resources><string-array name="colors"><item>green</item><item>red</item></string-array></resources>' )
def test_invalid_xhtml(): """Ensure we can deal with broken XML in messages. """ c = Catalog() c.add('Foo', '<i>Tag is not closed', context="foo") # [bug] This caused an exception in 16263b. dom = write_xml(po2xml(c)) # The tag was closed automatically (our loose parser tries to fix errors). assert etree.tostring(dom) == b'<resources><string name="foo"><i>Tag is not closed</i></string></resources>'
def test_write_skipped_ids(): """Test that arrays were ids are missing are written properly out as well. """ # TODO: Indices missing at the end of the array will not be noticed, # because we are not aware of the arrays full length. # TODO: If we where smart enough to look in the original resource XML, # we could fill in missing array strings with the untranslated value. catalog = Catalog() catalog.add('red', context='colors:3') catalog.add('green', context='colors:1') assert po2xml(catalog) == {'colors': [None, 'green', None, 'red']}
def test_write_skipped_ids(): """Test that catalogs were ids are missing are written properly out as well. """ # TODO: We currently simply maintain order, but shouldn't we instead # write out missing ids as empty strings? If the source file says # colors:9, that likely means the dev. expects 8 strings before it. catalog = Catalog() catalog.add('red', context='colors:9') catalog.add('green', context='colors:4') assert etree.tostring(po2xml(catalog, with_untranslated=True)) == \ '<resources><string-array name="colors"><item>green</item><item>red</item></string-array></resources>'
def test_write_order(): """Test that when writing a catalog with string-arrays, order is maintained; both of the string-array tag in the list of all strings, as well as the array strings themselves. """ catalog = Catalog() catalog.add('foo', context='before') catalog.add('red', context='colors:1') catalog.add('green', context='colors:0') catalog.add('bar', context='after') assert etree.tostring(po2xml(catalog, with_untranslated=True)) == \ '<resources><string name="before">foo</string><string-array name="colors"><item>green</item><item>red</item></string-array><string name="after">bar</string></resources>'
def test_invalid_xhtml(): """Ensure we can deal with broken XML in messages. """ c = Catalog() c.add('Foo', '<i>Tag is not closed', context="foo") # [bug] This caused an exception in 16263b. dom = write_xml(po2xml(c)) # The tag was closed automatically (our loose parser tries to fix errors). assert etree.tostring( dom ) == b'<resources><string name="foo"><i>Tag is not closed</i></string></resources>'
def test_write_order(): """Test that when writing a catalog with string-arrays, order is maintained; both of the string-array tag in the list of all strings, as well as the array strings themselves. """ catalog = Catalog() catalog.add('foo', 'foo', context='before') catalog.add('red', 'rot', context='colors:1') catalog.add('green', 'gruen', context='colors:0') catalog.add('bar', 'bar', context='after') assert po2xml(catalog) == { 'before': 'foo', 'colors': ['gruen', 'rot'], 'after': 'bar'}
def test_write_order(): """Test that when writing a catalog with string-arrays, order is maintained; both of the string-array tag in the list of all strings, as well as the array strings themselves. """ catalog = Catalog() catalog.add("foo", context="before") catalog.add("red", context="colors:1") catalog.add("green", context="colors:0") catalog.add("bar", context="after") assert ( etree.tostring(po2xml(catalog, with_untranslated=True)) == '<resources><string name="before">foo</string><string-array name="colors"><item>green</item><item>red</item></string-array><string name="after">bar</string></resources>' )
def test_write_order_long_array(): """[Regression] Test that order is maintained for a long array. """ catalog = Catalog() catalog.add('foo', 'foo', context='before') for i in range(0, 13): catalog.add('loop%d' % i, 'schleife%d' % i, context='colors:%d' % i) catalog.add('bar', 'bar', context='after') assert po2xml(catalog) == { 'before': 'foo', 'colors': ['schleife0', 'schleife1', 'schleife2', 'schleife3', 'schleife4', 'schleife5', 'schleife6', 'schleife7', 'schleife8', 'schleife9', 'schleife10', 'schleife11', 'schleife12'], 'after': 'bar'}
def test_write_order(): """Test that when writing a catalog with string-arrays, order is maintained; both of the string-array tag in the list of all strings, as well as the array strings themselves. """ catalog = Catalog() catalog.add('foo', 'foo', context='before') catalog.add('red', 'rot', context='colors:1') catalog.add('green', 'gruen', context='colors:0') catalog.add('bar', 'bar', context='after') assert po2xml(catalog) == { 'before': 'foo', 'colors': ['gruen', 'rot'], 'after': 'bar' }
def test_write_order_long_array(): """[Regression] Test that order is maintained for a long array. """ catalog = Catalog() catalog.add("foo", context="before") expected_item_xml = "" for i in range(1, 13): catalog.add("loop%d" % i, context="colors:%d" % i) expected_item_xml = expected_item_xml + "<item>%s</item>" % ("loop%d" % i) catalog.add("bar", context="after") assert ( etree.tostring(po2xml(catalog, with_untranslated=True)) == '<resources><string name="before">foo</string><string-array name="colors">%s</string-array><string name="after">bar</string></resources>' % expected_item_xml )
def test_write_incorrect_plural(): """Test what happens when the .po catalog contains the wrong plural information. """ catalog = Catalog() catalog.language = Language('lt') # Lithuanian # Lithuanian has three plurals, we define 2. catalog._num_plurals, catalog._plural_expr = 2, '(n != 1)' catalog.add(('foo', 'foos'), ('a', 'b',), context='foo') wfunc = TestWarnFunc() xml = po2xml(catalog, warnfunc=wfunc) # A warning was written assert len(wfunc.logs) == 1 assert '2 plurals, we expect 3' in wfunc.logs[0] # The missing plural is empty assert xml == {'foo': {'few': 'a', 'other': None, 'one': 'b'}}
def test_write_order_long_array(): """[Regression] Test that order is maintained for a long array. """ catalog = Catalog() catalog.add('foo', 'foo', context='before') for i in range(0, 13): catalog.add('loop%d' % i, 'schleife%d' % i, context='colors:%d' % i) catalog.add('bar', 'bar', context='after') assert po2xml(catalog) == { 'before': 'foo', 'colors': [ 'schleife0', 'schleife1', 'schleife2', 'schleife3', 'schleife4', 'schleife5', 'schleife6', 'schleife7', 'schleife8', 'schleife9', 'schleife10', 'schleife11', 'schleife12' ], 'after': 'bar' }