Exemplo n.º 1
0
 def test_element_nested(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             with xf.element('toast'):
                 with xf.element('taste'):
                     xf.write('conTent')
     self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
 def test_namespace_nested_anonymous(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             with xf.element('{nsURI}toast'):
                 pass
     self.assertXml(
         '<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
Exemplo n.º 3
0
 def test_element_nested(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             with xf.element('toast'):
                 with xf.element('taste'):
                     xf.write('conTent')
     self.assertXml('<test><toast><taste>conTent</taste></toast></test>')
Exemplo n.º 4
0
 def test_escaping(self):
     with etree.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>')
 def test_element_nested(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element("test"):
             with xf.element("toast"):
                 with xf.element("taste"):
                     xf.write("conTent")
     self.assertXml("<test><toast><taste>conTent</taste></toast></test>")
Exemplo n.º 6
0
 def test_escaping(self):
     with etree.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>')
Exemplo n.º 7
0
 def test_failure_preceding_text(self):
     try:
         with etree.xmlfile(self._file) as xf:
             xf.write('toast')
     except etree.LxmlSyntaxError:
         self.assertTrue(True)
     else:
         self.assertTrue(False)
Exemplo n.º 8
0
 def test_failure_preceding_text(self):
     try:
         with etree.xmlfile(self._file) as xf:
             xf.write('toast')
     except etree.LxmlSyntaxError:
         self.assertTrue(True)
     else:
         self.assertTrue(False)
 def test_nested_default_namespace_and_other(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element("{nsURI}test", nsmap={None: "nsURI", "p": "ns2"}):
             with xf.element("{nsURI}toast"):
                 pass
             with xf.element("{ns2}toast"):
                 pass
     self.assertXml('<test xmlns="nsURI" xmlns:p="ns2"><toast></toast><p:toast></p:toast></test>')
Exemplo n.º 10
0
 def test_failure_trailing_Element(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
         try:
             xf.write(etree.Element('test'))
         except etree.LxmlSyntaxError:
             self.assertTrue(True)
         else:
             self.assertTrue(False)
Exemplo n.º 11
0
 def test_nested_default_namespace_and_other(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('{nsURI}test', nsmap={None: 'nsURI', 'p': 'ns2'}):
             with xf.element('{nsURI}toast'):
                 pass
             with xf.element('{ns2}toast'):
                 pass
     self.assertXml(
         '<test xmlns="nsURI" xmlns:p="ns2"><toast></toast><p:toast></p:toast></test>'
     )
Exemplo n.º 12
0
 def test_failure_trailing_Element(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
         try:
             xf.write(etree.Element('test'))
         except etree.LxmlSyntaxError:
             self.assertTrue(True)
         else:
             self.assertTrue(False)
Exemplo n.º 13
0
 def test_element_nested_with_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element("test"):
             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("<test>con<toast>tent<taste>inside</taste>" "tnet</toast>noc</test>")
Exemplo n.º 14
0
    def test_write_Element_repeatedly(self):
        element = etree.Element('test')
        with etree.xmlfile(self._file) as xf:
            with xf.element('test'):
                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(['test']), set(el.tag for el in tree.getroot()))
Exemplo n.º 15
0
    def test_write_Element_repeatedly(self):
        element = etree.Element('test')
        with etree.xmlfile(self._file) as xf:
            with xf.element('test'):
                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(['test']), set(el.tag for el in tree.getroot()))
Exemplo n.º 16
0
 def test_element_nested_with_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             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('<test>con<toast>tent<taste>inside</taste>'
                    'tnet</toast>noc</test>')
Exemplo n.º 17
0
 def test_element_nested_with_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             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('<test>con<toast>tent<taste>inside</taste>'
                    'tnet</toast>noc</test>')
Exemplo n.º 18
0
 def test_flush(self):
     with etree.xmlfile(self._file, buffered=True) as xf:
         with xf.element('test'):
             self.assertXml("")
             xf.write('toast')
             self.assertXml("")
             with xf.element('taste'):
                 self.assertXml("")
                 xf.flush()
                 self.assertXml("<test>toast<taste>")
             self.assertXml("<test>toast<taste>")
         self.assertXml("<test>toast<taste>")
     self.assertXml("<test>toast<taste></taste></test>")
Exemplo n.º 19
0
 def test_flush(self):
     with etree.xmlfile(self._file, buffered=True) as xf:
         with xf.element('test'):
             self.assertXml("")
             xf.write('toast')
             self.assertXml("")
             with xf.element('taste'):
                 self.assertXml("")
                 xf.flush()
                 self.assertXml("<test>toast<taste>")
             self.assertXml("<test>toast<taste>")
         self.assertXml("<test>toast<taste>")
     self.assertXml("<test>toast<taste></taste></test>")
Exemplo n.º 20
0
 def test_buffering(self):
     with etree.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>")
Exemplo n.º 21
0
 def test_buffering(self):
     with etree.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>")
Exemplo n.º 22
0
 def test_closing_out_of_order_in_error_case(self):
     cm_exit = None
     try:
         with etree.xmlfile(self._file) as xf:
             x = xf.element('test')
             cm_exit = x.__exit__
             x.__enter__()
             raise ValueError('123')
     except ValueError:
         self.assertTrue(cm_exit)
         try:
             cm_exit(ValueError, ValueError("huhu"), None)
         except etree.LxmlSyntaxError:
             self.assertTrue(True)
         else:
             self.assertTrue(False)
     else:
         self.assertTrue(False)
Exemplo n.º 23
0
 def test_closing_out_of_order_in_error_case(self):
     cm_exit = None
     try:
         with etree.xmlfile(self._file) as xf:
             x = xf.element('test')
             cm_exit = x.__exit__
             x.__enter__()
             raise ValueError('123')
     except ValueError:
         self.assertTrue(cm_exit)
         try:
             cm_exit(ValueError, ValueError("huhu"), None)
         except etree.LxmlSyntaxError:
             self.assertTrue(True)
         else:
             self.assertTrue(False)
     else:
         self.assertTrue(False)
Exemplo n.º 24
0
 def test_element_write_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element("test"):
             xf.write("toast")
     self.assertXml("<test>toast</test>")
Exemplo n.º 25
0
 def test_encoding(self):
     with etree.xmlfile(self._file, encoding='utf16') as xf:
         with xf.element('test'):
             xf.write('toast')
     self.assertXml('<test>toast</test>', encoding='utf16')
Exemplo n.º 26
0
 def test_filelike_not_closing(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
     self.assertFalse(self._file.closed)
Exemplo n.º 27
0
 def test_pi(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.ProcessingInstruction('pypi'))
         with xf.element('test'):
             pass
     self.assertXml('<?pypi ?><test></test>')
Exemplo n.º 28
0
 def test_encoding(self):
     with etree.xmlfile(self._file, encoding='utf16') as xf:
         with xf.element('test'):
             xf.write('toast')
     self.assertXml('<test>toast</test>', encoding='utf16')
Exemplo n.º 29
0
 def test_attribute(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', attrib={'k': 'v'}):
             pass
     self.assertXml('<test k="v"></test>')
Exemplo n.º 30
0
 def test_namespace_nested_anonymous(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             with xf.element('{nsURI}toast'):
                 pass
     self.assertXml('<test><ns0:toast xmlns:ns0="nsURI"></ns0:toast></test>')
Exemplo n.º 31
0
 def test_write_Element(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.Element("test"))
     self.assertXml("<test/>")
Exemplo n.º 32
0
 def test_filelike_close(self):
     with etree.xmlfile(self._file, close=True) as xf:
         with xf.element('test'):
             pass
     self.assertTrue(self._file.closed)
     self._file = None  # prevent closing in tearDown()
Exemplo n.º 33
0
 def test_element(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
     self.assertXml('<test></test>')
Exemplo n.º 34
0
 def test_filelike_close(self):
     with etree.xmlfile(self._file, close=True) as xf:
         with xf.element('test'):
             pass
     self.assertRaises(ValueError, self._file.getvalue)
Exemplo n.º 35
0
 def test_attribute_extra_duplicate(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', attrib={'k': 'v'}, k='V'):
             pass
     self.assertXml('<test k="V"></test>')
Exemplo n.º 36
0
 def test_write_Element(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.Element('test'))
     self.assertXml('<test/>')
Exemplo n.º 37
0
 def test_filelike_close(self):
     with etree.xmlfile(self._file, close=True) as xf:
         with xf.element('test'):
             pass
     self.assertRaises(ValueError, self._file.getvalue)
Exemplo n.º 38
0
 def test_element(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
     self.assertXml('<test></test>')
Exemplo n.º 39
0
 def test_element_write_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             xf.write('toast')
     self.assertXml('<test>toast</test>')
Exemplo n.º 40
0
 def test_namespace_nested_nsmap(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element("test", nsmap={"x": "nsURI"}):
             with xf.element("{nsURI}toast"):
                 pass
     self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
Exemplo n.º 41
0
 def test_namespace_nested_nsmap(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', nsmap={'x': 'nsURI'}):
             with xf.element('{nsURI}toast'):
                 pass
     self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
Exemplo n.º 42
0
 def test_anonymous_namespace(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('{nsURI}test'):
             pass
     self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
Exemplo n.º 43
0
 def test_default_namespace(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
             pass
     self.assertXml('<test xmlns="nsURI"></test>')
Exemplo n.º 44
0
 def test_element_write_text(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             xf.write('toast')
     self.assertXml('<test>toast</test>')
Exemplo n.º 45
0
 def test_write_Element(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.Element('test'))
     self.assertXml('<test/>')
Exemplo n.º 46
0
 def test_filelike_not_closing(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test'):
             pass
     self.assertFalse(self._file.closed)
Exemplo n.º 47
0
 def test_namespace_nested_nsmap(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', nsmap={'x': 'nsURI'}):
             with xf.element('{nsURI}toast'):
                 pass
     self.assertXml('<test xmlns:x="nsURI"><x:toast></x:toast></test>')
Exemplo n.º 48
0
 def test_pi(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.ProcessingInstruction('pypi'))
         with xf.element('test'):
             pass
     self.assertXml('<?pypi ?><test></test>')
Exemplo n.º 49
0
 def test_comment(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.Comment('a comment'))
         with xf.element('test'):
             pass
     self.assertXml('<!--a comment--><test></test>')
Exemplo n.º 50
0
 def test_comment(self):
     with etree.xmlfile(self._file) as xf:
         xf.write(etree.Comment('a comment'))
         with xf.element('test'):
             pass
     self.assertXml('<!--a comment--><test></test>')
Exemplo n.º 51
0
 def test_attribute_extra(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', attrib={'k': 'v'}, n='N'):
             pass
     self.assertXml('<test k="v" n="N"></test>')
Exemplo n.º 52
0
 def test_anonymous_namespace(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('{nsURI}test'):
             pass
     self.assertXml('<ns0:test xmlns:ns0="nsURI"></ns0:test>')
Exemplo n.º 53
0
 def test_attribute_extra_duplicate(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('test', attrib={'k': 'v'}, k='V'):
             pass
     self.assertXml('<test k="V"></test>')
Exemplo n.º 54
0
 def test_default_namespace(self):
     with etree.xmlfile(self._file) as xf:
         with xf.element('{nsURI}test', nsmap={None: 'nsURI'}):
             pass
     self.assertXml('<test xmlns="nsURI"></test>')
Exemplo n.º 55
0
 def test_filelike_close(self):
     with etree.xmlfile(self._file, close=True) as xf:
         with xf.element('test'):
             pass
     self.assertTrue(self._file.closed)
     self._file = None  # prevent closing in tearDown()