Пример #1
0
 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>')
Пример #2
0
 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>')
Пример #3
0
 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: &amp;')
     self.assertXml(
         '<test>Comments: &lt;!-- text --&gt;\nEntities: &amp;amp;</test>')
Пример #5
0
 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)
Пример #6
0
 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)
Пример #7
0
    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()))
Пример #8
0
 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>')
Пример #9
0
 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>")
Пример #11
0
 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>')
Пример #13
0
 def test_element_write_text(self):
     with xmlfile(self._file) as xf:
         with xf.element('tests'):
             xf.write('toast')
     self.assertXml('<tests>toast</tests>')
Пример #14
0
 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()
Пример #15
0
 def test_filelike_not_closing(self):
     with xmlfile(self._file) as xf:
         with xf.element('tests'):
             pass
     self.assertFalse(self._file.closed)
Пример #16
0
 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>')
Пример #17
0
 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 />"
Пример #19
0
 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')
Пример #20
0
 def test_attribute(self):
     with xmlfile(self._file) as xf:
         with xf.element('tests', attrib={'k': 'v'}):
             pass
     self.assertXml('<tests k="v"></tests>')
Пример #21
0
 def test_write_Element(self):
     with xmlfile(self._file) as xf:
         xf.write(Element('tests'))
     self.assertXml('<tests/>')
Пример #22
0
 def test_filelike_close(self):
     with xmlfile(self._file, close=True) as xf:
         with xf.element('tests'):
             pass
     self.assertRaises(ValueError, self._file.getvalue)
Пример #23
0
 def test_element(self):
     with xmlfile(self._file) as xf:
         with xf.element('tests'):
             pass
     self.assertXml('<tests></tests>')
Пример #24
0
 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>')
Пример #25
0
 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>')