def test_namespace_nested_anonymous(self): with xmlfile(self._file) as xf: with xf.element('tests'): with xf.element('{nsURI}toast'): pass self.assertXml( '<tests><ns0:toast xmlns:ns0="nsURI"></ns0:toast></tests>')
def test_pi(self): from et_xmlfile.xmlfile import ProcessingInstruction with xmlfile(self._file) as xf: xf.write(ProcessingInstruction('pypi')) with xf.element('tests'): pass self.assertXml('<?pypi ?><tests></tests>')
def test_element_nested(self): with xmlfile(self._file) as xf: with xf.element('tests'): with xf.element('toast'): with xf.element('taste'): xf.write('conTent') self.assertXml('<tests><toast><taste>conTent</taste></toast></tests>')
def test_escaping(self): with xmlfile(self._file) as xf: with xf.element('test'): xf.write('Comments: <!-- text -->\n') xf.write('Entities: &') self.assertXml( '<test>Comments: <!-- text -->\nEntities: &amp;</test>')
def test_failure_preceding_text(self): try: with xmlfile(self._file) as xf: xf.write('toast') except LxmlSyntaxError: self.assertTrue(True) else: self.assertTrue(False)
def test_failure_trailing_Element(self): with xmlfile(self._file) as xf: with xf.element('tests'): pass try: xf.write(Element('tests')) except LxmlSyntaxError: self.assertTrue(True) else: self.assertTrue(False)
def test_write_Element_repeatedly(self): element = Element('tests') with xmlfile(self._file) as xf: with xf.element('tests'): for i in range(100): xf.write(element) tree = self._parse_file() self.assertTrue(tree is not None) self.assertEqual(100, len(tree.getroot())) self.assertEqual(set(['tests']), set(el.tag for el in tree.getroot()))
def test_element_nested_with_text(self): with xmlfile(self._file) as xf: with xf.element('tests'): xf.write('con') with xf.element('toast'): xf.write('tent') with xf.element('taste'): xf.write('inside') xf.write('tnet') xf.write('noc') self.assertXml('<tests>con<toast>tent<taste>inside</taste>' 'tnet</toast>noc</tests>')
def test_flush(self): with xmlfile(self._file, buffered=True) as xf: with xf.element('tests'): self.assertXml("") xf.write('toast') self.assertXml("") with xf.element('taste'): self.assertXml("") xf.flush() self.assertXml("<tests>toast<taste>") self.assertXml("<tests>toast<taste>") self.assertXml("<tests>toast<taste>") self.assertXml("<tests>toast<taste></taste></tests>")
def test_buffering(self): with xmlfile(self._file, buffered=False) as xf: with xf.element('test'): self.assertXml("<test>") xf.write('toast') self.assertXml("<test>toast") with xf.element('taste'): self.assertXml("<test>toast<taste>") xf.write('some', etree.Element("more"), "toast") self.assertXml("<test>toast<taste>some<more/>toast") self.assertXml("<test>toast<taste>some<more/>toast</taste>") xf.write('end') self.assertXml("<test>toast<taste>some<more/>toast</taste>end") self.assertXml( "<test>toast<taste>some<more/>toast</taste>end</test>") self.assertXml("<test>toast<taste>some<more/>toast</taste>end</test>")
def test_closing_out_of_order_in_error_case(self): cm_exit = None try: with xmlfile(self._file) as xf: x = xf.element('tests') cm_exit = x.__exit__ x.__enter__() raise ValueError('123') except ValueError: self.assertTrue(cm_exit) try: cm_exit(ValueError, ValueError("huhu"), None) except LxmlSyntaxError: self.assertTrue(True) else: self.assertTrue(False) else: self.assertTrue(False)
def test_namespace_nsmap(self): with xmlfile(self._file) as xf: with xf.element('{nsURI}test', nsmap={'x': 'nsURI'}): pass self.assertXml('<x:test xmlns:x="nsURI"></x:test>')
def test_element_write_text(self): with xmlfile(self._file) as xf: with xf.element('tests'): xf.write('toast') self.assertXml('<tests>toast</tests>')
def test_filelike_close(self): with xmlfile(self._file, close=True) as xf: with xf.element('tests'): pass self.assertTrue(self._file.closed) self._file = None # prevent closing in tearDown()
def test_filelike_not_closing(self): with xmlfile(self._file) as xf: with xf.element('tests'): pass self.assertFalse(self._file.closed)
def test_comment(self): with xmlfile(self._file) as xf: xf.write(etree.Comment('a comment')) with xf.element('tests'): pass self.assertXml('<!--a comment--><tests></tests>')
def test_namespace_nested_nsmap(self): with xmlfile(self._file) as xf: with xf.element('tests', nsmap={'x': 'nsURI'}): with xf.element('{nsURI}toast'): pass self.assertXml('<tests xmlns:x="nsURI"><x:toast></x:toast></tests>')
from io import BytesIO from xml.etree.ElementTree import Element from et_xmlfile import xmlfile out = BytesIO() with xmlfile(out) as xf: el = Element("root") xf.write(el) # write the XML straight to the file-like object assert out.getvalue() == b"<root />"
def test_encoding(self): with xmlfile(self._file, encoding='utf16') as xf: with xf.element('tests'): xf.write('toast') self.assertXml('<tests>toast</tests>', encoding='utf16')
def test_attribute(self): with xmlfile(self._file) as xf: with xf.element('tests', attrib={'k': 'v'}): pass self.assertXml('<tests k="v"></tests>')
def test_write_Element(self): with xmlfile(self._file) as xf: xf.write(Element('tests')) self.assertXml('<tests/>')
def test_filelike_close(self): with xmlfile(self._file, close=True) as xf: with xf.element('tests'): pass self.assertRaises(ValueError, self._file.getvalue)
def test_element(self): with xmlfile(self._file) as xf: with xf.element('tests'): pass self.assertXml('<tests></tests>')
def test_anonymous_namespace(self): with xmlfile(self._file) as xf: with xf.element('{nsURI}tests'): pass self.assertXml('<ns0:tests xmlns:ns0="nsURI"></ns0:tests>')
def test_nested_default_namespace(self): with xmlfile(self._file) as xf: with xf.element('{nsURI}tests', nsmap={None: 'nsURI'}): with xf.element('{nsURI}toast'): pass self.assertXml('<tests xmlns="nsURI"><toast></toast></tests>')