diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2005-12-15 18:41:22 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2005-12-15 18:41:22 (GMT) |
commit | 9235ea4f26c883c37ea6d3655cb96848e25fd87d (patch) | |
tree | 21cf6e657197e152afa2542dd4a5fd8f5f78e41a /Lib | |
parent | 6f0d479c78280572196419917c367d581d6d0e3f (diff) | |
download | cpython-9235ea4f26c883c37ea6d3655cb96848e25fd87d.zip cpython-9235ea4f26c883c37ea6d3655cb96848e25fd87d.tar.gz cpython-9235ea4f26c883c37ea6d3655cb96848e25fd87d.tar.bz2 |
added cElementTree tests
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_xml_etree_c.py | 203 | ||||
-rw-r--r-- | Lib/test/xmltests.py | 1 |
2 files changed, 204 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py new file mode 100644 index 0000000..fcb6233 --- /dev/null +++ b/Lib/test/test_xml_etree_c.py @@ -0,0 +1,203 @@ +# xmlcore.etree test for cElementTree + +import doctest, sys + +from test import test_support + +SAMPLE_XML = """ +<body> + <tag>text</tag> + <tag /> + <section> + <tag>subtext</tag> + </section> +</body> +""" + +SAMPLE_XML_NS = """ +<body xmlns="http://effbot.org/ns"> + <tag>text</tag> + <tag /> + <section> + <tag>subtext</tag> + </section> +</body> +""" + +def sanity(): + """ + Import sanity. + + >>> from xmlcore.etree import cElementTree + """ + +def check_method(method): + if not callable(method): + print method, "not callable" + +def serialize(ET, elem, encoding=None): + import StringIO + file = StringIO.StringIO() + tree = ET.ElementTree(elem) + if encoding: + tree.write(file, encoding) + else: + tree.write(file) + return file.getvalue() + +def summarize(elem): + return elem.tag + +def summarize_list(seq): + return map(summarize, seq) + +def interface(): + """ + Test element tree interface. + + >>> from xmlcore.etree import cElementTree as ET + + >>> element = ET.Element("tag", key="value") + >>> tree = ET.ElementTree(element) + + Make sure all standard element methods exist. + + >>> check_method(element.append) + >>> check_method(element.insert) + >>> check_method(element.remove) + >>> check_method(element.getchildren) + >>> check_method(element.find) + >>> check_method(element.findall) + >>> check_method(element.findtext) + >>> check_method(element.clear) + >>> check_method(element.get) + >>> check_method(element.set) + >>> check_method(element.keys) + >>> check_method(element.items) + >>> check_method(element.getiterator) + + Basic method sanity checks. + + >>> serialize(ET, element) # 1 + '<tag key="value" />' + >>> subelement = ET.Element("subtag") + >>> element.append(subelement) + >>> serialize(ET, element) # 2 + '<tag key="value"><subtag /></tag>' + >>> element.insert(0, subelement) + >>> serialize(ET, element) # 3 + '<tag key="value"><subtag /><subtag /></tag>' + >>> element.remove(subelement) + >>> serialize(ET, element) # 4 + '<tag key="value"><subtag /></tag>' + >>> element.remove(subelement) + >>> serialize(ET, element) # 5 + '<tag key="value" />' + >>> element.remove(subelement) + Traceback (most recent call last): + ValueError: list.remove(x): x not in list + >>> serialize(ET, element) # 6 + '<tag key="value" />' + """ + +def find(): + """ + Test find methods (including xpath syntax). + + >>> from xmlcore.etree import cElementTree as ET + + >>> elem = ET.XML(SAMPLE_XML) + >>> elem.find("tag").tag + 'tag' + >>> ET.ElementTree(elem).find("tag").tag + 'tag' + >>> elem.find("section/tag").tag + 'tag' + >>> ET.ElementTree(elem).find("section/tag").tag + 'tag' + >>> elem.findtext("tag") + 'text' + >>> elem.findtext("tog") + >>> elem.findtext("tog", "default") + 'default' + >>> ET.ElementTree(elem).findtext("tag") + 'text' + >>> elem.findtext("section/tag") + 'subtext' + >>> ET.ElementTree(elem).findtext("section/tag") + 'subtext' + >>> summarize_list(elem.findall("tag")) + ['tag', 'tag'] + >>> summarize_list(elem.findall("*")) + ['tag', 'tag', 'section'] + >>> summarize_list(elem.findall(".//tag")) + ['tag', 'tag', 'tag'] + >>> summarize_list(elem.findall("section/tag")) + ['tag'] + >>> summarize_list(elem.findall("section//tag")) + ['tag'] + >>> summarize_list(elem.findall("section/*")) + ['tag'] + >>> summarize_list(elem.findall("section//*")) + ['tag'] + >>> summarize_list(elem.findall("section/.//*")) + ['tag'] + >>> summarize_list(elem.findall("*/*")) + ['tag'] + >>> summarize_list(elem.findall("*//*")) + ['tag'] + >>> summarize_list(elem.findall("*/tag")) + ['tag'] + >>> summarize_list(elem.findall("*/./tag")) + ['tag'] + >>> summarize_list(elem.findall("./tag")) + ['tag', 'tag'] + >>> summarize_list(elem.findall(".//tag")) + ['tag', 'tag', 'tag'] + >>> summarize_list(elem.findall("././tag")) + ['tag', 'tag'] + >>> summarize_list(ET.ElementTree(elem).findall("/tag")) + ['tag', 'tag'] + >>> summarize_list(ET.ElementTree(elem).findall("./tag")) + ['tag', 'tag'] + >>> elem = ET.XML(SAMPLE_XML_NS) + >>> summarize_list(elem.findall("tag")) + [] + >>> summarize_list(elem.findall("{http://effbot.org/ns}tag")) + ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag'] + >>> summarize_list(elem.findall(".//{http://effbot.org/ns}tag")) + ['{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag', '{http://effbot.org/ns}tag'] + """ + +def parseliteral(): + r""" + + >>> from xmlcore.etree import cElementTree as ET + + >>> element = ET.XML("<html><body>text</body></html>") + >>> ET.ElementTree(element).write(sys.stdout) + <html><body>text</body></html> + >>> element = ET.fromstring("<html><body>text</body></html>") + >>> ET.ElementTree(element).write(sys.stdout) + <html><body>text</body></html> + >>> print ET.tostring(element) + <html><body>text</body></html> + >>> print ET.tostring(element, "ascii") + <?xml version='1.0' encoding='ascii'?> + <html><body>text</body></html> + >>> _, ids = ET.XMLID("<html><body>text</body></html>") + >>> len(ids) + 0 + >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>") + >>> len(ids) + 1 + >>> ids["body"].tag + 'body' + """ + +def test_main(): + from test import test_xml_etree_c + test_support.run_doctest(test_xml_etree_c, verbosity=True) + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/xmltests.py b/Lib/test/xmltests.py index ced670b..d203709 100644 --- a/Lib/test/xmltests.py +++ b/Lib/test/xmltests.py @@ -16,5 +16,6 @@ runtest("test.test_minidom") runtest("test.test_pyexpat") runtest("test.test_sax") runtest("test.test_xml_etree") +runtest("test.test_xml_etree_c") runtest("test.test_xmllib") runtest("test.test_xmlrpc") |