diff options
author | Eli Bendersky <eliben@gmail.com> | 2012-05-29 06:02:56 +0300 |
---|---|---|
committer | Eli Bendersky <eliben@gmail.com> | 2012-05-29 06:02:56 +0300 |
commit | f0fe14f13a23e12a4c88a04c03e80802a2bc38f7 (patch) | |
tree | dd1cdc8a36183e5bf8667d4c6a62cb0bdc18bbad /Lib/test/test_xml_etree.py | |
parent | 74e71423f690386aee3c7f4a67e9df9ffef0d8fe (diff) | |
download | cpython-f0fe14f13a23e12a4c88a04c03e80802a2bc38f7.tar.gz |
Issue 14814: Add namespaces keyword arg to find(*) methods in _elementtree.
Add attrib keyword to Element and SubElement in _elementtree.
Patch developed with Ezio Melotti.
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index df1f771440..cdba2b634c 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -62,6 +62,22 @@ SAMPLE_XML_NS = """ </body> """ +SAMPLE_XML_NS_ELEMS = """ +<root> +<h:table xmlns:h="hello"> + <h:tr> + <h:td>Apples</h:td> + <h:td>Bananas</h:td> + </h:tr> +</h:table> + +<f:table xmlns:f="foo"> + <f:name>African Coffee Table</f:name> + <f:width>80</f:width> + <f:length>120</f:length> +</f:table> +</root> +""" def sanity(): """ @@ -1995,6 +2011,17 @@ class NoAcceleratorTest(unittest.TestCase): self.assertEqual(pyET.SubElement.__module__, 'xml.etree.ElementTree') +class NamespaceParseTest(unittest.TestCase): + def test_find_with_namespace(self): + nsmap = {'h': 'hello', 'f': 'foo'} + doc = ET.fromstring(SAMPLE_XML_NS_ELEMS) + + self.assertEqual(len(doc.findall('{hello}table', nsmap)), 1) + self.assertEqual(len(doc.findall('.//{hello}td', nsmap)), 2) + self.assertEqual(len(doc.findall('.//{foo}name', nsmap)), 1) + + + class ElementSlicingTest(unittest.TestCase): def _elem_tags(self, elemlist): return [e.tag for e in elemlist] @@ -2102,6 +2129,41 @@ class ParseErrorTest(unittest.TestCase): ERRORS.codes[ERRORS.XML_ERROR_SYNTAX]) +class KeywordArgsTest(unittest.TestCase): + # Test various issues with keyword arguments passed to ET.Element + # constructor and methods + def test_issue14818(self): + x = ET.XML("<a>foo</a>") + self.assertEqual(x.find('a', None), + x.find(path='a', namespaces=None)) + self.assertEqual(x.findtext('a', None, None), + x.findtext(path='a', default=None, namespaces=None)) + self.assertEqual(x.findall('a', None), + x.findall(path='a', namespaces=None)) + self.assertEqual(list(x.iterfind('a', None)), + list(x.iterfind(path='a', namespaces=None))) + + self.assertEqual(ET.Element('a').attrib, {}) + elements = [ + ET.Element('a', dict(href="#", id="foo")), + ET.Element('a', attrib=dict(href="#", id="foo")), + ET.Element('a', dict(href="#"), id="foo"), + ET.Element('a', href="#", id="foo"), + ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"), + ] + for e in elements: + self.assertEqual(e.tag, 'a') + self.assertEqual(e.attrib, dict(href="#", id="foo")) + + e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'}) + self.assertEqual(e2.attrib['key1'], 'value1') + + with self.assertRaisesRegex(TypeError, 'must be dict, not str'): + ET.Element('a', "I'm not a dict") + with self.assertRaisesRegex(TypeError, 'must be dict, not str'): + ET.Element('a', attrib="I'm not a dict") + + # -------------------------------------------------------------------- @@ -2157,7 +2219,9 @@ def test_main(module=pyET): StringIOTest, ParseErrorTest, ElementTreeTest, - TreeBuilderTest] + NamespaceParseTest, + TreeBuilderTest, + KeywordArgsTest] if module is pyET: # Run the tests specific to the Python implementation test_classes += [NoAcceleratorTest] |