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_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_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>'